Method: ActiveModel::AttributeFilters#for_each_attr_from_set

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

#for_each_attr_from_set(set_name, *flags, *args) {|attribute_value, set_name, attribute_name, *args| ... } Also known as: attribute_call_for_set, call_attrs_from_set, for_attributes_which, for_attributes_that, for_attributes_that_are, for_attributes_which_are

Note:

If you’re looking for a method that is designed to alter attributes by rewritting their contents see #filter_attrs_from_set

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 calls the passed block for each attribute that belongs to the given set of attributes. It’s major purpose is to iterate through attributes and/or work directly with their values.

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 not used to set the processed attribute. The only way to alter attribute values using this method is to use bang method in a block or explicitly assign new, calculated value to the attribute using its name (also passed to a block as one of arguments).

Examples:

class User < ActiveRecord::Base

  attributes_that should_be_stripped: [ :username, :email ]
  before_filter :strip_names

  def strip_names
    for_attributes_that :should_be_stripped do |atr|
      atr.strip!
    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 a 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 into set only the existing attributes

    • :include_missing – includes attributes that does not exist in a resulting iteration (their values are always nil); has the effect only when process_blank and no_presence_check are set

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



168
169
170
# File 'lib/attribute-filters/dsl_filters.rb', line 168

def for_each_attr_from_set(set_name, *args, &block)
  operate_on_attrs_from_set(set_name, false, *args, &block)
end