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

passing serialization options #46

Closed
mrroach opened this issue Mar 15, 2010 · 7 comments
Closed

passing serialization options #46

mrroach opened this issue Mar 15, 2010 · 7 comments

Comments

@mrroach
Copy link

mrroach commented Mar 15, 2010

Is there any way to pass options to to_xml? I have many situations where I need to pass things like :root, :include, or :methods and the only way I have figured out is to override the whole action. Is there another way?

for example, in my app I have a base controller which defines a to_xml_parameters method which I always want to pass to to_xml. It seems redundant to put that in all controllers.

Thanks.

@josevalim
Copy link
Contributor

There is no way to pass serialization options, but you don't need to overwrite your whole action, just the required pieces.

And if you have always to pass the same options, you should also consider overwriting your models or creating your own responder.

@mrroach
Copy link
Author

mrroach commented Mar 16, 2010

It's not always the same options, the options depend on parameters passed in by the user. I'm assuming that by overwriting "the required pieces" you mean like this:

def index
  index! do |format|
    format.xml { render :xml => collection.to_xml(to_xml_parameters) }
  end
end

Is that correct? It kind of sucks to have to repeat myself in all my controllers that way; perhaps a responder is a better choice...

The stuff under legacy hurts my brain :-). Do I need to create an ActionController::Responder or an ActionController::MimeResponds::Responder?

@josevalim
Copy link
Contributor

If you need to add the same line in all controllers, you can put the logic in a module and include the module in your controllers:

module ToXmlParameters
def index
index! do |format|
format.xml { render :xml => collection.to_xml(to_xml_parameters) }
end
end

def to_xml_parameters
  # implement your logic here
end

end

And include this module in your controllers. If you want to go the responders road, I would suggest you to inherit from InheritedResources::Responder and add the logic you want. The ActionController::Responder is the one what you should look to know what to overwrite though.

When you are done, do:

InheritedResources::Base.responder = YourResponder

@mrroach
Copy link
Author

mrroach commented Mar 16, 2010

Thanks for your help. I think the responder route is going to work well for me

@jpshackelford
Copy link

I too am finding that I am doing--or am about to do--the following in many controllers.

def index 
  index! do |format|
    format.xml{  render :xml  => collection.to_xml(  render_options ) }
    format.json{ render :json => collection.to_json( render_options ) }        
  end
end

def show
  show! do |format|
    format.xml{ render :xml  => resource.to_xml(  render_options ) }
    format.xml{ render :json => resource.to_json( render_options ) }
  end
end

mrroach, how did your Respondor solution work out? Care to share a snippet? Jose, these seems a fairly typical scenario. Would you accept a patch to do something about this in inherited_resources itself?

@josevalim
Copy link
Contributor

The customization does not belong to Inherited Resources. It should be at the Responders level. Take a look at responders documentation and blog posts available. :)

@jpshackelford
Copy link

Jose,

I have attempted this at https://github.com/jpshackelford/resource_inclusion . I'll be interested in your feedback.

John-Mason

This issue was closed.
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

3 participants