# File lib/active_record/attribute_methods.rb, line 121 121: def attribute_method?(attribute) 122: super || (table_exists? && column_names.include?(attribute.to_s.sub(/=$/, ''))) 123: end
# File lib/active_record/attribute_methods.rb, line 72 72: def attribute_methods_generated? 73: @attribute_methods_generated ||= false 74: end
Returns an array of column names as strings if it’s not an abstract class and table exists. Otherwise it returns an empty array.
# File lib/active_record/attribute_methods.rb, line 128 128: def attribute_names 129: @attribute_names ||= if !abstract_class? && table_exists? 130: column_names 131: else 132: [] 133: end 134: end
A method name is ‘dangerous’ if it is already defined by Active Record, but not by any ancestors. (So ‘puts’ is not dangerous but ‘save’ is.)
# File lib/active_record/attribute_methods.rb, line 105 105: def dangerous_attribute_method?(name) 106: method_defined_within?(name, Base) 107: end
Generates all the attribute related methods for columns in the database accessors, mutators and query methods.
# File lib/active_record/attribute_methods.rb, line 38 38: def define_attribute_methods 39: unless defined?(@attribute_methods_mutex) 40: msg = "It looks like something (probably a gem/plugin) is overriding the " "ActiveRecord::Base.inherited method. It is important that this hook executes so " "that your models are set up correctly. A workaround has been added to stop this " "causing an error in 3.2, but future versions will simply not work if the hook is " "overridden. If you are using Kaminari, please upgrade as it is known to have had " "this problem.\n\n" 41: msg << "The following may help track down the problem:" 42: 43: meth = method(:inherited) 44: if meth.respond_to?(:source_location) 45: msg << " #{meth.source_location.inspect}" 46: else 47: msg << " #{meth.inspect}" 48: end 49: msg << "\n\n" 50: 51: ActiveSupport::Deprecation.warn(msg) 52: 53: @attribute_methods_mutex = Mutex.new 54: end 55: 56: # Use a mutex; we don't want two thread simaltaneously trying to define 57: # attribute methods. 58: @attribute_methods_mutex.synchronize do 59: return if attribute_methods_generated? 60: superclass.define_attribute_methods unless self == base_class 61: super(column_names) 62: column_names.each { |name| define_external_attribute_method(name) } 63: @attribute_methods_generated = true 64: end 65: end
We will define the methods as instance methods, but will call them as singleton methods. This allows us to use method_defined? to check if the method exists, which is fast and won’t give any false positives from the ancestors (because there are no ancestors).
# File lib/active_record/attribute_methods.rb, line 80 80: def generated_external_attribute_methods 81: @generated_external_attribute_methods ||= Module.new { extend self } 82: end
# File lib/active_record/attribute_methods.rb, line 89 89: def instance_method_already_implemented?(method_name) 90: if dangerous_attribute_method?(method_name) 91: raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" 92: end 93: 94: if superclass == Base 95: super 96: else 97: # If B < A and A defines its own attribute method, then we don't want to overwrite that. 98: defined = method_defined_within?(method_name, superclass, superclass.generated_attribute_methods) 99: defined && !ActiveRecord::Base.method_defined?(method_name) || super 100: end 101: end
# File lib/active_record/attribute_methods.rb, line 109 109: def method_defined_within?(name, klass, sup = klass.superclass) 110: if klass.method_defined?(name) || klass.private_method_defined?(name) 111: if sup.method_defined?(name) || sup.private_method_defined?(name) 112: klass.instance_method(name).owner != sup.instance_method(name).owner 113: else 114: true 115: end 116: else 117: false 118: end 119: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.