Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created May 18, 2011 02:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metaskills/977890 to your computer and use it in GitHub Desktop.
Save metaskills/977890 to your computer and use it in GitHub Desktop.
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