Module: DynamicFieldsetsHelper

Defined in:
app/helpers/dynamic_fieldsets_helper.rb

Instance Method Summary collapse

Instance Method Details

#dynamic_fieldset_form_renderer(fsa) ⇒ String

Build HTML for a specific dynamic fieldset on a form page

Parameters:

  • The (FieldsetAssociator)

    fieldset associator for the dynamic fieldset to render

Returns:

  • (String)

    The HTML for the entire dynamic fieldset



125
126
127
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 125

def dynamic_fieldset_form_renderer(fsa)
  return dynamic_fieldset_renderer(fsa, "form") << javascript_renderer(fsa,"form")
end

#dynamic_fieldset_renderer(fsa, form_type) ⇒ String

Builds HTML for a specific dynamic fieldset in a form.

Parameters:

  • The (FieldsetAssociator)

    fieldset associator for the dynamic fieldset to render

Returns:

  • (String)

    The HTML for the entire dynamic fieldset



132
133
134
135
136
137
138
139
140
141
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 132

def dynamic_fieldset_renderer(fsa, form_type)
  rendered_dynamic_fieldset = "<div id='#{DynamicFieldsets.config.form_fieldset_associator_prefix}#{fsa.id}'>\n"
  rendered_dynamic_fieldset += "<input type='hidden' name='#{DynamicFieldsets.config.form_fieldset_associator_prefix}#{fsa.id}[fieldset_id]' value='#{fsa.fieldset_id}' />\n"
  rendered_dynamic_fieldset += "<input type='hidden' name='#{DynamicFieldsets.config.form_fieldset_associator_prefix}#{fsa.id}[fieldset_model_name]' value='#{fsa.fieldset_model_name}' />\n"
  fieldset_renderer( fsa, fsa.fieldset, fsa.field_values, form_type ).each do |line|
    rendered_dynamic_fieldset += line + "\n"
  end
  rendered_dynamic_fieldset += "</div>"
  return rendered_dynamic_fieldset.html_safe
end

#dynamic_fieldset_show_renderer(fsa) ⇒ String

Build HTML for a specific dynamic fieldset on a show page

Parameters:

  • The (FieldsetAssociator)

    fieldset associator for the dynamic fieldset to render

Returns:

  • (String)

    The HTML for the entire dynamic fieldset



118
119
120
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 118

def dynamic_fieldset_show_renderer(fsa)
  return dynamic_fieldset_renderer(fsa, "show") << javascript_renderer(fsa,"show")
end

#field_form_renderer(fsa, fieldset_child, values = []) ⇒ Array

Builds HTML for the provided field for a form.

Parameters:

  • fsa (FieldsetAssociator)

    parent FieldsetAssociator

  • fieldset_child (FieldsetChild)

    The FieldsetChild to render

  • values (Array) (defaults to: [])

    Saved values for the field

Returns:

  • (Array)

    The HTML elements for the field



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
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 54

def field_form_renderer(fsa, fieldset_child, values = [])
  field = fieldset_child.child
  # maybe turn this into an instance method
  classes  = "#{field.type.gsub("DynamicFieldsets::", "").underscore.downcase} "
  classes += ( field.required ? 'required' : 'optional' )
  
  field_markup = []
  if field.use_form_header_partial?
    field_markup.push render(:partial => field.form_header_partial, :locals => {
        :classes => classes,
        :field => field,
        :fieldset_child => fieldset_child,
        :field_input_name => DynamicFieldsets.config.form_fieldset_associator_prefix + fsa.id.to_s + "_" + DynamicFieldsets.config.form_field_prefix + fieldset_child.id.to_s,
      })
  end
    
  attrs = field.html_attribute_hash
  
  attributes = {
    :fsa => fsa,
    :fieldset_child => fieldset_child,
    :values => values,
    :value => values
  }

  field_markup.push render(:partial => field.form_partial, :locals => field.form_partial_locals(attributes))
  
  if field.use_form_footer_partial?
    field_markup.push render(:partial => field.form_footer_partial)
  end

  return field_markup
end

#field_renderer(fsa, fieldset_child, values = [], form_type) ⇒ Array

Builds HTML for the provided field.

Parameters:

  • fsa (FieldsetAssociator)

    parent FieldsetAssociator

  • fieldset_child (FieldsetChild)

    The FieldsetChild to render

  • values (Array) (defaults to: [])

    Saved values for the field

Returns:

  • (Array)

    The HTML elements for the field



9
10
11
12
13
14
15
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 9

def field_renderer(fsa, fieldset_child, values = [], form_type)
  if form_type == "form"
    return field_form_renderer(fsa, fieldset_child, values)
  else
    return field_show_renderer(fsa, fieldset_child, values)
  end
end

#field_show_renderer(fsa, fieldset_child, values = []) ⇒ Array

Builds HTML for the provided field for a show page.

The code to get the names for fields using field options (select, multiple select, checkbox, and radio) is not optimized to minimize hitting the database

Parameters:

  • fsa (FieldsetAssociator)

    parent FieldsetAssociator

  • fieldset_child (FieldsetChild)

    The FieldsetChild to render

  • values (Array) (defaults to: [])

    Saved values for the field

Returns:

  • (Array)

    The HTML elements for the field



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 27

def field_show_renderer(fsa, fieldset_child, values = [])
  field = fieldset_child.child
  lines = []
  if field.use_show_header_partial?
    lines.push render(:partial => field.show_header_partial )
  end

  args = {
    :fsa => fsa,
    :fieldset_child => fieldset_child,
    :value => values,
    :values => values
  }
  lines.push render(:partial => field.show_partial, :locals => field.show_partial_locals(args) )

  if field.use_show_footer_partial?
    lines.push render(:partial => field.show_footer_partial )
  end

  return lines
end

#fieldset_renderer(fsa, fieldset, values, form_type) ⇒ Array

Builds HTML for the provided fieldset and its children.

Parameters:

  • fsa (FieldsetAssociator)

    parent FieldsetAssociator

  • fieldset (Fieldset)

    The Fieldset to render

  • values (Hash)

    Stored values for the fieldset

Returns:

  • (Array)

    The HTML elements for the fieldset



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

def fieldset_renderer(fsa, fieldset, values, form_type)
  lines = []
  lines.push render(:partial => "/dynamic_fieldsets/shared/fieldset_header", :locals => {:fieldset => fieldset})

  children = fieldset.children
      
  # this returns field/fieldset objects rather than fieldset children
  # that is why this code looks like it is accessing odd objects
  children.each do |child|
    if child.is_a? DynamicFieldsets::Fieldset
      lines += fieldset_renderer( fsa, child, values, form_type )
    else # one of many possible types of child
      fieldset_child = DynamicFieldsets::FieldsetChild.where( :child_id => child.id, :fieldset_id => fieldset.id, :child_type => "DynamicFieldsets::Field" ).first
      lines += field_renderer( fsa, fieldset_child, values[fieldset_child.id], form_type )
    end
  end

  lines.push render(:partial => "/dynamic_fieldsets/shared/fieldset_footer", :locals => {:fieldset => fieldset})

  return lines
end

#javascript_renderer(fsa, form_type) ⇒ String

Method that returns the javascript in string format to be pushed on with the rest of the generated form

Returns:

  • (String)

    The javascript variable that shows what fields have dependencies



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'app/helpers/dynamic_fieldsets_helper.rb', line 148

def javascript_renderer(fsa, form_type)
  unless fsa.id == nil
    rendered_javascript = "<script type='text/javascript'> 
      var current_fsa = #{fsa.id};
      if ( typeof dynamic_fieldsets_dependencies == 'undefined' ){
        var dynamic_fieldsets_dependencies = {};
        dynamic_fieldsets_dependencies[current_fsa] = #{fsa.dependency_child_hash.to_json}; 
      } else {
        dynamic_fieldsets_dependencies[current_fsa] = #{fsa.dependency_child_hash.to_json}; 
      }</script>"
    
    if form_type == "form"
      rendered_javascript += render "dynamic_fieldsets/shared/form_javascript_watcher"
    elsif form_type == "show"
      rendered_javascript += render "dynamic_fieldsets/shared/show_javascript_watcher"
    end 
    
    return rendered_javascript.html_safe
  else
    return ""
  end
end