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
-
.included(base)
This method is called when the module is included into some class.
Instance Method Summary collapse
-
#af_attribute_set(set_name = nil) ⇒ AttributeSet
(also: #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)
Returns the attribute set of the given name or the set containing all attributes (if the argument is not given).
-
#all_accessible_attributes(simple = false) ⇒ AttributeSet
(also: #accessible_attributes_set)
Returns a set containing all accessible attributes.
-
#all_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet
(also: #all_attributes_set)
Returns a set containing all known attributes without wrapping the result in a proxy.
-
#all_inaccessible_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet
(also: #all_non_accessible_attributes, #inaccessible_attributes_set)
Returns a set containing all attributes that are not accessible attributes.
-
#all_protected_attributes(simple = false) ⇒ AttributeSet
(also: #protected_attributes_set)
Returns a set containing all protected attributes.
-
#all_semi_real_attributes(simple = false, no_presence_check = true) ⇒ AttributeSet
(also: #semi_real_attributes_set, #treat_as_real)
Returns a set containing attributes declared as semi-real.
-
#all_virtual_attributes(simple = false) ⇒ AttributeSet
(also: #virtual_attributes_set, #attribute_filters_virtual)
Returns a set containing attributes declared as virtual with
attr_virtual
. -
#attribute_set_exists?(set_name) ⇒ Boolean
Checks if the given set exists.
-
#attribute_set_simple(set_name) ⇒ AttributeSet
Returns the attribute set of the given name without wrapping the result in proxy methods.
-
#attribute_sets ⇒ MetaSet{Symbol => AttributeSet<String>}
(also: #attributes_sets, #properties_sets)
Gets all the defined attribute sets.
-
#attributes_to_filter(set_name, process_all = false, no_presence_check = false) ⇒ Object
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.
-
#attributes_to_sets ⇒ MetaSet{String => AttributeSet<Symbol>}
(also: #attribute_sets_map)
Gets all the defined attribute set names hashed by attribute names.
-
#filter_attributes ⇒ nil
This method is a callback method that tries to call all known filtering methods if they are in use.
-
#filter_attrs_from_set(set_name, *flags, *args) {|attribute_value, set_name, attribute_name, *args| ... }
(also: #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)
This generic method writes the result of execution of the passed block to each attribute that belongs to the given set of attributes.
-
#filtered_attribute(attribute_name = nil) ⇒ AttributeSet
(also: #are_attributes, #the_attribute, #is_the_attribute, #has_the_attribute, #are_the_attributes)
Returns the set of set names that the attribute of the given name belongs to.
-
#filtered_attribute_simple(attribute_name) ⇒ AttributeSet
(also: #the_attribute_simple)
Returns the set of set names that the attribute of the given name belongs to.
-
#filtering_methods ⇒ MetaSet{Symbol => Symbol}
Gets a list of filtering hooks that are in use.
-
#for_each_attr_from_set(set_name, *flags, *args) {|attribute_value, set_name, attribute_name, *args| ... }
(also: #attribute_call_for_set, #call_attrs_from_set, #for_attributes_which, #for_attributes_that, #for_attributes_that_are, #for_attributes_which_are)
This generic method calls the passed block for each attribute that belongs to the given set of attributes.
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
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.
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.
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.
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.
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.
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.
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
.
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.
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
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.
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_sets ⇒ MetaSet{Symbol => AttributeSet<String>} Also known as: attributes_sets, properties_sets
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.
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.
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_sets ⇒ MetaSet{String => AttributeSet<Symbol>} Also known as: attribute_sets_map
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.
232 233 234 |
# File 'lib/attribute-filters/dsl_sets.rb', line 232 def attributes_to_sets self.class.attributes_to_sets end |
#filter_attributes ⇒ nil
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.
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
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
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.
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
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.
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_methods ⇒ MetaSet{Symbol => Symbol}
Gets a list of filtering hooks that are in use.
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
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 |