Class: DatashiftJourney::Collector::BaseCollectorForm
- Inherits:
-
Reform::Form
- Object
- Reform::Form
- DatashiftJourney::Collector::BaseCollectorForm
- Includes:
- FormMixin
- Defined in:
- app/forms/datashift_journey/collector/base_collector_form.rb
Overview
This class represents the View backing Form
Reform API :
initialize always requires a model that the form represents.
validate(params) updates the form's fields with the input data (only the form, not the model) and then runs all validations. The return value is the boolean result of the validations.
errors returns validation messages in a classic ActiveModel style.
sync writes form data back to the model. This will only use setter methods on the model(s).
save (optional) will call #save on the model and nested models. Note that this implies a #sync call.
prepopulate! (optional) will run pre-population hooks to "fill out" your form before rendering.
Instance Attribute Summary collapse
-
#definition ⇒ Object
Returns the value of attribute definition.
Attributes included from FormMixin
#journey_plan, #redirection_url
Instance Method Summary collapse
-
#initialize(journey_plan) ⇒ BaseCollectorForm
constructor
Called from CONTROLLER.
-
#params_key ⇒ Object
Over ride in your form if your view forms have non standard key field.
- #save(params) ⇒ Object
Methods included from FormMixin
Constructor Details
#initialize(journey_plan) ⇒ BaseCollectorForm
Called from CONTROLLER
Creates a form object backed by the current Plan object
Data is collected generically from fields defined by FormDefinition and stored in data nodes associated with current JourneyPlan instance (through polymorphic plan association)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/forms/datashift_journey/collector/base_collector_form.rb', line 30 def initialize(journey_plan) super(journey_plan) @journey_plan = journey_plan @definition = DatashiftJourney::Collector::FormDefinition.where(klass: self.class.name).first # For brand new forms, add one data node per form field - data nodes hold the COLLECTED VALUES # If this page already been visited we should have a completed data node already form_definition.form_fields.map(&:id).each do |id| next if journey_plan.data_nodes.where('form_field_id = ?', id).exists? journey_plan.data_nodes << DatashiftJourney::Collector::DataNode.new(plan: journey_plan, form_field_id: id) end end |
Instance Attribute Details
#definition ⇒ Object
Returns the value of attribute definition.
21 22 23 |
# File 'app/forms/datashift_journey/collector/base_collector_form.rb', line 21 def definition @definition end |
Instance Method Details
#params_key ⇒ Object
Over ride in your form if your view forms have non standard key field.
The default naming format for form elements in the view is : “#params_key[field_value]”
For example:
<%= select_tag "#{params_key}[data_nodes][field_value][#{i}]".... %>
79 80 81 |
# File 'app/forms/datashift_journey/collector/base_collector_form.rb', line 79 def params_key DatashiftJourney::FormObjectFactory.state_name(self.class.name) end |
#save(params) ⇒ Object
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 |
# File 'app/forms/datashift_journey/collector/base_collector_form.rb', line 46 def save(params) form_params = params.fetch(params_key, {}) data_nodes = form_params["data_nodes"] # =>{"form_field"=>{"0"=>"name", "1"=>"namespace"}, "field_value"=>{"0"=>"dfsdf", "1"=>"ghfghf"}}} if data_nodes.present? fields = data_nodes["form_field"] values = data_nodes["field_value"] fields.each do |idx, name| ff = Collector::FormField.where(name: name, form_definition: definition).first next unless ff # Ensure when user goes back and changes a value we reflect the changed value pp journey_plan Collector::DataNode.find_or_initialize_by(plan: journey_plan, form_field: ff).tap do |node| node.field_value = values[idx] node.save end end end end |