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

Remove Site ID from nodes #1807

Merged
merged 2 commits into from
May 6, 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
2 changes: 0 additions & 2 deletions app/controllers/alchemy/admin/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ def index

def new
@node = Node.new(
site: Alchemy::Site.current,
parent_id: params[:parent_id],
language: @current_language,
)
Expand All @@ -21,7 +20,6 @@ def new

def resource_params
params.require(:node).permit(
:site_id,
:parent_id,
:language_id,
:page_id,
Expand Down
3 changes: 2 additions & 1 deletion app/models/alchemy/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class Node < BaseRecord
acts_as_nested_set scope: "language_id", touch: true
stampable stamper_class_name: Alchemy.user_class_name

belongs_to :site, class_name: "Alchemy::Site"
mamhoff marked this conversation as resolved.
Show resolved Hide resolved
belongs_to :language, class_name: "Alchemy::Language"
belongs_to :page, class_name: "Alchemy::Page", optional: true, inverse_of: :nodes

has_one :site, through: :language

has_many :essence_nodes, class_name: "Alchemy::Node", inverse_of: :node

validates :name, presence: true, if: -> { page.nil? }
Expand Down
4 changes: 1 addition & 3 deletions app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,8 @@ def set_published_at
end

def attach_to_menu!
current_site_id = Alchemy::Site.current.id
node = Alchemy::Node.find_by!(id: menu_id, site_id: current_site_id)
node = Alchemy::Node.find_by!(id: menu_id, language_id: language_id)
node.children.create!(
site_id: current_site_id,
language_id: language_id,
page_id: id,
name: name,
Expand Down
1 change: 0 additions & 1 deletion app/views/alchemy/admin/nodes/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<%= f.input :external %>
<%= f.hidden_field :parent_id %>
<% end %>
<%= f.hidden_field :site_id %>
<%= f.hidden_field :language_id %>
<%= f.submit button_label %>
<% end %>
Expand Down
28 changes: 28 additions & 0 deletions db/migrate/20200504210159_remove_site_id_from_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true
class RemoveSiteIdFromNodes < ActiveRecord::Migration[5.2]
def up
remove_foreign_key :alchemy_nodes, :alchemy_sites
remove_index :alchemy_nodes, :site_id
remove_column :alchemy_nodes, :site_id, :integer, null: false
end

def down
add_column :alchemy_nodes, :site_id, :integer, null: true
sql = <<~SQL
UPDATE alchemy_nodes
SET site_id = (
SELECT alchemy_languages.site_id FROM alchemy_languages WHERE alchemy_nodes.language_id = alchemy_languages.id
) WHERE
EXISTS (
SELECT *
FROM alchemy_languages
WHERE alchemy_languages.id = alchemy_nodes.language_id
);
SQL

connection.execute(sql)
change_column :alchemy_nodes, :site_id, :integer, null: false
add_index :alchemy_nodes, :site_id
add_foreign_key :alchemy_nodes, :alchemy_sites, column: :site_id
end
end
1 change: 0 additions & 1 deletion lib/alchemy/test_support/factories/node_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

FactoryBot.define do
factory :alchemy_node, class: "Alchemy::Node" do
site { Alchemy::Site.default || create(:alchemy_site) }
language { Alchemy::Language.default || create(:alchemy_language) }
name { "A Node" }

Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/alchemy/admin/nodes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module Alchemy

it "creates node and redirects to index" do
expect {
post :create, params: { node: { name: "Node", language_id: language.id, site_id: language.site_id } }
post :create, params: { node: { name: "Node", language_id: language.id } }
}.to change { Alchemy::Node.count }.by(1)
expect(response).to redirect_to(admin_nodes_path)
end
Expand Down
5 changes: 1 addition & 4 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_04_23_073425) do
ActiveRecord::Schema.define(version: 2020_05_04_210159) do

create_table "alchemy_attachments", force: :cascade do |t|
t.string "name"
Expand Down Expand Up @@ -228,14 +228,12 @@
t.integer "updater_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "site_id", null: false
t.index ["creator_id"], name: "index_alchemy_nodes_on_creator_id"
t.index ["language_id"], name: "index_alchemy_nodes_on_language_id"
t.index ["lft"], name: "index_alchemy_nodes_on_lft"
t.index ["page_id"], name: "index_alchemy_nodes_on_page_id"
t.index ["parent_id"], name: "index_alchemy_nodes_on_parent_id"
t.index ["rgt"], name: "index_alchemy_nodes_on_rgt"
t.index ["site_id"], name: "index_alchemy_nodes_on_site_id"
t.index ["updater_id"], name: "index_alchemy_nodes_on_updater_id"
end

Expand Down Expand Up @@ -371,5 +369,4 @@
add_foreign_key "alchemy_essence_pages", "alchemy_pages", column: "page_id"
add_foreign_key "alchemy_nodes", "alchemy_languages", column: "language_id"
add_foreign_key "alchemy_nodes", "alchemy_pages", column: "page_id", on_delete: :cascade
add_foreign_key "alchemy_nodes", "alchemy_sites", column: "site_id", on_delete: :cascade
end
2 changes: 1 addition & 1 deletion spec/features/admin/menus_features_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
select "Main Menu", from: "Name"
click_button "create"

expect(node.site_id).to eq(default_site.id)
expect(node.site).to eq(default_site)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/helpers/alchemy/pages_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ module Alchemy

context "with multiple sites" do
let!(:site_2) { create(:alchemy_site, host: "another-site.com") }
let!(:menu) { create(:alchemy_node, name: name, site: Alchemy::Site.current) }
let!(:menu) { create(:alchemy_node, name: name, language: Alchemy::Language.current) }
let!(:node) { create(:alchemy_node, parent: menu, url: "/default-site") }
let!(:menu_2) { create(:alchemy_node, name: name, site: site_2) }
let!(:node_2) { create(:alchemy_node, parent: menu_2, site: site_2, url: "/site-2") }
let!(:menu_2) { create(:alchemy_node, name: name, language: klingon) }
let!(:node_2) { create(:alchemy_node, parent: menu_2, language: klingon, url: "/site-2") }

it "renders menu from current site" do
is_expected.to have_selector('ul.nav > li.nav-item > a.nav-link[href="/default-site"]')
Expand Down