diff --git a/lib/active_model_serializers/model.rb b/lib/active_model_serializers/model.rb index 9437d68b5..9b3540407 100644 --- a/lib/active_model_serializers/model.rb +++ b/lib/active_model_serializers/model.rb @@ -4,10 +4,7 @@ module ActiveModelSerializers class Model include ActiveModel::Serializers::JSON - include ActiveModel::Validations - include ActiveModel::Conversion - extend ActiveModel::Naming - extend ActiveModel::Translation + include ActiveModel::Model class_attribute :attribute_names self.attribute_names = [] @@ -20,6 +17,13 @@ def self.attributes(*names) attributes :id attr_writer :updated_at + attr_reader :errors + + def initialize(attributes = {}) + @errors = ActiveModel::Errors.new(self) + super + end + # Defaults to the downcased model name. def id @id ||= self.class.name.downcase @@ -35,14 +39,6 @@ def updated_at defined?(@updated_at) ? @updated_at : File.mtime(__FILE__) end - attr_reader :errors - - def initialize(attributes = {}) - assign_attributes(attributes) if attributes - @errors = ActiveModel::Errors.new(self) - super() - end - def attributes attribute_names.each_with_object({}) do |attribute_name, result| result[attribute_name] = public_send(attribute_name) @@ -59,43 +55,5 @@ def self.lookup_ancestors [self] end # :nocov: - - def assign_attributes(new_attributes) - unless new_attributes.respond_to?(:stringify_keys) - fail ArgumentError, 'When assigning attributes, you must pass a hash as an argument.' - end - return if new_attributes.blank? - - attributes = new_attributes.stringify_keys - _assign_attributes(attributes) - end - - private - - def _assign_attributes(attributes) - attributes.each do |k, v| - _assign_attribute(k, v) - end - end - - def _assign_attribute(k, v) - fail UnknownAttributeError.new(self, k) unless respond_to?("#{k}=") - public_send("#{k}=", v) - end - - def persisted? - false - end - - # Raised when unknown attributes are supplied via mass assignment. - class UnknownAttributeError < NoMethodError - attr_reader :record, :attribute - - def initialize(record, attribute) - @record = record - @attribute = attribute - super("unknown attribute '#{attribute}' for #{@record.class}.") - end - end end end