activemodel 4.2.11.3 → 5.0.7.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +149 -56
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +8 -16
  5. data/lib/active_model/attribute_assignment.rb +52 -0
  6. data/lib/active_model/attribute_methods.rb +16 -16
  7. data/lib/active_model/callbacks.rb +3 -3
  8. data/lib/active_model/conversion.rb +5 -5
  9. data/lib/active_model/dirty.rb +41 -40
  10. data/lib/active_model/errors.rb +175 -68
  11. data/lib/active_model/forbidden_attributes_protection.rb +3 -2
  12. data/lib/active_model/gem_version.rb +5 -5
  13. data/lib/active_model/lint.rb +32 -28
  14. data/lib/active_model/locale/en.yml +2 -1
  15. data/lib/active_model/model.rb +3 -4
  16. data/lib/active_model/naming.rb +5 -4
  17. data/lib/active_model/secure_password.rb +2 -9
  18. data/lib/active_model/serialization.rb +36 -9
  19. data/lib/active_model/type/big_integer.rb +13 -0
  20. data/lib/active_model/type/binary.rb +50 -0
  21. data/lib/active_model/type/boolean.rb +21 -0
  22. data/lib/active_model/type/date.rb +54 -0
  23. data/lib/active_model/type/date_time.rb +44 -0
  24. data/lib/active_model/type/decimal.rb +66 -0
  25. data/lib/active_model/type/decimal_without_scale.rb +11 -0
  26. data/lib/active_model/type/float.rb +25 -0
  27. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +35 -0
  28. data/lib/active_model/type/helpers/mutable.rb +18 -0
  29. data/lib/active_model/type/helpers/numeric.rb +34 -0
  30. data/lib/active_model/type/helpers/time_value.rb +77 -0
  31. data/lib/active_model/type/helpers.rb +4 -0
  32. data/lib/active_model/type/immutable_string.rb +29 -0
  33. data/lib/active_model/type/integer.rb +66 -0
  34. data/lib/active_model/type/registry.rb +64 -0
  35. data/lib/active_model/type/string.rb +24 -0
  36. data/lib/active_model/type/text.rb +11 -0
  37. data/lib/active_model/type/time.rb +42 -0
  38. data/lib/active_model/type/unsigned_integer.rb +15 -0
  39. data/lib/active_model/type/value.rb +116 -0
  40. data/lib/active_model/type.rb +59 -0
  41. data/lib/active_model/validations/absence.rb +1 -1
  42. data/lib/active_model/validations/acceptance.rb +57 -9
  43. data/lib/active_model/validations/callbacks.rb +3 -3
  44. data/lib/active_model/validations/clusivity.rb +4 -3
  45. data/lib/active_model/validations/confirmation.rb +16 -4
  46. data/lib/active_model/validations/exclusion.rb +3 -1
  47. data/lib/active_model/validations/format.rb +1 -1
  48. data/lib/active_model/validations/helper_methods.rb +13 -0
  49. data/lib/active_model/validations/inclusion.rb +3 -3
  50. data/lib/active_model/validations/length.rb +49 -18
  51. data/lib/active_model/validations/numericality.rb +20 -11
  52. data/lib/active_model/validations/validates.rb +1 -1
  53. data/lib/active_model/validations/with.rb +0 -10
  54. data/lib/active_model/validations.rb +34 -1
  55. data/lib/active_model/validator.rb +5 -5
  56. data/lib/active_model/version.rb +1 -1
  57. data/lib/active_model.rb +4 -2
  58. metadata +31 -22
  59. data/lib/active_model/serializers/xml.rb +0 -238
@@ -1,238 +0,0 @@
1
- require 'active_support/core_ext/module/attribute_accessors'
2
- require 'active_support/core_ext/array/conversions'
3
- require 'active_support/core_ext/hash/conversions'
4
- require 'active_support/core_ext/hash/slice'
5
- require 'active_support/core_ext/time/acts_like'
6
-
7
- module ActiveModel
8
- module Serializers
9
- # == Active Model XML Serializer
10
- module Xml
11
- extend ActiveSupport::Concern
12
- include ActiveModel::Serialization
13
-
14
- included do
15
- extend ActiveModel::Naming
16
- end
17
-
18
- class Serializer #:nodoc:
19
- class Attribute #:nodoc:
20
- attr_reader :name, :value, :type
21
-
22
- def initialize(name, serializable, value)
23
- @name, @serializable = name, serializable
24
-
25
- if value.acts_like?(:time) && value.respond_to?(:in_time_zone)
26
- value = value.in_time_zone
27
- end
28
-
29
- @value = value
30
- @type = compute_type
31
- end
32
-
33
- def decorations
34
- decorations = {}
35
- decorations[:encoding] = 'base64' if type == :binary
36
- decorations[:type] = (type == :string) ? nil : type
37
- decorations[:nil] = true if value.nil?
38
- decorations
39
- end
40
-
41
- protected
42
-
43
- def compute_type
44
- return if value.nil?
45
- type = ActiveSupport::XmlMini::TYPE_NAMES[value.class.name]
46
- type ||= :string if value.respond_to?(:to_str)
47
- type ||= :yaml
48
- type
49
- end
50
- end
51
-
52
- class MethodAttribute < Attribute #:nodoc:
53
- end
54
-
55
- attr_reader :options
56
-
57
- def initialize(serializable, options = nil)
58
- @serializable = serializable
59
- @options = options ? options.dup : {}
60
- end
61
-
62
- def serializable_hash
63
- @serializable.serializable_hash(@options.except(:include))
64
- end
65
-
66
- def serializable_collection
67
- methods = Array(options[:methods]).map(&:to_s)
68
- serializable_hash.map do |name, value|
69
- name = name.to_s
70
- if methods.include?(name)
71
- self.class::MethodAttribute.new(name, @serializable, value)
72
- else
73
- self.class::Attribute.new(name, @serializable, value)
74
- end
75
- end
76
- end
77
-
78
- def serialize
79
- require 'builder' unless defined? ::Builder
80
-
81
- options[:indent] ||= 2
82
- options[:builder] ||= ::Builder::XmlMarkup.new(indent: options[:indent])
83
-
84
- @builder = options[:builder]
85
- @builder.instruct! unless options[:skip_instruct]
86
-
87
- root = (options[:root] || @serializable.model_name.element).to_s
88
- root = ActiveSupport::XmlMini.rename_key(root, options)
89
-
90
- args = [root]
91
- args << { xmlns: options[:namespace] } if options[:namespace]
92
- args << { type: options[:type] } if options[:type] && !options[:skip_types]
93
-
94
- @builder.tag!(*args) do
95
- add_attributes_and_methods
96
- add_includes
97
- add_extra_behavior
98
- add_procs
99
- yield @builder if block_given?
100
- end
101
- end
102
-
103
- private
104
-
105
- def add_extra_behavior
106
- end
107
-
108
- def add_attributes_and_methods
109
- serializable_collection.each do |attribute|
110
- key = ActiveSupport::XmlMini.rename_key(attribute.name, options)
111
- ActiveSupport::XmlMini.to_tag(key, attribute.value,
112
- options.merge(attribute.decorations))
113
- end
114
- end
115
-
116
- def add_includes
117
- @serializable.send(:serializable_add_includes, options) do |association, records, opts|
118
- add_associations(association, records, opts)
119
- end
120
- end
121
-
122
- # TODO: This can likely be cleaned up to simple use ActiveSupport::XmlMini.to_tag as well.
123
- def add_associations(association, records, opts)
124
- merged_options = opts.merge(options.slice(:builder, :indent))
125
- merged_options[:skip_instruct] = true
126
-
127
- [:skip_types, :dasherize, :camelize].each do |key|
128
- merged_options[key] = options[key] if merged_options[key].nil? && !options[key].nil?
129
- end
130
-
131
- if records.respond_to?(:to_ary)
132
- records = records.to_ary
133
-
134
- tag = ActiveSupport::XmlMini.rename_key(association.to_s, options)
135
- type = options[:skip_types] ? { } : { type: "array" }
136
- association_name = association.to_s.singularize
137
- merged_options[:root] = association_name
138
-
139
- if records.empty?
140
- @builder.tag!(tag, type)
141
- else
142
- @builder.tag!(tag, type) do
143
- records.each do |record|
144
- if options[:skip_types]
145
- record_type = {}
146
- else
147
- record_class = (record.class.to_s.underscore == association_name) ? nil : record.class.name
148
- record_type = { type: record_class }
149
- end
150
-
151
- record.to_xml merged_options.merge(record_type)
152
- end
153
- end
154
- end
155
- else
156
- merged_options[:root] = association.to_s
157
-
158
- unless records.class.to_s.underscore == association.to_s
159
- merged_options[:type] = records.class.name
160
- end
161
-
162
- records.to_xml merged_options
163
- end
164
- end
165
-
166
- def add_procs
167
- if procs = options.delete(:procs)
168
- Array(procs).each do |proc|
169
- if proc.arity == 1
170
- proc.call(options)
171
- else
172
- proc.call(options, @serializable)
173
- end
174
- end
175
- end
176
- end
177
- end
178
-
179
- # Returns XML representing the model. Configuration can be
180
- # passed through +options+.
181
- #
182
- # Without any +options+, the returned XML string will include all the
183
- # model's attributes.
184
- #
185
- # user = User.find(1)
186
- # user.to_xml
187
- #
188
- # <?xml version="1.0" encoding="UTF-8"?>
189
- # <user>
190
- # <id type="integer">1</id>
191
- # <name>David</name>
192
- # <age type="integer">16</age>
193
- # <created-at type="dateTime">2011-01-30T22:29:23Z</created-at>
194
- # </user>
195
- #
196
- # The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
197
- # attributes included, and work similar to the +attributes+ method.
198
- #
199
- # To include the result of some method calls on the model use <tt>:methods</tt>.
200
- #
201
- # To include associations use <tt>:include</tt>.
202
- #
203
- # For further documentation, see <tt>ActiveRecord::Serialization#to_xml</tt>
204
- def to_xml(options = {}, &block)
205
- Serializer.new(self, options).serialize(&block)
206
- end
207
-
208
- # Sets the model +attributes+ from an XML string. Returns +self+.
209
- #
210
- # class Person
211
- # include ActiveModel::Serializers::Xml
212
- #
213
- # attr_accessor :name, :age, :awesome
214
- #
215
- # def attributes=(hash)
216
- # hash.each do |key, value|
217
- # instance_variable_set("@#{key}", value)
218
- # end
219
- # end
220
- #
221
- # def attributes
222
- # instance_values
223
- # end
224
- # end
225
- #
226
- # xml = { name: 'bob', age: 22, awesome:true }.to_xml
227
- # person = Person.new
228
- # person.from_xml(xml) # => #<Person:0x007fec5e3b3c40 @age=22, @awesome=true, @name="bob">
229
- # person.name # => "bob"
230
- # person.age # => 22
231
- # person.awesome # => true
232
- def from_xml(xml)
233
- self.attributes = Hash.from_xml(xml).values.first
234
- self
235
- end
236
- end
237
- end
238
- end