Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not render trashed elements #4

Merged
merged 1 commit into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/serializers/alchemy/json_api/page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class PageSerializer
belongs_to :language

has_many :elements
has_many :all_elements, record_type: :element, serializer: ElementSerializer

has_many :all_elements, record_type: :element, serializer: ElementSerializer do |page|
page.all_elements.select { |e| e.public? && !e.trashed? }
end

with_options if: ->(_, params) { params[:admin] == true } do
attribute :tag_list
Expand Down
7 changes: 5 additions & 2 deletions spec/serializers/alchemy/json_api/page_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,16 @@

describe "relationships" do
let(:element) { FactoryBot.create(:alchemy_element) }
let(:trashed_element) { FactoryBot.create(:alchemy_element, :trashed) }
subject { serializer.serializable_hash[:data][:relationships] }

before do
page.elements << element
page.all_elements << element
page.all_elements << trashed_element
trashed_element.trash!
end

it "has the right keys and values" do
it "has the right keys and values, and does not include trashed elements" do
expect(subject[:elements]).to eq(data: [{ id: element.id.to_s, type: :element }])
expect(subject[:all_elements]).to eq(data: [{ id: element.id.to_s, type: :element }])
expect(subject[:language]).to eq(data: { id: page.language_id.to_s, type: :language })
Expand Down