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.
  • Loading branch information
mamhoff committed May 18, 2020
1 parent 2cb3228 commit 3e4d476
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 54 deletions.
13 changes: 0 additions & 13 deletions app/models/alchemy/essence_page.rb
Expand Up @@ -2,8 +2,6 @@

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

acts_as_essence(
ingredient_column: :page,
preview_text_method: :name,
Expand All @@ -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
Expand Up @@ -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'
Expand Down
40 changes: 0 additions & 40 deletions spec/models/alchemy/essence_page_spec.rb
Expand Up @@ -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.