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