Module: Mongoid::Likeable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_likes/likeable.rb
Instance Method Summary collapse
-
#all_likers ⇒ Object
view all selfs likers.
-
#all_likers_by_model(model) ⇒ Object
view all selfs likers by model.
-
#common_likers_with(model) ⇒ Object
view all common likers of self against model.
-
#liker?(model) ⇒ Boolean
know if self is liked by model.
-
#likers_count ⇒ Object
get likers count Note: this is a cache counter.
-
#likers_count_by_model(model) ⇒ Object
get likers count by model.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(missing_method, *args, &block) ⇒ Object (private)
77 78 79 80 81 82 83 84 85 |
# File 'lib/mongoid_likes/likeable.rb', line 77 def method_missing(missing_method, *args, &block) if missing_method.to_s =~ /^(.+)_likers_count$/ likers_count_by_model($1.camelize) elsif missing_method.to_s =~ /^all_(.+)_likers$/ all_likers_by_model($1.camelize) else super end end |
Instance Method Details
#all_likers ⇒ Object
view all selfs likers
Example:
> @track.all_likers
> [@joe, @max]
43 44 45 |
# File 'lib/mongoid_likes/likeable.rb', line 43 def all_likers get_likers_of(self) end |
#all_likers_by_model(model) ⇒ Object
view all selfs likers by model
Example:
> @track.all_likers_by_model
> [@joe]
52 53 54 |
# File 'lib/mongoid_likes/likeable.rb', line 52 def all_likers_by_model(model) get_likers_of(self, model) end |
#common_likers_with(model) ⇒ Object
view all common likers of self against model
Example:
> @track.common_likers_with(@gang)
> [@joe, @max]
61 62 63 64 65 66 |
# File 'lib/mongoid_likes/likeable.rb', line 61 def common_likers_with(model) model_likers = get_likers_of(model) self_likers = get_likers_of(self) self_likers & model_likers end |
#liker?(model) ⇒ Boolean
know if self is liked by model
Example:
> @track.liker?(@joe)
> true
15 16 17 |
# File 'lib/mongoid_likes/likeable.rb', line 15 def liker?(model) self.likers_assoc.find(:all, conditions: {like_id: model.id}).limit(1).count > 0 end |
#likers_count ⇒ Object
get likers count Note: this is a cache counter
Example:
> @track.likers_count
> 1
25 26 27 |
# File 'lib/mongoid_likes/likeable.rb', line 25 def likers_count self.liked_count_field end |
#likers_count_by_model(model) ⇒ Object
get likers count by model
Example:
> @track.likers_count_by_model(User)
> 1
34 35 36 |
# File 'lib/mongoid_likes/likeable.rb', line 34 def likers_count_by_model(model) self.likers_assoc.where(:like_type => model.to_s).count end |