Declare and check for suffixed attribute methods.
- attribute_method_suffix
- cache_attribute?
- cache_attributes
- cached_attributes
- define_attribute_methods
- define_read_methods
- generated_methods?
- instance_method_already_implemented?
- match_attribute_method?
Declares 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
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 46 46: def attribute_method_suffix(*suffixes) 47: attribute_method_suffixes.concat suffixes 48: rebuild_attribute_method_regexp 49: end
Returns true if the provided attribute is being cached.
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 125 125: def cache_attribute?(attr_name) 126: cached_attributes.include?(attr_name) 127: 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 time related columns (e.g. created_at, updated_at).
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 113 113: def cache_attributes(*attribute_names) 114: attribute_names.each {|attr| cached_attributes << attr.to_s} 115: end
Returns the attributes which are cached. By default time related columns with datatype :datetime, :timestamp, :time, :date are cached.
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 119 119: def cached_attributes 120: @cached_attributes ||= 121: columns.select{|c| attribute_types_cached_by_default.include?(c.type)}.map(&:name).to_set 122: end
Generates all the attribute related methods for columns in the database accessors, mutators and query methods.
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 69 69: def define_attribute_methods 70: return if generated_methods? 71: columns_hash.each do |name, column| 72: unless instance_method_already_implemented?(name) 73: if self.serialized_attributes[name] 74: define_read_method_for_serialized_attribute(name) 75: elsif create_time_zone_conversion_attribute?(name, column) 76: define_read_method_for_time_zone_conversion(name) 77: else 78: define_read_method(name.to_sym, name, column) 79: end 80: end 81: 82: unless instance_method_already_implemented?("#{name}=") 83: if create_time_zone_conversion_attribute?(name, column) 84: define_write_method_for_time_zone_conversion(name) 85: else 86: define_write_method(name.to_sym) 87: end 88: end 89: 90: unless instance_method_already_implemented?("#{name}?") 91: define_question_method(name) 92: end 93: end 94: end
Alias for define_attribute_methods
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 63 63: def generated_methods? 64: !generated_methods.empty? 65: end
Checks whether the method is defined in the model or any of its subclasses that also derive from Active Record. Raises DangerousAttributeError if the method is defined by Active Record though.
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 99 99: def instance_method_already_implemented?(method_name) 100: method_name = method_name.to_s 101: return true if method_name =~ /^id(=$|\?$|$)/ 102: @_defined_class_methods ||= ancestors.first(ancestors.index(ActiveRecord::Base)).sum([]) { |m| m.public_instance_methods(false) | m.private_instance_methods(false) | m.protected_instance_methods(false) }.map(&:to_s).to_set 103: @@_defined_activerecord_methods ||= (ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)).map(&:to_s).to_set 104: raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name) 105: @_defined_class_methods.include?(method_name) 106: end
Returns MatchData if method_name is an attribute method.
[ show source ]
# File activerecord/lib/active_record/attribute_methods.rb, line 52 52: def match_attribute_method?(method_name) 53: rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp 54: @@attribute_method_regexp.match(method_name) 55: end