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
-
#rules(set = nil) ⇒ Object
Get rules for an activity instance.
- #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
35 36 37 38 39 40 41 |
# File 'lib/simple_activity/models/activity.rb', line 35 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
16 17 18 19 20 |
# File 'lib/simple_activity/models/activity.rb', line 16 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
23 24 25 26 27 |
# File 'lib/simple_activity/models/activity.rb', line 23 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
58 59 60 |
# File 'lib/simple_activity/models/activity.rb', line 58 def actor actor_type.capitalize.constantize.find(actor_id) end |
#cache ⇒ Object
29 30 31 |
# File 'lib/simple_activity/models/activity.rb', line 29 def cache read_attribute(:cache) || [] end |
#can_display? ⇒ Boolean
66 67 68 |
# File 'lib/simple_activity/models/activity.rb', line 66 def can_display? display end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
43 44 45 |
# File 'lib/simple_activity/models/activity.rb', line 43 def respond_to_missing?(method_name, include_private = false) method_name.to_s.match /(actor|target)_.*/ || super end |
#rules(set = nil) ⇒ Object
Get rules for an activity instance. This can’t replace similar method in Processor
74 75 76 |
# File 'lib/simple_activity/models/activity.rb', line 74 def rules(set=nil) @rules ||= Rule.get_rules(self.target_type, set) end |
#target ⇒ Object
62 63 64 |
# File 'lib/simple_activity/models/activity.rb', line 62 def target target_type.capitalize.constantize.find(target_id) end |
#update_cache(cache_rule) ⇒ Object
TODO: Untested
48 49 50 51 52 53 54 55 56 |
# File 'lib/simple_activity/models/activity.rb', line 48 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 |