Public Instance methods
Keep track of the inputs used.
# File lib/sequel/plugins/forme_set.rb 36 def forme_input(_form, field, _opts) 37 frozen? ? super : (forme_inputs[field] = super) 38 end
Hash with column name symbol keys and Forme::SequelInput
values
# File lib/sequel/plugins/forme_set.rb 22 def forme_inputs 23 return (@forme_inputs || {}) if frozen? 24 @forme_inputs ||= {} 25 end
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. |
# File lib/sequel/plugins/forme_set.rb 48 def forme_parse(params) 49 hash = {} 50 hash_values = hash[:values] = {} 51 validations = hash[:validations] = {} 52 53 forme_inputs.each do |field, input| 54 next unless column = forme_column_for_input(input) 55 hash_values[column] = params[column] || params[column.to_s] 56 57 next unless validation = forme_validation_for_input(field, input) 58 validations[column] = validation 59 end 60 61 hash 62 end
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.
# File lib/sequel/plugins/forme_set.rb 66 def forme_set(params) 67 hash = forme_parse(params) 68 set(hash[:values]) 69 unless hash[:validations].empty? 70 forme_validations.merge!(hash[:validations]) 71 end 72 nil 73 end
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.
# File lib/sequel/plugins/forme_set.rb 30 def forme_validations 31 return (@forme_validations || {}) if frozen? 32 @forme_validations ||= {} 33 end
Check associated values to ensure they match one of options in the form.
# File lib/sequel/plugins/forme_set.rb 76 def validate 77 super 78 79 if validations = @forme_validations 80 validations.each do |column, (type, values)| 81 value = send(column) 82 83 valid = case type 84 when :subset 85 # Handle missing value the same as the empty array, 86 # can happen with PostgreSQL array associations 87 !value || (value - values).empty? 88 when :include 89 values.include?(value) 90 when :valid 91 values 92 else 93 raise Forme::Error, "invalid type used in forme_validations" 94 end 95 96 unless valid 97 errors.add(column, 'invalid value submitted') 98 end 99 end 100 end 101 end