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, line 32
def forme_input(_form, field, _opts)
  forme_inputs[field] = super
end
forme_inputs ()

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

[show source]
# File lib/sequel/plugins/forme_set.rb, line 20
def forme_inputs
  @forme_inputs ||= {}
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, line 44
def forme_parse(params)
  hash = {}
  hash_values = hash[:values] = {}
  validations = hash[:validations] = {}

  forme_inputs.each do |field, input|
    opts = input.opts
    next if SKIP_FORMATTERS.include?(opts.fetch(:formatter){input.form.opts[:formatter]})

    if attr = opts[:attr]
      name = attr[:name] || attr['name']
    end
    name ||= opts[:name] || opts[:key] || next

    # Pull out last component of the name if there is one
    column = (name =~ /\[([^\[\]]+)\]\z/ ? $1 : name)
    column = column.to_s.sub(/\[\]\z/, '').to_sym

    hash_values[column] = params[column] || params[column.to_s]

    next unless ref = model.association_reflection(field)
    next unless options = opts[:options]

    values = if opts[:text_method]
      value_method = opts[:value_method] || opts[:text_method]
      options.map(&value_method)
    else
      options.map{|obj| obj.is_a?(Array) ? obj.last : obj}
    end

    if ref[:type] == :many_to_one && !opts[:required]
      values << nil
    end
    validations[column] = [ref[:type] != :many_to_one ? :subset : :include, values]
  end

  hash
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, line 85
def forme_set(params)
  hash = forme_parse(params)
  set(hash[:values])
  unless hash[:validations].empty?
    forme_validations.merge!(hash[:validations])
  end
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, line 27
def forme_validations
  @forme_validations ||= {}
end
validate ()

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

[show source]
# File lib/sequel/plugins/forme_set.rb, line 94
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)
      else
        raise Forme::Error, "invalid type used in forme_validations"
      end

      unless valid
        errors.add(column, 'invalid value submitted')
      end
    end
  end
end