Permalink
Browse files

Allow specifying touch_{actor} and touch_{target} as option for acts_…

…as_{*}_store.

This option adds the "touch" argument to the belongs_to statement on the model.
  • Loading branch information...
1 parent 892b9df commit 9d2a16417309962c65279589fe084e7bbaaba714 @cmer committed Mar 1, 2012
@@ -20,7 +20,14 @@ def acts_as_followable(opts = nil)
end
# Make the current class a {Socialization::FollowStore}
+ # @param [Hash] opts the options to create a message with.
+ # @option opts [Boolean] :touch_follower :touch value for belongs_to :follower association
+ # @option opts [Boolean] :touch_followable :touch value for belongs_to :followable association
def acts_as_follow_store(opts = nil)
+ opts ||= {}
+ belongs_to :follower, :polymorphic => true, :touch => opts[:touch_follower] || false
+ belongs_to :followable, :polymorphic => true, :touch => opts[:touch_followable] || false
+ validates_uniqueness_of :followable_type, :scope => [:followable_id, :follower_type, :follower_id], :message => 'You cannot follow the same thing twice.'
include Socialization::FollowStore
end
@@ -35,7 +42,14 @@ def acts_as_likeable(opts = nil)
end
# Make the current class a {Socialization::LikeStore}
+ # @param [Hash] opts the options to create a message with.
+ # @option opts [Boolean] :touch_liker :touch value for belongs_to :liker association
+ # @option opts [Boolean] :touch_likeable :touch value for belongs_to :likeable association
def acts_as_like_store(opts = nil)
+ opts ||= {}
+ belongs_to :liker, :polymorphic => true, :touch => opts[:touch_liker] || false
+ belongs_to :likeable, :polymorphic => true, :touch => opts[:touch_likeable] || false
+ validates_uniqueness_of :likeable_type, :scope => [:likeable_id, :liker_type, :liker_id], :message => 'You cannot like the same thing twice.'
include Socialization::LikeStore
end
@@ -50,7 +64,14 @@ def acts_as_mentionable(opts = nil)
end
# Make the current class a {Socialization::MentionStore}
+ # @param [Hash] opts the options to create a message with.
+ # @option opts [Boolean] :touch_mentioner :touch value for belongs_to :mentioner association
+ # @option opts [Boolean] :touch_mentionable :touch value for belongs_to :mentionable association
def acts_as_mention_store(opts = nil)
+ opts ||= {}
+ belongs_to :mentioner, :polymorphic => true, :touch => opts[:touch_mentioner] || false
+ belongs_to :mentionable, :polymorphic => true, :touch => opts[:touch_mentionable] || false
+ validates_uniqueness_of :mentionable_type, :scope => [:mentionable_id, :mentioner_type, :mentioner_id], :message => 'You cannot mention the same thing twice in a given object.'
include Socialization::MentionStore
end
@@ -3,11 +3,6 @@ module FollowStore
extend ActiveSupport::Concern
included do
- belongs_to :follower, :polymorphic => true
- belongs_to :followable, :polymorphic => true
-
- validates_uniqueness_of :followable_type, :scope => [:followable_id, :follower_type, :follower_id], :message => 'You cannot follow the same thing twice.'
-
def self.human_attribute_name(*args); ''; end
end
end
@@ -3,11 +3,6 @@ module LikeStore
extend ActiveSupport::Concern
included do
- belongs_to :liker, :polymorphic => true
- belongs_to :likeable, :polymorphic => true
-
- validates_uniqueness_of :likeable_type, :scope => [:likeable_id, :liker_type, :liker_id], :message => 'You cannot like the same thing twice.'
-
def self.human_attribute_name(*args); ''; end
end
end
@@ -3,11 +3,6 @@ module MentionStore
extend ActiveSupport::Concern
included do
- belongs_to :mentioner, :polymorphic => true
- belongs_to :mentionable, :polymorphic => true
-
- validates_uniqueness_of :mentionable_type, :scope => [:mentionable_id, :mentioner_type, :mentioner_id], :message => 'You cannot mention the same thing twice in a given object.'
-
def self.human_attribute_name(*args); ''; end
end
end
View
@@ -126,6 +126,17 @@ class FollowTest < Test::Unit::TestCase
end
end
+ context "acts_as_follow_store" do
+ should "touch associated record when touch_follower and/or touch_followable are set" do
+ class Foo < ActiveRecord::Base
+ self.table_name = 'follows'; acts_as_follow_store touch_follower: true, touch_followable: true
+ end
+ f = Foo.new
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_followable)
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_follower)
+ end
+ end
+
def seed
@follower1 = ImAFollower.create
@follower2 = ImAFollower.create
View
@@ -126,6 +126,17 @@ class LikeTest < Test::Unit::TestCase
end
end
+ context "acts_as_like_store" do
+ should "touch associated record when touch_liker and/or touch_likeable are set" do
+ class Foo < ActiveRecord::Base
+ self.table_name = 'likes'; acts_as_like_store touch_liker: true, touch_likeable: true
+ end
+ f = Foo.new
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_likeable)
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_liker)
+ end
+ end
+
context "Single Table Inheritance" do
setup do
@liker = ImALiker.create
View
@@ -130,6 +130,17 @@ class MentionTest < Test::Unit::TestCase
end
end
+ context "acts_as_mention_store" do
+ should "touch associated record when touch_mentioner and/or touch_mentionable are set" do
+ class Foo < ActiveRecord::Base
+ self.table_name = 'mentions'; acts_as_mention_store touch_mentioner: true, touch_mentionable: true
+ end
+ f = Foo.new
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_mentionable)
+ assert f.methods.include?(:belongs_to_touch_after_save_or_destroy_for_mentioner)
+ end
+ end
+
context "Single Table Inheritance" do
setup do
@mentioner = ImAMentioner.create

0 comments on commit 9d2a164

Please sign in to comment.