Module: Lightrail::Wrapper

Defined in:
lib/lightrail/wrapper.rb,
lib/lightrail/wrapper/model.rb,
lib/lightrail/wrapper/controller.rb,
lib/lightrail/wrapper/associations.rb,
lib/lightrail/wrapper/active_record.rb

Defined Under Namespace

Modules: ActiveRecord, Associations, Controller Classes: Model, Result

Class Method Summary collapse

Class Method Details

.find(model) ⇒ Object

Receives an object and returns its wrapper.



14
15
16
17
# File 'lib/lightrail/wrapper.rb', line 14

def find(model)
  klass = model.is_a?(Class) ? model : model.class
  ActiveSupport::Dependencies.constantize("#{klass.name}Wrapper")
end

.render(object, scope, options = {}, result = {}) ⇒ Object

Receives an object, find its wrapper and render it. If the object is an array, loop for each element in the array and call render_many (instead of render).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lightrail/wrapper.rb', line 22

def render(object, scope, options={}, result={})
  result = Result.new.merge!(result)

  if object.blank?
    handle_blank(object, result, options)
  elsif object.respond_to?(:each)
    object.each { |i| Lightrail::Wrapper.find(i).new(i, scope).render_many(options, result) }
  else
    Lightrail::Wrapper.find(object).new(object, scope).render(options, result)
  end

  result.associations.inject(result) do |final, (key, value)|
    value.uniq!
    render_association(value, scope, { :as => key }, final)
  end
end

.render_association(value, scope, options, result) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lightrail/wrapper.rb', line 39

def render_association(value, scope, options, result)
  if value.empty?
    handle_blank(value, result, options)
    result
  else
    wrapper = Lightrail::Wrapper.find(value.first)
    wrapper.around_association(value, scope) do
      value.each { |i| wrapper.new(i, scope).render_many(options, result) }
      result
    end
  end
end