merge upstream update fieldset

This commit is contained in:
Aaron Renoir
2014-11-13 17:45:47 -08:00
22 changed files with 623 additions and 148 deletions

View File

@@ -2,11 +2,13 @@ module ActiveModel
class Serializer
class Fieldset
attr_reader :fields, :root
def initialize(fields, root = nil)
@root = root
@fields = parse(fields)
@root = root
@raw_fields = fields
end
def fields
@fields ||= parsed_fields
end
def fields_for(serializer)
@@ -16,15 +18,17 @@ module ActiveModel
private
def parse(fields)
if fields.is_a?(Hash)
fields.inject({}) { |h,(k,v)| h[k.to_sym] = v.map(&:to_sym); h}
elsif fields.is_a?(Array)
attr_reader :raw_fields, :root
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 fileds argument is an array.'
end
hash = {}
hash[root.to_sym] = fields.map(&:to_sym)
hash[root.to_sym] = raw_fields.map(&:to_sym)
hash
else
{}