Class: TableHelpers::ReflectionHelper

Inherits:
Object
  • Object
show all
Defined in:
app/utils/table_helpers.rb

Instance Method Summary collapse

Constructor Details

#initialize(method, template) ⇒ ReflectionHelper

Returns a new instance of ReflectionHelper.



126
127
128
129
# File 'app/utils/table_helpers.rb', line 126

def initialize(method, template)
  @method = method
  @template = template
end

Instance Method Details

#format_value(item, format) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/utils/table_helpers.rb', line 131

def format_value(item, format)
  target = item.send(@method)
  case format
    when Proc
      format.call(target)
    when Hash, nil
      if target
        options = {label_attr: :id, type: :link, target: :_blank}.merge(format || {})
        v = target.send(options[:label_attr])
        case options[:type]
          when :link
            @template.link_to(v, target, target: options[:target])
          else
            v.to_s
        end
      else
        ''
      end
    else
      target.to_s
  end
end