module ActiveComponent::Renderable

Public Instance Methods

render_component(type, args={}) click to toggle source
# File lib/active_component/renderable.rb, line 18
def render_component(type, args={})
  raise_errors(type, args) if type.nil? or args.nil?

  unless args[:collection].present?
    element(type, args)
  else
    collection(type, args[:collection], args[:as], args[:locals])
  end
end

Private Instance Methods

collection(type, collection, as, locals) click to toggle source
# File lib/active_component/renderable.rb, line 35
def collection(type, collection, as, locals)

  error_string = "Attempting to render a collection without specifying the ':as' value"
  raise ArgumentError, error_string if as.nil?

  locals = locals || {}

  html_string = collection.map do |item|
    element(
      type,
      locals.merge(as => item)
    )
  end

  html_string.join('')
end
constantize(type) click to toggle source
# File lib/active_component/renderable.rb, line 52
def constantize(type)
  type = type.camelize.constantize
  type
end
element(type, args) click to toggle source
# File lib/active_component/renderable.rb, line 30
def element(type, args)
  resource = constantize("#{type.to_s}_component").new(args)
  resource.render
end
raise_errors(type, args) click to toggle source
# File lib/active_component/renderable.rb, line 57
def raise_errors(type, args)
  text = "Invalid arguments. Received: #{type.class}, #{args.class}. Expected: Symbol, Hash"
  raise ArgumentError, text
end