44module ActiveModelSerializers
55 class Model
66 include ActiveModel ::Serializers ::JSON
7- include ActiveModel ::Validations
8- include ActiveModel ::Conversion
9- extend ActiveModel ::Naming
10- extend ActiveModel ::Translation
7+ include ActiveModel ::Model
118
129 class_attribute :attribute_names
1310 self . attribute_names = [ ]
@@ -20,6 +17,13 @@ def self.attributes(*names)
2017 attributes :id
2118 attr_writer :updated_at
2219
20+ attr_reader :errors
21+
22+ def initialize ( attributes = { } )
23+ @errors = ActiveModel ::Errors . new ( self )
24+ super
25+ end
26+
2327 # Defaults to the downcased model name.
2428 def id
2529 @id ||= self . class . name . downcase
@@ -35,14 +39,6 @@ def updated_at
3539 defined? ( @updated_at ) ? @updated_at : File . mtime ( __FILE__ )
3640 end
3741
38- attr_reader :errors
39-
40- def initialize ( attributes = { } )
41- assign_attributes ( attributes ) if attributes
42- @errors = ActiveModel ::Errors . new ( self )
43- super ( )
44- end
45-
4642 def attributes
4743 attribute_names . each_with_object ( { } ) do |attribute_name , result |
4844 result [ attribute_name ] = public_send ( attribute_name )
@@ -59,43 +55,5 @@ def self.lookup_ancestors
5955 [ self ]
6056 end
6157 # :nocov:
62-
63- def assign_attributes ( new_attributes )
64- unless new_attributes . respond_to? ( :stringify_keys )
65- fail ArgumentError , 'When assigning attributes, you must pass a hash as an argument.'
66- end
67- return if new_attributes . blank?
68-
69- attributes = new_attributes . stringify_keys
70- _assign_attributes ( attributes )
71- end
72-
73- private
74-
75- def _assign_attributes ( attributes )
76- attributes . each do |k , v |
77- _assign_attribute ( k , v )
78- end
79- end
80-
81- def _assign_attribute ( k , v )
82- fail UnknownAttributeError . new ( self , k ) unless respond_to? ( "#{ k } =" )
83- public_send ( "#{ k } =" , v )
84- end
85-
86- def persisted?
87- false
88- end
89-
90- # Raised when unknown attributes are supplied via mass assignment.
91- class UnknownAttributeError < NoMethodError
92- attr_reader :record , :attribute
93-
94- def initialize ( record , attribute )
95- @record = record
96- @attribute = attribute
97- super ( "unknown attribute '#{ attribute } ' for #{ @record . class } ." )
98- end
99- end
10058 end
10159end
0 commit comments