Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 75 lines (64 sloc) 2.752 kB
e8550ee @jeremy Cherry-pick core extensions
jeremy authored
1 require 'active_support/core_ext/enumerable'
2
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
3 module ActiveRecord
4ad6103 @rizwanreza Adds title and basic description where needed.
rizwanreza authored
4 # = Active Record Attribute Methods
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
5 module AttributeMethods #:nodoc:
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored
6 extend ActiveSupport::Concern
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
7 include ActiveModel::AttributeMethods
a2875be @brynary Use DependencyModule for included hooks in ActiveRecord
brynary authored
8
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
9 module ClassMethods
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
10 # Generates all the attribute related methods for columns in the database
11 # accessors, mutators and query methods.
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
12 def define_attribute_methods
24faddd @wildchild Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
wildchild authored
13 return if attribute_methods_generated?
2e748fe @tenderlove just use the list of column names to generate attribute methods
tenderlove authored
14 super(column_names)
24faddd @wildchild Move ActiveModel::AttributeMethods#attribute_methods_generated? to Ac…
wildchild authored
15 @attribute_methods_generated = true
16 end
17
18 def attribute_methods_generated?
19 @attribute_methods_generated ||= false
20 end
21
22 def undefine_attribute_methods(*args)
23 super
24 @attribute_methods_generated = false
e129c56 @josh Wrap up attribute method reset concerns in 'undefine_attribute_methods'
josh authored
25 end
26
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
27 # Checks whether the method is defined in the model or any of its subclasses
3e58f8e @josh Restore DangerousAttributeError
josh authored
28 # that also derive from Active Record. Raises DangerousAttributeError if the
29 # method is defined by Active Record though.
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored
30 def instance_method_already_implemented?(method_name)
240b4c5 @jeremy Ruby 1.9 compat: attribute methods
jeremy authored
31 method_name = method_name.to_s
30679bc @tenderlove AR::AttributeMethods does not need to be included in an AR::Base class.
tenderlove authored
32 index = ancestors.index(ActiveRecord::Base) || ancestors.length
33 @_defined_class_methods ||= ancestors.first(index).map { |m|
34 m.instance_methods(false) | m.private_instance_methods(false)
35 }.flatten.map {|m| m.to_s }.to_set
36
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
37 @@_defined_activerecord_methods ||= defined_activerecord_methods
3e58f8e @josh Restore DangerousAttributeError
josh authored
38 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
39 @_defined_class_methods.include?(method_name)
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored
40 end
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
41
42 def defined_activerecord_methods
43 active_record = ActiveRecord::Base
44 super_klass = ActiveRecord::Base.superclass
289cc15 @tenderlove instance_methods includes public and protected, so reduce funcalls an…
tenderlove authored
45 methods = (active_record.instance_methods - super_klass.instance_methods) +
46 (active_record.private_instance_methods - super_klass.private_instance_methods)
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
47 methods.map {|m| m.to_s }.to_set
48 end
c30a0ce @paulgillard Modified ActiveRecord::AttributeMethods to allow classes to specify a…
paulgillard authored
49 end
64eecdd @josh whitespace
josh authored
50
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
51 def method_missing(method_id, *args, &block)
52 # If we haven't generated any methods yet, generate them, then
53 # see if we've created the method we're looking for.
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
54 if !self.class.attribute_methods_generated?
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
55 self.class.define_attribute_methods
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
56 method_name = method_id.to_s
d60d7ed @samg Make it so AR attributes which conflict with object-private methods (…
samg authored
57 guard_private_attribute_method!(method_name, args)
364a8f3 @jeremy No need to check for generated method, just redispatch
jeremy authored
58 send(method_id, *args, &block)
59 else
60 super
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
61 end
62 end
63
6a283d5 @tenderlove match method signature of the superclass
tenderlove authored
64 def respond_to?(name, include_private = false)
e0e3adf @jeremy Cheaper attribute reads and respond_to?. Add underscore-prefixed meth…
jeremy authored
65 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
66 super
67 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
68
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
69 protected
70 def attribute_method?(attr_name)
96eec09 @NZKoz Work around a strange piece of Syck behaviour where it checks Model#r…
NZKoz authored
71 attr_name == 'id' || (defined?(@attributes) && @attributes.include?(attr_name))
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
72 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
73 end
74 end
Something went wrong with that request. Please try again.