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 has_and_belongs_to_many relation #27

Closed
Closed
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
5 changes: 3 additions & 2 deletions app/models/spree/page.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Spree::Page < ActiveRecord::Base
default_scope -> { order("position ASC") }

has_and_belongs_to_many :stores, :join_table => 'spree_pages_stores'
has_many :page_stores, dependent: :destroy
has_many :stores, through: :page_stores

validates_presence_of :title
validates_presence_of [:slug, :body], :if => :not_using_foreign_link?
Expand All @@ -15,7 +16,7 @@ class Spree::Page < ActiveRecord::Base
scope :footer_links, -> { where(:show_in_footer => true).visible }
scope :sidebar_links, -> { where(:show_in_sidebar => true).visible }

scope :by_store, lambda { |store| joins(:stores).where("spree_pages_stores.store_id = ?", store) }
scope :by_store, lambda { |store| joins(:stores).where("spree_page_stores.store_id = ?", store) }

before_save :update_positions_and_slug

Expand Down
6 changes: 6 additions & 0 deletions app/models/spree/page_store.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Spree
class PageStore < Spree::Base
belongs_to :page
belongs_to :store
end
end
10 changes: 10 additions & 0 deletions app/models/spree/store_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module StoreDecorator
extend ActiveSupport::Concern

included do
has_many :store_pages, dependent: :destroy
has_many :pages, through: :store_pages
end
end

Spree::Store.include StoreDecorator
2 changes: 0 additions & 2 deletions db/migrate/20140926121757_add_pages_stores.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class AddPagesStores < SolidusSupport::Migration[4.2]

def change
create_table :spree_pages_stores, :id => false do |t|
t.integer :store_id
Expand All @@ -9,6 +8,5 @@ def change

add_index :spree_pages_stores, :store_id
add_index :spree_pages_stores, :page_id

end
end
5 changes: 5 additions & 0 deletions db/migrate/20190512074939_rename_pages_stores_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RenamePagesStoresTable < SolidusSupport::Migration[4.2]
def change
rename_table :spree_pages_stores, :spree_page_stores
end
end