api: refactor only param.
This commit is contained in:
@@ -215,7 +215,7 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
module ApiMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def as_json(options = {})
|
||||
def serializable_hash(options = {})
|
||||
options ||= {}
|
||||
|
||||
options[:include] ||= []
|
||||
@@ -226,51 +226,12 @@ class ApplicationRecord < ActiveRecord::Base
|
||||
options[:methods] ||= []
|
||||
options[:methods] += method_attributes
|
||||
|
||||
if !options.key?(:only) && RequestStore.exist?(:only_param)
|
||||
options[:only] = RequestStore[:only_param]
|
||||
end
|
||||
|
||||
if options[:only].is_a?(String)
|
||||
options[:only] = options[:only].split(/,/)
|
||||
end
|
||||
|
||||
if options[:only]
|
||||
options[:methods] = options[:methods] & options[:only].map(&:to_sym)
|
||||
options[:include] = options[:include] & options[:only].map(&:to_sym)
|
||||
end
|
||||
|
||||
super(options)
|
||||
end
|
||||
|
||||
def to_xml(options = {}, &block)
|
||||
options ||= {}
|
||||
|
||||
options[:include] ||= []
|
||||
|
||||
options[:except] ||= []
|
||||
options[:except] += hidden_attributes
|
||||
|
||||
options[:methods] ||= []
|
||||
options[:methods] += method_attributes
|
||||
|
||||
if !options.key?(:only) && RequestStore.exist?(:only_param)
|
||||
options[:only] = RequestStore[:only_param]
|
||||
end
|
||||
|
||||
if options[:only].is_a?(String)
|
||||
options[:only] = options[:only].split(/,/)
|
||||
end
|
||||
|
||||
if options[:only]
|
||||
options[:methods] = options[:methods] & options[:only]
|
||||
options[:include] = options[:include] & options[:only]
|
||||
end
|
||||
|
||||
super(options, &block)
|
||||
end
|
||||
|
||||
def serializable_hash(*args)
|
||||
hash = super(*args)
|
||||
hash = super(options)
|
||||
hash.transform_keys { |key| key.delete("?") }
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user