ruby-grape / grape-rabl

Use rabl with grape
MIT License
136 stars 29 forks source link

Changing rabl template dynamically #14

Closed mixellent closed 11 years ago

mixellent commented 11 years ago

It would be nice if you could change the :rabl option dynamically depending on a certain condition.

For example, if you have:

resource 'articles' do
  get '/', :rabl => "articles" do
    @article = Article.all
  end

  get ':id', :rabl => "article" do
    @article = Article.find(params[:id])
  end
end

You could do:

resource 'articles' do
  get '/', :rabl => "articles" do
    @article = Article.all
  end

  get ':id', :rabl => "article" do
    @article = Article.find(params[:id])

    if @article.category == "technology"
      render :rabl => "article_with_technology"
    end
  end
end

such that the rendered rabl will depend on one or more specific article object attribute(s).

Is there a way using your gem to perform something close to the above?

Thanks, Michael

LTe commented 11 years ago

@mixellent I added support for render method. So you can use render :rabl => "template_name" inside Grape::API blocks. https://github.com/LTe/grape-rabl/commit/4ba2620cea455bf3e76173b21d09993b59a3fa15