Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makes passed in options accessible inside serializers #836

Merged
merged 1 commit into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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