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

Grape Support #1258

Closed
bf4 opened this issue Oct 8, 2015 · 8 comments
Closed

Grape Support #1258

bf4 opened this issue Oct 8, 2015 · 8 comments

Comments

@bf4
Copy link
Member

bf4 commented Oct 8, 2015

https://github.com/jrhe/grape-active_model_serializers/blob/ca9c8faaa99727300ff21419aa20471ac27eed42/lib/grape-active_model_serializers/formatter.rb#L15-L25

        def fetch_serializer(resource, env)
          endpoint = env['api.endpoint']
          options = build_options_from_endpoint(endpoint)

          serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
          return nil unless serializer

          options[:scope] = endpoint unless options.key?(:scope)
          # ensure we have an root to fallback on
          options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
          serializer.new(resource, options.merge(other_options(env)))
        end

which uses serializer = ActiveModel::Serializer.serializer_for(resource)).new(resource, options) which is (somewhat intentionally) not supported in 0.10.

I'd be in favor of adding a grape serializer something or other (grape users can help here).

In the meantime, this may work

Grape::Formatter::ActiveModelSerializers.module_eval do # might be instance_eval
      class << self
        def fetch_serializer(resource, env)
          endpoint = env['api.endpoint']
          options = build_options_from_endpoint(endpoint)

           serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
-          return nil unless serializer


          options[:scope] = endpoint unless options.key?(:scope)
          # ensure we have an root to fallback on
          options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
+        serialization_options = options.merge(adapter: :json, serializer: merge!(other_options(env))).serializer) 
+          ActiveModel::SerializableResource.new(resource, serialization_options) # assuming you want json, I don't know which adapter to use

-          serializer.new(resource, options.merge(other_options(env)))
        end
  end
end

Related issues

@bf4
Copy link
Member Author

bf4 commented Oct 8, 2015

@rails-api/ams

@bf4
Copy link
Member Author

bf4 commented Oct 14, 2015

ref: #1268

@joaomdmoura
Copy link
Member

Should we put it somewhere else, like docs? @bf4
It's kind confusing IMO

@hut8
Copy link
Contributor

hut8 commented Oct 23, 2015

Hey @bf4 -- thanks for documenting this. I'm a bit lost on this line:

serialization_options = options.merge(adapter: :json, serializer: merge!(other_options(env))).serializer) 

Looks like unbalanced parenthesis and merge! isn't defined on that context.... what should this be?

@bf4
Copy link
Member Author

bf4 commented Oct 25, 2015

I just wrote it up, untested, so do what makes sense? There's also a grape pr

B mobile phone

On Oct 23, 2015, at 3:17 PM, Liam notifications@github.com wrote:

Hey @bf4 -- thanks for documenting this. I'm a bit lost on this line:

serialization_options = options.merge(adapter: :json, serializer: merge!(other_options(env))).serializer)
Looks like unbalanced parenthesis and merge! isn't defined on that context.... what should this be?


Reply to this email directly or view it on GitHub.

@bf4
Copy link
Member Author

bf4 commented Oct 25, 2015

Implementation PR is in #1273

@bf4
Copy link
Member Author

bf4 commented Nov 26, 2015

Updated, is #1336

johnhamelink pushed a commit to johnhamelink/active_model_serializers that referenced this issue Nov 26, 2015
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - Add integration test
 - Refine scope of Grape version dependency
 - Assert that the response is equal to a manually defined JSON string
 - Add single module to include in Grape projects
 - Create a Serializable Resource to test rails-api from Grape
 - Update docs
 - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
 - Updated Changelog
johnhamelink pushed a commit to johnhamelink/active_model_serializers that referenced this issue Dec 10, 2015
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - Add integration test
 - Refine scope of Grape version dependency
 - Assert that the response is equal to a manually defined JSON string
 - Add single module to include in Grape projects
 - Create a Serializable Resource to test rails-api from Grape
 - Update docs
 - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
 - Updated Changelog
johnhamelink pushed a commit to johnhamelink/active_model_serializers that referenced this issue Dec 10, 2015
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - Add integration test
 - Refine scope of Grape version dependency
 - Assert that the response is equal to a manually defined JSON string
 - Add single module to include in Grape projects
 - Create a Serializable Resource to test rails-api from Grape
 - Update docs
 - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
 - Updated Changelog
 - Remove parens from `render`, use `serializable` in all tests.
bf4 added a commit that referenced this issue Dec 18, 2015
Grape formatter feature requested in #1258 - Rebased and Repushed (#1273)
@bf4
Copy link
Member Author

bf4 commented Dec 22, 2015

Closed by #1336

@bf4 bf4 closed this as completed Dec 22, 2015
beauby pushed a commit to beauby/active_model_serializers that referenced this issue Dec 30, 2015
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - Add integration test
 - Refine scope of Grape version dependency
 - Assert that the response is equal to a manually defined JSON string
 - Add single module to include in Grape projects
 - Create a Serializable Resource to test rails-api from Grape
 - Update docs
 - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
 - Updated Changelog
 - Remove parens from `render`, use `serializable` in all tests.
joaomdmoura pushed a commit to joaomdmoura/active_model_serializers that referenced this issue Jan 19, 2016
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - Add integration test
 - Refine scope of Grape version dependency
 - Assert that the response is equal to a manually defined JSON string
 - Add single module to include in Grape projects
 - Create a Serializable Resource to test rails-api from Grape
 - Update docs
 - Fix discrepency between ActiveRecord 4.0 - 4.1 and 4.2
 - Updated Changelog
 - Remove parens from `render`, use `serializable` in all tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants