Module: CustomFields::BlueFormParameters

Defined in:
lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb

Overview

The BlueFormParameters module is used to build an array of parameters for various BlueForm methods based on the type and method of a custom field. Out of the box this module provides a set of methods that can be used to generate the parameters for most methods in the BlueForm helper that ships with Ramaze. However, there may be a time when you want to add your own method. Don't worry, this is quite easy.

Each method (they should be class methods) in this module takes two parameters, the first one is an instance of Model::CustomField and the second one an instance of Model::CustomFieldValue. The return value should be an array containing all the parameters that will be passed to BlueForm.

Lets start with the basic skeleton of such a method:

module CustomFields
  module BlueFormParameters
    def self.my_method(field, field_value)
      params = [:input_text]

      return params
    end
  end
end

So far the only thing this method does is telling BlueForm that we want to invoke the input_text() method. Let's add some extra parameters to it such as the label and name:

module CustomFields
  module BlueFormParameters
    def self.my_method(field, field_value)
      params = [
        :input_text,
        field.label,
        "custom_field_value_#{field.id}"
      ]

      return params
    end
  end
end

The second parameter in this array is the label of the field, the third the name. The names of these fields should be specified in the format of custom_field_value_N where N is the ID of the custom field.

Let's add our value to the list. BlueForm allows you to specify a hash containing additional parameters (including the value). Adding this hash to the array is as easy as, well, just adding it:

module CustomFields
  module BlueFormParameters
    def self.my_method(field, field_value)
      params = [
        :input_text,
        field.label,
        "custom_field_value_#{field.id}",
        {:value => field_value.value}
      ]

      return params
    end
  end
end

And there you have it, a very basic example of how to add custom parameters allowing you to build your own HTML markup for your fields.

Since:

Class Method Summary (collapse)

Class Method Details

+ (Object) input_checkbox(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#input_checkbox.

See Also:

Since:

  • 0.2.8



201
202
203
204
205
206
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 201

def input_checkbox(field, field_value)
  params    = input_radio(field, field_value)
  params[0] = :input_checkbox

  return params
end

+ (Object) input_password(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#input_password.

See Also:

Since:

  • 0.2.8



124
125
126
127
128
129
130
131
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 124

def input_password(field, field_value)
  params    = input_text(field, field_value)
  params[0] = :input_password

  params.last.delete(:data-format')

  return params
end

+ (Object) input_radio(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#input_radio.

See Also:

Since:

  • 0.2.8



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 164

def input_radio(field, field_value)
  type   = field.custom_field_type
  value  = field_value.value rescue nil
  params = [
    :input_radio,
    field.name,
    "custom_field_value_#{field.id}",
    value,
    {}
  ]

  # Convert the string containing the possible values to a hash.
  if !field.possible_values.nil? and !field.possible_values.empty?
    params.last[:values] = {}

    field.possible_values.split(/\n|\r\n/).each do |row|
      if row.include?('|')
        row        = row.split('|', 2)
        key, value = row[0], row[1]
      else
        key = value = row
      end

      params.last[:values][key] = value
    end
  end

  return params
end

+ (Array) input_text(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#input_text.

Parameters:

Returns:

  • (Array)

Since:

  • 0.2.8



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/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 87

def input_text(field, field_value)
  type   = field.custom_field_type
  value  = field_value.value rescue nil
  params = [
    :input_text,
    field.name,
    "custom_field_value_#{field.id}",
    {:value => value}
  ]

  if !field.text_limit.nil?
    params.last[:maxlength] = field.text_limit
  end

  if !field.description.nil?
    params.last[:title] = field.description
  end

  if !field.format.nil? and !field.format.empty? \
  and type.allow_markup == true
    params.last[:data-format'] = field.format
  end

  if !type.html_class.nil? and !type.html_class.empty?
    params.last[:class] = type.html_class
  end

  return params
end

+ (Object) select(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#select.

See Also:

Since:

  • 0.2.8



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 215

def select(field, field_value)
  type   = field.custom_field_type
  value  = field_value.value rescue nil
  params = [
    :select,
    field.name,
    "custom_field_value_#{field.id}",
    {:selected => value, :size => 1}
  ]

  # Convert the string containing the possible values to a hash.
  if !field.possible_values.nil? and !field.possible_values.empty?
    params.last[:values] = {}

    field.possible_values.split(/\n|\r\n/).each do |row|
      if row.include?('|')
        row        = row.split('|', 2)
        key, value = row[0], row[1]
      else
        key = value = row
      end

      params.last[:values][key] = value
    end
  end

  return params
end

+ (Object) select_multiple(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#select but allows multiple values to be selected.

See Also:

Since:

  • 0.2.8



252
253
254
255
256
257
258
259
260
261
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 252

def select_multiple(field, field_value)
  params = select(field, field_value)

  params.last[:multiple] = :multiple
  params.last[:values]   = params.last[:values].invert

  params.last.delete(:size)

  return params
end

+ (Object) textarea(field, field_value)

Generates the required parameters for Ramaze::Helper::BlueForm::Form#textarea.

See Also:

Since:

  • 0.2.8



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/zen/package/custom_fields/lib/custom_fields/blue_form_parameters.rb', line 140

def textarea(field, field_value)
  params    = input_text(field, field_value)
  params[0] = :textarea

  if !field.textarea_rows.nil?
    params.last[:rows] = field.textarea_rows
  end

  # Remove the class "text]editor" if the field does not allow the text
  # editor to be used.
  if field.text_editor == false
    params.last[:class].gsub!('text_editor', '')
  end

  return params
end