activemodel 4.2.0 → 6.1.0

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 (71) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +49 -37
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +16 -22
  5. data/lib/active_model/attribute/user_provided_default.rb +51 -0
  6. data/lib/active_model/attribute.rb +248 -0
  7. data/lib/active_model/attribute_assignment.rb +55 -0
  8. data/lib/active_model/attribute_methods.rb +150 -73
  9. data/lib/active_model/attribute_mutation_tracker.rb +181 -0
  10. data/lib/active_model/attribute_set/builder.rb +191 -0
  11. data/lib/active_model/attribute_set/yaml_encoder.rb +40 -0
  12. data/lib/active_model/attribute_set.rb +106 -0
  13. data/lib/active_model/attributes.rb +132 -0
  14. data/lib/active_model/callbacks.rb +31 -25
  15. data/lib/active_model/conversion.rb +20 -9
  16. data/lib/active_model/dirty.rb +142 -116
  17. data/lib/active_model/error.rb +207 -0
  18. data/lib/active_model/errors.rb +436 -202
  19. data/lib/active_model/forbidden_attributes_protection.rb +6 -3
  20. data/lib/active_model/gem_version.rb +5 -3
  21. data/lib/active_model/lint.rb +47 -42
  22. data/lib/active_model/locale/en.yml +2 -1
  23. data/lib/active_model/model.rb +7 -7
  24. data/lib/active_model/naming.rb +36 -18
  25. data/lib/active_model/nested_error.rb +22 -0
  26. data/lib/active_model/railtie.rb +8 -0
  27. data/lib/active_model/secure_password.rb +61 -67
  28. data/lib/active_model/serialization.rb +48 -17
  29. data/lib/active_model/serializers/json.rb +22 -13
  30. data/lib/active_model/translation.rb +5 -4
  31. data/lib/active_model/type/big_integer.rb +14 -0
  32. data/lib/active_model/type/binary.rb +52 -0
  33. data/lib/active_model/type/boolean.rb +46 -0
  34. data/lib/active_model/type/date.rb +52 -0
  35. data/lib/active_model/type/date_time.rb +46 -0
  36. data/lib/active_model/type/decimal.rb +69 -0
  37. data/lib/active_model/type/float.rb +35 -0
  38. data/lib/active_model/type/helpers/accepts_multiparameter_time.rb +49 -0
  39. data/lib/active_model/type/helpers/mutable.rb +20 -0
  40. data/lib/active_model/type/helpers/numeric.rb +48 -0
  41. data/lib/active_model/type/helpers/time_value.rb +90 -0
  42. data/lib/active_model/type/helpers/timezone.rb +19 -0
  43. data/lib/active_model/type/helpers.rb +7 -0
  44. data/lib/active_model/type/immutable_string.rb +35 -0
  45. data/lib/active_model/type/integer.rb +67 -0
  46. data/lib/active_model/type/registry.rb +70 -0
  47. data/lib/active_model/type/string.rb +35 -0
  48. data/lib/active_model/type/time.rb +46 -0
  49. data/lib/active_model/type/value.rb +133 -0
  50. data/lib/active_model/type.rb +53 -0
  51. data/lib/active_model/validations/absence.rb +6 -4
  52. data/lib/active_model/validations/acceptance.rb +72 -14
  53. data/lib/active_model/validations/callbacks.rb +23 -19
  54. data/lib/active_model/validations/clusivity.rb +18 -12
  55. data/lib/active_model/validations/confirmation.rb +27 -14
  56. data/lib/active_model/validations/exclusion.rb +7 -4
  57. data/lib/active_model/validations/format.rb +27 -27
  58. data/lib/active_model/validations/helper_methods.rb +15 -0
  59. data/lib/active_model/validations/inclusion.rb +8 -7
  60. data/lib/active_model/validations/length.rb +35 -32
  61. data/lib/active_model/validations/numericality.rb +72 -34
  62. data/lib/active_model/validations/presence.rb +3 -3
  63. data/lib/active_model/validations/validates.rb +17 -15
  64. data/lib/active_model/validations/with.rb +6 -12
  65. data/lib/active_model/validations.rb +58 -23
  66. data/lib/active_model/validator.rb +23 -17
  67. data/lib/active_model/version.rb +4 -2
  68. data/lib/active_model.rb +18 -11
  69. metadata +44 -25
  70. data/lib/active_model/serializers/xml.rb +0 -238
  71. data/lib/active_model/test_case.rb +0 -4
@@ -1,7 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/core_ext/module/anonymous"
2
4
 
3
5
  module ActiveModel
4
-
5
6
  # == Active \Model \Validator
6
7
  #
7
8
  # A simple base class that can be used along with
@@ -15,7 +16,7 @@ module ActiveModel
15
16
  # class MyValidator < ActiveModel::Validator
16
17
  # def validate(record)
17
18
  # if some_complex_logic
18
- # record.errors[:base] = "This record is invalid"
19
+ # record.errors.add(:base, "This record is invalid")
19
20
  # end
20
21
  # end
21
22
  #
@@ -83,13 +84,13 @@ module ActiveModel
83
84
  # end
84
85
  #
85
86
  # It can be useful to access the class that is using that validator when there are prerequisites such
86
- # as an +attr_accessor+ being present. This class is accessible via +options[:class]+ in the constructor.
87
- # To setup your validator override the constructor.
87
+ # as an +attr_accessor+ being present. This class is accessible via <tt>options[:class]</tt> in the constructor.
88
+ # To set up your validator override the constructor.
88
89
  #
89
90
  # class MyValidator < ActiveModel::Validator
90
91
  # def initialize(options={})
91
92
  # super
92
- # options[:class].send :attr_accessor, :custom_attribute
93
+ # options[:class].attr_accessor :custom_attribute
93
94
  # end
94
95
  # end
95
96
  class Validator
@@ -98,20 +99,20 @@ module ActiveModel
98
99
  # Returns the kind of the validator.
99
100
  #
100
101
  # PresenceValidator.kind # => :presence
101
- # UniquenessValidator.kind # => :uniqueness
102
+ # AcceptanceValidator.kind # => :acceptance
102
103
  def self.kind
103
- @kind ||= name.split('::').last.underscore.sub(/_validator$/, '').to_sym unless anonymous?
104
+ @kind ||= name.split("::").last.underscore.chomp("_validator").to_sym unless anonymous?
104
105
  end
105
106
 
106
107
  # Accepts options that will be made available through the +options+ reader.
107
108
  def initialize(options = {})
108
- @options = options.except(:class).freeze
109
+ @options = options.except(:class).freeze
109
110
  end
110
111
 
111
112
  # Returns the kind for this validator.
112
113
  #
113
- # PresenceValidator.new.kind # => :presence
114
- # UniquenessValidator.new.kind # => :uniqueness
114
+ # PresenceValidator.new(attributes: [:username]).kind # => :presence
115
+ # AcceptanceValidator.new(attributes: [:terms]).kind # => :acceptance
115
116
  def kind
116
117
  self.class.kind
117
118
  end
@@ -127,7 +128,7 @@ module ActiveModel
127
128
  # in the options hash invoking the <tt>validate_each</tt> method passing in the
128
129
  # record, attribute and value.
129
130
  #
130
- # All Active Model validations are built on top of this validator.
131
+ # All \Active \Model validations are built on top of this validator.
131
132
  class EachValidator < Validator #:nodoc:
132
133
  attr_reader :attributes
133
134
 
@@ -142,12 +143,13 @@ module ActiveModel
142
143
  end
143
144
 
144
145
  # Performs validation on the supplied record. By default this will call
145
- # +validates_each+ to determine validity therefore subclasses should
146
- # override +validates_each+ with validation logic.
146
+ # +validate_each+ to determine validity therefore subclasses should
147
+ # override +validate_each+ with validation logic.
147
148
  def validate(record)
148
149
  attributes.each do |attribute|
149
150
  value = record.read_attribute_for_validation(attribute)
150
151
  next if (value.nil? && options[:allow_nil]) || (value.blank? && options[:allow_blank])
152
+ value = prepare_value_for_validation(value, record, attribute)
151
153
  validate_each(record, attribute, value)
152
154
  end
153
155
  end
@@ -163,6 +165,11 @@ module ActiveModel
163
165
  # +ArgumentError+ when invalid options are supplied.
164
166
  def check_validity!
165
167
  end
168
+
169
+ private
170
+ def prepare_value_for_validation(value, record, attr_name)
171
+ value
172
+ end
166
173
  end
167
174
 
168
175
  # +BlockValidator+ is a special +EachValidator+ which receives a block on initialization
@@ -174,9 +181,8 @@ module ActiveModel
174
181
  end
175
182
 
176
183
  private
177
-
178
- def validate_each(record, attribute, value)
179
- @block.call(record, attribute, value)
180
- end
184
+ def validate_each(record, attribute, value)
185
+ @block.call(record, attribute, value)
186
+ end
181
187
  end
182
188
  end
@@ -1,7 +1,9 @@
1
- require_relative 'gem_version'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "gem_version"
2
4
 
3
5
  module ActiveModel
4
- # Returns the version of the currently loaded ActiveModel as a <tt>Gem::Version</tt>
6
+ # Returns the version of the currently loaded \Active \Model as a <tt>Gem::Version</tt>
5
7
  def self.version
6
8
  gem_version
7
9
  end
data/lib/active_model.rb CHANGED
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  #--
2
- # Copyright (c) 2004-2014 David Heinemeier Hansson
4
+ # Copyright (c) 2004-2020 David Heinemeier Hansson
3
5
  #
4
6
  # Permission is hereby granted, free of charge, to any person obtaining
5
7
  # a copy of this software and associated documentation files (the
@@ -21,34 +23,40 @@
21
23
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
24
  #++
23
25
 
24
- require 'active_support'
25
- require 'active_support/rails'
26
- require 'active_model/version'
26
+ require "active_support"
27
+ require "active_support/rails"
28
+ require "active_model/version"
27
29
 
28
30
  module ActiveModel
29
31
  extend ActiveSupport::Autoload
30
32
 
33
+ autoload :Attribute
34
+ autoload :Attributes
35
+ autoload :AttributeAssignment
31
36
  autoload :AttributeMethods
32
- autoload :BlockValidator, 'active_model/validator'
37
+ autoload :BlockValidator, "active_model/validator"
33
38
  autoload :Callbacks
34
39
  autoload :Conversion
35
40
  autoload :Dirty
36
- autoload :EachValidator, 'active_model/validator'
41
+ autoload :EachValidator, "active_model/validator"
37
42
  autoload :ForbiddenAttributesProtection
38
43
  autoload :Lint
39
44
  autoload :Model
40
- autoload :Name, 'active_model/naming'
45
+ autoload :Name, "active_model/naming"
41
46
  autoload :Naming
42
47
  autoload :SecurePassword
43
48
  autoload :Serialization
44
- autoload :TestCase
45
49
  autoload :Translation
50
+ autoload :Type
46
51
  autoload :Validations
47
52
  autoload :Validator
48
53
 
49
54
  eager_autoload do
50
55
  autoload :Errors
51
- autoload :StrictValidationFailed, 'active_model/errors'
56
+ autoload :Error
57
+ autoload :RangeError, "active_model/errors"
58
+ autoload :StrictValidationFailed, "active_model/errors"
59
+ autoload :UnknownAttributeError, "active_model/errors"
52
60
  end
53
61
 
54
62
  module Serializers
@@ -56,7 +64,6 @@ module ActiveModel
56
64
 
57
65
  eager_autoload do
58
66
  autoload :JSON
59
- autoload :Xml
60
67
  end
61
68
  end
62
69
 
@@ -67,5 +74,5 @@ module ActiveModel
67
74
  end
68
75
 
69
76
  ActiveSupport.on_load(:i18n) do
70
- I18n.load_path << File.dirname(__FILE__) + '/active_model/locale/en.yml'
77
+ I18n.load_path << File.expand_path("active_model/locale/en.yml", __dir__)
71
78
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.2.0
4
+ version: 6.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-20 00:00:00.000000000 Z
11
+ date: 2020-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -16,28 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: 6.1.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
27
- - !ruby/object:Gem::Dependency
28
- name: builder
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '3.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '3.1'
26
+ version: 6.1.0
41
27
  description: A toolkit for building modeling frameworks like Active Record. Rich support
42
28
  for attributes, callbacks, validations, serialization, internationalization, and
43
29
  testing.
@@ -50,10 +36,19 @@ files:
50
36
  - MIT-LICENSE
51
37
  - README.rdoc
52
38
  - lib/active_model.rb
39
+ - lib/active_model/attribute.rb
40
+ - lib/active_model/attribute/user_provided_default.rb
41
+ - lib/active_model/attribute_assignment.rb
53
42
  - lib/active_model/attribute_methods.rb
43
+ - lib/active_model/attribute_mutation_tracker.rb
44
+ - lib/active_model/attribute_set.rb
45
+ - lib/active_model/attribute_set/builder.rb
46
+ - lib/active_model/attribute_set/yaml_encoder.rb
47
+ - lib/active_model/attributes.rb
54
48
  - lib/active_model/callbacks.rb
55
49
  - lib/active_model/conversion.rb
56
50
  - lib/active_model/dirty.rb
51
+ - lib/active_model/error.rb
57
52
  - lib/active_model/errors.rb
58
53
  - lib/active_model/forbidden_attributes_protection.rb
59
54
  - lib/active_model/gem_version.rb
@@ -61,13 +56,32 @@ files:
61
56
  - lib/active_model/locale/en.yml
62
57
  - lib/active_model/model.rb
63
58
  - lib/active_model/naming.rb
59
+ - lib/active_model/nested_error.rb
64
60
  - lib/active_model/railtie.rb
65
61
  - lib/active_model/secure_password.rb
66
62
  - lib/active_model/serialization.rb
67
63
  - lib/active_model/serializers/json.rb
68
- - lib/active_model/serializers/xml.rb
69
- - lib/active_model/test_case.rb
70
64
  - lib/active_model/translation.rb
65
+ - lib/active_model/type.rb
66
+ - lib/active_model/type/big_integer.rb
67
+ - lib/active_model/type/binary.rb
68
+ - lib/active_model/type/boolean.rb
69
+ - lib/active_model/type/date.rb
70
+ - lib/active_model/type/date_time.rb
71
+ - lib/active_model/type/decimal.rb
72
+ - lib/active_model/type/float.rb
73
+ - lib/active_model/type/helpers.rb
74
+ - lib/active_model/type/helpers/accepts_multiparameter_time.rb
75
+ - lib/active_model/type/helpers/mutable.rb
76
+ - lib/active_model/type/helpers/numeric.rb
77
+ - lib/active_model/type/helpers/time_value.rb
78
+ - lib/active_model/type/helpers/timezone.rb
79
+ - lib/active_model/type/immutable_string.rb
80
+ - lib/active_model/type/integer.rb
81
+ - lib/active_model/type/registry.rb
82
+ - lib/active_model/type/string.rb
83
+ - lib/active_model/type/time.rb
84
+ - lib/active_model/type/value.rb
71
85
  - lib/active_model/validations.rb
72
86
  - lib/active_model/validations/absence.rb
73
87
  - lib/active_model/validations/acceptance.rb
@@ -76,6 +90,7 @@ files:
76
90
  - lib/active_model/validations/confirmation.rb
77
91
  - lib/active_model/validations/exclusion.rb
78
92
  - lib/active_model/validations/format.rb
93
+ - lib/active_model/validations/helper_methods.rb
79
94
  - lib/active_model/validations/inclusion.rb
80
95
  - lib/active_model/validations/length.rb
81
96
  - lib/active_model/validations/numericality.rb
@@ -84,10 +99,15 @@ files:
84
99
  - lib/active_model/validations/with.rb
85
100
  - lib/active_model/validator.rb
86
101
  - lib/active_model/version.rb
87
- homepage: http://www.rubyonrails.org
102
+ homepage: https://rubyonrails.org
88
103
  licenses:
89
104
  - MIT
90
- metadata: {}
105
+ metadata:
106
+ bug_tracker_uri: https://github.com/rails/rails/issues
107
+ changelog_uri: https://github.com/rails/rails/blob/v6.1.0/activemodel/CHANGELOG.md
108
+ documentation_uri: https://api.rubyonrails.org/v6.1.0/
109
+ mailing_list_uri: https://discuss.rubyonrails.org/c/rubyonrails-talk
110
+ source_code_uri: https://github.com/rails/rails/tree/v6.1.0/activemodel
91
111
  post_install_message:
92
112
  rdoc_options: []
93
113
  require_paths:
@@ -96,15 +116,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
116
  requirements:
97
117
  - - ">="
98
118
  - !ruby/object:Gem::Version
99
- version: 1.9.3
119
+ version: 2.5.0
100
120
  required_rubygems_version: !ruby/object:Gem::Requirement
101
121
  requirements:
102
122
  - - ">="
103
123
  - !ruby/object:Gem::Version
104
124
  version: '0'
105
125
  requirements: []
106
- rubyforge_project:
107
- rubygems_version: 2.2.2
126
+ rubygems_version: 3.1.4
108
127
  signing_key:
109
128
  specification_version: 4
110
129
  summary: A toolkit for building modeling frameworks (part of Rails).
@@ -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
@@ -1,4 +0,0 @@
1
- module ActiveModel #:nodoc:
2
- class TestCase < ActiveSupport::TestCase #:nodoc:
3
- end
4
- end