Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new ActiveModel::SerializableResource #138

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nikolai-b
Copy link

as_json does not work with the newest version of https://github.com/rails-api/active_model_serializers and it is suggested that you use the ActiveModel::SerializableResource class.

@@ -5,14 +5,17 @@ module Respondable
SerializerWrapper = Struct.new(:serializer, :object) do

def serializable_hash(options = {})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

coming here from: rails-api/active_model_serializers#1321 (comment)

could be a little more efficient by just defining a different method per version

if ActiveModel::Serializer::VERSION.start_with?('0.9.')
        def serializable_hash(options = {})
          instance = serializer.new(object, options)
          if instance.respond_to?(:serializable_hash)
            instance.serializable_hash
          else
            instance.as_json options
          end
        end
elsif ActiveModel::Serializer::VERSION.start_with?('0.10.')
  defined?(ActiveModel::SerializableResource) or fail "Your ActiveModelSerializers is too old. Please update to the latest minor or patch release."
  def serializable_hash(options = {})
    ActiveModel::SerializableResource.new(object, options.merge(serializer: serializer)).serializable_hash
  end
else
  fail ArgumentError, "We don't know how to serialize in #{ActiveModel::Serializer::VERSION}"
end

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This stems partly from the super loose constraint in the Gemfile

gem 'active_model_serializers', ENV['AMS_VERSION'] || '> 0.0'

(I'm guessing it's a soft dependency?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants