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

feature request: conditional statements in api template #10

Closed
atwoodjw opened this issue Apr 21, 2011 · 3 comments
Closed

feature request: conditional statements in api template #10

atwoodjw opened this issue Apr 21, 2011 · 3 comments
Assignees

Comments

@atwoodjw
Copy link

it would be really handy to conditionally add attributes to an api template.

class User < ActiveRecord::Base

    acts_as_api

    api_accessible :user_show do |t|
        t.add :first_name
        t.add :last_name
        t.add :age

        t.add :favorite_beer if age > 21
    end
end

this is a contrived example, but i do have a (legacy) class that has many, many one-to-many relationships. it would be helpful to conditionally include certain attributes based on the value of another.

cheers!

@fabrik42
Copy link
Owner

Hi,
this seems to be a neat idea.

I would want to check for these attributes at rendering time. So I propose to do it the same way activerecord validations and callbacks work, e.g.:

t.add :favorite_beer, :if => lambda{|u| user.age > 21 }

and

t.add :favorite_beer, :if => :is_adult?

to call a method of the model instance.

What do you think?

@ghost ghost assigned fabrik42 Apr 22, 2011
@atwoodjw
Copy link
Author

that sounds terrific -- very straight-forward. i like it.

@atwoodjw atwoodjw reopened this Apr 22, 2011
@fabrik42
Copy link
Owner

Hey,
I just released acts_as_api 0.3.3 which adds support for the :if and :unless options.

See:
https://github.com/fabrik42/acts_as_api/wiki/Conditional-statements-for-adding-a-field-to-the-api-template

250e5eb

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

2 participants