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 include deprecated elements and essences #26

Merged
merged 2 commits into from
Jan 18, 2021
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
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ gemspec
# gem 'byebug', group: [:development, :test]
gem "sqlite3"

gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: "master"
gem "alchemy-devise", github: "AlchemyCMS/alchemy-devise", branch: "master"
gem "alchemy_cms", github: "AlchemyCMS/alchemy_cms", branch: "main"
gem "alchemy-devise", github: "AlchemyCMS/alchemy-devise", branch: "main"
gem "rufo"
gem "rubocop"
gem "pry-byebug"
2 changes: 1 addition & 1 deletion app/serializers/alchemy/json_api/element_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ElementSerializer
belongs_to :page, record_type: :page, serializer: ::Alchemy::JsonApi::PageSerializer

has_many :essences, polymorphic: true do |element|
element.contents.map(&:essence)
element.contents.reject { |c| !!c.try(:deprecated?) }.map!(&:essence)
end

has_many :nested_elements, record_type: :element, serializer: self
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/alchemy/json_api/page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PageSerializer
has_many :fixed_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer

has_many :all_elements, record_type: :element, serializer: ::Alchemy::JsonApi::ElementSerializer do |page|
page.all_elements.select { |e| e.public? && !e.trashed? }
page.all_elements.select { |e| e.public? && !e.trashed? && !e.try(:deprecated?) }
end

with_options if: ->(_, params) { params[:admin] == true } do
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/config/alchemy/elements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
- name: essence_html
type: EssenceHtml
hint: true
deprecated: true
- name: essence_link
type: EssenceLink
hint: true
Expand Down Expand Up @@ -160,3 +161,6 @@
fixed: true
unique: true
nestable_elements: [text]

- name: old
deprecated: true
25 changes: 24 additions & 1 deletion spec/requests/alchemy/json_api/pages_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,34 @@
context "when including elements and essences" do
let(:page) { FactoryBot.create(:alchemy_page, :public, elements: [element]) }
let(:element) { FactoryBot.create(:alchemy_element, name: "article", autogenerate_contents: true) }
let(:included) { JSON.parse(response.body)["included"] }

it "includes the data" do
get alchemy_json_api.page_path(page, include: "all_elements.essences")
included = JSON.parse(response.body)["included"]
expect(included).to include(have_type("element").and(have_id(element.id.to_s)))
expect(included.length).to eq(5)
end

context "if deprecated elements are present" do
let!(:deprecated_element) do
FactoryBot.create(:alchemy_element, page: page, name: "old", autogenerate_contents: true)
end

it "returns only public elements" do
get alchemy_json_api.page_path(page, include: "all_elements.essences")
expect(included.length).to eq(5)
end
end

context "if deprecated contents in public elements are present" do
let!(:deprecated_element) do
FactoryBot.create(:alchemy_element, page: page, name: "all_you_can_eat", autogenerate_contents: true)
end

it "returns only public essences" do
get alchemy_json_api.page_path(page, include: "all_elements.essences")
expect(included.length).to eq(14)
end
end
end

Expand Down