nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Is there a way to declare attributes using Ruby code in a rabl template? #64

Closed Florent2 closed 13 years ago

Florent2 commented 13 years ago

Hi,

In a particular rabl view, I want to declare all the attributes of a model. I tried

attributes MyModel.column_names.map(&:to_sym)

but I get ActionView::Template::Error error:

[:id, :attr1, :attr2, ..., :attrn, :created_at, :updated_at] is not a symbol

Is it possible with rabl to not have to manually list the model attributes?

Thanks :)

nesquena commented 13 years ago

How about (notice the splat argument)

attributes *MyModel.column_names.map(&:to_sym)

or

MyModel.column_names.map(&:to_sym).each { |a| attribute a }
Florent2 commented 13 years ago

Thanks Nathan! The splat argument is the solution.

spilth commented 12 years ago

Any chance this kind of functionality would be pushed into RABL? Such as an all_attributes method?

nesquena commented 12 years ago

But RABL is not tied to an ORM or any object expectation. So what would all_attributes do that would work across all objects? I would recommend just creating your own helper and using that.

spilth commented 12 years ago

Ah, right. My mistake for only thinking in the context of Rails.

drewB commented 12 years ago

Just for the record the map(&:to_sym) is not necessary. *MyModel.column_names works just fine.