Skip to content
Newer
Older
100644 121 lines (103 sloc) 3.91 KB
e8550ee @jeremy Cherry-pick core extensions
jeremy authored May 13, 2009
1 require 'active_support/core_ext/enumerable'
1a421ce @jonleighton Deprecate using method_missing for attributes that are columns.
jonleighton authored Sep 12, 2011
2 require 'active_support/deprecation'
e8550ee @jeremy Cherry-pick core extensions
jeremy authored May 13, 2009
3
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
4 module ActiveRecord
4ad6103 @rizwanreza Adds title and basic description where needed.
rizwanreza authored Jun 15, 2010
5 # = Active Record Attribute Methods
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
6 module AttributeMethods #:nodoc:
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored May 28, 2009
7 extend ActiveSupport::Concern
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored Aug 4, 2009
8 include ActiveModel::AttributeMethods
a2875be @brynary Use DependencyModule for included hooks in ActiveRecord
brynary authored May 11, 2009
9
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
10 module ClassMethods
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
11 # Generates all the attribute related methods for columns in the database
12 # accessors, mutators and query methods.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
13 def define_attribute_methods
24faddd @wildchild Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
wildchild authored Feb 13, 2011
14 return if attribute_methods_generated?
eecfa84 @jonleighton Always generate attribute methods on the base class.
jonleighton authored Sep 11, 2011
15
16 if base_class == self
17 super(column_names)
18 @attribute_methods_generated = true
19 else
20 base_class.define_attribute_methods
21 end
24faddd @wildchild Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
wildchild authored Feb 13, 2011
22 end
23
24 def attribute_methods_generated?
eecfa84 @jonleighton Always generate attribute methods on the base class.
jonleighton authored Sep 11, 2011
25 if base_class == self
26 @attribute_methods_generated ||= false
27 else
28 base_class.attribute_methods_generated?
29 end
24faddd @wildchild Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
wildchild authored Feb 13, 2011
30 end
31
f1a534a @jonleighton Remove the need for type_cast_attribute.
jonleighton authored Dec 1, 2011
32 def generated_attribute_methods
33 @generated_attribute_methods ||= (base_class == self ? super : base_class.generated_attribute_methods)
34 end
35
bb44e5a @jonleighton Use a separate module for 'external' attribute methods.
jonleighton authored Dec 14, 2011
36 def generated_external_attribute_methods
37 @generated_external_attribute_methods ||= begin
38 if base_class == self
39 # We will define the methods as instance methods, but will call them as singleton
40 # methods. This allows us to use method_defined? to check if the method exists,
41 # which is fast and won't give any false positives from the ancestors (because
42 # there are no ancestors).
43 Module.new { extend self }
44 else
45 base_class.generated_external_attribute_methods
46 end
47 end
48 end
49
365e10b @jonleighton Remove unnecessary *args
jonleighton authored Dec 1, 2011
50 def undefine_attribute_methods
eecfa84 @jonleighton Always generate attribute methods on the base class.
jonleighton authored Sep 11, 2011
51 if base_class == self
52 super
53 @attribute_methods_generated = false
54 else
365e10b @jonleighton Remove unnecessary *args
jonleighton authored Dec 1, 2011
55 base_class.undefine_attribute_methods
eecfa84 @jonleighton Always generate attribute methods on the base class.
jonleighton authored Sep 11, 2011
56 end
e129c56 @josh Wrap up attribute method reset concerns in 'undefine_attribute_methods'
josh authored Jul 29, 2009
57 end
58
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored Oct 3, 2007
59 def instance_method_already_implemented?(method_name)
55da28d @jonleighton We don't need to build a set for DangerousAttributeError.
jonleighton authored Sep 13, 2011
60 if dangerous_attribute_method?(method_name)
61 raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord"
62 end
63
3b8a7cf @jonleighton Stop trying to be clever about when to define attribute methods.
jonleighton authored Sep 13, 2011
64 super
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored Oct 3, 2007
65 end
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored May 9, 2010
66
55da28d @jonleighton We don't need to build a set for DangerousAttributeError.
jonleighton authored Sep 13, 2011
67 # A method name is 'dangerous' if it is already defined by Active Record, but
68 # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
69 def dangerous_attribute_method?(method_name)
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored May 8, 2010
70 active_record = ActiveRecord::Base
55da28d @jonleighton We don't need to build a set for DangerousAttributeError.
jonleighton authored Sep 13, 2011
71 superclass = ActiveRecord::Base.superclass
72
73 (active_record.method_defined?(method_name) ||
74 active_record.private_method_defined?(method_name)) &&
75 !superclass.method_defined?(method_name) &&
76 !superclass.private_method_defined?(method_name)
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored May 8, 2010
77 end
c30a0ce @paulgillard Modified ActiveRecord::AttributeMethods to allow classes to specify a…
paulgillard authored Aug 4, 2009
78 end
64eecdd @josh whitespace
josh authored Aug 4, 2009
79
ac687ed @jonleighton Let Ruby deal with method visibility.
jonleighton authored Sep 12, 2011
80 # If we haven't generated any methods yet, generate them, then
81 # see if we've created the method we're looking for.
82 def method_missing(method, *args, &block)
83 unless self.class.attribute_methods_generated?
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
84 self.class.define_attribute_methods
ac687ed @jonleighton Let Ruby deal with method visibility.
jonleighton authored Sep 12, 2011
85
86 if respond_to_without_attributes?(method)
87 send(method, *args, &block)
88 else
89 super
90 end
364a8f3 @jeremy No need to check for generated method, just redispatch
jeremy authored Nov 14, 2009
91 else
92 super
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
93 end
94 end
95
1a421ce @jonleighton Deprecate using method_missing for attributes that are columns.
jonleighton authored Sep 12, 2011
96 def attribute_missing(match, *args, &block)
97 if self.class.columns_hash[match.attr_name]
98 ActiveSupport::Deprecation.warn(
99 "The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \
100 "dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \
101 "is a column of the table. If this error has happened through normal usage of Active " \
102 "Record (rather than through your own code or external libraries), please report it as " \
103 "a bug."
104 )
105 end
106
107 super
108 end
109
6a283d5 @tenderlove match method signature of the superclass
tenderlove authored Jun 30, 2011
110 def respond_to?(name, include_private = false)
e0e3adf @jeremy Cheaper attribute reads and respond_to?. Add underscore-prefixed meth…
jeremy authored Aug 30, 2010
111 self.class.define_attribute_methods unless self.class.attribute_methods_generated?
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
112 super
113 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
114
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored Aug 5, 2009
115 protected
116 def attribute_method?(attr_name)
96eec09 @NZKoz Work around a strange piece of Syck behaviour where it checks Model#r…
NZKoz authored Dec 2, 2010
117 attr_name == 'id' || (defined?(@attributes) && @attributes.include?(attr_name))
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
118 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
119 end
120 end
Something went wrong with that request. Please try again.