Sha256: 546509bbdb7de47171ae57675e1aa3e8cd52607828f6c2f2782f8574aa8eea0d

Contents?: true

Size: 1.19 KB

Versions: 34

Compression:

Stored size: 1.19 KB

Contents

module ActiveRemote
  module Serialization
    extend ActiveSupport::Concern

    included do
      include ::ActiveModel::Serializers::JSON
    end

    module ClassMethods
      # Serialize the given records into Active Remote objects.
      #
      # ====Examples
      #
      #   records = [ Generic::Remote::TagRequest.new(:name => 'foo') ]
      #
      #   Tag.serialize_records(records) # => [ Tag#{:name => 'foo'} ]
      #
      def serialize_records(records)
        records.map do |record|
          model = instantiate(record.to_hash)
          model.add_errors(record.errors) if record.respond_to?(:errors)
          model
        end
      end
    end

    # Add the given errors to our internal errors list
    #
    # ====Examples
    #
    #   response = remote_call(:action_that_returns_errors, { :stuff => 'foo' })
    #
    #   add_errors(response.errors)
    #
    def add_errors(errors)
      errors.each do |error|
        if error.respond_to?(:message)
          self.errors.add(error.field, error.message)
        elsif error.respond_to?(:messages)
          error.messages.each do |message|
            self.errors.add(error.field, message)
          end
        end
      end
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
active_remote-5.0.0.pre lib/active_remote/serialization.rb
active_remote-3.3.1 lib/active_remote/serialization.rb
active_remote-3.3.0 lib/active_remote/serialization.rb
active_remote-3.2.2 lib/active_remote/serialization.rb
active_remote-3.2.1 lib/active_remote/serialization.rb
active_remote-3.2.0 lib/active_remote/serialization.rb
active_remote-3.2.0.pre lib/active_remote/serialization.rb
active_remote-3.1.3 lib/active_remote/serialization.rb
active_remote-3.1.2 lib/active_remote/serialization.rb
active_remote-3.1.2.pre lib/active_remote/serialization.rb
active_remote-3.1.1 lib/active_remote/serialization.rb
active_remote-3.1.0 lib/active_remote/serialization.rb
active_remote-3.0.0 lib/active_remote/serialization.rb
active_remote-3.0.0.pre1 lib/active_remote/serialization.rb