Merge pull request #339 from mhluska/fix-params-not-being-default
Fix params not being hash by default
This commit is contained in:
commit
74bb9d6f6d
@ -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?
|
||||||
|
|
||||||
|
@ -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|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user