Class: AgileFormFields::AgileFormField

Inherits:
Object
  • Object
show all
Defined in:
app/models/agile_form_fields/agile_form_field.rb

Overview

Template method for AgileRails form field definition. This is abstract class with most of the common code for custom form field already implemented.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, record, yaml) ⇒ AgileFormField

AgileFormField initialization code.

Parameters:

env

Controller object. Controller object from where object is created. Usually self is send.

record

Document object. Document object which holds fields data.

yaml

Hash. Hash object holding field definition data.

Returns: Self



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/agile_form_fields/agile_form_field.rb', line 77

def initialize( env, record, yaml )
  @env    = env
  @record = record
  @yaml   = yaml
  @form   = env.form
  @yaml['html'] ||= {}
  # set readonly field
  #@readonly = (@yaml and @yaml['readonly']) || (@form and @form['readonly'])
  @readonly = @yaml&.dig('readonly') || @form&.dig('readonly')
  @yaml['html']['readonly'] = true if @readonly
  # assign size to html element if not already there
  @yaml['html']['size'] ||= @yaml['size'] if @yaml['size'] 
    
  @html   = ''  
  @js     = ''
  @css    = set_css_code @yaml['css']
  self
end

Instance Attribute Details

#cssObject (readonly)

Returns the value of attribute css.



64
65
66
# File 'app/models/agile_form_fields/agile_form_field.rb', line 64

def css
  @css
end

#jsObject (readonly)

Returns the value of attribute js.



63
64
65
# File 'app/models/agile_form_fields/agile_form_field.rb', line 63

def js
  @js
end

Class Method Details

.get_data(params, name) ⇒ Object

Default get_data method for retrieving data from form. If data that will be written to database must be recalculated, then AgileRails field class definition will define it’s own get_data method.

Parameters:

params

Controllers params object.

name

Field name

Most of classes will use this default method which simply returns params[name].



317
318
319
# File 'app/models/agile_form_fields/agile_form_field.rb', line 317

def self.get_data(params, name)
  params['record'][name]
end

Instance Method Details

#hash_to_options(options) ⇒ Object

Will return ruby hash formated as javascript string which can be used for passing parameters in javascript code.

Parameters:

Hash

Hash. Ruby hash parameters.

Form example: As used in forms

  options:
    height: 400
    width: 800
    toolbar: "basic"

=> "height:400, width:800, toolbar: 'basic'"

Return: String: Options formated as javascript options.



248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/agile_form_fields/agile_form_field.rb', line 248

def hash_to_options(options)
  options.inject('') do |r, element|
    key, option = element

    r += "#{key} : "
    r += case
         when option.to_s.match(/function/i) then option
         when option.class == String then "\"#{option}\""
         else option.to_s
         end
    r += ",\n"
  end
end

#htmlObject

Returns html code together with CSS code.



99
100
101
# File 'app/models/agile_form_fields/agile_form_field.rb', line 99

def html
  @html
end

#options_to_hash(options) ⇒ Object

Options may be defined on form as hash or as string. This method will ensure conversion of options into hash.

Parameters:

String or Hash

: Form options

Form example: As used in forms

  options:
    height: 400
    width: 800
    toolbar: "'basic'"
or

options: "height:400, width:800, toolbar:'basic'"

Return: Hash: Options as Hash



281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/models/agile_form_fields/agile_form_field.rb', line 281

def options_to_hash(options)
  return {} if options.blank?
  return options unless options.class == String

  options.chomp.split(',').inject({}) do |r, e|
    key, value = e.chomp.split(':')
    value.strip!
    value = value[1..value.size - 2] if value[0] =~ /\'|\"/
    r[key.strip] = value
    r
  end
end

#record_text_for(name) ⇒ Object

Checks if field name exists in document and alters record parameters if necesary. Method was added after fields that do not belong to current edited record were added to forms. Valid form only field name must start with underscore (_) letter.

Return: String: ‘record’ or ‘_record’ when valid nonexisting field is used



302
303
304
# File 'app/models/agile_form_fields/agile_form_field.rb', line 302

def record_text_for(name)
  (!@record.respond_to?(name) && name[0] == '_') ? '_record' : 'record'
end

#ro_standard(value = nil) ⇒ Object

Standard code for returning readonly field.



130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/agile_form_fields/agile_form_field.rb', line 130

def ro_standard(value = nil)
  if value.nil?
    value = if @yaml['html']['value']
      @yaml['html']['value']
    else
      @record.respond_to?(@yaml['name']) ? @record.send(@yaml['name']) : nil
    end
  end
  @html += %(<div id="#{@yaml['name']}" class="ar-readonly">#{value}</div>)
  self
end

#set_css_code(css) ⇒ Object

Sets css code for the field if specified. It replaces all occurences of ‘# ’ with field name id, as defined on form.



222
223
224
225
226
227
# File 'app/models/agile_form_fields/agile_form_field.rb', line 222

def set_css_code(css)
  return '' if css.blank?

  css.gsub!('# ', "#td_record_#{@yaml['name']} ") if css.match('# ')
  css
end

#set_default_value(opt1, opt2) ⇒ Object

Will set default value for field on a AgileRails form.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/agile_form_fields/agile_form_field.rb', line 168

def set_default_value(opt1, opt2)
  return if @yaml[opt1][opt2].present?
  return if @record&.send(@yaml['name']).present?

  @yaml[opt1][opt2] = if @yaml['default'].class.to_s == 'Hash'
                        evaluate = @yaml['default']['eval']
                        return if evaluate.blank?
                        # add @env if it's a method call and @env is not present
                        if evaluate[0] != evaluate[0].upcase && !evaluate.match('@env')
                          evaluate.prepend('@env.')
                        end
                        eval(evaluate)
                      else
                        @yaml['default']
                      end
end

#set_initial_value(opt1 = 'html', opt2 = 'value') ⇒ Object

Set initial value of the field when initial value is set in url parameters..

Example: Form has field named picture. Field can be initialized by setting value of param p_picture.

params['p_picture'] = '/path/to_picture'

When multiple initial values are assigned it is more convinient to assign them through flash object.

flash[:record] = {}
flash[:record]['picture'] = '/path/to_picture'


154
155
156
157
158
159
160
161
162
163
# File 'app/models/agile_form_fields/agile_form_field.rb', line 154

def set_initial_value(opt1 = 'html', opt2 = 'value')
  @yaml['html'] ||= {}
  value_send_as = 'p_' + @yaml['name']
  if @env.params[value_send_as]
    @yaml[opt1][opt2] = @env.params[value_send_as] 
  elsif @env.flash[:record] and @env.flash[:record][@yaml['name']]
    @yaml[opt1][opt2] = @env.flash[:record][@yaml['name']]
  end
  set_default_value(opt1, opt2) if @yaml['default']
end

#set_styleObject

Returns style html code for AgileRails Form object if style directive is present in field definition. Otherwise returns empty string.

Style may be defined like:

  style:
    height: 400px
    width: 800px
    padding: 10px 20px

  or 

  style: "height:400px; width:800px; padding: 10px 20px;"

Style directive may also be defined under html directive.
  html:
    style:
      height: 400px
      width: 800px


207
208
209
210
211
212
213
214
215
216
# File 'app/models/agile_form_fields/agile_form_field.rb', line 207

def set_style
  style = @yaml['html']['style'] || @yaml['style']
  case style.class
    when String then "style=\"#{style}\""
    when Hash then
      value = style.to_a.map { "#{_1}: #{_2}" }.join(';')
      "style=\"#{value}\"" 
    else ''
  end 
end

#t(key, default = '') ⇒ Object

Wrapper for i18 t method, with some spice added. If translation is not found English translation value will be returned. And if still not found default value will be returned if passed.

Parameters:

key

String. String to be translated into locale.

default

String. Value returned if translation is not found.

Example:

t('translate.this','Enter text for ....')

Returns: String. Translated text.



117
118
119
120
121
122
123
124
125
# File 'app/models/agile_form_fields/agile_form_field.rb', line 117

def t(key, default = '')
  c = I18n.t(key)
  if c.match(/translation missing/i)
    c = I18n.t(key, locale: 'en') 
    # Still not found. Return default if set
    c = default unless default.blank?
  end
  c
end