Skip to content
This repository
Newer
Older
100644 106 lines (90 sloc) 3.405 kb
e8550ee0 » jeremy
2009-05-13 Cherry-pick core extensions
1 require 'active_support/core_ext/enumerable'
1a421cec » jonleighton
2011-09-12 Deprecate using method_missing for attributes that are columns.
2 require 'active_support/deprecation'
e8550ee0 » jeremy
2009-05-13 Cherry-pick core extensions
3
2b3cc247 » jeremy
2006-07-31 r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
4 module ActiveRecord
4ad6103c » rizwanreza
2010-06-15 Adds title and basic description where needed.
5 # = Active Record Attribute Methods
2b3cc247 » jeremy
2006-07-31 r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
6 module AttributeMethods #:nodoc:
4e50a35f » josh
2009-05-28 Break up DependencyModule's dual function of providing a "depend_on" …
7 extend ActiveSupport::Concern
f8d3c72c » josh
2009-08-04 Extract generic attribute method generation to AMo
8 include ActiveModel::AttributeMethods
a2875bec » brynary
2009-05-11 Use DependencyModule for included hooks in ActiveRecord
9
2b3cc247 » jeremy
2006-07-31 r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
10 module ClassMethods
46f30f90 » lifo
2008-05-16 Merge documentation changes from docrails.
11 # Generates all the attribute related methods for columns in the database
12 # accessors, mutators and query methods.
5b801b59 » NZKoz
2007-08-14 Change the implementation of ActiveRecord's attribute reader and writ…
13 def define_attribute_methods
24faddd6 » wildchild
2011-02-13 Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
14 return if attribute_methods_generated?
eecfa84a » jonleighton
2011-09-11 Always generate attribute methods on the base class.
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
24faddd6 » wildchild
2011-02-13 Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
22 end
23
24 def attribute_methods_generated?
eecfa84a » jonleighton
2011-09-11 Always generate attribute methods on the base class.
25 if base_class == self
26 @attribute_methods_generated ||= false
27 else
28 base_class.attribute_methods_generated?
29 end
24faddd6 » wildchild
2011-02-13 Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
30 end
31
f1a534af » jonleighton
2011-12-01 Remove the need for type_cast_attribute.
32 def generated_attribute_methods
33 @generated_attribute_methods ||= (base_class == self ? super : base_class.generated_attribute_methods)
34 end
35
365e10b8 » jonleighton
2011-12-01 Remove unnecessary *args
36 def undefine_attribute_methods
eecfa84a » jonleighton
2011-09-11 Always generate attribute methods on the base class.
37 if base_class == self
38 super
39 @attribute_methods_generated = false
40 else
365e10b8 » jonleighton
2011-12-01 Remove unnecessary *args
41 base_class.undefine_attribute_methods
eecfa84a » jonleighton
2011-09-11 Always generate attribute methods on the base class.
42 end
e129c567 » josh
2009-07-29 Wrap up attribute method reset concerns in 'undefine_attribute_methods'
43 end
44
b31aa639 » Tobias Lütke
2007-10-03 Allow column accessors to be created even if Kernel. or Object# metho…
45 def instance_method_already_implemented?(method_name)
55da28dd » jonleighton
2011-09-13 We don't need to build a set for DangerousAttributeError.
46 if dangerous_attribute_method?(method_name)
47 raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord"
48 end
49
3b8a7cf2 » jonleighton
2011-09-13 Stop trying to be clever about when to define attribute methods.
50 super
b31aa639 » Tobias Lütke
2007-10-03 Allow column accessors to be created even if Kernel. or Object# metho…
51 end
d916c62c » wycats
2010-05-09 eliminate alias_method_chain from ActiveRecord
52
55da28dd » jonleighton
2011-09-13 We don't need to build a set for DangerousAttributeError.
53 # A method name is 'dangerous' if it is already defined by Active Record, but
54 # not by any ancestors. (So 'puts' is not dangerous but 'save' is.)
55 def dangerous_attribute_method?(method_name)
d916c62c » wycats
2010-05-09 eliminate alias_method_chain from ActiveRecord
56 active_record = ActiveRecord::Base
55da28dd » jonleighton
2011-09-13 We don't need to build a set for DangerousAttributeError.
57 superclass = ActiveRecord::Base.superclass
58
59 (active_record.method_defined?(method_name) ||
60 active_record.private_method_defined?(method_name)) &&
61 !superclass.method_defined?(method_name) &&
62 !superclass.private_method_defined?(method_name)
d916c62c » wycats
2010-05-09 eliminate alias_method_chain from ActiveRecord
63 end
c30a0ce3 » paulgillard
2009-08-04 Modified ActiveRecord::AttributeMethods to allow classes to specify a…
64 end
64eecdd1 » josh
2009-08-04 whitespace
65
ac687ed6 » jonleighton
2011-09-12 Let Ruby deal with method visibility.
66 # If we haven't generated any methods yet, generate them, then
67 # see if we've created the method we're looking for.
68 def method_missing(method, *args, &block)
69 unless self.class.attribute_methods_generated?
5b801b59 » NZKoz
2007-08-14 Change the implementation of ActiveRecord's attribute reader and writ…
70 self.class.define_attribute_methods
ac687ed6 » jonleighton
2011-09-12 Let Ruby deal with method visibility.
71
72 if respond_to_without_attributes?(method)
73 send(method, *args, &block)
74 else
75 super
76 end
364a8f39 » jeremy
2009-11-14 No need to check for generated method, just redispatch
77 else
78 super
5b801b59 » NZKoz
2007-08-14 Change the implementation of ActiveRecord's attribute reader and writ…
79 end
80 end
81
1a421cec » jonleighton
2011-09-12 Deprecate using method_missing for attributes that are columns.
82 def attribute_missing(match, *args, &block)
83 if self.class.columns_hash[match.attr_name]
84 ActiveSupport::Deprecation.warn(
85 "The method `#{match.method_name}', matching the attribute `#{match.attr_name}' has " \
86 "dispatched through method_missing. This shouldn't happen, because `#{match.attr_name}' " \
87 "is a column of the table. If this error has happened through normal usage of Active " \
88 "Record (rather than through your own code or external libraries), please report it as " \
89 "a bug."
90 )
91 end
92
93 super
94 end
95
6a283d59 » tenderlove
2011-06-30 match method signature of the superclass
96 def respond_to?(name, include_private = false)
e0e3adff » jeremy
2010-08-30 Cheaper attribute reads and respond_to?. Add underscore-prefixed meth…
97 self.class.define_attribute_methods unless self.class.attribute_methods_generated?
5b801b59 » NZKoz
2007-08-14 Change the implementation of ActiveRecord's attribute reader and writ…
98 super
99 end
2b3cc247 » jeremy
2006-07-31 r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
100
f8d3c72c » josh
2009-08-04 Extract generic attribute method generation to AMo
101 protected
102 def attribute_method?(attr_name)
96eec090 » NZKoz
2010-12-02 Work around a strange piece of Syck behaviour where it checks Model#r…
103 attr_name == 'id' || (defined?(@attributes) && @attributes.include?(attr_name))
5b801b59 » NZKoz
2007-08-14 Change the implementation of ActiveRecord's attribute reader and writ…
104 end
2b3cc247 » jeremy
2006-07-31 r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
105 end
106 end
Something went wrong with that request. Please try again.