-
Notifications
You must be signed in to change notification settings - Fork 68
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
Conversation
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 |
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 renderingThere are three optional callbacks that occur before_api_response
after_api_response
around_api_response All three take the requested 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 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 |
Good work! :) |
Hey Matt, I just pushed acts_as_api 0.3.7 to rubygems, including this feature :D Thanks again for your work :) |
As discussed here, I have added support for the
before_api_response
,around_api_response
, andafter_api_response
calls. Tests were written, andrake spec
passes.