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. -
#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.
36 37 38 |
# File 'lib/sequel/plugins/forme_set.rb', line 36 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.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sequel/plugins/forme_set.rb', line 48 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.
66 67 68 69 70 71 72 73 |
# File 'lib/sequel/plugins/forme_set.rb', line 66 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.
30 31 32 33 |
# File 'lib/sequel/plugins/forme_set.rb', line 30 def forme_validations return (@forme_validations || {}) if frozen? @forme_validations ||= {} end |
#validate ⇒ Object
Check associated values to ensure they match one of options in the form.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sequel/plugins/forme_set.rb', line 76 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 |