Skip to content

Commit

Permalink
Allow only to pass scope to associations
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Jan 3, 2014
1 parent 60fc550 commit 5fa4002
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
9 changes: 5 additions & 4 deletions lib/active_model/array_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@ class << self

def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@each_serializer = options[:each_serializer]
@options = options.merge(root: nil)
@resource_name = options[:resource_name]
end
attr_accessor :object, :root, :meta_key, :meta, :options
attr_accessor :object, :scope, :root, :meta_key, :meta

def json_key
if root.nil?
@options[:resource_name]
@resource_name
else
root
end
end

def serializer_for(item)
serializer_class = @each_serializer || Serializer.serializer_for(item) || DefaultSerializer
serializer_class.new(item, @options)
serializer_class.new(item, scope: scope)
end

def serializable_object
Expand Down
16 changes: 8 additions & 8 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ def associate(klass, *attrs)
end

def initialize(object, options={})
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@options = options.reject{|k,v| [:scope, :root, :meta_key, :meta].include?(k) }
@object = object
@scope = options[:scope]
@root = options.fetch(:root, self.class._root)
@meta_key = options[:meta_key] || :meta
@meta = options[@meta_key]
@wrap_in_array = options[:_wrap_in_array]
end
attr_accessor :object, :scope, :meta_key, :meta, :root, :options
attr_accessor :object, :scope, :root, :meta_key, :meta

def json_key
if root == true || root.nil?
Expand Down Expand Up @@ -186,7 +186,7 @@ def serializable_object(options={})
return nil if object.nil?
hash = attributes
hash.merge! associations
@options[:_wrap_in_array] ? [hash] : hash
@wrap_in_array ? [hash] : hash
end
alias_method :serializable_hash, :serializable_object
end
Expand Down
17 changes: 0 additions & 17 deletions test/unit/active_model/serializer/options_test.rb

This file was deleted.

0 comments on commit 5fa4002

Please sign in to comment.