Created
May 18, 2011 02:36
-
-
Save metaskills/977890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActiveRecord::Base | |
cattr_accessor :my_json_attributes_key | |
self.set_my_json_attributes_key = :apiv1 | |
end | |
class Api::V2::Controller < ApplicationController | |
around_filter :set_my_json_attributes_key | |
... | |
private | |
def set_my_json_attributes_key | |
old = ActiveRecord::Base.my_json_attributes_key | |
ActiveRecord::Base.my_json_attributes_key = :apiv2 | |
yield | |
ensure | |
ActiveRecord::Base.my_json_attributes_key = old | |
end | |
end | |
class Bookmark < ActiveRecord::Base | |
JSON_ATTRS = { | |
:apiv1 => [:id, :owner_id, :owner_type, :url, :name, :position], | |
:apiv2 => [:id, :owner_id, :owner_type, :url, :name, :position, :foo, :bar] | |
} | |
def as_json(options=nil) | |
attributes.slice(*JSON_ATTRS[my_json_attributes_key]) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment