Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 61 lines (53 sloc) 2.447 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
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
13 super(columns_hash.keys)
e129c56 @josh Wrap up attribute method reset concerns in 'undefine_attribute_methods'
josh authored
14 end
15
46f30f9 @lifo Merge documentation changes from docrails.
lifo authored
16 # Checks whether the method is defined in the model or any of its subclasses
3e58f8e @josh Restore DangerousAttributeError
josh authored
17 # that also derive from Active Record. Raises DangerousAttributeError if the
18 # method is defined by Active Record though.
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored
19 def instance_method_already_implemented?(method_name)
240b4c5 @jeremy Ruby 1.9 compat: attribute methods
jeremy authored
20 method_name = method_name.to_s
3e58f8e @josh Restore DangerousAttributeError
josh authored
21 @_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
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
22 @@_defined_activerecord_methods ||= defined_activerecord_methods
3e58f8e @josh Restore DangerousAttributeError
josh authored
23 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
24 @_defined_class_methods.include?(method_name)
b31aa63 Allow column accessors to be created even if Kernel. or Object# metho…
Tobias Lütke authored
25 end
d916c62 @wycats eliminate alias_method_chain from ActiveRecord
wycats authored
26
27 def defined_activerecord_methods
28 active_record = ActiveRecord::Base
29 super_klass = ActiveRecord::Base.superclass
30 methods = active_record.public_instance_methods - super_klass.public_instance_methods
31 methods += active_record.private_instance_methods - super_klass.private_instance_methods
32 methods += active_record.protected_instance_methods - super_klass.protected_instance_methods
33 methods.map {|m| m.to_s }.to_set
34 end
c30a0ce @paulgillard Modified ActiveRecord::AttributeMethods to allow classes to specify a…
paulgillard authored
35 end
64eecdd @josh whitespace
josh authored
36
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
37 def method_missing(method_id, *args, &block)
38 # If we haven't generated any methods yet, generate them, then
39 # see if we've created the method we're looking for.
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
40 if !self.class.attribute_methods_generated?
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
41 self.class.define_attribute_methods
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
42 method_name = method_id.to_s
d60d7ed @samg Make it so AR attributes which conflict with object-private methods (…
samg authored
43 guard_private_attribute_method!(method_name, args)
364a8f3 @jeremy No need to check for generated method, just redispatch
jeremy authored
44 send(method_id, *args, &block)
45 else
46 super
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
47 end
48 end
49
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
50 def respond_to?(*args)
51 self.class.define_attribute_methods
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
52 super
53 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
54
f8d3c72 @josh Extract generic attribute method generation to AMo
josh authored
55 protected
56 def attribute_method?(attr_name)
57 attr_name == 'id' || attributes.include?(attr_name)
5b801b5 @NZKoz Change the implementation of ActiveRecord's attribute reader and writ…
NZKoz authored
58 end
2b3cc24 @jeremy r4854@ks: jeremy | 2006-07-30 00:59:18 -0700
jeremy authored
59 end
60 end
Something went wrong with that request. Please try again.