Skip to content

Commit 73aeba4

Browse files
committed
Merge pull request #836 from rails-api/stores-passed-in-options
Makes passed in options accessible inside serializers
2 parents 3e8325b + 48650ec commit 73aeba4

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/active_model/serializer.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def self.root_name
140140

141141
def initialize(object, options = {})
142142
@object = object
143+
@options = options
143144
@root = options[:root] || (self.class._root ? self.class.root_name : false)
144145
@meta = options[:meta]
145146
@meta_key = options[:meta_key]
@@ -201,6 +202,8 @@ def serializer_from_options(options)
201202

202203
private
203204

205+
attr_reader :options
206+
204207
def self.get_serializer_for(klass)
205208
serializer_class_name = "#{klass.name}Serializer"
206209
serializer_class = serializer_class_name.safe_constantize

test/fixtures/poro.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ class ProfileSerializer < ActiveModel::Serializer
4545
attributes :name, :description
4646

4747
urls :posts, :comments
48+
49+
def arguments_passed_in?
50+
options[:my_options] == :accessible
51+
end
4852
end
4953

5054
class ProfilePreviewSerializer < ActiveModel::Serializer

test/serializers/options_test.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'test_helper'
2+
3+
module ActiveModel
4+
class Serializer
5+
class OptionsTest < Minitest::Test
6+
def setup
7+
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
8+
end
9+
10+
def test_options_are_accessible
11+
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
12+
assert @profile_serializer.arguments_passed_in?
13+
end
14+
15+
def test_no_option_is_passed_in
16+
@profile_serializer = ProfileSerializer.new(@profile)
17+
refute @profile_serializer.arguments_passed_in?
18+
end
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)