Class: DrgcmsFormFields::DrgcmsField
- Inherits:
-
Object
- Object
- DrgcmsFormFields::DrgcmsField
- Defined in:
- app/models/drgcms_form_fields.rb
Overview
Template method for DRG CMS form field definition. This is abstract class with most of the common code for custom form field already implemented.
Direct Known Subclasses
CheckBox, Comment, DatePicker, DateSelect, DatetimePicker, DatetimeSelect, Embedded, FileSelect, HiddenField, HtmlField, JournalDiff, LinkTo, MultitextAutocomplete, PasswordField, Readonly, Select, SubmitTag, TextArea, TextAutocomplete, TextField
Instance Attribute Summary collapse
-
#html ⇒ Object
readonly
Returns the value of attribute html.
-
#js ⇒ Object
readonly
Returns the value of attribute js.
Class Method Summary collapse
-
.get_data(params, name) ⇒ Object
Default get_data method for retrieving data from parameters.
Instance Method Summary collapse
-
#hash_to_options(hash) ⇒ Object
Will return ruby hash formated as javascript string which can be used for passing parameters in javascript code.
-
#initialize(parent, record, yaml) ⇒ DrgcmsField
constructor
DrgcmsField initialization code.
-
#record_text_for(name) ⇒ Object
Checks if field name exists in document and alters record parameters if necesary.
-
#ro_standard(value = nil) ⇒ Object
Standard code for returning readonly field.
-
#set_initial_value(opt1 = 'html', opt2 = 'value') ⇒ Object
Set initial value of the field when initial value is set in url parameters..
-
#set_style ⇒ Object
Returns style html code for DRGForm object if style directive is present in field definition.
-
#t(key, default = '') ⇒ Object
Wrapper for i18 t method, with some spice added.
Constructor Details
#initialize(parent, record, yaml) ⇒ DrgcmsField
DrgcmsField initialization code.
Parameters:
- parent
-
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 |
# File 'app/models/drgcms_form_fields.rb', line 77 def initialize( parent, record, yaml ) @parent = parent @record = record @yaml = yaml @form = parent.form @readonly = (@yaml and @yaml['readonly']) || (@form and @form['readonly']) if @yaml['size'] # move size to html element if not already there @yaml['html'] ||= {} @yaml['html']['size'] ||= @yaml['size'] end @html = '' @js = '' self end |
Instance Attribute Details
#html ⇒ Object (readonly)
Returns the value of attribute html.
64 65 66 |
# File 'app/models/drgcms_form_fields.rb', line 64 def html @html end |
#js ⇒ Object (readonly)
Returns the value of attribute js.
64 65 66 |
# File 'app/models/drgcms_form_fields.rb', line 64 def js @js end |
Class Method Details
.get_data(params, name) ⇒ Object
Default get_data method for retrieving data from parameters. Class method is called for every entry field defined on form before field value is saved to database.
Parameters:
- params
-
Controllers params object.
- name
-
Field name
Most of classes will use this default method which returns params[name]. When field data is more complex class should implement its own get_data method.
228 229 230 |
# File 'app/models/drgcms_form_fields.rb', line 228 def self.get_data(params, name) params['record'][name] end |
Instance Method Details
#hash_to_options(hash) ⇒ 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.
200 201 202 |
# File 'app/models/drgcms_form_fields.rb', line 200 def (hash) hash.to_a.inject([]) {|r,v| r << "#{v[0]}: #{v[1]}" }.join(',') 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 document were added to forms. Valid nonexisting form field names must start with underscore (_) letter.
Return: String: ‘record’ or ‘_record’ when valid nonexisting field is used
212 213 214 |
# File 'app/models/drgcms_form_fields.rb', line 212 def record_text_for(name) (!@record.respond_to?(name) and name[0,1] == '_') ? '_record' : 'record' end |
#ro_standard(value = nil) ⇒ Object
Standard code for returning readonly field.
119 120 121 122 123 |
# File 'app/models/drgcms_form_fields.rb', line 119 def ro_standard(value=nil) value = @record[@yaml['name']] if value.nil? and @record.respond_to?(@yaml['name']) @html << (value.blank? ? '' : "<div class='dc-readonly'>#{value}</div>") self 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'
137 138 139 140 141 142 143 144 145 |
# File 'app/models/drgcms_form_fields.rb', line 137 def set_initial_value(opt1='html', opt2='value') @yaml['html'] ||= {} value_send_as = 'p_' + @yaml['name'] if @parent.params[value_send_as] @yaml[opt1][opt2] = @parent.params[value_send_as] elsif @parent.flash[:record] and @parent.flash[:record][@yaml['name']] @yaml[opt1][opt2] = @parent.flash[:record][@yaml['name']] end end |
#set_style ⇒ Object
Returns style html code for DRGForm object if style directive is present in field definition. Otherwiese 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
169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/drgcms_form_fields.rb', line 169 def set_style() style = @yaml['html']['style'] || @yaml['style'] case when style.nil? then '' when style.class == String then "style=\"#{style}\"" when style.class == Hash then value = style.to_a.inject([]) {|r,v| r << "#{v[0]}: #{v[1]}" }.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.
106 107 108 109 110 111 112 113 114 |
# File 'app/models/drgcms_form_fields.rb', line 106 def t(key, default='') c = I18n.t(key) if c.match( 'translation missing' ) c = I18n.t(key, locale: 'en') # Still not found. Return default if set c = default unless default.blank? end c end |