allow conditional relationships
This commit is contained in:
parent
2b01d8ce70
commit
5558dcd703
@ -221,7 +221,8 @@ module FastJsonapi
|
||||
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
|
||||
relationship_type: relationship_type,
|
||||
cached: options[:cached] || false,
|
||||
polymorphic: fetch_polymorphic_option(options)
|
||||
polymorphic: fetch_polymorphic_option(options),
|
||||
conditional_proc: options[:if]
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -82,6 +82,8 @@ module FastJsonapi
|
||||
relationships = relationships_to_serialize if relationships.nil?
|
||||
|
||||
relationships.each_with_object({}) do |(_k, relationship), hash|
|
||||
conditional_proc = relationship[:conditional_proc]
|
||||
if conditional_proc.blank? || conditional_proc.call(record, params)
|
||||
name = relationship[:key]
|
||||
empty_case = relationship[:relationship_type] == :has_many ? [] : nil
|
||||
hash[name] = {
|
||||
@ -89,6 +91,7 @@ module FastJsonapi
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def record_hash(record, params = {})
|
||||
if cached
|
||||
|
@ -341,4 +341,33 @@ describe FastJsonapi::ObjectSerializer do
|
||||
expect(serializable_hash['data']['attributes'].has_key?('director')).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'when optional relationships are determined by record data' do
|
||||
it 'returns optional relationship when relationship is included' do
|
||||
json = MovieOptionalRelationshipSerializer.new(movie).serialized_json
|
||||
serializable_hash = JSON.parse(json)
|
||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy
|
||||
end
|
||||
|
||||
it "doesn't return optional relationship when relationship is not included" do
|
||||
movie.actor_ids = []
|
||||
json = MovieOptionalRelationshipSerializer.new(movie).serialized_json
|
||||
serializable_hash = JSON.parse(json)
|
||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
|
||||
end
|
||||
end
|
||||
|
||||
context 'when optional relationships are determined by params data' do
|
||||
it 'returns optional relationship when relationship is included' do
|
||||
json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: true }}).serialized_json
|
||||
serializable_hash = JSON.parse(json)
|
||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_truthy
|
||||
end
|
||||
|
||||
it "doesn't return optional relationship when relationship is not included" do
|
||||
json = MovieOptionalRelationshipWithParamsSerializer.new(movie, { params: { admin: false }}).serialized_json
|
||||
serializable_hash = JSON.parse(json)
|
||||
expect(serializable_hash['data']['relationships'].has_key?('actors')).to be_falsey
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -292,6 +292,20 @@ RSpec.shared_context 'movie class' do
|
||||
attributes :name
|
||||
attribute :director, if: Proc.new { |record, params| params && params[:admin] == true }
|
||||
end
|
||||
|
||||
class MovieOptionalRelationshipSerializer
|
||||
include FastJsonapi::ObjectSerializer
|
||||
set_type :movie
|
||||
attributes :name
|
||||
has_many :actors, if: Proc.new { |record| record.actors.any? }
|
||||
end
|
||||
|
||||
class MovieOptionalRelationshipWithParamsSerializer
|
||||
include FastJsonapi::ObjectSerializer
|
||||
set_type :movie
|
||||
attributes :name
|
||||
has_many :actors, if: Proc.new { |record, params| params && params[:admin] == true }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user