Class: Activr::Entity

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(name, value, options = { }) ⇒ Entity

Returns a new instance of Entity.

Parameters:

  • name (String)

    Entity name

  • value (Object)

    Model instance or model id

  • options (Hash) (defaults to: { })

    Options hash

Options Hash (options):

  • :activity (Activity)

    Entity belongs to that activity

  • :class (Class)

    Entity model class

  • :humanize (Symbol)

    A method name to call on entity model instance to humanize it

  • :default (String)

    Default humanization value

  • :optional (true, false)

    Is it an optional 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, options = { })
  @name = name
  @options = 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}: #{options.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

#activityActivity (readonly)

Returns activity owning that entity.

Returns:

  • (Activity)

    activity owning that entity



17
18
19
# File 'lib/activr/entity.rb', line 17

def activity
  @activity
end

#model_classClass (readonly)

Returns entity model class.

Returns:

  • (Class)

    entity model class



20
21
22
# File 'lib/activr/entity.rb', line 20

def model_class
  @model_class
end

#model_idObjecy (readonly)

Returns entity model id.

Returns:

  • (Objecy)

    entity model id



23
24
25
# File 'lib/activr/entity.rb', line 23

def model_id
  @model_id
end

#nameSymbol (readonly)

Returns entity name.

Returns:

  • (Symbol)

    entity name



11
12
13
# File 'lib/activr/entity.rb', line 11

def name
  @name
end

#optionsHash (readonly)

Returns entity options.

Returns:

  • (Hash)

    entity options



14
15
16
# File 'lib/activr/entity.rb', line 14

def options
  @options
end

Instance Method Details

#humanize(options = { }) ⇒ String

Humanize entity

Parameters:

  • options (hash) (defaults to: { })

    Options

Options Hash (options):

  • :html (true, false)

    Generate HTML ?

Returns:

  • (String)

    Humanized sentence



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(options = { })
  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, options)
      htmlized = true
    else
      result = self.model.__send__(humanize_meth)
    end
  end

  if result.nil? && @options[:default]
    result = @options[:default]
  end

  if !result.nil? && options[: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

#modelObject

Get model instance

Returns:

  • (Object)

    Model instance



58
59
60
# File 'lib/activr/entity.rb', line 58

def model
  @model ||= self.model_class.find(self.model_id)
end