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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/rocket_pants/controller/respondable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?)

instance = serializer.new(object, options)
if instance.respond_to?(:serializable_hash)
instance.serializable_hash
if Object.const_defined?('ActiveModel::SerializableResource')
ActiveModel::SerializableResource.new(object, options.merge(serializer: serializer)).serializable_hash
else
instance.as_json options
instance = serializer.new(object, options)
if instance.respond_to?(:serializable_hash)
instance.serializable_hash
else
instance.as_json options
end
end
end

end

def self.pagination_type(object)
Expand Down