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

Is there a way to use template rather than exposing models fully #27

Closed
filvo opened this issue Aug 8, 2012 · 6 comments
Closed

Is there a way to use template rather than exposing models fully #27

filvo opened this issue Aug 8, 2012 · 6 comments

Comments

@filvo
Copy link

filvo commented Aug 8, 2012

Yo Man! First of all nice work you have done. Is there a way / plans for using templates for response like acts_as_api gem does. I want to use certain records and customized response, not the active records fields.

Thanks

@Sutto
Copy link
Owner

Sutto commented Aug 9, 2012

Hi filvo, thanks!

There isn't a way / plan to use templates at the moment - we purposed strip out rails built in templates. You can write custom serializers (e.g. check out activerecord serializers / write your own DelegateClass-based implementations, you simply need to define serializable_hash to return what you want) but the general top level structure is designed as part of rocket_pants (e.g. the response nesting etc).

There are plans to make this customisable, but it likely wont happen until 2.0. In the mean time, you can check #20 and #23 for some more guidance / ideas.

@joenoon
Copy link

joenoon commented Aug 18, 2012

I'm new to rocket_pants myself, and was trying to get expose to automatically use my ActiveModel::Serializer classes so I could just do expose @post instead of expose PostSerializer.new(@post, current_user). I came up with this. Not sure if its right or wrong, but seems to be working so far in an initializer. Any feedback would be great:

module RocketPants
  module Respondable
    private
    alias_method :orig_normalise_object, :normalise_object
    def normalise_object(json, options = {})
      # modified from: https://github.com/josevalim/active_model_serializers/blob/master/lib/action_controller/serialization.rb#L42
      if serializer = options.delete(:serializer) || (json.respond_to?(:active_model_serializer) && json.active_model_serializer)
        if json.respond_to?(:to_ary)
          if options[:root] != false && serializer.root != false
            # default root element for arrays is serializer's root or the controller name
            # the serializer for an Array is ActiveModel::ArraySerializer
            options[:root] ||= serializer.root || controller_name
          end
        end
        options[:scope] = serialization_scope unless options.has_key?(:scope)
        options[:url_options] = url_options
        serializer.new(json, options.merge(default_serializer_options || {}))
      else
        orig_normalise_object(json, options)
      end
    end
  end
end

ActiveModel::ArraySerializer.root = false

UPDATE:

Forgot a few details:

Gemfile:
gem "active_model_serializers", :git => "git://github.com/josevalim/active_model_serializers.git"

Controllers:

include ::ActionController::Serialization

def default_serializer_options
  { :root => false }
end

I use Mongoid, so I did:

Mongoid::Document.send :include, ActiveModel::SerializerSupport

@fabn
Copy link
Contributor

fabn commented Nov 17, 2012

@joenoon your code worked like a charm, thank you.

@Sutto joenoon code could be integrated as an addition to easily support ActiveModel::Serializer which often is enough to build json responses from models, what do you think?

@Sutto
Copy link
Owner

Sutto commented Dec 31, 2012

@fabn @joenoon Integrating it now and will release as 1.6.0 - Cheers!

@Sutto
Copy link
Owner

Sutto commented Jan 2, 2013

@fabn @joenoon @filvo Take a look at 1.6.0.rc1, it should support them now out of the box.

Just use expose and pass options as the second argument (e.g. for custom serializers).

@Sutto
Copy link
Owner

Sutto commented Feb 24, 2013

Closing this now as 1.6.* is out with it.

@Sutto Sutto closed this as completed Feb 24, 2013
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

No branches or pull requests

4 participants