Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored and DRYed up stores

  • Loading branch information...
commit 8a044d8fb42ff19733c0d7e14a1f46e6359a865c 1 parent 512c4be
@cmer authored
View
40 lib/socialization/stores/active_record/follow.rb
@@ -1,8 +1,9 @@
module Socialization
module ActiveRecordStores
class Follow < ActiveRecord::Base
- include Socialization::ActiveRecordStores::Mixins::Base
- include Socialization::Stores::Mixins::Follow
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Follow
+ extend Socialization::ActiveRecordStores::Mixins::Base
belongs_to :follower, :polymorphic => true
belongs_to :followable, :polymorphic => true
@@ -24,9 +25,7 @@ def follow!(follower, followable)
follow.follower = follower
follow.followable = followable
end
- call_after_create_hook(follower, followable)
- follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
- followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ call_after_create_hooks(follower, followable)
true
else
false
@@ -36,9 +35,7 @@ def follow!(follower, followable)
def unfollow!(follower, followable)
if follows?(follower, followable)
follow_for(follower, followable).destroy_all
- call_after_destroy_hook(follower, followable)
- follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
- followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ call_after_destroy_hooks(follower, followable)
true
else
false
@@ -101,34 +98,7 @@ def followables(follower, klass, opts = {})
end
end
- def touch(what = nil)
- if what.nil?
- @touch || false
- else
- raise ArgumentError unless [:all, :follower, :followable, false, nil].include?(what)
- @touch = what
- end
- end
-
- def after_follow(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_create_hook = method
- end
-
- def after_unfollow(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_destroy_hook = method
- end
-
private
- def call_after_create_hook(follower, followable)
- self.send(@after_create_hook, follower, followable) if @after_create_hook
- end
-
- def call_after_destroy_hook(follower, followable)
- self.send(@after_destroy_hook, follower, followable) if @after_destroy_hook
- end
-
def follow_for(follower, followable)
followed_by(follower).following(followable)
end
View
40 lib/socialization/stores/active_record/like.rb
@@ -1,8 +1,9 @@
module Socialization
module ActiveRecordStores
class Like < ActiveRecord::Base
- include Socialization::ActiveRecordStores::Mixins::Base
- include Socialization::Stores::Mixins::Like
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Like
+ extend Socialization::ActiveRecordStores::Mixins::Base
belongs_to :liker, :polymorphic => true
belongs_to :likeable, :polymorphic => true
@@ -24,9 +25,7 @@ def like!(liker, likeable)
like.liker = liker
like.likeable = likeable
end
- call_after_create_hook(liker, likeable)
- liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
- likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
+ call_after_create_hooks(liker, likeable)
true
else
false
@@ -36,9 +35,7 @@ def like!(liker, likeable)
def unlike!(liker, likeable)
if likes?(liker, likeable)
like_for(liker, likeable).destroy_all
- call_after_destroy_hook(liker, likeable)
- liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
- likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
+ call_after_destroy_hooks(liker, likeable)
true
else
false
@@ -101,34 +98,7 @@ def likeables(liker, klass, opts = {})
end
end
- def touch(what = nil)
- if what.nil?
- @touch || false
- else
- raise ArgumentError unless [:all, :liker, :likeable, false, nil].include?(what)
- @touch = what
- end
- end
-
- def after_like(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_create_hook = method
- end
-
- def after_unlike(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_destroy_hook = method
- end
-
private
- def call_after_create_hook(liker, likeable)
- self.send(@after_create_hook, liker, likeable) if @after_create_hook
- end
-
- def call_after_destroy_hook(liker, likeable)
- self.send(@after_destroy_hook, liker, likeable) if @after_destroy_hook
- end
-
def like_for(liker, likeable)
liked_by(liker).liking( likeable)
end
View
40 lib/socialization/stores/active_record/mention.rb
@@ -1,8 +1,9 @@
module Socialization
module ActiveRecordStores
class Mention < ActiveRecord::Base
- include Socialization::ActiveRecordStores::Mixins::Base
- include Socialization::Stores::Mixins::Mention
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Mention
+ extend Socialization::ActiveRecordStores::Mixins::Base
belongs_to :mentioner, :polymorphic => true
belongs_to :mentionable, :polymorphic => true
@@ -24,9 +25,7 @@ def mention!(mentioner, mentionable)
mention.mentioner = mentioner
mention.mentionable = mentionable
end
- call_after_create_hook(mentioner, mentionable)
- mentioner.touch if [:all, :mentioner].include?(touch) && mentioner.respond_to?(:touch)
- mentionable.touch if [:all, :mentionable].include?(touch) && mentionable.respond_to?(:touch)
+ call_after_create_hooks(mentioner, mentionable)
true
else
false
@@ -36,9 +35,7 @@ def mention!(mentioner, mentionable)
def unmention!(mentioner, mentionable)
if mentions?(mentioner, mentionable)
mention_for(mentioner, mentionable).destroy_all
- call_after_destroy_hook(mentioner, mentionable)
- mentioner.touch if [:all, :mentioner].include?(touch) && mentioner.respond_to?(:touch)
- mentionable.touch if [:all, :mentionable].include?(touch) && mentionable.respond_to?(:touch)
+ call_after_destroy_hooks(mentioner, mentionable)
true
else
false
@@ -101,34 +98,7 @@ def mentionables(mentioner, klass, opts = {})
end
end
- def touch(what = nil)
- if what.nil?
- @touch || false
- else
- raise ArgumentError unless [:all, :mentioner, :mentionable, false, nil].include?(what)
- @touch = what
- end
- end
-
- def after_mention(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_create_hook = method
- end
-
- def after_unmention(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_destroy_hook = method
- end
-
private
- def call_after_create_hook(mentioner, mentionable)
- self.send(@after_create_hook, mentioner, mentionable) if @after_create_hook
- end
-
- def call_after_destroy_hook(mentioner, mentionable)
- self.send(@after_destroy_hook, mentioner, mentionable) if @after_destroy_hook
- end
-
def mention_for(mentioner, mentionable)
mentioned_by(mentioner).mentioning(mentionable)
end
View
1  lib/socialization/stores/active_record/mixins/base.rb
@@ -2,7 +2,6 @@ module Socialization
module ActiveRecordStores
module Mixins
module Base
- include Socialization::Stores::Mixins::Base
end
end
end
View
16 lib/socialization/stores/mixins/base.rb
@@ -2,8 +2,20 @@ module Socialization
module Stores
module Mixins
module Base
- @@after_create_hook = nil
- @@after_destroy_hook = nil
+ def touch_dependents(actor, victim)
+ actor.touch if touch_actor?(actor)
+ victim.touch if touch_victim?(victim)
+ end
+
+ def touch_actor?(actor)
+ return false unless actor.respond_to?(:touch)
+ touch == :all || touch.to_s =~ /er$/i
+ end
+
+ def touch_victim?(victim)
+ return false unless victim.respond_to?(:touch)
+ touch == :all || touch.to_s =~ /able$/i
+ end
end
end
end
View
31 lib/socialization/stores/mixins/follow.rb
@@ -2,6 +2,37 @@ module Socialization
module Stores
module Mixins
module Follow
+
+ public
+ def touch(what = nil)
+ if what.nil?
+ @touch || false
+ else
+ raise ArgumentError unless [:all, :follower, :followable, false, nil].include?(what)
+ @touch = what
+ end
+ end
+
+ def after_follow(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_create_hook = method
+ end
+
+ def after_unfollow(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_destroy_hook = method
+ end
+
+ protected
+ def call_after_create_hooks(follower, followable)
+ self.send(@after_create_hook, follower, followable) if @after_create_hook
+ touch_dependents(follower, followable)
+ end
+
+ def call_after_destroy_hooks(follower, followable)
+ self.send(@after_destroy_hook, follower, followable) if @after_destroy_hook
+ touch_dependents(follower, followable)
+ end
end
end
end
View
32 lib/socialization/stores/mixins/like.rb
@@ -2,6 +2,38 @@ module Socialization
module Stores
module Mixins
module Like
+
+ public
+ def touch(what = nil)
+ if what.nil?
+ @touch || false
+ else
+ raise ArgumentError unless [:all, :liker, :likeable, false, nil].include?(what)
+ @touch = what
+ end
+ end
+
+ def after_like(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_create_hook = method
+ end
+
+ def after_unlike(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_destroy_hook = method
+ end
+
+ protected
+ def call_after_create_hooks(liker, likeable)
+ self.send(@after_create_hook, liker, likeable) if @after_create_hook
+ touch_dependents(liker, likeable)
+ end
+
+ def call_after_destroy_hooks(liker, likeable)
+ self.send(@after_destroy_hook, liker, likeable) if @after_destroy_hook
+ touch_dependents(liker, likeable)
+ end
+
end
end
end
View
32 lib/socialization/stores/mixins/mention.rb
@@ -2,6 +2,38 @@ module Socialization
module Stores
module Mixins
module Mention
+
+ public
+ def touch(what = nil)
+ if what.nil?
+ @touch || false
+ else
+ raise ArgumentError unless [:all, :mentioner, :mentionable, false, nil].include?(what)
+ @touch = what
+ end
+ end
+
+ def after_mention(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_create_hook = method
+ end
+
+ def after_unmention(method)
+ raise ArgumentError unless method.is_a?(Symbol) || method.nil?
+ @after_destroy_hook = method
+ end
+
+ protected
+ def call_after_create_hooks(mentioner, mentionable)
+ self.send(@after_create_hook, mentioner, mentionable) if @after_create_hook
+ touch_dependents(mentioner, mentionable)
+ end
+
+ def call_after_destroy_hooks(mentioner, mentionable)
+ self.send(@after_destroy_hook, mentioner, mentionable) if @after_destroy_hook
+ touch_dependents(mentioner, mentionable)
+ end
+
end
end
end
View
40 lib/socialization/stores/redis/follow.rb
@@ -3,8 +3,9 @@
module Socialization
module RedisStores
class Follow < Socialization::RedisStores::Base
- include Socialization::RedisStores::Mixins::Base
- include Socialization::Stores::Mixins::Follow
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Follow
+ extend Socialization::RedisStores::Mixins::Base
class << self
def follow!(follower, followable)
@@ -12,9 +13,7 @@ def follow!(follower, followable)
Socialization.redis.sadd generate_followers_key(follower, followable), follower.id
Socialization.redis.sadd generate_followables_key(follower, followable), followable.id
- call_after_create_hook(follower, followable)
- follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
- followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ call_after_create_hooks(follower, followable)
true
else
false
@@ -26,9 +25,7 @@ def unfollow!(follower, followable)
Socialization.redis.srem generate_followers_key(follower, followable), follower.id
Socialization.redis.srem generate_followables_key(follower, followable), followable.id
- call_after_destroy_hook(follower, followable)
- follower.touch if [:all, :follower].include?(touch) && follower.respond_to?(:touch)
- followable.touch if [:all, :followable].include?(touch) && followable.respond_to?(:touch)
+ call_after_destroy_hooks(follower, followable)
true
else
false
@@ -73,34 +70,7 @@ def followables(follower, klass, opts = {})
end
end
- def touch(what = nil)
- if what.nil?
- @touch || false
- else
- raise ArgumentError unless [:all, :follower, :followable, false, nil].include?(what)
- @touch = what
- end
- end
-
- def after_follow(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_create_hook = method
- end
-
- def after_unfollow(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_destroy_hook = method
- end
-
private
- def call_after_create_hook(follower, followable)
- self.send(@after_create_hook, follower, followable) if @after_create_hook
- end
-
- def call_after_destroy_hook(follower, followable)
- self.send(@after_destroy_hook, follower, followable) if @after_destroy_hook
- end
-
def generate_followers_key(follower, followable)
raise ArgumentError.new("`followable` needs to be an acts_as_followable objecs, not a class.") if followable.class == Class
follower_class = if follower.class == Class
View
13 lib/socialization/stores/redis/like.rb
@@ -3,8 +3,9 @@
module Socialization
module RedisStores
class Like < Socialization::RedisStores::Base
- include Socialization::RedisStores::Mixins::Base
- include Socialization::Stores::Mixins::Follow
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Like
+ extend Socialization::RedisStores::Mixins::Base
class << self
def like!(liker, likeable)
@@ -12,7 +13,7 @@ def like!(liker, likeable)
Socialization.redis.sadd generate_likers_key(liker, likeable), liker.id
Socialization.redis.sadd generate_likeables_key(liker, likeable), likeable.id
- call_after_create_hook(liker, likeable)
+ call_after_create_hooks(liker, likeable)
liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
true
@@ -26,7 +27,7 @@ def unlike!(liker, likeable)
Socialization.redis.srem generate_likers_key(liker, likeable), liker.id
Socialization.redis.srem generate_likeables_key(liker, likeable), likeable.id
- call_after_destroy_hook(liker, likeable)
+ call_after_destroy_hooks(liker, likeable)
liker.touch if [:all, :liker].include?(touch) && liker.respond_to?(:touch)
likeable.touch if [:all, :likeable].include?(touch) && likeable.respond_to?(:touch)
true
@@ -93,11 +94,11 @@ def after_unlike(method)
end
private
- def call_after_create_hook(liker, likeable)
+ def call_after_create_hooks(liker, likeable)
self.send(@after_create_hook, liker, likeable) if @after_create_hook
end
- def call_after_destroy_hook(liker, likeable)
+ def call_after_destroy_hooks(liker, likeable)
self.send(@after_destroy_hook, liker, likeable) if @after_destroy_hook
end
View
40 lib/socialization/stores/redis/mention.rb
@@ -3,8 +3,9 @@
module Socialization
module RedisStores
class Mention < Socialization::RedisStores::Base
- include Socialization::RedisStores::Mixins::Base
- include Socialization::Stores::Mixins::Mention
+ extend Socialization::Stores::Mixins::Base
+ extend Socialization::Stores::Mixins::Mention
+ extend Socialization::RedisStores::Mixins::Base
class << self
def mention!(mentioner, mentionable)
@@ -12,9 +13,7 @@ def mention!(mentioner, mentionable)
Socialization.redis.sadd generate_mentioners_key(mentioner, mentionable), mentioner.id
Socialization.redis.sadd generate_mentionables_key(mentioner, mentionable), mentionable.id
- call_after_create_hook(mentioner, mentionable)
- mentioner.touch if [:all, :mentioner].include?(touch) && mentioner.respond_to?(:touch)
- mentionable.touch if [:all, :mentionable].include?(touch) && mentionable.respond_to?(:touch)
+ call_after_create_hooks(mentioner, mentionable)
true
else
false
@@ -26,9 +25,7 @@ def unmention!(mentioner, mentionable)
Socialization.redis.srem generate_mentioners_key(mentioner, mentionable), mentioner.id
Socialization.redis.srem generate_mentionables_key(mentioner, mentionable), mentionable.id
- call_after_destroy_hook(mentioner, mentionable)
- mentioner.touch if [:all, :mentioner].include?(touch) && mentioner.respond_to?(:touch)
- mentionable.touch if [:all, :mentionable].include?(touch) && mentionable.respond_to?(:touch)
+ call_after_destroy_hooks(mentioner, mentionable)
true
else
false
@@ -73,34 +70,7 @@ def mentionables(mentioner, klass, opts = {})
end
end
- def touch(what = nil)
- if what.nil?
- @touch || false
- else
- raise ArgumentError unless [:all, :mentioner, :mentionable, false, nil].include?(what)
- @touch = what
- end
- end
-
- def after_mention(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_create_hook = method
- end
-
- def after_unmention(method)
- raise ArgumentError unless method.is_a?(Symbol) || method.nil?
- @after_destroy_hook = method
- end
-
private
- def call_after_create_hook(mentioner, mentionable)
- self.send(@after_create_hook, mentioner, mentionable) if @after_create_hook
- end
-
- def call_after_destroy_hook(mentioner, mentionable)
- self.send(@after_destroy_hook, mentioner, mentionable) if @after_destroy_hook
- end
-
def generate_mentioners_key(mentioner, mentionable)
raise ArgumentError.new("`mentionable` needs to be an acts_as_mentionable objecs, not a class.") if mentionable.class == Class
mentioner_class = if mentioner.class == Class
Please sign in to comment.
Something went wrong with that request. Please try again.