Skip to content

Commit

Permalink
make pages store dependend.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterberkenbosch committed Sep 26, 2014
1 parent 2550e50 commit 2e5d4a4
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/controllers/spree/static_content_controller.rb
Expand Up @@ -5,7 +5,7 @@ class Spree::StaticContentController < Spree::StoreController
layout :determine_layout

def show
@page = Spree::Page.visible.find_by_slug!(request.path)
@page = Spree::Page.by_store(current_store).visible.find_by_slug!(request.path)
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/models/spree/page.rb
@@ -1,6 +1,8 @@
class Spree::Page < ActiveRecord::Base
default_scope -> { order("position ASC") }

has_and_belongs_to_many :stores, :join_table => 'spree_pages_stores'

validates_presence_of :title
validates_presence_of [:slug, :body], :if => :not_using_foreign_link?
validates_presence_of :layout, :if => :render_layout_as_partial?
Expand All @@ -13,6 +15,8 @@ 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) }

before_save :update_positions_and_slug

def initialize(*args)
Expand Down
10 changes: 9 additions & 1 deletion app/views/spree/admin/pages/_form.html.erb
Expand Up @@ -62,7 +62,15 @@
<%= f.label :render_layout_as_partial %>
<% end %>
<%= f.field_container :stores do %>
<%= f.label :stores, Spree.t(:stores)%><br />
<% Spree::Store.all.each do |store| %>
<%= check_box_tag "page[store_ids][]", store.id, @page.stores.include?(store) %> <%= store.name %>
<% end %>
<% end %>

</div>

<div data-hook="admin_page_form_meta" class="alpha omega twelve columns">

<%= f.field_container :meta_title do %>
Expand All @@ -87,4 +95,4 @@
<div class="clear"></div>
<div data-hook="admin_page_form_additional_fields"></div>
<div class="clear"></div>
</div>
</div>
@@ -1,3 +1,3 @@
<nav id="footer-pages">
<ul><%= render :partial => "spree/static_content/static_content_list", :collection => Spree::Page.visible.footer_links, :as => :page %></ul>
</nav>
<ul><%= render :partial => "spree/static_content/static_content_list", :collection => Spree::Page.by_store(current_store).visible.footer_links, :as => :page %></ul>
</nav>
@@ -1 +1 @@
<%= render :partial => "spree/static_content/static_content_list", :collection => Spree::Page.visible.header_links, :as => :page %>
<%= render :partial => "spree/static_content/static_content_list", :collection => Spree::Page.by_store(current_store).visible.header_links, :as => :page %>
@@ -1,4 +1,4 @@
<% if Spree::Page.visible.sidebar_links.any? %>
<% if Spree::Page.by_store(current_store).visible.sidebar_links.any? %>
<nav id="pages" class="sidebar-item">
<h6 class="pages-root"><%= Spree.t(:pages) %></h6>
<ul class="pages-list"><%= render :partial => "spree/static_content/static_content_list", :collection => Spree::Page.visible.sidebar_links, :as => :page %></ul>
Expand Down
14 changes: 14 additions & 0 deletions db/migrate/20140926121757_add_pages_stores.rb
@@ -0,0 +1,14 @@
class AddPagesStores < ActiveRecord::Migration

def change
create_table :spree_pages_stores, :id => false do |t|
t.integer :store_id
t.integer :page_id
t.timestamps
end

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

end
end
14 changes: 9 additions & 5 deletions spec/controllers/static_content_controller_spec.rb
@@ -1,25 +1,29 @@
require 'spec_helper'

describe Spree::StaticContentController do
before { controller.stub spree_current_user: nil }
before do
controller.stub spree_current_user: nil
end

let!(:store) { create(:store, default: true) }

context '#show' do
it 'accepts path as root' do
page = create(:page, slug: '/')
page = create(:page, slug: '/', stores: [store])
controller.request.stub(:path).and_return(page.slug)
spree_get :show, path: page.slug
expect(response).to be_success
end

it 'accepts path as string' do
page = create(:page, slug: 'hello')
page = create(:page, slug: 'hello', stores: [store])
controller.request.stub(:path).and_return(page.slug)
spree_get :show, path: page.slug
expect(response).to be_success
end

it 'accepts path as nested' do
page = create(:page, slug: 'aa/bb/cc')
page = create(:page, slug: 'aa/bb/cc', stores: [store])
controller.request.stub(:path).and_return(page.slug)
spree_get :show, path: page.slug
expect(response).to be_success
Expand All @@ -30,4 +34,4 @@
expect(response.response_code).to eq(404)
end
end
end
end
23 changes: 13 additions & 10 deletions spec/features/static_content_spec.rb
@@ -1,42 +1,45 @@
require 'spec_helper'

feature 'Static Content Page', js: true do

let!(:store) { create(:store, default: true) }

context 'render page' do
scenario 'is a query string' do
create(:page, slug: '/page', title: 'Query Test')
create(:page, slug: '/page', title: 'Query Test', stores: [store])
visit '/page?test'
expect(page).to have_text 'Query Test'
end

scenario 'can have slug not starting by /' do
create(:page, slug: 'page2', title: 'No Slash Prefix Test')
create(:page, slug: 'page2', title: 'No Slash Prefix Test', stores: [store])
visit '/page2'
expect(page).to have_text 'No Slash Prefix Test'
end

scenario 'can have slug with multiple /' do
create(:page, slug: '/hello/shoppers/page3', title: 'Multiple Slash Test')
create(:page, slug: '/hello/shoppers/page3', title: 'Multiple Slash Test', stores: [store])
visit '/hello/shoppers/page3'
expect(page).to have_text 'Multiple Slash Test'
end

scenario 'can be a custom root page' do
create(:page, slug: '/', title: 'Root Page Test')
create(:page, slug: '/', title: 'Root Page Test', stores: [store])
visit '/'
expect(page).to have_text 'Root Page Test'
end

scenario 'is limited within its own constraints' do
create(:page, slug: '/t/categories/page3', title: 'Constraint Test')
create(:page, slug: '/t/categories/page3', title: 'Constraint Test', stores: [store])
visit '/t/categories/page3'
expect(page).not_to have_text 'Constraint Test'
end

scenario 'fetch correct page' do
create(:page, slug: '/')
create(:page, slug: 'hello', title: 'Hello')
create(:page, slug: 'somwhere')
create(:page, :with_foreign_link, slug: 'whatever')
create(:page, slug: '/', stores: [store])
create(:page, slug: 'hello', title: 'Hello', stores: [store])
create(:page, slug: 'somwhere', stores: [store])
create(:page, :with_foreign_link, slug: 'whatever', stores: [store])
visit '/hello'
expect(page).to have_text 'Hello'
end
Expand All @@ -47,4 +50,4 @@
expect(page).to have_text product.name
end
end
end
end
20 changes: 19 additions & 1 deletion spec/models/page_spec.rb
Expand Up @@ -25,4 +25,22 @@
expect(page.link).to eq page.foreign_link
end
end
end


context "pages in stores" do

before(:each) do
@store = create(:store)
@page = create(:page, :stores => [@store])
@page2 = create(:page)
end

it 'should correctly find pages by store' do
pages_by_store = Spree::Page.by_store(@store)
expect(pages_by_store).to include(@page)
expect(pages_by_store).to_not include(@page2)
end

end

end

0 comments on commit 2e5d4a4

Please sign in to comment.