Skip to content

Commit

Permalink
Allow owner association options to be overwritten via PublicActivity:…
Browse files Browse the repository at this point in the history
…:Config
  • Loading branch information
mateusg authored and oboxodo committed Jan 16, 2025
1 parent 1f49035 commit 014791d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
14 changes: 14 additions & 0 deletions lib/public_activity/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/public_activity/orm/active_record/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/public_activity/orm/mongo_mapper/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 9 additions & 6 deletions lib/public_activity/orm/mongoid/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ class Activity
include ::Mongoid::Attributes::Dynamic if ::Mongoid::VERSION.split('.')[0].to_i >= 4
include Renderable

if ::Mongoid::VERSION.split('.')[0].to_i >= 7
opts = { polymorphic: true, optional: false }
else
opts = { polymorphic: true }
end
extra_options =
if ::Mongoid::VERSION.split('.')[0].to_i >= 7
{ optional: false }
else
{}
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

Expand Down
25 changes: 24 additions & 1 deletion test/test_activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) }

Expand Down

0 comments on commit 014791d

Please sign in to comment.