module Sequel::Plugins::FormeSet::InstanceMethods

  1. lib/sequel/plugins/forme_set.rb

Public Instance methods

forme_input (_form, field, _opts)

Keep track of the inputs used.

[show source]
   # File lib/sequel/plugins/forme_set.rb
32 def forme_input(_form, field, _opts)
33   forme_inputs[field] = super
34 end
forme_inputs ()

Hash with column name symbol keys and Forme::SequelInput values

[show source]
   # File lib/sequel/plugins/forme_set.rb
20 def forme_inputs
21   @forme_inputs ||= {}
22 end
forme_parse (params)

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.

[show source]
   # File lib/sequel/plugins/forme_set.rb
44 def forme_parse(params)
45   hash = {}
46   hash_values = hash[:values] = {}
47   validations = hash[:validations] = {}
48 
49   forme_inputs.each do |field, input|
50     next unless column = forme_column_for_input(input)
51     hash_values[column] = params[column] || params[column.to_s]
52 
53     next unless validation = forme_validation_for_input(field, input)
54     validations[column] = validation
55   end
56 
57   hash
58 end
forme_set (params)

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.

[show source]
   # File lib/sequel/plugins/forme_set.rb
62 def forme_set(params)
63   hash = forme_parse(params)
64   set(hash[:values])
65   unless hash[:validations].empty?
66     forme_validations.merge!(hash[:validations])
67   end
68   nil
69 end
forme_validations ()

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.

[show source]
   # File lib/sequel/plugins/forme_set.rb
27 def forme_validations
28   @forme_validations ||= {}
29 end
validate ()

Check associated values to ensure they match one of options in the form.

[show source]
   # File lib/sequel/plugins/forme_set.rb
72 def validate
73   super
74 
75   if validations = @forme_validations
76     validations.each do |column, (type, values)|
77       value = send(column)
78 
79       valid = case type
80       when :subset
81         # Handle missing value the same as the empty array,
82         # can happen with PostgreSQL array associations
83         !value || (value - values).empty?
84       when :include
85         values.include?(value)
86       when :valid
87         values
88       else
89         raise Forme::Error, "invalid type used in forme_validations"
90       end
91 
92       unless valid
93         errors.add(column, 'invalid value submitted')
94       end
95     end
96   end
97 end