mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 15:23:06 +00:00
Extract attributes filtering from serializer into adapter.
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
module ActiveModel
|
||||
class Serializer
|
||||
class Fieldset
|
||||
def initialize(fields, root = nil)
|
||||
@root = root
|
||||
def initialize(fields)
|
||||
@raw_fields = fields
|
||||
end
|
||||
|
||||
@@ -10,27 +9,19 @@ module ActiveModel
|
||||
@fields ||= parsed_fields
|
||||
end
|
||||
|
||||
def fields_for(serializer)
|
||||
key = serializer.json_key
|
||||
fields[key.to_sym] || fields[key.pluralize.to_sym]
|
||||
def fields_for(type)
|
||||
fields[type.singularize.to_sym] || fields[type.pluralize.to_sym]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
ActiveModelSerializers.silence_warnings do
|
||||
attr_reader :raw_fields, :root
|
||||
attr_reader :raw_fields
|
||||
end
|
||||
|
||||
def parsed_fields
|
||||
if raw_fields.is_a?(Hash)
|
||||
raw_fields.inject({}) { |h, (k, v)| h[k.to_sym] = v.map(&:to_sym); h }
|
||||
elsif raw_fields.is_a?(Array)
|
||||
if root.nil?
|
||||
raise ArgumentError, 'The root argument must be specified if the fields argument is an array.'.freeze
|
||||
end
|
||||
hash = {}
|
||||
hash[root.to_sym] = raw_fields.map(&:to_sym)
|
||||
hash
|
||||
else
|
||||
{}
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user