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),
|
serializer: compute_serializer_name(options[:serializer] || base_key_sym),
|
||||||
relationship_type: relationship_type,
|
relationship_type: relationship_type,
|
||||||
cached: options[:cached] || false,
|
cached: options[:cached] || false,
|
||||||
polymorphic: fetch_polymorphic_option(options)
|
polymorphic: fetch_polymorphic_option(options),
|
||||||
|
conditional_proc: options[:if]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -82,11 +82,14 @@ module FastJsonapi
|
|||||||
relationships = relationships_to_serialize if relationships.nil?
|
relationships = relationships_to_serialize if relationships.nil?
|
||||||
|
|
||||||
relationships.each_with_object({}) do |(_k, relationship), hash|
|
relationships.each_with_object({}) do |(_k, relationship), hash|
|
||||||
name = relationship[:key]
|
conditional_proc = relationship[:conditional_proc]
|
||||||
empty_case = relationship[:relationship_type] == :has_many ? [] : nil
|
if conditional_proc.blank? || conditional_proc.call(record, params)
|
||||||
hash[name] = {
|
name = relationship[:key]
|
||||||
data: ids_hash_from_record_and_relationship(record, relationship, params) || empty_case
|
empty_case = relationship[:relationship_type] == :has_many ? [] : nil
|
||||||
}
|
hash[name] = {
|
||||||
|
data: ids_hash_from_record_and_relationship(record, relationship, params) || empty_case
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -341,4 +341,33 @@ describe FastJsonapi::ObjectSerializer do
|
|||||||
expect(serializable_hash['data']['attributes'].has_key?('director')).to be_falsey
|
expect(serializable_hash['data']['attributes'].has_key?('director')).to be_falsey
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
@ -292,6 +292,20 @@ RSpec.shared_context 'movie class' do
|
|||||||
attributes :name
|
attributes :name
|
||||||
attribute :director, if: Proc.new { |record, params| params && params[:admin] == true }
|
attribute :director, if: Proc.new { |record, params| params && params[:admin] == true }
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user