Module: Partisan::Follower
- Extended by:
- ActiveModel::Callbacks, ActiveSupport::Concern
- Defined in:
- lib/partisan/follower.rb
Instance Method Summary collapse
-
#fetch_follows(resource) ⇒ Follow, ...
Get all follows record related to a resource.
-
#follow(resource) ⇒ Follow
(also: #start_following)
Add follow record if it doesn’t exist.
-
#following_by_type(followable_type) ⇒ ActiveRecord::Relation
Get resource records for a specific type, used by #method_missing It conveniently returns an ActiveRecord::Relation for easy chaining of useful ActiveRecord methods.
-
#following_fields_by_type(followable_type, followable_field) ⇒ Array
Get ids of resources following self Use #pluck for an optimized sql query.
-
#follows?(resource) ⇒ Boolean
(also: #following?)
Return true or false if the resource is following another.
- #method_missing(m, *args) ⇒ Object
- #respond_to?(m, include_private = false) ⇒ Boolean
-
#unfollow(resource) ⇒ Follow
(also: #stop_following)
Delete follow record if it exists.
-
#update_followings_counter ⇒ Object
Update cache counter Called in after_create and after_destroy.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# File 'lib/partisan/follower.rb', line 103 def method_missing(m, *args) if m.to_s[/following_(.+)_(.+)s$/] following_fields_by_type($1.singularize.classify, $2) elsif m.to_s[/following_(.+)/] following_by_type($1.singularize.classify) else super end end |
Instance Method Details
#fetch_follows(resource) ⇒ Follow, ...
Get all follows record related to a resource
64 65 66 |
# File 'lib/partisan/follower.rb', line 64 def fetch_follows(resource) follows.where followable_id: resource.id, followable_type: Partisan::Helper.parent_class_name(resource) end |
#follow(resource) ⇒ Follow Also known as: start_following
Add follow record if it doesn’t exist
20 21 22 23 24 25 |
# File 'lib/partisan/follower.rb', line 20 def follow(resource) return if self == resource follow = fetch_follows(resource).first_or_initialize follow.save! end |
#following_by_type(followable_type) ⇒ ActiveRecord::Relation
Get resource records for a specific type, used by #method_missing It conveniently returns an ActiveRecord::Relation for easy chaining of useful ActiveRecord methods
76 77 78 79 80 81 82 83 |
# File 'lib/partisan/follower.rb', line 76 def following_by_type(followable_type) opts = { 'follows.follower_id' => self.id, 'follows.follower_type' => Partisan::Helper.parent_class_name(self) } followable_type.constantize.joins(:followings).where(opts) end |
#following_fields_by_type(followable_type, followable_field) ⇒ Array
Get ids of resources following self Use #pluck for an optimized sql query
93 94 95 |
# File 'lib/partisan/follower.rb', line 93 def following_fields_by_type(followable_type, followable_field) following_by_type(followable_type).pluck("#{followable_type.tableize}.#{followable_field}") end |
#follows?(resource) ⇒ Boolean Also known as: following?
Return true or false if the resource is following another
50 51 52 53 54 |
# File 'lib/partisan/follower.rb', line 50 def follows?(resource) return false if self == resource !!fetch_follows(resource).exists? end |
#respond_to?(m, include_private = false) ⇒ Boolean
113 114 115 |
# File 'lib/partisan/follower.rb', line 113 def respond_to?(m, include_private = false) super || m.to_s[/following_(.+)_(.+)s$/] || m.to_s[/following_(.+)/] end |
#unfollow(resource) ⇒ Follow Also known as: stop_following
Delete follow record if it exists
35 36 37 38 39 40 |
# File 'lib/partisan/follower.rb', line 35 def unfollow(resource) return if self == resource record = fetch_follows(resource).first record.try(:destroy) end |
#update_followings_counter ⇒ Object
Update cache counter Called in after_create and after_destroy
99 100 101 |
# File 'lib/partisan/follower.rb', line 99 def update_followings_counter self.update_attribute('followings_count', self.follows.count) if self.respond_to?(:followings_count) end |