public
Created

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.