Class: Toolbox::Renderer

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

Direct Known Subclasses

ActionRenderer, ControlRenderer, FieldRenderer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, widget_config, dialog = false) ⇒ Renderer

Returns a new instance of Renderer.



22
23
24
25
26
# File 'lib/toolbox/rendering.rb', line 22

def initialize view, widget_config, dialog = false
  @view = view
  @widget_config = widget_config
  @dialog = dialog
end

Instance Attribute Details

#widget_configObject (readonly)

Returns the value of attribute widget_config.



20
21
22
# File 'lib/toolbox/rendering.rb', line 20

def widget_config
  @widget_config
end

Instance Method Details

#label(show_model = nil, html = true) ⇒ Object

Renders the label of the widget. If show_model is set to a model name, the model name of this widget will be added in parenthesis if it differs to the value in show_model.



51
52
53
54
55
56
57
58
59
# File 'lib/toolbox/rendering.rb', line 51

def label(show_model = nil, html = true)
  unless @widget_config.suppress_label
    if @widget_config.label
      html ? @view.send(:h, @widget_config.label) : @widget_config.label
    else
      translate_field(show_model, html)
    end
  end
end

#translate_field(show_model = nil, html = true) ⇒ Object

Translates a column name of a model using gettext The show_model argument is optional. If true, the model name will also be translated and appended to the column name. Ensure, that the model name is also translated (gettext does not extract this automatically).



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/toolbox/rendering.rb', line 66

def translate_field(show_model = nil, html = true)
  col_name = @widget_config.name.to_s
  model_name = @widget_config.model_name.classify
  a = col_name.split('.')
  if a.length > 1
    model_name = a[-2].classify
    col_name = a[-1]
  end

  text = @view.send(:s_, model_name + '|' + col_name.humanize)
  text += ' (' + @view.send(:s_, model_name) + ')' if show_model && model_name != show_model
  
  html ? @view.send(:h, text) : text
end

#value(rec) ⇒ Object

Get the field value out of a model object If the attribute method is defined and it’s a symbol, this method is called on the model object If the attribute method is defined and it’s a block (Proc), the block is called with the model object as parameter In all other cases, the field name is interpreted as method and called on the model object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/toolbox/rendering.rb', line 33

def value(rec)
  val = rec.send(@widget_config.model_method) if @widget_config.model_method && @widget_config.model_method.is_a?(Symbol)
  val = @widget_config.model_method.call(rec) if @widget_config.model_method && @widget_config.model_method.is_a?(Proc)
  unless val # fallback
    a = @widget_config.name.to_s.split('.')
    a.each do |t|
      rec = rec.send(t)
      break unless rec # stop if nil is returned
    end
    val = rec
  end
  val = nil if val == ''
  val
end