Class: Activr::Entity
- Inherits:
-
Object
- Object
- Activr::Entity
- Defined in:
- lib/activr/entity.rb
Overview
An Entity represents one of your application model involved in activities
Defined Under Namespace
Modules: ModelMixin
Instance Attribute Summary collapse
-
#activity ⇒ Activity
readonly
Activity owning that entity.
-
#model_class ⇒ Class
readonly
Entity model class.
-
#model_id ⇒ Objecy
readonly
Entity model id.
-
#name ⇒ Symbol
readonly
Entity name.
-
#options ⇒ Hash
readonly
Entity options.
Instance Method Summary collapse
-
#humanize(options = { }) ⇒ String
Humanize entity.
-
#initialize(name, value, options = { }) ⇒ Entity
constructor
A new instance of Entity.
-
#model ⇒ Object
Get model instance.
Constructor Details
#initialize(name, value, options = { }) ⇒ Entity
Returns a new instance of Entity.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/activr/entity.rb', line 31 def initialize(name, value, = { }) @name = name @options = .dup @activity = @options.delete(:activity) @model_class = @options.delete(:class) if Activr.storage.valid_id?(value) @model_id = value raise "Missing :class option for #{name} / #{value}: #{.inspect}" if @model_class.nil? raise "Model class MUST implement #find method" unless @model_class.respond_to?(:find) else @model = value if (@model_class && (@model_class != @model.class)) raise "Model class mismatch: #{@model_class} != #{@model.class}" end @model_class ||= @model.class @model_id = @model.id end end |
Instance Attribute Details
#activity ⇒ Activity (readonly)
17 18 19 |
# File 'lib/activr/entity.rb', line 17 def activity @activity end |
#model_class ⇒ Class (readonly)
20 21 22 |
# File 'lib/activr/entity.rb', line 20 def model_class @model_class end |
#model_id ⇒ Objecy (readonly)
23 24 25 |
# File 'lib/activr/entity.rb', line 23 def model_id @model_id end |
#name ⇒ Symbol (readonly)
11 12 13 |
# File 'lib/activr/entity.rb', line 11 def name @name end |
#options ⇒ Hash (readonly)
14 15 16 |
# File 'lib/activr/entity.rb', line 14 def @options end |
Instance Method Details
#humanize(options = { }) ⇒ String
Humanize entity
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/activr/entity.rb', line 67 def humanize( = { }) result = nil htmlized = false humanize_meth = @options[:humanize] if humanize_meth.nil? && (self.model.respond_to?(:humanize)) humanize_meth = :humanize end if humanize_meth case self.model.method(humanize_meth).arity when 1 result = self.model.__send__(humanize_meth, ) htmlized = true else result = self.model.__send__(humanize_meth) end end if result.nil? && @options[:default] result = @options[:default] end if !result.nil? && [:html] && !htmlized && Activr::RailsCtx.view_context # let Rails sanitize and htmlize the entity result = Activr::RailsCtx.view_context.sanitize(result) result = Activr::RailsCtx.view_context.link_to(result, self.model) end result ||= "" result end |
#model ⇒ Object
Get model instance
58 59 60 |
# File 'lib/activr/entity.rb', line 58 def model @model ||= self.model_class.find(self.model_id) end |