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

Model Callbacks Added #27

Merged
merged 5 commits into from
Jun 13, 2011
Merged

Model Callbacks Added #27

merged 5 commits into from
Jun 13, 2011

Conversation

coneybeare
Copy link
Contributor

As discussed here, I have added support for the before_api_response, around_api_response, and after_api_response calls. Tests were written, and rake spec passes.

@coneybeare
Copy link
Contributor Author

I use these methods like this:

  def before_api_response(api_template)
    puts "CALLED: before_api_response with api_template '#{api_template}'"
  end

  def after_api_response(api_template)
    puts "CALLED: after_api_response with api_template '#{api_template}'"
  end

  def around_api_response(api_template)
    puts "CALLED: around_api_response with api_template '#{api_template}'"
    if MyServer::Application.config.action_controller.perform_caching
      Rails.cache.fetch("api_response_#{self.class.to_s}_#{id}_#{api_template.to_s}", :expires_in => 5.minutes) do
        yield
      end
    else
      yield
    end    
  end

@coneybeare
Copy link
Contributor Author

Here is the wiki page for it… I am not sure how to do a push to the wiki page.

Model callbacks with api response rendering

There are three optional callbacks that occur before, after and around the as_api_response call.

before_api_response
after_api_response
around_api_response

All three take the requested api_template as an argument and are placed in the model that is being rendered

class User < ActiveRecord::Base

  acts_as_api

  api_accessible :public do |t|
    t.add :first_name
    t.add :last_name
  end

  def before_api_response(api_template)
    puts "Called before the response hash is rendered with api_template: #{api_template}"
  end

  def around_api_response(api_template)
    puts "Called around the response hash is rendered with api_template: #{api_template}"
    yield
  end

  def after_api_response(api_template)
    puts "Called after the response hash is rendered with api_template: #{api_template}"
  end

end

The before and after callbacks are just hooks that allow you to do something before or after the rendering of the model. The around callback has the added bonus of being passed a block. This means that you may choose to alter the structure of it as it is being rendered, or even bypass the as_api_response if need be.

For example, if you wanted to cache the model's api_response, and only generate the rendered response if the cached version does not exist, you could do something like:

def around_api_response(api_template)
  Rails.cache.fetch("api_response_#{self.class.to_s}_#{id}_#{api_template.to_s}", :expires_in => 1.hour) do
    yield
  end
end

@fabrik42
Copy link
Owner

Good work! :)
I will take care of the pull request and the wiki article this weekend.

fabrik42 pushed a commit that referenced this pull request Jun 13, 2011
@fabrik42 fabrik42 merged commit c6ef696 into fabrik42:master Jun 13, 2011
@fabrik42
Copy link
Owner

Hey Matt,

I just pushed acts_as_api 0.3.7 to rubygems, including this feature :D
The wiki article can be found here: https://github.com/fabrik42/acts_as_api/wiki/Model-callbacks-for-acts-as-api

Thanks again for your work :)

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

Successfully merging this pull request may close these issues.

2 participants