Module: RoarExtensions::Presenter

Included in:
MoneyPresenter, PaginatedCollectionPresenter
Defined in:
lib/roar_extensions/presenter.rb

Defined Under Namespace

Modules: ConditionalEmbeds

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/roar_extensions/presenter.rb', line 25

def self.included(base)
  base.send(:include, Roar::Representer::JSON::HAL)
  base.send(:include, ConditionalEmbeds)
  base.extend(RoarExtensions::Representer)

  base.class_eval do
    alias :as_json :to_hash

    def self.delegated_property(name, options = {})
      if from = options.delete(:from)
        define_method(name) do
          record.send(from)
        end
      else
        delegate name, :to => :record, :allow_nil => true
      end

      property name, options
    end


    def self.delegated_collection(name, options ={})
      if from = options.delete(:from)
        define_method(name) do
          record.send(from)
        end
      else
        delegate name, :to => :record, :allow_nil => true
      end

      collection name, options
    end

  end
end

Instance Method Details

#initialize(record) ⇒ Object



20
21
22
23
# File 'lib/roar_extensions/presenter.rb', line 20

def initialize(record)
  @record = record
  @embedded ||= []
end