Class: ActsPresentably::Presenters::DefaultPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_presentably/presenters/default_presenter.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ DefaultPresenter



31
32
33
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 31

def initialize object
  @object = object
end

Class Method Details

.as_json_using(object, options, presenter_key) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 83

def self.as_json_using object, options, presenter_key
  if options && options.has_key?(presenter_key)
     options[presenter_key].new(object).as_json(options)
   else
     self.new(object).as_json(options)
   end
end

.associations(*associations) ⇒ Object



12
13
14
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 12

def self.associations(*associations)
  associations.any? ? (@associations = associations) : @associations
end

.fields(*fields) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 4

def self.fields(*fields)
  if fields.any?
    @fields = fields
  else
    @fields
  end
end

.parse_options(attributes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 16

def self.parse_options(attributes)
  attributes.collect do |attribute_or_options|
    case attribute_or_options
    when Hash
      key = attribute_or_options[:key]
      display_as = attribute_or_options[:display_as]
    else
      key = attribute_or_options
      display_as = attribute_or_options
    end
    {:key => key, :display_as => display_as}
  end
end

Instance Method Details

#add_root(hash) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 55

def add_root(hash)
   if include_root
     custom_root = @options && @options[:root]
     hash = { custom_root || @object.class.model_name.element => hash }
   end
   hash
end

#as_json(options = {}) ⇒ Object



43
44
45
46
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 43

def as_json(options={})
  @options = options
  add_root(include_related_in build_hash)
end

#associationsObject



39
40
41
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 39

def associations
  self.class.associations || []
end

#build_hashObject



67
68
69
70
71
72
73
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 67

def build_hash
  hash = {}
  fields.each do |field|
    hash[field] = @object.send field
  end
  hash
end

#fieldsObject



35
36
37
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 35

def fields
  self.class.fields || [:id]
end


75
76
77
78
79
80
81
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 75

def include_related_in hash
  hash.tap do |hash|
    associations.each do |assoc|
      hash[assoc] = @object.send(assoc).as_json(@options)
    end
  end
end

#include_rootObject



48
49
50
51
52
53
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 48

def include_root
  unless defined?(@include_root)
    @include_root = @object.respond_to?(:include_root_in_json) && @object.include_root_in_json
  end
  @include_root
end

#object_nameObject



63
64
65
# File 'lib/acts_presentably/presenters/default_presenter.rb', line 63

def object_name
  @object.class.name
end