Skip to content

Commit

Permalink
Merge pull request #836 from rails-api/stores-passed-in-options
Browse files Browse the repository at this point in the history
Makes passed in options accessible inside serializers
  • Loading branch information
guilleiguaran committed Mar 11, 2015
2 parents 3e8325b + 48650ec commit 73aeba4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ def self.root_name

def initialize(object, options = {})
@object = object
@options = options
@root = options[:root] || (self.class._root ? self.class.root_name : false)
@meta = options[:meta]
@meta_key = options[:meta_key]
Expand Down Expand Up @@ -201,6 +202,8 @@ def serializer_from_options(options)

private

attr_reader :options

def self.get_serializer_for(klass)
serializer_class_name = "#{klass.name}Serializer"
serializer_class = serializer_class_name.safe_constantize
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description

urls :posts, :comments

def arguments_passed_in?
options[:my_options] == :accessible
end
end

class ProfilePreviewSerializer < ActiveModel::Serializer
Expand Down
21 changes: 21 additions & 0 deletions test/serializers/options_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'test_helper'

module ActiveModel
class Serializer
class OptionsTest < Minitest::Test
def setup
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
end

def test_options_are_accessible
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
assert @profile_serializer.arguments_passed_in?
end

def test_no_option_is_passed_in
@profile_serializer = ProfileSerializer.new(@profile)
refute @profile_serializer.arguments_passed_in?
end
end
end
end

0 comments on commit 73aeba4

Please sign in to comment.