Module: ActiveModel::AttributeFilters::ClassMethods

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

Overview

This module contains class methods that create DSL for managing attribute sets.

Instance Method Summary collapse

Instance Method Details

#attribute_setHash{Symbol => AttributeSet<String>} #attribute_set(set_name) ⇒ AttributeSet<String> #attribute_set(set_name, *attribute_names) ⇒ nil #attribute_set(associations) ⇒ nil #attribute_set(associations, *attribute_names) ⇒ nil Also known as: attributes_that_are, attributes_that, attributes_are, attributes_for, attributes_set, properties_that

Overloads:

  • #attribute_setHash{Symbol => AttributeSet<String>}

    Gets all the defined attribute sets.

    Returns:

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

      the collection of attribute sets indexed by their names

  • #attribute_set(set_name) ⇒ AttributeSet<String>

    Gets the contents of an attribute set of the given name.

    Parameters:

    • set_name (Symbol, String)

      name of a set

    Returns:

    • (AttributeSet<String>)

      the collection of attribute sets

  • #attribute_set(set_name, *attribute_names) ⇒ nil

    Adds new attributes to a set of attributes.

    Parameters:

    • set_name (Symbol, String)

      name of a set

    • attribute_names (Array<Symbol,String>)

      names of attributes to be stored in set

    Returns:

    • (nil)
  • #attribute_set(associations) ⇒ nil

    Adds new attributes to a set of attributes.

    Parameters:

    • associations (Hash{Symbol,String => Array<Symbol,String>})

      hash containing set names and arrays of attributes

    Returns:

    • (nil)
  • #attribute_set(associations, *attribute_names) ⇒ nil

    Creates one new set of attributes of the given names.

    Parameters:

    • associations (Hash{Symbol,String => String,Array<Symbol,String>})

      hash containing set names and arrays of attributes

    • attribute_names (Array<Symbol,String>)

      names of additional attributes to be stored in set

    Returns:

    • (nil)


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/attribute-filters/dsl_sets.rb', line 115

def attribute_set(*args)
  AttributeFiltersHelpers.check_wanted_methods(self)
  case args.size
  when 0
    attribute_sets
  when 1
    first_arg = args.first
    if first_arg.is_a?(Hash)
      first_arg.each_pair { |k, v| attribute_set(k, v) }
      nil
    else
      attribute_sets[first_arg.to_sym]
    end
  else
    first_arg = args.shift
    if first_arg.is_a?(Hash)
      first_arg.each_pair do |k, v|
        attribute_set(k, v, args)
      end
    else
      set_name = first_arg.to_sym
      atrs = args.flatten.compact.map{|a|a.to_s}.freeze
      atrs.each do |atr_name|
        __attributes_to_sets_map[atr_name] ||= ActiveModel::AttributeSet.new
        __attributes_to_sets_map[atr_name] << set_name
      end
      __attribute_sets[set_name] ||= ActiveModel::AttributeSet.new
      __attribute_sets[set_name] << atrs
    end
    nil
  end
end

#attribute_setsHash{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:

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

    the collection of attribute sets indexed by their names



222
223
224
225
226
# File 'lib/attribute-filters/dsl_sets.rb', line 222

def attribute_sets
  d = __attribute_sets.dup
  d.default = ActiveModel::AttributeSet.new
  d
end

#attributes_to_setsHash{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:

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

    the collection of attribute set names indexed by attribute names



235
236
237
238
239
# File 'lib/attribute-filters/dsl_sets.rb', line 235

def attributes_to_sets
  d = __attributes_to_sets_map.dup
  d.default = ActiveModel::AttributeSet.new
  d
end

#filter_attributeHash{String => AttributeSet<Symbol>} #filter_attribute(attribute_name) ⇒ AttributeSet<Symbol> #filter_attribute(attribute_name, *set_names) ⇒ nil #filter_attribute(associations) ⇒ nil #filter_attribute(associations, *set_names) ⇒ nil Also known as: the_attribute, add_attribute_to_set, add_attribute_to_sets, attribute_to_set, filtered_attribute, filtered_attributes

Overloads:

  • #filter_attributeHash{String => AttributeSet<Symbol>}

    Gets all the defined attribute sets.

    Returns:

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

      the collection of attribute set names indexed by attribute names

  • #filter_attribute(attribute_name) ⇒ AttributeSet<Symbol>

    Gets the names of all attribute sets that an attribute of the given name belongs to.

    Parameters:

    • attribute_name (Symbol, String)

      attribute name

    Returns:

    • (AttributeSet<Symbol>)

      the collection of attribute set names

  • #filter_attribute(attribute_name, *set_names) ⇒ nil

    Adds an attribute of the given name to attribute sets.

    Parameters:

    • attribute_name (Symbol, String)

      name of an attribute

    • set_names (Array<Symbol,String>)

      names of attribute sets to add the attribute to

    Returns:

    • (nil)
  • #filter_attribute(associations) ⇒ nil

    Adds an attribute of the given name to attribute sets.

    Parameters:

    • associations (Hash{Symbol,String => Array<Symbol,String>})

      hash containing attribute names and arrays of attribute set names

    Returns:

    • (nil)
  • #filter_attribute(associations, *set_names) ⇒ nil

    Creates one new set of attributes of the given names.

    Parameters:

    • associations (Hash{Symbol,String => String,Array<Symbol,String>})

      hash containing attribute names and arrays of attribute set name

    • set_names (Array<Symbol,String>)

      names of additional sets to assign attributes to

    Returns:

    • (nil)


182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/attribute-filters/dsl_sets.rb', line 182

def filter_attribute(*args)
  AttributeFiltersHelpers.check_wanted_methods(self)
  case args.size
  when 0
    attributes_to_sets
  when 1
    first_arg = args.first
    if first_arg.is_a?(Hash)
      first_arg.each_pair { |k, v| filter_attribute(k, v) }
      nil
    else
      attributes_to_sets[first_arg.to_s]
    end
  else
    first_arg = args.shift
    if first_arg.is_a?(Hash)
      first_arg.each_pair do |k, v|
        filter_attribute(k, v, args)
      end
    else
      first_arg = first_arg.to_s
      args.flatten.compact.each do |set_name|
        attribute_set(set_name, first_arg)
      end
    end
    nil
  end
end

#filter_virtual_attributes_that_changed?Boolean

Gets the internal flag that causes to check virtual attributes for changes when selecting attributes for filtering.

Returns:

  • (Boolean)

    true if the virtual attributes should be checked for a change, false otherwise



207
208
209
# File 'lib/attribute-filters/dsl_filters.rb', line 207

def filter_virtual_attributes_that_changed?
  !!@filter_virtual_attributes_that_changed
end

#filter_virtual_attributes_that_have_changed Also known as: filter_virtual_attributes_that_changed, filter_changed_virtual_attributes

This method returns an undefined value.

Sets the internal flag that causes to check virtual attributes for changes when selecting attributes for filtering.



198
199
200
# File 'lib/attribute-filters/dsl_filters.rb', line 198

def filter_virtual_attributes_that_have_changed
  @filter_virtual_attributes_that_changed = true
end

#treat_as_real(*attributes) #treat_as_realAttributeSet Also known as: treat_attribute_as_real, treat_attributes_as_real

Overloads:

  • #treat_as_real(*attributes)

    This method returns an undefined value.

    Informs Attribute Filters that the given attributes should be treated as present, even they are not in attributes hash provided by ORM or ActiveModel. Useful when operating on virtual attributes.

    Parameters:

    • attributes (Array)

      list of attribute names

  • #treat_as_realAttributeSet

    Gets the memorized attribute names that should be treated as existing.

    Returns:



187
188
189
190
191
# File 'lib/attribute-filters/dsl_filters.rb', line 187

def treat_as_real(*args)
  return __treat_as_real.dup if args.blank?
  __treat_as_real << args.flatten.compact.map { |atr| atr.to_s }
  nil
end