module Presenting::Helpers

Public Instance Methods

present(*args, &block) click to toggle source
# File lib/presenting/helpers.rb, line 3
def present(*args, &block)
  options = args.length > 1 ? args.extract_options! : {}

  if args.first.is_a? Symbol
    object, presentation = nil, args.first
  else
    object, presentation = args.first, args.second
  end

  if presentation
    klass = "Presentation::#{presentation.to_s.camelcase}".constantize rescue nil
    if klass
      instance = klass.new(options, &block)
      instance.presentable = object
      instance.controller = controller
      instance.render
    elsif respond_to?(method_name = "present_#{presentation}", true)
      send(method_name, object, options)
    else
      raise ArgumentError, "unknown presentation `#{presentation}'"
    end
  elsif object.respond_to?(:loaded?) # AssociationProxy
    present_association(object, options)
  else
    present_by_class(object, options)
  end
end

Protected Instance Methods

normalize_dropdown_options_to_strings(options) click to toggle source

We want to normalize the value elements of the dropdown options to strings so that they will match against params.

Need to handle the three different dropdown options formats:

  • array of strings

  • array of arrays

  • hash

# File lib/presenting/helpers.rb, line 58
def normalize_dropdown_options_to_strings(options)
  options.to_a.map do |element|
    if element.is_a? String
      [element, element]
    else
      [element.first, element.last.to_s]
    end
  end
end
present_association(object, options = {}) click to toggle source

TODO: special handling for associations (displaying activerecords)

# File lib/presenting/helpers.rb, line 69
def present_association(object, options = {})
  present_by_class(object, options)
end
present_by_class(object, options = {}) click to toggle source
# File lib/presenting/helpers.rb, line 73
def present_by_class(object, options = {})
  case object
  when Array
    content_tag "ol" do
      object.collect do |i|
        content_tag "li", present(i, options)
      end.join.html_safe
    end

  when Hash
    # sort by keys
    content_tag "dl" do
      object.keys.sort.collect do |k|
        content_tag("dt", k) +
        content_tag("dd", present(object[k], options))
      end.join.html_safe
    end

  when TrueClass, FalseClass
    object ? "True" : "False"

  when Date, Time, DateTime
    l(object, :format => :default)

  else
    options[:raw] ? object.to_s.html_safe : object.to_s
  end
end