diff --git a/app/controllers/alchemy/admin/nodes_controller.rb b/app/controllers/alchemy/admin/nodes_controller.rb index 452a7eae06..b47b6b5b06 100644 --- a/app/controllers/alchemy/admin/nodes_controller.rb +++ b/app/controllers/alchemy/admin/nodes_controller.rb @@ -11,7 +11,6 @@ def index def new @node = Node.new( - site: Alchemy::Site.current, parent_id: params[:parent_id], language: @current_language, ) @@ -21,7 +20,6 @@ def new def resource_params params.require(:node).permit( - :site_id, :parent_id, :language_id, :page_id, diff --git a/app/models/alchemy/node.rb b/app/models/alchemy/node.rb index c4a120b9a2..6965b72fb4 100644 --- a/app/models/alchemy/node.rb +++ b/app/models/alchemy/node.rb @@ -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" 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? } diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index 93807c0754..f72f4478cb 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -571,10 +571,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, diff --git a/app/views/alchemy/admin/nodes/_form.html.erb b/app/views/alchemy/admin/nodes/_form.html.erb index dca2a49c03..fc54cb7cf1 100644 --- a/app/views/alchemy/admin/nodes/_form.html.erb +++ b/app/views/alchemy/admin/nodes/_form.html.erb @@ -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 %> diff --git a/db/migrate/20200504210159_remove_site_id_from_nodes.rb b/db/migrate/20200504210159_remove_site_id_from_nodes.rb new file mode 100644 index 0000000000..629056023b --- /dev/null +++ b/db/migrate/20200504210159_remove_site_id_from_nodes.rb @@ -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 diff --git a/lib/alchemy/test_support/factories/node_factory.rb b/lib/alchemy/test_support/factories/node_factory.rb index 13cc1292ea..6a168f8eae 100644 --- a/lib/alchemy/test_support/factories/node_factory.rb +++ b/lib/alchemy/test_support/factories/node_factory.rb @@ -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" } diff --git a/spec/controllers/alchemy/admin/nodes_controller_spec.rb b/spec/controllers/alchemy/admin/nodes_controller_spec.rb index b20f12ec21..a2793626e5 100644 --- a/spec/controllers/alchemy/admin/nodes_controller_spec.rb +++ b/spec/controllers/alchemy/admin/nodes_controller_spec.rb @@ -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 diff --git a/spec/dummy/db/migrate/20200504210159_remove_site_id_from_nodes.rb b/spec/dummy/db/migrate/20200504210159_remove_site_id_from_nodes.rb new file mode 120000 index 0000000000..6c80fe7846 --- /dev/null +++ b/spec/dummy/db/migrate/20200504210159_remove_site_id_from_nodes.rb @@ -0,0 +1 @@ +../../../../db/migrate/20200504210159_remove_site_id_from_nodes.rb \ No newline at end of file diff --git a/spec/dummy/db/schema.rb b/spec/dummy/db/schema.rb index 8b70f9e34e..3df25bb3e4 100644 --- a/spec/dummy/db/schema.rb +++ b/spec/dummy/db/schema.rb @@ -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" @@ -193,14 +193,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 @@ -340,5 +338,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 diff --git a/spec/features/admin/menus_features_spec.rb b/spec/features/admin/menus_features_spec.rb index 5fec88c4a3..6ffcb61479 100644 --- a/spec/features/admin/menus_features_spec.rb +++ b/spec/features/admin/menus_features_spec.rb @@ -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 diff --git a/spec/helpers/alchemy/pages_helper_spec.rb b/spec/helpers/alchemy/pages_helper_spec.rb index f5e33af3ab..c932c6ceec 100644 --- a/spec/helpers/alchemy/pages_helper_spec.rb +++ b/spec/helpers/alchemy/pages_helper_spec.rb @@ -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"]')