class ActiveInteraction::ArrayFilter

@private

Constants

FILTER_NAME_OR_OPTION

The array starts with the class override key and then contains any additional options which halt explicit setting of the class.

Public Instance Methods

process(value, context) click to toggle source
Calls superclass method ActiveInteraction::Filter#process
# File lib/active_interaction/filters/array_filter.rb, line 41
def process(value, context)
  input = super

  return ArrayInput.new(self, value: input.value, error: input.errors.first) if input.errors.any?
  return ArrayInput.new(self, value: default(context), error: input.errors.first) if input.value.nil?

  value = input.value
  error = nil
  children = []

  unless filters.empty?
    value = value.map do |item|
      result = filters[:'0'].process(item, context)
      children.push(result)
      result.value
    end
  end

  ArrayInput.new(self, value: value, error: error, children: children, index_errors: index_errors?)
end

Private Instance Methods

add_option_in_place_of_name(klass, options) click to toggle source
# File lib/active_interaction/filters/array_filter.rb, line 109
def add_option_in_place_of_name(klass, options)
  if (keys = FILTER_NAME_OR_OPTION[klass.to_s]) && (keys & options.keys).empty?
    options.merge(
      "#{keys.first}": name.to_s.singularize.camelize.to_sym
    )
  else
    options
  end
end
adjust_output(value, _context) click to toggle source
# File lib/active_interaction/filters/array_filter.rb, line 95
def adjust_output(value, _context)
  value.to_a
end
convert(value) click to toggle source
Calls superclass method ActiveInteraction::Filter#convert
# File lib/active_interaction/filters/array_filter.rb, line 99
def convert(value)
  if value.respond_to?(:to_ary)
    [value.to_ary, nil]
  else
    super
  end
rescue NoMethodError # BasicObject
  super
end
index_errors?() click to toggle source
# File lib/active_interaction/filters/array_filter.rb, line 64
def index_errors?
  klass = 'ActiveRecord'.safe_constantize

  default =
    if !klass
      false
    elsif klass.respond_to?(:index_nested_attribute_errors)
      klass.index_nested_attribute_errors # Moved to here in Rails 7.0
    else
      klass::Base.index_nested_attribute_errors
    end
  options.fetch(:index_errors, default)
end
klasses() click to toggle source
# File lib/active_interaction/filters/array_filter.rb, line 78
def klasses
  %w[
    ActiveRecord::Relation
    ActiveRecord::Associations::CollectionProxy
  ].each_with_object([Array]) do |name, result|
    next unless (klass = name.safe_constantize)

    result.push(klass)
  end
end
matches?(value) click to toggle source
# File lib/active_interaction/filters/array_filter.rb, line 89
def matches?(value)
  klasses.any? { |klass| value.is_a?(klass) }
rescue NoMethodError # BasicObject
  false
end
method_missing(*, &block) click to toggle source

rubocop:disable Style/MissingRespondToMissing

# File lib/active_interaction/filters/array_filter.rb, line 120
def method_missing(*, &block)
  super do |klass, names, options|
    options = add_option_in_place_of_name(klass, options)

    filter = klass.new(names.first || '', options, &block)

    filters[filters.size.to_s.to_sym] = filter

    validate!(names)
  end
end
validate!(names) click to toggle source

@param filter [Filter] @param names [Array<Symbol>]

@raise [InvalidFilterError]

# File lib/active_interaction/filters/array_filter.rb, line 137
def validate!(names)
  raise InvalidFilterError, 'multiple filters in array block' if filters.size > 1

  raise InvalidFilterError, 'attribute names in array block' unless names.empty?

  nil
end