Class: Toolbox::FieldRenderer

Inherits:
Renderer
  • Object
show all
Defined in:
lib/toolbox/rendering.rb

Instance Attribute Summary

Attributes inherited from Renderer

#widget_config

Instance Method Summary collapse

Methods inherited from Renderer

#initialize, #label, #translate_field, #value

Constructor Details

This class inherits a constructor from Toolbox::Renderer

Instance Method Details

#render_value(object, html = true) ⇒ Object

Renders the value of an attribute of a given object. Parameters:

  • object: the object to get the attribute of

  • html: if true, returns the value in html (escaped etc.)

The following options are used:

  • :model_method => an alternative method or block of the object to get the display value

  • :render_method => an (helper/view) method or block that returns the rendered value (parameters: object, val, html)

  • :hide_empty: returns nil if the display value is empty. Otherwise, an empty string

will be returned (or   in case of html)

  • :suppress_link: if the display value is an ActiveRecord usually a link to it will

be returned (only if html = true of course). If this option is set, only the string represenation will be returned

  • :join if the value is an array and :join is defined, the values are joined using the string

representation of the :join value. Else, a unnumbered list (ul) is generated

If the display value is an instance of Date, it will be formatted using the :local_date format



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/toolbox/rendering.rb', line 133

def render_value(object, html = true)
  val =  value(object)
  unless val == nil && @widget_config.hide_empty
    if @widget_config.render_method
      render_own object, val, html
    elsif val.is_a? Array
      render_array object, val, html
    else
      value_text val, html, object
    end
  end
end

Generate a sort link for the given field A list can only be sorted by one field. Multiple list on one page can be distinguished by the prefix The field parameter is a field form a list configuration (Symbol, String or Hash) The text is the link text.

The following css classes are assigend regarding the current sort direction

  • no sorting: ‘sort-none’

  • asc sorting: ‘sort-asc’

  • desc sorting: ‘sort-desc’



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/toolbox/rendering.rb', line 94

def sort_link(params, prefix = nil)
  if @widget_config.suppress_sorting
    label
  else
    # see if we are already sorted
    current_dir = Toolbox::Sorting.current(params, prefix, @widget_config.name.to_s)
    case current_dir
      when nil
        new_dir = 'asc'
        class_attr = 'sort-none'
      when 'asc'
        new_dir = 'desc'
        class_attr = 'sort-asc'
      else
        new_dir = nil
        class_attr = 'sort-desc'
    end
    @view.link_to(label, Toolbox::Sorting.params(params, prefix, @widget_config.name.to_s, new_dir), {:class => class_attr})
  end
end