rocket_pants icon indicating copy to clipboard operation
rocket_pants copied to clipboard

Added presenter method `attr_expose` to AR

Open adamburmister opened this issue 12 years ago • 4 comments

I added a method attr_expose to the AR model which allows for easy declaration of attributes to include in the JSON response.

Under the cover it stores a list of attribute names and automatically passes those into the options[:only] array when serializable_hash is called.

class Account < ActiveRecord::Base
  attr_expose :full_name, :email
end

adamburmister avatar Dec 19 '12 14:12 adamburmister

I like the idea - it's nice and simple yet suitable powerful! I'm not sure it's something that necessarily belongs in RocketPants itself at the moment. I'd be more inclined to add it as a rocket_pants-presenter gem (which I'm happy to link to) if that'd make sense to you?

Sutto avatar Dec 31 '12 06:12 Sutto

Just came across this

Did anyone put these into a gem that could we could hook in?

softwaregravy avatar Aug 10 '13 17:08 softwaregravy

@softwaregravy I'm not aware of a gem, but you can solve it pretty similarly (using different syntax, and admittedly a bit more weight) using ActiveModel Serializers integration.

Sutto avatar Aug 12 '13 15:08 Sutto

I did end up going with the Serializers, thanks

softwaregravy avatar Aug 12 '13 15:08 softwaregravy