Skip to content
Newer
Older
100644 203 lines (181 sloc) 7.87 KB
e8550ee @jeremy Cherry-pick core extensions
jeremy authored May 13, 2009
1 require 'active_support/core_ext/enumerable'
2
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
3 module ActiveRecord
4 module AttributeMethods #:nodoc:
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored May 28, 2009
5 extend ActiveSupport::Concern
a2875be @brynary Use DependencyModule for included hooks in ActiveRecord
brynary authored May 11, 2009
6
4db718e @NZKoz Only cache attributes which need it for performance reasons. Closes #…
NZKoz authored Oct 6, 2007
7 ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
8
a2875be @brynary Use DependencyModule for included hooks in ActiveRecord
brynary authored May 12, 2009
9 included do
10 cattr_accessor :attribute_types_cached_by_default, :instance_writer => false
11 self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
12 end
13
14 # Declare and check for suffixed attribute methods.
15 module ClassMethods
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
16 # Declares a method available for all attributes with the given suffix.
17 # Uses +method_missing+ and <tt>respond_to?</tt> to rewrite the method
18 #
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
19 # #{attr}#{suffix}(*args, &block)
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
20 #
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
21 # to
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
22 #
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
23 # attribute#{suffix}(#{attr}, *args, &block)
24 #
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
25 # An <tt>attribute#{suffix}</tt> instance method must exist and accept at least
26 # the +attr+ argument.
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
27 #
28 # For example:
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
29 #
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
30 # class Person < ActiveRecord::Base
31 # attribute_method_suffix '_changed?'
32 #
33 # private
34 # def attribute_changed?(attr)
35 # ...
36 # end
37 # end
38 #
39 # person = Person.find(1)
40 # person.name_changed? # => false
41 # person.name = 'Hubert'
42 # person.name_changed? # => true
43 def attribute_method_suffix(*suffixes)
44 attribute_method_suffixes.concat suffixes
45 rebuild_attribute_method_regexp
46 end
47
48 # Returns MatchData if method_name is an attribute method.
49 def match_attribute_method?(method_name)
50 rebuild_attribute_method_regexp unless defined?(@@attribute_method_regexp) && @@attribute_method_regexp
51 @@attribute_method_regexp.match(method_name)
52 end
53
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
54 # Contains the names of the generated attribute methods.
55 def generated_methods #:nodoc:
56 @generated_methods ||= Set.new
57 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
58
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
59 def generated_methods?
60 !generated_methods.empty?
61 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
62
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
63 # Generates all the attribute related methods for columns in the database
64 # accessors, mutators and query methods.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
65 def define_attribute_methods
66 return if generated_methods?
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
67 columns_hash.keys.each do |name|
94dabf9 @josh Generate methods for all suffixes
josh authored Jul 28, 2009
68 attribute_method_suffixes.each do |suffix|
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
69 method_name = "#{name}#{suffix}"
70 unless instance_method_already_implemented?(method_name)
71 generate_method = "define_attribute_method#{suffix}"
72 if respond_to?(generate_method)
73 send(generate_method, name)
74 else
75 evaluate_attribute_method(name, "def #{method_name}(*args); attribute#{suffix}('#{name}', *args); end", method_name)
76 end
72385a7 @technoweenie Add Time Zone support to ActiveRecord, and config.time_zone property …
technoweenie authored Feb 6, 2008
77 end
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
78 end
79 end
80 end
acbec3e @NZKoz Ensure that custom mutators aren't redefined by define_attribute_meth…
NZKoz authored Sep 17, 2007
81
e129c56 @josh Wrap up attribute method reset concerns in 'undefine_attribute_methods'
josh authored Jul 29, 2009
82 def undefine_attribute_methods
83 generated_methods.each { |name| undef_method(name) }
84 @generated_methods = nil
85 end
86
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
87 # Checks whether the method is defined in the model or any of its subclasses
3e58f8e @josh Restore DangerousAttributeError
josh authored Jul 30, 2009
88 # that also derive from Active Record. Raises DangerousAttributeError if the
89 # method is defined by Active Record though.
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored Oct 3, 2007
90 def instance_method_already_implemented?(method_name)
240b4c5 @jeremy Ruby 1.9 compat: attribute methods
jeremy authored Dec 27, 2007
91 method_name = method_name.to_s
3e58f8e @josh Restore DangerousAttributeError
josh authored Jul 30, 2009
92 return true if method_name =~ /^id(=$|\?$|_before_type_cast$|$)/
93 @_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 {|m| m.to_s }.to_set
94 @@_defined_activerecord_methods ||= (ActiveRecord::Base.public_instance_methods(false) | ActiveRecord::Base.private_instance_methods(false) | ActiveRecord::Base.protected_instance_methods(false)).map{|m| m.to_s }.to_set
95 raise DangerousAttributeError, "#{method_name} is defined by ActiveRecord" if @@_defined_activerecord_methods.include?(method_name)
5b2e8b1 @technoweenie Fix that ActiveRecord would create attribute methods and override cus…
technoweenie authored Oct 6, 2007
96 @_defined_class_methods.include?(method_name)
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored Oct 3, 2007
97 end
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
98
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
99 private
100 # Suffixes a, ?, c become regexp /(a|\?|c)$/
101 def rebuild_attribute_method_regexp
102 suffixes = attribute_method_suffixes.map { |s| Regexp.escape(s) }
103 @@attribute_method_regexp = /(#{suffixes.join('|')})$/.freeze
104 end
105
106 def attribute_method_suffixes
107 @@attribute_method_suffixes ||= []
108 end
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
109
110 # Evaluate the definition for an attribute related method
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
111 def evaluate_attribute_method(attr_name, method_definition, method_name)
3e58f8e @josh Restore DangerousAttributeError
josh authored Jul 30, 2009
112 unless method_name.to_s == primary_key.to_s
113 generated_methods << method_name
114 end
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
115
116 begin
cff25aa @jeremy eval with __FILE__ and __LINE__
jeremy authored Oct 10, 2007
117 class_eval(method_definition, __FILE__, __LINE__)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
118 rescue SyntaxError => err
119 generated_methods.delete(attr_name)
120 if logger
121 logger.warn "Exception occurred during reader method compilation."
122 logger.warn "Maybe #{attr_name} is not a valid Ruby identifier?"
4d092ba @clemens Some performance goodness for AR.
clemens authored Sep 2, 2008
123 logger.warn err.message
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
124 end
125 end
126 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
127 end
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
128
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
129 # Allows access to the object attributes, which are held in the <tt>@attributes</tt> hash, as though they
0faa4ca @dhh Doc fix (closes #9323) [Henrik N]
dhh authored Sep 22, 2007
130 # were first-class methods. So a Person class with a name attribute can use Person#name and
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
131 # Person#name= and never directly use the attributes hash -- except for multiple assigns with
132 # ActiveRecord#attributes=. A Milestone class can also ask Milestone#completed? to test that
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
133 # the completed attribute is not +nil+ or 0.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
134 #
135 # It's also possible to instantiate related objects, so a Client class belonging to the clients
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
136 # table with a +master_id+ foreign key can instantiate master through Client#master.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
137 def method_missing(method_id, *args, &block)
138 method_name = method_id.to_s
139
140 # If we haven't generated any methods yet, generate them, then
141 # see if we've created the method we're looking for.
142 if !self.class.generated_methods?
143 self.class.define_attribute_methods
d60d7ed @samg Make it so AR attributes which conflict with object-private methods (…
samg authored Jul 8, 2009
144 guard_private_attribute_method!(method_name, args)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
145 if self.class.generated_methods.include?(method_name)
146 return self.send(method_id, *args, &block)
147 end
148 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
149
d60d7ed @samg Make it so AR attributes which conflict with object-private methods (…
samg authored Jul 8, 2009
150 guard_private_attribute_method!(method_name, args)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
151 if self.class.primary_key.to_s == method_name
152 id
153 elsif md = self.class.match_attribute_method?(method_name)
154 attribute_name, method_type = md.pre_match, md.to_s
155 if @attributes.include?(attribute_name)
156 __send__("attribute#{method_type}", attribute_name, *args, &block)
157 else
158 super
159 end
160 else
161 super
162 end
163 end
164
1646e8c @clemens More symbols for send and respond_to?.
clemens authored Sep 2, 2008
165 # A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>,
166 # <tt>person.respond_to?(:name=)</tt>, and <tt>person.respond_to?(:name?)</tt>
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored May 16, 2008
167 # which will all return +true+.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
168 alias :respond_to_without_attributes? :respond_to?
4d9a7ab Changed ActiveRecord attributes to respect access control.
Adam Milligan authored Sep 21, 2008
169 def respond_to?(method, include_private_methods = false)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
170 method_name = method.to_s
171 if super
172 return true
f550c86 Fix performance bug in AttibuteMethods#respond_to? in handling of pri…
Aliaksey Kandratsenka authored Oct 3, 2008
173 elsif !include_private_methods && super(method, true)
174 # If we're here than we haven't found among non-private methods
175 # but found among all methods. Which means that given method is private.
4d9a7ab Changed ActiveRecord attributes to respect access control.
Adam Milligan authored Sep 21, 2008
176 return false
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
177 elsif !self.class.generated_methods?
178 self.class.define_attribute_methods
179 if self.class.generated_methods.include?(method_name)
180 return true
181 end
182 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
183
586baf8 @josh read_attribute is always available through attribute
josh authored Jul 29, 2009
184 if md = self.class.match_attribute_method?(method_name)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
185 return true if @attributes.include?(md.pre_match)
186 end
187 super
188 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
189
190 private
d60d7ed @samg Make it so AR attributes which conflict with object-private methods (…
samg authored Jul 8, 2009
191 # prevent method_missing from calling private methods with #send
192 def guard_private_attribute_method!(method_name, args)
193 if self.class.private_method_defined?(method_name)
194 raise NoMethodError.new("Attempt to call private method", method_name, args)
195 end
196 end
c2b075b @josh Concernify AR AttributeMethods
josh authored Jul 24, 2009
197
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored Aug 14, 2007
198 def missing_attribute(attr_name, stack)
199 raise ActiveRecord::MissingAttributeError, "missing attribute: #{attr_name}", stack
200 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored Jul 31, 2006
201 end
202 end
Something went wrong with that request. Please try again.