Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 64 lines (54 sloc) 2.065 kb
c2b075b @josh Concernify AR AttributeMethods
josh authored
1 module ActiveRecord
2 module AttributeMethods
3 module Write
4 extend ActiveSupport::Concern
5
6 included do
7 attribute_method_suffix "="
8 end
9
10 module ClassMethods
11 protected
ae934ae @jonleighton Don't allocate new strings in compiled attribute methods
jonleighton authored
12
13 # See define_method_attribute in read.rb for an explanation of
14 # this code.
15 def define_method_attribute=(name)
16 safe_name = name.unpack('h*').first
17 generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
18 def __temp__#{safe_name}=(value)
19 write_attribute(AttrNames::ATTR_#{safe_name}, value)
baa237c @spastorino Allow to read and write AR attributes with non valid identifiers
spastorino authored
20 end
ae934ae @jonleighton Don't allocate new strings in compiled attribute methods
jonleighton authored
21 alias_method #{(name + '=').inspect}, :__temp__#{safe_name}=
22 undef_method :__temp__#{safe_name}=
23 STR
24 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
25 end
26
c57064a @frodsan update AR/attribute_methods documentation [ci skip]
frodsan authored
27 # Updates the attribute identified by <tt>attr_name</tt> with the
28 # specified +value+. Empty strings for fixnum and float columns are
29 # turned into +nil+.
c2b075b @josh Concernify AR AttributeMethods
josh authored
30 def write_attribute(attr_name, value)
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
31 attr_name = attr_name.to_s
50d395f @jonleighton Raise error when using write_attribute with a non-existent attribute.
jonleighton authored
32 attr_name = self.class.primary_key if attr_name == 'id' && self.class.primary_key
9e5f7cc @jonleighton Revert "Key the attributes hash with symbols"
jonleighton authored
33 @attributes_cache.delete(attr_name)
50d395f @jonleighton Raise error when using write_attribute with a non-existent attribute.
jonleighton authored
34 column = column_for_attribute(attr_name)
35
c50cb4a @tenderlove PG column consults oid types when typecasting
tenderlove authored
36 # If we're dealing with a binary column, write the data to the cache
37 # so we don't attempt to typecast multiple times.
38 if column && column.binary?
9e5f7cc @jonleighton Revert "Key the attributes hash with symbols"
jonleighton authored
39 @attributes_cache[attr_name] = value
c50cb4a @tenderlove PG column consults oid types when typecasting
tenderlove authored
40 end
41
7895182 @jonleighton omg computer science!
jonleighton authored
42 if column || @attributes.has_key?(attr_name)
7a4949e @jonleighton consistency
jonleighton authored
43 @attributes[attr_name] = type_cast_attribute_for_write(column, value)
50d395f @jonleighton Raise error when using write_attribute with a non-existent attribute.
jonleighton authored
44 else
45 raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{attr_name}'"
6d30002 @lifo Revert "Refactoring attributes/types" [#3348 state:open]
lifo authored
46 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
47 end
245542e @smartinez87 Added new #update_column method.
smartinez87 authored
48 alias_method :raw_write_attribute, :write_attribute
c2b075b @josh Concernify AR AttributeMethods
josh authored
49
50 private
3e0ecde @tenderlove adding a comment for myself
tenderlove authored
51 # Handle *= for method_missing.
52 def attribute=(attribute_name, value)
53 write_attribute(attribute_name, value)
54 end
7895182 @jonleighton omg computer science!
jonleighton authored
55
3e0ecde @tenderlove adding a comment for myself
tenderlove authored
56 def type_cast_attribute_for_write(column, value)
57 return value unless column
ceb33f8 @jonleighton Split out most of the AR::Base code into separate modules :cake:
jonleighton authored
58
3e0ecde @tenderlove adding a comment for myself
tenderlove authored
59 column.type_cast_for_write value
60 end
c2b075b @josh Concernify AR AttributeMethods
josh authored
61 end
62 end
63 end
Something went wrong with that request. Please try again.