Module: GSectionedShower::ViewHelpers

Defined in:
lib/g_sectioned_shower/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#g_sectioned_shower(ar_obj, *args) ⇒ Object

GUIlded component.

parameters

ar_obj

The ActiveRecord object containing the information.

options

id

(required) The id of the element. Must be unique on the page.

class
title_attr

The attribute to use in the title.

title_pre

A string to override the first part of the title. defaults to the name

of the active record object's class.
date_format

A symbol representing the date format to use. Defaults to :default.

attributes

A list of attributes to include in the main section.

status

True to include a status section.

namespace

A namespace to append onto all path helpers that are not overridden.

sections
title

The title for this section.

attributes

List of attibutes to include in this section.

associated_attribute_name_attributes

List of attributes from associated attribute to include.

associated_attribute_name_max

The max number of associated records to show. This will load

all and display max unless you define a limited named scope on the respective model, allowing
the association to be limited to the max, not returning items that will not be displayed.
links

A list of fields or field => path_helper to generate links to this item in the associated

object.
list_link

(Boolean, string, or symbol)

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/g_sectioned_shower/view_helpers.rb', line 30

def g_sectioned_shower( ar_obj, *args )

  options = args.extract_options!
  raise ArgumentError, "'title_attr' option required" unless options.include?( :title_attr )
  raise ArgumentError, "'sections' option required" unless options.include?( :sections )
  options.merge! :class => "shower" unless options.include?( :class )
  options.merge! :exclude_css => true, :exclude_js => true
  Guilded::Guilder.instance.add( :sectioned_shower, options )
  html = ""

  html << "<div class=\"status #{options[:status]}\">#{options[:status].to_s.humanize}</div>" if options[:status]

  title_pre = options[:title_pre] || "#{ar_obj.class.to_s.underscore.humanize} |"
  html << "<h2>#{title_pre} #{ar_obj.send( options[:title_attr].to_sym )}</h2>"

  # Resolve sections and meta data there-in
  sections = options[:sections]
  reflections = ar_obj.class.inheritable_attributes[:reflections]

  # Resolve main section
  main_section = sections[0]
  raise ArgumentError, "'attributes' option required within main section configuration" unless main_section.include?( :attributes )

  methods, titles = Guilded::Rails::Helpers.resolve_field_methods_and_titles( main_section[:attributes], ar_obj )

  html << "<div class=\"infoItem tableDisplay\"><dl>"

  methods.each_with_index do |method, i|

    # Handle associative relationships
    if !reflections.nil? && reflections.has_key?( method.to_sym )
      handle_associated( html, ar_obj, main_section, reflections, methods, i )
    else
      # Normal attribute from this model
      html << "<dt>#{titles[i]}</dt>"
      translate_method = "translate_#{method}".to_sym
      val = ar_obj.send( method )
      if respond_to?( translate_method )
        html << "<dd>#{h( send( translate_method, val ) )}</dd>"
      else
        val = val.to_formatted_s( options[:date_format] || :default ) if val.respond_to?( :to_formatted_s )
        html << "<dd>#{h( val )}</dd>"
      end
    end

  end

  html << "</dl><div class=\"clear\"></div></div>"

  # Get rid of the main section as it was already handled
  sections.delete_at( 0 )

  # Resolve other sections
  sections.each do |section|

    raise ArgumentError, "'attributes' option required within main section configuration" unless section.include?( :attributes )
    raise ArgumentError, "'title' option required within all section configurations except the main section" unless section.include?( :title )

    methods, titles = Guilded::Rails::Helpers.resolve_field_methods_and_titles( section[:attributes], ar_obj )
    html << "<div class=\"infoItem tableDisplay\"><h3>#{section[:title]}</h3><dl>"

    methods.each_with_index do |method, i|

      # Handle associative relationships
      if !reflections.nil? && reflections.has_key?( method.to_sym )
        handle_associated( html, ar_obj, section, reflections, methods, i )
      else
        # Normal attribute from this model
        html << "<dt>#{titles[i]}</dt>"
        translate_method = "translate_#{method}".to_sym
        val = ar_obj.send( method )
        if respond_to?( translate_method )
          "<dd>#{h( send( translate_method, val ) )}</dd>"
        else
          html << "<dd>#{h( val )}</dd>"
        end
      end

    end

    html << "</dl><div class=\"clear\"></div></div>"

  end

  html
end