Module | ActiveRecord::AttributeMethods::ClassMethods |
In: |
vendor/rails/activerecord/lib/active_record/attribute_methods.rb
|
Declare and check for suffixed attribute methods.
Declare a method available for all attributes with the given suffix. Uses method_missing and respond_to? to rewrite the method
#{attr}#{suffix}(*args, &block)
to
attribute#{suffix}(#{attr}, *args, &block)
An attribute#{suffix} instance method must exist and accept at least the attr argument.
For example:
class Person < ActiveRecord::Base attribute_method_suffix '_changed?' private def attribute_changed?(attr) ... end end person = Person.find(1) person.name_changed? # => false person.name = 'Hubert' person.name_changed? # => true
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 38 38: def attribute_method_suffix(*suffixes) 39: attribute_method_suffixes.concat suffixes 40: rebuild_attribute_method_regexp 41: end
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 107 107: def cache_attribute?(attr_name) 108: cached_attributes.include?(attr_name) 109: end
cache_attributes allows you to declare which converted attribute values should be cached. Usually caching only pays off for attributes with expensive conversion methods, like date columns (e.g. created_at, updated_at).
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 97 97: def cache_attributes(*attribute_names) 98: attribute_names.each {|attr| cached_attributes << attr.to_s} 99: end
returns the attributes where
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 102 102: def cached_attributes 103: @cached_attributes ||= 104: columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map(&:name).to_set 105: end
generates all the attribute related methods for columns in the database accessors, mutators and query methods
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 61 61: def define_attribute_methods 62: return if generated_methods? 63: columns_hash.each do |name, column| 64: unless instance_method_already_implemented?(name) 65: if self.serialized_attributes[name] 66: define_read_method_for_serialized_attribute(name) 67: else 68: define_read_method(name.to_sym, name, column) 69: end 70: end 71: 72: unless instance_method_already_implemented?("#{name}=") 73: define_write_method(name.to_sym) 74: end 75: 76: unless instance_method_already_implemented?("#{name}?") 77: define_question_method(name) 78: end 79: end 80: end
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 55 55: def generated_methods? 56: !generated_methods.empty? 57: end
Check to see if the method is defined in the model or any of its subclasses that also derive from ActiveRecord. Raise DangerousAttributeError if the method is defined by ActiveRecord though.
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 84 84: def instance_method_already_implemented?(method_name) 85: return true if method_name =~ /^id(=$|\?$|$)/ 86: @_defined_class_methods ||= Set.new(ancestors.first(ancestors.index(ActiveRecord::Base)).collect! { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.flatten) 87: @@_defined_activerecord_methods ||= Set.new(ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)) 88: raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name) 89: @_defined_class_methods.include?(method_name) 90: end
Returns MatchData if method_name is an attribute method.
# File vendor/rails/activerecord/lib/active_record/attribute_methods.rb, line 44 44: def match_attribute_method?(method_name) 45: rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp 46: @@attribute_method_regexp.match(method_name) 47: end