Class: RailsPresenter::Base

Inherits:
SimpleDelegator
  • Object
show all
Includes:
PresenterHelper
Defined in:
lib/rails_presenter/base.rb

Constant Summary collapse

@@nil_formatter =
'----'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_object, template) ⇒ Base

Returns a new instance of Base.



112
113
114
115
# File 'lib/rails_presenter/base.rb', line 112

def initialize(base_object, template)
  @template = template
  super(base_object)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rails_presenter/base.rb', line 162

def method_missing(method_name, *args)
  case method_name.to_s
  when /^h_(.*)$/
    get_iv_from_view($1)
  when /^get_(.*)$/
    return target if target.is_a? $1.camelize.constantize
    get_iv_from_view($1) || target.public_send($1)
  else
    super
  end
end

Class Method Details

.format(*attrs) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rails_presenter/base.rb', line 54

def format(*attrs)
  formatter = attrs.pop.values.first

  module_name = formatter.to_s.camelize

  unless const_defined? module_name
    include const_set(module_name, Module.new)
  end

  formatter_module = const_get(module_name)

  formatter_module.module_eval do
    attrs.each do |attr|
      define_method(attr) do
        h.public_send(formatter, super())
      end
    end
  end
end

.format_blank_attributes(*attrs) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_presenter/base.rb', line 80

def format_blank_attributes(*attrs)
  attr_module = Module.new do
    attrs.each do |attr|
      define_method(attr) do
        return nil_formatter if super().blank?
        super()
      end
    end
  end

  include const_set("BlankAttributes", attr_module)
end

.inherited(child_class) ⇒ Object



74
75
76
77
78
# File 'lib/rails_presenter/base.rb', line 74

def inherited(child_class)
  child_class.instance_eval do
    format_blank_attributes(*possible_blank_attributes) if based_on_active_record?
  end
end

.location(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rails_presenter/base.rb', line 10

def location(*args)
  define_method(:self_location, ->(location=nil) do
    location ||= args.map do |p|
      p = p.to_s
      if p.delete! "@"
        public_send("get_#{p}")
      else
        p
      end
    end

    super(location)
  end)
end

.present(*args, &block) ⇒ 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
# File 'lib/rails_presenter/base.rb', line 25

def present(*args, &block)
  module_name = "#{name}Associations"

  unless const_defined? module_name
    include const_set(module_name, Module.new)
  end

  associations_module = const_get(module_name)

  block ||= proc { self }

  associations_module.module_eval do
    args.each do |assoc_name|
      define_method(assoc_name) do
        instance_variable_get("@#{assoc_name}") ||
        begin
          association = if super().is_a?(Array) && super().respond_to?(:scoped)
            super().scoped
          else
            super()
          end
          instance_variable_set("@#{assoc_name}", present(association.instance_eval(&block)))
        end
      end
    end
  end
end

Instance Method Details

#hObject



121
122
123
# File 'lib/rails_presenter/base.rb', line 121

def h
  @template
end


152
153
154
155
156
# File 'lib/rails_presenter/base.rb', line 152

def link_to_self(options={})
  text = options[:text] || self.to_s
  path = options[:path] || self_location
  h.link_to text, path
end

#nil_formatterObject



158
159
160
# File 'lib/rails_presenter/base.rb', line 158

def nil_formatter
  @@nil_formatter
end

#present(object) ⇒ Object



117
118
119
# File 'lib/rails_presenter/base.rb', line 117

def present(object)
  super(object, h)
end

#self_location(location = target) ⇒ Object



148
149
150
# File 'lib/rails_presenter/base.rb', line 148

def self_location(location = target)
  h.polymorphic_path location
end

#targetObject Also known as: o



125
126
127
# File 'lib/rails_presenter/base.rb', line 125

def target
  __getobj__
end

#to_sObject



131
132
133
# File 'lib/rails_presenter/base.rb', line 131

def to_s
  respond_to?(:name) ? name : super
end

#with_attrs(*attrs) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rails_presenter/base.rb', line 135

def with_attrs(*attrs)
  attrs_hash = attrs.reduce({}) do |hash, attr|
    if attr.is_a? Array
      hash[attr.first]= attr.last.call(self)
    else
      hash[attr]= public_send(attr)
    end
    hash
  end

  h.render partial: 'shared/show_with_attrs', locals: {attrs_hash: attrs_hash}
end