Permalink
Browse files

On my way to a cleaner, less repetitive codebase.

  • Loading branch information...
1 parent 694228e commit 28402702bb9146cf3f1c6552c55ddbdc376845fb @cmer committed Jun 7, 2012
View
@@ -1,4 +1,19 @@
-require 'socialization/config'
-require 'socialization/acts_as_helpers'
+%w(
+ stores/mixins/base.rb
+ stores/mixins/**/*.rb
+ stores/active_record/mixins/base.rb
+ stores/active_record/mixins/**/*.rb
+ stores/redis/mixins/base.rb
+ stores/redis/mixins/**/*.rb
+ stores/active_record/**/*.rb
+ stores/redis/base.rb
+ stores/redis/**/*.rb
+ actors/**/*.rb
+ victims/**/*.rb
+ helpers/**/*.rb
+ config/**/*.rb
+).each do |path|
+ Dir["#{File.dirname(__FILE__)}/socialization/#{path}"].each { |f| require(f) }
+end
ActiveRecord::Base.send :include, Socialization::ActsAsHelpers
@@ -1,9 +1,5 @@
require 'active_support/concern'
-%w(helpers stores actors victims).each do |path|
- Dir["#{File.dirname(__FILE__)}/#{path}/**/*.rb"].each { |f| require(f) }
-end
-
module Socialization
module ActsAsHelpers
extend ActiveSupport::Concern
@@ -1,6 +1,9 @@
module Socialization
module ActiveRecordStores
class Follow < ActiveRecord::Base
+ include Socialization::ActiveRecordStores::Mixins::Base
+ include Socialization::Stores::Mixins::Follow
+
belongs_to :follower, :polymorphic => true
belongs_to :followable, :polymorphic => true
@@ -14,9 +17,6 @@ class Follow < ActiveRecord::Base
:followable_id => followable.id)
}
- @@after_create_hook = nil
- @@after_destroy_hook = nil
-
class << self
def follow!(follower, followable)
unless follows?(follower, followable)
@@ -1,139 +0,0 @@
-module Socialization
- module ActiveRecordStores
- class Follow < ActiveRecord::Base
- belongs_to :follower, :polymorphic => true
- belongs_to :followable, :polymorphic => true
-
- scope :followed_by, lambda { |follower| where(
- :follower_type => follower.class.table_name.classify,
- :follower_id => follower.id)
- }
-
- scope :following, lambda { |followable| where(
- :followable_type => followable.class.table_name.classify,
- :followable_id => followable.id)
- }
-
- @@after_create_hook = nil
- @@after_destroy_hook = nil
-
- class << self
- def follow!(follower, followable)
- unless follows?(follower, followable)
- self.create! do |follow|
- 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)
- true
- else
- false
- end
- end
-
- 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)
- true
- else
- false
- end
- end
-
- def follows?(follower, followable)
- !follow_for(follower, followable).empty?
- end
-
- # Returns an ActiveRecord::Relation of all the followers of a certain type that are following followable
- def followers_relation(followable, klass, opts = {})
- rel = klass.where(:id =>
- self.select(:follower_id).
- where(:follower_type => klass.table_name.classify).
- where(:followable_type => followable.class.to_s).
- where(:followable_id => followable.id)
- )
-
- if opts[:pluck]
- rel.pluck(opts[:pluck])
- else
- rel
- end
- end
-
- # Returns all the followers of a certain type that are following followable
- def followers(followable, klass, opts = {})
- rel = followers_relation(followable, klass, opts)
- if rel.is_a?(ActiveRecord::Relation)
- rel.all
- else
- rel
- end
- end
-
- # Returns an ActiveRecord::Relation of all the followables of a certain type that are followed by follower
- def followables_relation(follower, klass, opts = {})
- rel = klass.where(:id =>
- self.select(:followable_id).
- where(:followable_type => klass.table_name.classify).
- where(:follower_type => follower.class.to_s).
- where(:follower_id => follower.id)
- )
-
- if opts[:pluck]
- rel.pluck(opts[:pluck])
- else
- rel
- end
- end
-
- # Returns all the followables of a certain type that are followed by follower
- def followables(follower, klass, opts = {})
- rel = followables_relation(follower, klass, opts)
- if rel.is_a?(ActiveRecord::Relation)
- rel.all
- else
- rel
- 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
- end # class << self
-
- end
- end
-end
@@ -1,6 +1,9 @@
module Socialization
module ActiveRecordStores
class Like < ActiveRecord::Base
+ include Socialization::ActiveRecordStores::Mixins::Base
+ include Socialization::Stores::Mixins::Like
+
belongs_to :liker, :polymorphic => true
belongs_to :likeable, :polymorphic => true
@@ -14,9 +17,6 @@ class Like < ActiveRecord::Base
:likeable_id => likeable.id)
}
- @@after_create_hook = nil
- @@after_destroy_hook = nil
-
class << self
def like!(liker, likeable)
unless likes?(liker, likeable)
@@ -1,6 +1,9 @@
module Socialization
module ActiveRecordStores
class Mention < ActiveRecord::Base
+ include Socialization::ActiveRecordStores::Mixins::Base
+ include Socialization::Stores::Mixins::Mention
+
belongs_to :mentioner, :polymorphic => true
belongs_to :mentionable, :polymorphic => true
@@ -14,9 +17,6 @@ class Mention < ActiveRecord::Base
:mentionable_id => mentionable.id)
}
- @@after_create_hook = nil
- @@after_destroy_hook = nil
-
class << self
def mention!(mentioner, mentionable)
unless mentions?(mentioner, mentionable)
@@ -0,0 +1,9 @@
+module Socialization
+ module ActiveRecordStores
+ module Mixins
+ module Base
+ include Socialization::Stores::Mixins::Base
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+module Socialization
+ module Stores
+ module Mixins
+ module Base
+ @@after_create_hook = nil
+ @@after_destroy_hook = nil
+ end
+ end
+ end
+end
@@ -0,0 +1,8 @@
+module Socialization
+ module Stores
+ module Mixins
+ module Follow
+ end
+ end
+ end
+end
@@ -0,0 +1,8 @@
+module Socialization
+ module Stores
+ module Mixins
+ module Like
+ end
+ end
+ end
+end
@@ -0,0 +1,8 @@
+module Socialization
+ module Stores
+ module Mixins
+ module Mention
+ end
+ end
+ end
+end
@@ -1,8 +1,6 @@
module Socialization
module RedisStores
class Base
- @@after_create_hook = nil
- @@after_destroy_hook = nil
end
end
end
@@ -1,8 +1,11 @@
-require File.expand_path(File.dirname(__FILE__)) + '/base'
+# require File.expand_path(File.dirname(__FILE__)) + '/base'
module Socialization
module RedisStores
class Follow < Socialization::RedisStores::Base
+ include Socialization::RedisStores::Mixins::Base
+ include Socialization::Stores::Mixins::Follow
+
class << self
def follow!(follower, followable)
unless follows?(follower, followable)
@@ -1,8 +1,11 @@
-require File.expand_path(File.dirname(__FILE__)) + '/base'
+# require File.expand_path(File.dirname(__FILE__)) + '/base'
module Socialization
module RedisStores
class Like < Socialization::RedisStores::Base
+ include Socialization::RedisStores::Mixins::Base
+ include Socialization::Stores::Mixins::Follow
+
class << self
def like!(liker, likeable)
unless likes?(liker, likeable)
@@ -1,8 +1,11 @@
-require File.expand_path(File.dirname(__FILE__)) + '/base'
+# require File.expand_path(File.dirname(__FILE__)) + '/base'
module Socialization
module RedisStores
class Mention < Socialization::RedisStores::Base
+ include Socialization::RedisStores::Mixins::Base
+ include Socialization::Stores::Mixins::Mention
+
class << self
def mention!(mentioner, mentionable)
unless mentions?(mentioner, mentionable)
@@ -0,0 +1,8 @@
+module Socialization
+ module RedisStores
+ module Mixins
+ module Base
+ end
+ end
+ end
+end
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__))+'/../../test_helper'
class ActiveRecordLikeStoreTest < Test::Unit::TestCase
- context "ActiveRecordStore::LikeStoreTest" do
+ context "ActiveRecordStores::LikeStoreTest" do
setup do
@klass = Socialization::ActiveRecordStores::Like
@klass.touch nil

0 comments on commit 2840270

Please sign in to comment.