Module: DesignElementSettingsControl
- Defined in:
- app/controls/design_element_settings_control.rb
Overview
DrgcmsControls for editing settings in a document.
Parameters to settings call: :location - model_name where settings document is located. Typicaly dc_page or dc_site. :field_name - name of the field where settings are saved :element - element name as defined on design :id - document id
Instance Method Summary collapse
-
#dc_before_save ⇒ Object
Called before save.
-
#dc_new_record ⇒ Object
Called before edit.
-
#get_settings ⇒ Object
Check if settings control document exists and return document and settings values as yaml string.
Instance Method Details
#dc_before_save ⇒ Object
Called before save.
Convert data from fields on form to yaml and save it to document settings field.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/controls/design_element_settings_control.rb', line 107 def dc_before_save() document, data = get_settings return false if document.class == FalseClass and data.nil? # fields_on_form.each do |v| session[:form_processing] = v['name'] # for debuging next if v['type'].nil? or v['readonly'] # fields with readonly option don't return value and would be wiped # return value from form field definition value = DrgcmsFormFields.const_get(v['type'].camelize).get_data(params, v['name']) value = value.map {|e| e.to_s} if value.class == Array # set to nil if blank value = nil if value.blank? data['settings'] ||= {} data['settings'][ params[:element] ] ||= {} data['settings'][ params[:element] ][ v['name'] ] = value end # remove nil elements data['settings'][ params[:element] ].compact! # save data to document field document.send("#{params[:field_name]}=", data.to_yaml) document.save # to re-set form again dc_new_record false # must be end |
#dc_new_record ⇒ Object
Called before edit.
Load fields on form with values from settings document.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/controls/design_element_settings_control.rb', line 84 def dc_new_record() document, data = get_settings return false if document.class == FalseClass and data.nil? # fill values with settings values if data['settings'] and data['settings'][ params[:element] ] data['settings'][ params[:element] ].each { |key, value| @record.send("#{key}=", value) } end # add some fields required at post as hidden fields to form form = @form['form']['tabs'] ? @form['form']['tabs'].to_a.last : @form['form']['fields'] form[9999] = {'type' => 'hidden_field', 'name' => 'dc_location', 'html' => {'value' => params[:location]}} form[9998] = {'type' => 'hidden_field', 'name' => 'dc_field_name'} @record[:dc_field_name] = params[:field_name] form[9997] = {'type' => 'hidden_field', 'name' => 'dc_element'} @record.dc_element = params[:element] form[9996] = {'type' => 'hidden_field', 'name' => 'dc_document_id', 'html' => {'value' => params[:id]}} true end |
#get_settings ⇒ Object
Check if settings control document exists and return document and settings values as yaml string.
Return:
[document, data] : Mongoid document, yaml as String
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/controls/design_element_settings_control.rb', line 43 def get_settings() # On save. Set required variables if params[:record] params[:location] = params[:record][:dc_location] params[:field_name] = params[:record][:dc_field_name] params[:element] = params[:record][:dc_element] params[:id] = params[:record][:dc_document_id] end # Check model begin model = params[:location].classify.constantize rescue flash[:error] = "Invalid or undefined model name! #{params[:location]}" return false end # Check fild name begin document = model.find(params[:id]) params[:field_name] ||= (params[:location] == 'dc_site' ? 'settings' : 'params') # field not defined on document raise unless document.respond_to?(params[:field_name]) yaml = document[params[:field_name]] || '' rescue flash[:error] = 'Invalid or undefined field name!' return false end # Check data begin data = YAML.load(yaml) || {} rescue flash[:error] = 'Invalid configuration data found!' return false end [document, data] end |