Method: ActiveModel::AttributeFilters#filter_attrs_from_set

Defined in:
lib/attribute-filters/dsl_filters.rb

#filter_attrs_from_set(set_name, *flags, *args) {|attribute_value, set_name, attribute_name, *args| ... } Also known as: attribute_filter_for_set, filter_attributes_which, filter_attributes_that, filter_attributes_that_are, filter_attributes_which_are, alter_attributes_which, alter_attributes_that, alter_attributes_that_are, alter_attributes_which_are

Note:

Combining the flags process_all and no_presence_check may raise exception if some attribute from the given set doesn’t exist

This method returns an undefined value.

This generic method writes the result of execution of the passed block to each attribute that belongs to the given set of attributes. It’s major purpose is to create filtering methods.

Only the changed attributes/properties are selected, unless the process_all flag is given. If that flag is given then presence of each attribute is verified, unless the no_presence_check flag is also set. Attributes with empty or unset values are ignored (but see the flag called process_blank).

The result of the given block is used to set a new values for processed attributes.

Examples:

class User < ActiveRecord::Base

  attributes_that should_be_downcased: [ :username, :email ]
  before_filter :downcase_names

  def downcase_names
    filter_attributes_that :should_be_downcased do |atr|
      atr.mb_chars.downcase.to_s
    end
  end

end

Parameters:

  • set_name (Symbol)

    name of the attribute set or a set object

  • args (Array)

    optional additional arguments that will be passed to the block

  • flags (Array<Symbol>)

    optional additional flags controlling the processing of attributes:

    • :process_blank – tells to also process attributes that are blank (empty or nil)

    • :process_all - tells to process all attributes, not just the ones that has changed

    • :no_presence_check – tells not to check for existence of each processed attribute when processing all attributes; increases performance but you must care about putting only the existing attributes into sets

Yields:

  • (attribute_value, set_name, attribute_name, *args)

    block that will be called for each attribute

Yield Parameters:

  • attribute_value (Object)

    current attribute value that should be altered

  • attribute_name (String)

    a name of currently processed attribute

  • set_object (Object)

    currently processed set that attribute belongs to

  • set_name (Symbol)

    a name of the processed attribute set

  • args (Array)

    optional arguments passed to the method

Yield Returns:

  • (Object)

    the result of calling the block



100
101
102
# File 'lib/attribute-filters/dsl_filters.rb', line 100

def filter_attrs_from_set(set_name, *args, &block)
  operate_on_attrs_from_set(set_name, true, *args, &block)
end