Module: Sequel::Plugins::FormeSet::InstanceMethods
- Defined in:
- lib/sequel/plugins/forme_set.rb
Instance Method Summary collapse
-
#forme_input(_form, field, _opts) ⇒ Object
Keep track of the inputs used.
-
#forme_inputs ⇒ Object
Hash with column name symbol keys and Forme::SequelInput values.
-
#forme_parse(params) ⇒ Object
Given the hash of submitted parameters, return a hash containing information on how to set values in the model based on the inputs used on the related form.
-
#forme_set(params) ⇒ Object
Set the values in the object based on the parameters parsed from the form, and add validations based on the form to ensure that associated objects match form values.
-
#forme_validations ⇒ Object
Hash with column name symbol keys and
[subset, allowed_values]
values. -
#isolate_forme_inputs ⇒ Object
Temporarily reset forme_inputs to the empty hash before yielding to the block.
-
#validate ⇒ Object
Check associated values to ensure they match one of options in the form.
Instance Method Details
#forme_input(_form, field, _opts) ⇒ Object
Keep track of the inputs used.
51 52 53 |
# File 'lib/sequel/plugins/forme_set.rb', line 51 def forme_input(_form, field, _opts) frozen? ? super : (forme_inputs[field] = super) end |
#forme_inputs ⇒ Object
Hash with column name symbol keys and Forme::SequelInput values
22 23 24 25 |
# File 'lib/sequel/plugins/forme_set.rb', line 22 def forme_inputs return (@forme_inputs || {}) if frozen? @forme_inputs ||= {} end |
#forme_parse(params) ⇒ Object
Given the hash of submitted parameters, return a hash containing information on how to set values in the model based on the inputs used on the related form. Currently, the hash contains the following information:
- :values
-
A hash of values that can be used to update the model, suitable for passing to Sequel::Model#set.
- :validations
-
A hash of values suitable for merging into forme_validations. Used to check that the submitted values for associated objects match one of the options for the input in the form.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/sequel/plugins/forme_set.rb', line 63 def forme_parse(params) hash = {} hash_values = hash[:values] = {} validations = hash[:validations] = {} forme_inputs.each do |field, input| next unless column = forme_column_for_input(input) hash_values[column] = params[column] || params[column.to_s] next unless validation = forme_validation_for_input(field, input) validations[column] = validation end hash end |
#forme_set(params) ⇒ Object
Set the values in the object based on the parameters parsed from the form, and add validations based on the form to ensure that associated objects match form values.
81 82 83 84 85 86 87 88 |
# File 'lib/sequel/plugins/forme_set.rb', line 81 def forme_set(params) hash = forme_parse(params) set(hash[:values]) unless hash[:validations].empty? forme_validations.merge!(hash[:validations]) end nil end |
#forme_validations ⇒ Object
Hash with column name symbol keys and [subset, allowed_values]
values. subset
is a boolean flag, if true, the uploaded values should be a subset of the allowed values, otherwise, there should be a single uploaded value that is a member of the allowed values.
45 46 47 48 |
# File 'lib/sequel/plugins/forme_set.rb', line 45 def forme_validations return (@forme_validations || {}) if frozen? @forme_validations ||= {} end |
#isolate_forme_inputs ⇒ Object
Temporarily reset forme_inputs to the empty hash before yielding to the block.
Used by the Roda forme_set plugin to make sure each form only includes metadata for inputs in that form, and not metadata for inputs for earlier forms on the same page.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sequel/plugins/forme_set.rb', line 30 def isolate_forme_inputs return yield if frozen? forme_inputs = self.forme_inputs begin @forme_inputs = {} yield ensure @forme_inputs = forme_inputs.merge(@forme_inputs) end end |
#validate ⇒ Object
Check associated values to ensure they match one of options in the form.
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 116 |
# File 'lib/sequel/plugins/forme_set.rb', line 91 def validate super if validations = @forme_validations validations.each do |column, (type, values)| value = send(column) valid = case type when :subset # Handle missing value the same as the empty array, # can happen with PostgreSQL array associations !value || (value - values).empty? when :include values.include?(value) when :valid values else raise Forme::Error, "invalid type used in forme_validations" end unless valid errors.add(column, 'invalid value submitted') end end end end |