Class: TableHelpers

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

Defined Under Namespace

Classes: BooleanFormatter, DateFormatter, EnumerizeFormatter, MethodHelper, ReflectionHelper, SelfHelper, SimpleFormatter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, template) ⇒ TableHelpers

Returns a new instance of TableHelpers.



156
157
158
159
160
161
162
# File 'app/utils/table_helpers.rb', line 156

def initialize(model_class, template)
  @model_class = model_class
  @columns = model_class.columns.index_by(&:name).with_indifferent_access
  @reflections = model_class.reflections.dup.with_indifferent_access
  @template = template
  @helpers = {}
end

Class Method Details

.for(model_class, template) ⇒ Object



164
165
166
# File 'app/utils/table_helpers.rb', line 164

def self.for(model_class, template)
  TableHelpers.new(model_class, template)
end

Instance Method Details

#[](method) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/utils/table_helpers.rb', line 168

def [](method)
  helper = @helpers[method]
  unless helper
    helper = case method
               when :self
                 SelfHelper.new
               else
                 r = @reflections[method]
                 if r
                   if r.macro == :belongs_to
                     ReflectionHelper.new(method, @template)
                   end
                 else
                   if @model_class.respond_to?(:enumerized_attributes) && @model_class.enumerized_attributes[method]
                     type = :enumerize
                   else
                     type = @columns[method] && @columns[method].type || :default
                   end
                   h = MethodHelper.new(method, type)
                   h.align = :right if [:integer, :decimal].include?(type.to_sym)
                   h
                 end
             end
    @helpers[method] = helper
  end
  helper
end