Devise 的旧版本 (< 1.4.2) 对 to_json 和 to_xml 方法执行了一个猴子补丁,用 attr_accessible 中定义的属性覆盖 :only => [] 选项。恼人的。
现在已经改变了,因此 serializable_hash 被覆盖,并且在 to_json 或 to_xml 中设置的任何 :only => [:attribute] 选项都被保留。
就我而言,我最终自己对 to_json 进行了猴子修补,并向所有 ActiveRecord 模型添加了一个方法 api_accessible。
class ActiveRecord::Base
def to_json(options = {}, &block)
if self.class.respond_to?(:api_attributes)
super(build_serialize_options(options), &block)
else
super(options, &block)
end
end
class << self
attr_reader :api_attributes
def api_accessible(*args)
@api_attributes ||= []
@api_attributes += args
end
end
private
def build_serialize_options(options)
return options if self.class.api_attributes.blank?
methods = self.class.instance_methods - self.class.attribute_names.map(&:to_sym)
api_methods = self.class.api_attributes.select { |m| methods.include?(m) }
api_attrs = self.class.api_attributes - api_methods
options.merge!(only: api_attrs) if api_attrs.present?
options.merge!(methods: api_methods) if api_methods.present?
return options
end
end
这意味着您现在可以定义在调用 to_json 时默认公开的属性列表(和方法!)。Respond_with 也使用 to_json,因此它适用于 API。
例如,user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
#Setup accessible (or protected) attributes for your model
attr_accessible :email,
:password,
:password_confirmation,
:remember_me,
:first_name,
:last_name,
api_accessible :id,
:name,
:created_at,
:first_name,
:last_name,
:some_model_method_also_works
end