Class: SimpleActivity::Activity
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SimpleActivity::Activity
- Defined in:
- lib/simple_activity/models/activity.rb
Direct Known Subclasses
Class Method Summary collapse
-
.actor_activities(obj) ⇒ Object
Show activities belongs to an actor.
-
.target_activities(obj) ⇒ Object
Show activities belongs to a target.
Instance Method Summary collapse
- #actor ⇒ Object
- #cache ⇒ Object
- #can_display? ⇒ Boolean
-
#method_missing(method_name, *arguments, &block) ⇒ Object
Delegate the methods start with “actor_” or “target_” to cached result.
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
- #target ⇒ Object
-
#update_cache(cache_rule) ⇒ Object
TODO: Untested.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *arguments, &block) ⇒ Object
Delegate the methods start with “actor_” or “target_” to cached result
33 34 35 36 37 38 39 |
# File 'lib/simple_activity/models/activity.rb', line 33 def method_missing(method_name, *arguments, &block) if method_name.to_s =~ /(actor|target)_(?!type|id).*/ self.cache.try(:[], method_name.to_s) else super end end |
Class Method Details
.actor_activities(obj) ⇒ Object
Show activities belongs to an actor
14 15 16 17 18 |
# File 'lib/simple_activity/models/activity.rb', line 14 def self.actor_activities(obj) type = obj.class.to_s id = obj.id self.where(actor_type: type, actor_id: id) end |
.target_activities(obj) ⇒ Object
Show activities belongs to a target
21 22 23 24 25 |
# File 'lib/simple_activity/models/activity.rb', line 21 def self.target_activities(obj) type = obj.class.to_s id = obj.id self.where(target_type: type, target_id: id) end |
Instance Method Details
#actor ⇒ Object
56 57 58 |
# File 'lib/simple_activity/models/activity.rb', line 56 def actor actor_type.capitalize.constantize.find(actor_id) end |
#cache ⇒ Object
27 28 29 |
# File 'lib/simple_activity/models/activity.rb', line 27 def cache read_attribute(:cache) || [] end |
#can_display? ⇒ Boolean
64 65 66 |
# File 'lib/simple_activity/models/activity.rb', line 64 def can_display? display end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
41 42 43 |
# File 'lib/simple_activity/models/activity.rb', line 41 def respond_to_missing?(method_name, include_private = false) method_name.to_s.match /(actor|target)_.*/ || super end |
#target ⇒ Object
60 61 62 |
# File 'lib/simple_activity/models/activity.rb', line 60 def target target_type.capitalize.constantize.find(target_id) end |
#update_cache(cache_rule) ⇒ Object
TODO: Untested
46 47 48 49 50 51 52 53 54 |
# File 'lib/simple_activity/models/activity.rb', line 46 def update_cache(cache_rule) cache_rule.each do |type, type_methods| type_methods.each do |method| value = self.send(type).send(method) self.cache["#{type}_#{method}"] = value end end save end |