diff --git a/lib/public_activity/config.rb b/lib/public_activity/config.rb index 203aec13..3f8a61e5 100644 --- a/lib/public_activity/config.rb +++ b/lib/public_activity/config.rb @@ -47,6 +47,12 @@ def table_name(name = nil) self.class.table_name(name) end + # alias for {#owner_options} + # @see Config#owner_options + def owner_options(owner_options = {}) + self.class.owner_options(owner_options) + end + # instance version of {Config#enabled} # @see Config#orm def enabled(value = nil) @@ -79,6 +85,14 @@ def self.enabled(value = nil) end end + def self.owner_options(owner_options = {}) + if owner_options.blank? + Thread.current[:owner_options] || { :polymorphic => true } + else + Thread.current[:owner_options] = owner_options + end + end + # Provides simple DSL for the config block. class Block # @see Config#orm diff --git a/lib/public_activity/orm/active_record/activity.rb b/lib/public_activity/orm/active_record/activity.rb index 7ec55a5d..4cfc32c7 100644 --- a/lib/public_activity/orm/active_record/activity.rb +++ b/lib/public_activity/orm/active_record/activity.rb @@ -28,7 +28,7 @@ class Activity < ::ActiveRecord::Base with_options(optional: true) do # Define ownership to a resource responsible for this activity - belongs_to :owner, polymorphic: true + belongs_to :owner, PublicActivity.config.owner_options # Define ownership to a resource targeted by this activity belongs_to :recipient, polymorphic: true end diff --git a/lib/public_activity/orm/mongo_mapper/activity.rb b/lib/public_activity/orm/mongo_mapper/activity.rb index 40b37b40..4bb511c1 100644 --- a/lib/public_activity/orm/mongo_mapper/activity.rb +++ b/lib/public_activity/orm/mongo_mapper/activity.rb @@ -21,7 +21,7 @@ def self.from_mongo(value) # Define polymorphic association to the parent belongs_to :trackable, polymorphic: true # Define ownership to a resource responsible for this activity - belongs_to :owner, polymorphic: true + belongs_to :owner, PublicActivity.config.owner_options # Define ownership to a resource targeted by this activity belongs_to :recipient, polymorphic: true diff --git a/lib/public_activity/orm/mongoid/activity.rb b/lib/public_activity/orm/mongoid/activity.rb index 1969abed..c39e2305 100644 --- a/lib/public_activity/orm/mongoid/activity.rb +++ b/lib/public_activity/orm/mongoid/activity.rb @@ -14,15 +14,17 @@ class Activity include Renderable if ::Mongoid::VERSION.split('.')[0].to_i >= 7 - opts = { polymorphic: true, optional: false } + extra_options = { optional: false } else - opts = { polymorphic: true } + extra_options = {} end + opts = { polymorphic: true }.merge(extra_options) + # Define polymorphic association to the parent belongs_to :trackable, opts # Define ownership to a resource responsible for this activity - belongs_to :owner, opts + belongs_to :owner, PublicActivity.config.owner_options.merge(extra_options) # Define ownership to a resource targeted by this activity belongs_to :recipient, opts diff --git a/test/test_activity.rb b/test/test_activity.rb index 0c30aec5..6bafa963 100644 --- a/test/test_activity.rb +++ b/test/test_activity.rb @@ -2,7 +2,30 @@ require 'test_helper' -describe 'PublicActivity::Activity Rendering' do +describe 'PublicActivity::Activity' do + it "allows owner association options to be overwritten" do + PublicActivity::Config.owner_options({ class_name: "User" }) + + klass = Class.new(PublicActivity::Activity) + klass.must_respond_to :owner + case ENV["PA_ORM"] + when "active_record" + klass.reflect_on_association(:owner).options[:polymorphic].must_equal wont_be_nil + when "mongoid" + klass.reflect_on_association(:owner).options[:polymorphic].must_equal nil + when "mongo_mapper" + klass.reflect_on_association(:owner).options[:polymorphic].must_equal nil + end + + if ENV["PA_ORM"] == "mongo_mapper" + klass.associations[:owner].options[:class_name].must_equal "User" + else + klass.reflect_on_association(:owner).options[:class_name].must_equal "User" + end + + PublicActivity::Config.owner_options({ polymorphic: true }) + end + describe '#text' do subject { PublicActivity::Activity.new(key: 'activity.test', parameters: { one: 1 }) }