Merge pull request #339 from mhluska/fix-params-not-being-default

Fix params not being hash by default
This commit is contained in:
Shishir Kakaraddi 2018-11-03 11:30:20 -07:00 committed by GitHub
commit 74bb9d6f6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

@ -240,8 +240,8 @@ RSpec.shared_context 'movie class' do
include FastJsonapi::ObjectSerializer include FastJsonapi::ObjectSerializer
attributes :id, :title attributes :id, :title
attribute :year, if: Proc.new { |record, params| attribute :year, if: Proc.new { |record, params|
params[:include_award_year].present? ? params[:include_award_year].present? ?
params[:include_award_year] : params[:include_award_year] :
false false
} }
belongs_to :actor belongs_to :actor
@ -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