Module: ActiveModel::AttributeFilters

Defined in:
lib/attribute-filters/dsl_sets.rb,
lib/attribute-filters/helpers.rb,
lib/attribute-filters/railtie.rb,
lib/attribute-filters/version.rb,
lib/attribute-filters/dsl_filters.rb,
lib/attribute-filters/attribute_set.rb,
lib/attribute-filters/common_filters.rb,
lib/attribute-filters/dsl_attr_virtual.rb,
lib/attribute-filters/common_filters/bare.rb,
lib/attribute-filters/common_filters/case.rb,
lib/attribute-filters/common_filters/join.rb,
lib/attribute-filters/common_filters/pick.rb,
lib/attribute-filters/common_filters/order.rb,
lib/attribute-filters/common_filters/split.rb,
lib/attribute-filters/common_filters/strip.rb,
lib/attribute-filters/common_filters/convert.rb,
lib/attribute-filters/common_filters/squeeze.rb,
lib/attribute-filters/common_filters/presence.rb,
lib/attribute-filters/attribute_set_annotations.rb

Overview

This module contains instance methods for getting and setting attribute sets established in classes (models).

Defined Under Namespace

Modules: AttributeFiltersHelpers, AttributeSetMethods, ClassMethods, Common, FilteringRegistration Classes: Railtie

Constant Summary collapse

AFHelpers =

Helpers module shortcut.

AttributeFiltersHelpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base)

This method returns an undefined value.

This method is called when the module is included into some class. It simply calls extend on the class with ClassMethods passed as an argument in order to add some DSL class methods to the model.



18
19
20
21
22
23
24
25
# File 'lib/attribute-filters/dsl_sets.rb', line 18

def self.included(base)
  base.extend ClassMethods
  base.module_eval do
    unless method_defined?(:attribute_set)
      alias_method :attribute_set, :af_attribute_set
    end
  end
end

Instance Method Details

#af_attribute_set(set_name = nil) ⇒ AttributeSet Also known as: attributes_that_are, from_attributes_that, are_attributes_that, are_attributes_that_are, are_attributes_for, from_attributes_that_are, in_attributes_that_are, attributes_that, attributes_are, attributes_for, attributes_set, properties_that

Note:

The returned value is a duplicate. Adding or removing elements to it will have no effect on class-level set. Altering attribute sets is possible on a class-level only, since attribute sets are part of models’ interfaces.

Returns the attribute set of the given name or the set containing all attributes (if the argument is not given).

If the given set_name is a kind of String or Symbol then the method returns a copy of a set that is stored within a model class. The copy is wrapped in a ‘AttributeSet::AttrQuery` instance.

If the argument is a kind of ActiveModel::AttributeSet then the local set is taken and wrapped in a ActiveModel::AttributeSet::AttrQuery instance.

If the argument is nil then the local set is guessed by assuming that the next method in a call chain is really the name of a set.

If the argument is other kind than the specified above then the method tries to initialize new, local set object and wraps it in a ‘AttributeSet::AttrQuery` instance.

Parameters:

  • set_name (Symbol) (defaults to: nil)

    name of attribute set, attribute object or any object that can initialize a set

Returns:



51
52
53
# File 'lib/attribute-filters/dsl_sets.rb', line 51

def af_attribute_set(set_name = nil)
  ActiveModel::AttributeSet::Query.new(set_name, self)
end

#all_accessible_attributes(simple = false) ⇒ AttributeSet Also known as: accessible_attributes_set

Returns a set containing all accessible attributes.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

Returns:



102
103
104
105
106
# File 'lib/attribute-filters/dsl_sets.rb', line 102

def all_accessible_attributes(simple = false)
  my_class = self.class
  c = my_class.respond_to?(:accessible_attributes) ? my_class.accessible_attributes : []
  simple ? AttributeSet.new(c) : AttributeSet::Query.new(c, self)
end

#all_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet Also known as: all_attributes_set

Returns a set containing all known attributes without wrapping the result in a proxy.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

  • no_presence_check (Boolean) (defaults to: true)

    optional parameter that disables checking for presence of setters and getters for each virtual and semi-real attribute (defaults to true)

Returns:



91
92
93
94
# File 'lib/attribute-filters/dsl_sets.rb', line 91

def all_attributes(simple = false, no_presence_check = true)
  r = __all_attributes(no_presence_check).deep_dup
  simple ? r : ActiveModel::AttributeSet::Query.new(r, self)
end

#all_inaccessible_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet Also known as: all_non_accessible_attributes, inaccessible_attributes_set

Returns a set containing all attributes that are not accessible attributes.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

  • no_presence_check (Boolean) (defaults to: true)

    optional parameter that disables checking for presence of setters and getters for each virtual and semi-real attribute (defaults to true)

Returns:



157
158
159
# File 'lib/attribute-filters/dsl_sets.rb', line 157

def all_inaccessible_attributes(simple = false, no_presence_check = true)
  all_attributes(simple, no_presence_check) - all_accessible_attributes(simple)
end

#all_protected_attributes(simple = false) ⇒ AttributeSet Also known as: protected_attributes_set

Returns a set containing all protected attributes.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

Returns:



114
115
116
117
118
# File 'lib/attribute-filters/dsl_sets.rb', line 114

def all_protected_attributes(simple = false)
  my_class = self.class
  c = my_class.respond_to?(:protected_attributes) ? my_class.protected_attributes : []
  simple ? AttributeSet.new(c) : AttributeSet::Query.new(c, self)
end

#all_semi_real_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet Also known as: semi_real_attributes_set, treat_as_real

Returns a set containing attributes declared as semi-real.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

  • no_presence_check (Boolean) (defaults to: true)

    optional parameter that disables checking for presence of setters and getters for each attribute (defaults to true)

Returns:



141
142
143
144
145
# File 'lib/attribute-filters/dsl_sets.rb', line 141

def all_semi_real_attributes(simple = false, no_presence_check = true)
  c = self.class.treat_as_real
  c = c.select_accessible(self) unless no_presence_check || c.empty? 
  simple ? c : AttributeSet::Query.new(c, self)
end

#all_virtual_attributes(simple = false) ⇒ AttributeSet Also known as: virtual_attributes_set, attribute_filters_virtual

Returns a set containing attributes declared as virtual with attr_virtual.

Parameters:

  • simple (Boolean) (defaults to: false)

    optional parameter that disables wrapping a resulting set in a proxy (defaults to false)

Returns:



126
127
128
129
# File 'lib/attribute-filters/dsl_sets.rb', line 126

def all_virtual_attributes(simple = false)
  c = self.class.attribute_filters_virtual
  simple ? c : AttributeSet::Query.new(c, self)
end

#attribute_set_exists?(set_name) ⇒ Boolean

Checks if the given set exists.

Parameters:

  • set_name (String, Symbol)

    name of a set

Returns:

  • (Boolean)

    true if a set of the given name exists, false otherwise



184
185
186
# File 'lib/attribute-filters/dsl_sets.rb', line 184

def attribute_set_exists?(set_name)
  set_name.present? && self.class.send(:__attribute_sets).key?(set_name.to_sym)
end

#attribute_set_simple(set_name) ⇒ AttributeSet

Note:

The returned value is a duplicate. Adding or removing elements to it will have no effect. Altering attribute sets is possible on a class-level only, since attribute sets are part of models’ interfaces.

Returns the attribute set of the given name without wrapping the result in proxy methods.

Parameters:

  • set_name (Symbol)

    name of attribute set

Returns:



78
79
80
# File 'lib/attribute-filters/dsl_sets.rb', line 78

def attribute_set_simple(set_name)
  self.class.af_attribute_set(set_name)
end

#attribute_setsMetaSet{Symbol => AttributeSet<String>} Also known as: attributes_sets, properties_sets

Note:

Use key method explicitly to check if the given set exists. The hash returned by this method will always return ActiveModel::AttributeSet object. If there is no such set defined then the returned, matching set will be empty.

Gets all the defined attribute sets.

Returns:

  • (MetaSet{Symbol => AttributeSet<String>})

    the collection of attribute sets indexed by their names



170
171
172
173
174
175
176
# File 'lib/attribute-filters/dsl_sets.rb', line 170

def attribute_sets
  s = self.class.attribute_sets
  s.each_pair do |set_name, set_object|
    s[set_name] = ActiveModel::AttributeSet::Query.new(set_object, self)
  end
  s
end

#attributes_to_filter(set_name, process_all, no_presence_check) ⇒ AttributeSet #attributes_to_filter(attribute_set, process_all, no_presence_check) ⇒ AttributeSet

Gets names of attributes for which filters should be applied by selecting attributes that are meeting certain criteria and belong to the given attribute set.

Overloads:

  • #attributes_to_filter(set_name, process_all, no_presence_check) ⇒ AttributeSet

    Returns set of attributes (attribute name => previous_value).

    Parameters:

    • set_name (String, Symbol)

      name of a set of attributes used to get attributes

    • process_all (Boolean)

      if set then all the attributes from the attribute set are selected, not just the attributes that have changed (defaults to false)

    • no_presence_check (Boolean)

      if set then the checking whether each attribute exists will be disabled (matters only when process_all is also set) (defaults to false)

    Returns:

    • (AttributeSet)

      set of attributes (attribute name => previous_value)

  • #attributes_to_filter(attribute_set, process_all, no_presence_check) ⇒ AttributeSet

    Returns set of attributes (attribute name => previous_value).

    Parameters:

    • attribute_set (AttributeSet)

      set of attributes used to get attributes

    • process_all (Boolean)

      if set then all the attributes from the attribute set are selected, not just the attributes that has changed (defaults to false)

    • no_presence_check (Boolean)

      if set then the checking whether each attribute exists will be disabled (matters only when process_all is also set) (defaults to false)

    Returns:

    • (AttributeSet)

      set of attributes (attribute name => previous_value)



42
43
44
45
46
47
48
49
50
51
# File 'lib/attribute-filters/dsl_filters.rb', line 42

def attributes_to_filter(set_name, process_all = false, no_presence_check = false)
  set_name.blank? and return ::ActiveModel::AttributeSet.new
  atf = set_name.is_a?(::ActiveModel::AttributeSet) ? set_name : self.class.send(:__attribute_sets)[set_name.to_sym]
  if process_all
    no_presence_check ? atf.deep_dup : atf & __all_attributes(false)
  else
    sr = self.class.send(:__attribute_filters_semi_real)
    atf & ((no_presence_check ? sr : sr.select_accessible(self)) + changes.keys)
  end
end

#attributes_to_setsMetaSet{String => AttributeSet<Symbol>} Also known as: attribute_sets_map

Note:

Use key method explicitly to check if the given attribute is assigned to any set. The hash returned by this method will always return ActiveModel::AttributeSet object. If the attribute is not assigned to any set then the returned, matching set will be empty.

Gets all the defined attribute set names hashed by attribute names.

Returns:

  • (MetaSet{String => AttributeSet<Symbol>})

    the collection of attribute set names indexed by attribute names



232
233
234
# File 'lib/attribute-filters/dsl_sets.rb', line 232

def attributes_to_sets
  self.class.attributes_to_sets
end

#filter_attributesnil

This method is a callback method that tries to call all known filtering methods if they are in use.

Calling order depends on sets registering order.

Returns:

  • (nil)


99
100
101
102
103
104
# File 'lib/attribute-filters/common_filters.rb', line 99

def filter_attributes
  as, fs = *self.class.class_eval { [__attribute_sets, @attribute_filters] }
  return if fs.blank? || as.blank?
  as.each_pair { |set_name, o| send(fs[set_name]) if fs.has_key?(set_name) }
  nil
end

#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

#filtered_attribute(attribute_name = nil) ⇒ AttributeSet Also known as: are_attributes, the_attribute, is_the_attribute, has_the_attribute, are_the_attributes

Note:

The returned value is a duplicate. Adding or removing elements to it will have no effect. Altering attribute sets is possible on a class-level only, since attribute sets are part of models’ interfaces.

Returns the set of set names that the attribute of the given name belongs to.

If the given attribute name is nil then it is taken from the name of a next method in a method call chain.

Parameters:

  • attribute_name (Symbol) (defaults to: nil)

    name of attribute set

Returns:



201
202
203
# File 'lib/attribute-filters/dsl_sets.rb', line 201

def filtered_attribute(attribute_name = nil)
  ActiveModel::AttributeSet::AttrQuery.new(attribute_name, self)
end

#filtered_attribute_simple(attribute_name) ⇒ AttributeSet Also known as: the_attribute_simple

Note:

The returned value is a duplicate. Adding or removing elements to it will have no effect. Altering attribute sets is possible on a class-level only, since attribute sets are part of models’ interfaces.

Returns the set of set names that the attribute of the given name belongs to.

Parameters:

  • attribute_name (Symbol)

    name of attribute set

Returns:



220
221
222
# File 'lib/attribute-filters/dsl_sets.rb', line 220

def filtered_attribute_simple(attribute_name)
  self.class.filter_attribute(attribute_name)
end

#filtering_methodsMetaSet{Symbol => Symbol}

Gets a list of filtering hooks that are in use.

Returns:

  • (MetaSet{Symbol => Symbol})

    a meta set of filtering methods and associated sets



109
110
111
112
# File 'lib/attribute-filters/common_filters.rb', line 109

def filtering_methods
  f = self.class.instance_variable_get(:@attribute_filters)
  f.nil? ? ActiveModel::MetaSet.new : f.dup
end

#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