Fix params not being hash by default

This commit is contained in:
Maros Hluska 2018-10-18 22:59:32 +07:00
parent ece607af5f
commit d5ea95370f
4 changed files with 25 additions and 5 deletions

View File

@ -1,5 +1,6 @@
# frozen_string_literal: true # frozen_string_literal: true
require 'active_support/time'
require 'active_support/json' require 'active_support/json'
require 'active_support/concern' require 'active_support/concern'
require 'active_support/inflector' require 'active_support/inflector'
@ -72,6 +73,7 @@ module FastJsonapi
def process_options(options) def process_options(options)
@fieldsets = deep_symbolize(options[:fields].presence || {}) @fieldsets = deep_symbolize(options[:fields].presence || {})
@params = {}
return if options.blank? return if options.blank?

View File

@ -15,7 +15,7 @@ describe FastJsonapi::ObjectSerializer do
class MovieSerializer class MovieSerializer
attribute :viewed do |movie, params| attribute :viewed do |movie, params|
params ? movie.viewed?(params[:user]) : false params[:user] ? movie.viewed?(params[:user]) : false
end end
attribute :no_param_attribute do |movie| attribute :no_param_attribute do |movie|

View File

@ -504,4 +504,10 @@ describe FastJsonapi::ObjectSerializer do
end end
end end
end end
context 'when attribute contents are determined by params data' do
it 'does not throw an error with no params are passed' do
expect { MovieOptionalAttributeContentsWithParamsSerializer.new(movie).serialized_json }.not_to raise_error
end
end
end end

View File

@ -313,7 +313,7 @@ RSpec.shared_context 'movie class' do
include FastJsonapi::ObjectSerializer include FastJsonapi::ObjectSerializer
set_type :movie set_type :movie
attributes :name attributes :name
attribute :director, if: Proc.new { |record, params| params && params[:admin] == true } attribute :director, if: Proc.new { |record, params| params[:admin] == true }
end end
class MovieOptionalRelationshipSerializer class MovieOptionalRelationshipSerializer
@ -327,7 +327,19 @@ RSpec.shared_context 'movie class' do
include FastJsonapi::ObjectSerializer include FastJsonapi::ObjectSerializer
set_type :movie set_type :movie
attributes :name attributes :name
belongs_to :owner, record_type: :user, if: Proc.new { |record, params| params && params[:admin] == true } belongs_to :owner, record_type: :user, if: Proc.new { |record, params| params[:admin] == true }
end
class MovieOptionalAttributeContentsWithParamsSerializer
include FastJsonapi::ObjectSerializer
set_type :movie
attributes :name
attribute :director do |record, params|
data = {}
data[:first_name] = 'steven'
data[:last_name] = 'spielberg' if params[:admin]
data
end
end end
end end