Skip to content

Commit

Permalink
Use Rails standards for deleting pages from EssencePage (AlchemyCMS#1833
Browse files Browse the repository at this point in the history
)

Rails offers ways for setting relations, let's use them.
mamhoff authored May 18, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 2cb3228 commit 3e4d476
Showing 3 changed files with 1 addition and 54 deletions.
13 changes: 0 additions & 13 deletions app/models/alchemy/essence_page.rb
Original file line number Diff line number Diff line change
@@ -2,8 +2,6 @@

module Alchemy
class EssencePage < BaseRecord
PAGE_ID = /\A\d+\z/

acts_as_essence(
ingredient_column: :page,
preview_text_method: :name,
@@ -14,16 +12,5 @@ class EssencePage < BaseRecord
optional: true,
},
)

def ingredient=(page)
case page
when PAGE_ID
self.page = Alchemy::Page.new(id: page)
when Alchemy::Page
self.page = page
else
super
end
end
end
end
2 changes: 1 addition & 1 deletion app/views/alchemy/essences/_essence_page_editor.html.erb
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
data: essence_page_editor.data_attributes do %>
<%= content_label(essence_page_editor) %>
<%= text_field_tag(
essence_page_editor.form_field_name,
essence_page_editor.form_field_name("page_id"),
essence_page_editor.essence.page_id,
id: essence_page_editor.form_field_id,
class: 'alchemy_selectbox full_width'
40 changes: 0 additions & 40 deletions spec/models/alchemy/essence_page_spec.rb
Original file line number Diff line number Diff line change
@@ -18,44 +18,4 @@
expect(essences[0].association(:ingredient_association)).to be_loaded
end
end

describe "ingredient=" do
subject(:ingredient) { essence.page }

context "when String value is only a number" do
let(:value) { "101" }

before do
essence.ingredient = value
end

it "sets page to an page instance with that id" do
is_expected.to be_a(Alchemy::Page)
expect(ingredient.id).to eq(101)
end
end

context "when value is an Alchemy Page" do
let(:value) { page }

before do
essence.ingredient = value
end

it "sets page to an page instance with that id" do
is_expected.to be_a(Alchemy::Page)
expect(ingredient).to eq(page)
end
end

context "when value is not only a number" do
let(:value) { "page1" }

it do
expect {
essence.ingredient = value
}.to raise_error(ActiveRecord::AssociationTypeMismatch)
end
end
end
end

0 comments on commit 3e4d476

Please sign in to comment.