Skip to content

Commit

Permalink
Basic store access. Associations between stores and taxonomies, produ…
Browse files Browse the repository at this point in the history
…cts.
  • Loading branch information
bullrico committed Oct 9, 2008
1 parent 78dadb3 commit 08622c6
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 56 deletions.
6 changes: 5 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Individual stores are accessed like this:
<pre>
http://example.com/store1 and http://example.com/store2
</pre>
A partial is added to the users/new form for the store info (views use HAML/SASS)

Admin URL:
<pre>
http://example.com/admin
</pre>
Store will be read via a current_store method using session[:store_id], similar to current_user

4 changes: 3 additions & 1 deletion app/controllers/stores_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def index
end

def show
@store = current_store
@store = Store.find(params[:id])
self.current_store = @store
redirect_to products_path
end
end
13 changes: 5 additions & 8 deletions app/models/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ class UndefinedError < StandardError; end

has_many :users
has_many :products

has_many :taxonomies

validates_presence_of :name
validates_uniqueness_of :host

# From Altered Beast - an empty host field makes it the default store to display
def host=(value)
write_attribute :host, value.to_s.downcase
end
make_permalink :with => :name, :field => :permalink

def self.main
@main ||= find :first, :conditions => {:host => ''}
def self.default
@default ||= find :first, :conditions => {:host => ''}
end

def self.find_by_host(name)
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/overview/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%h1
= @store.name
admin panel
%p Welcome to the Overview Page. This is a placeholder for some real content. Eventually we envision a bunch of "widgets" summarizing recent activity and other elements of interest.
12 changes: 12 additions & 0 deletions app/views/products/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
%h1= @store.name

#product-listing
= breadcrumbs(@taxon)
%br
= render :partial => "shared/products.html.erb", :locals => {:products => @products }

- content_for :sidebar do

%td{ :id => "shop-by-col", :valign => "top"}
= render :partial => "shared/taxonomies"
= render :partial => 'shared/paginate', :locals => {:collection => @products, :options => {}} unless @products.empty?
3 changes: 0 additions & 3 deletions app/views/stores/show.html.haml

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/users/_store.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,3 @@
%td
= error_message_on :store, :name
= s.text_field :name
%tr
%td Host
%td
= error_message_on :store, :host
= s.text_field :host
10 changes: 10 additions & 0 deletions db/migrate/20081008030311_add_store_to_taxonomies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddStoreToTaxonomies < ActiveRecord::Migration
def self.up
add_column :taxonomies, :store_id, :integer
end

def self.down
remove_column :taxonomies, :store_id

end
end
39 changes: 35 additions & 4 deletions lib/store_system.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
module StoreSystem
module MultiStore
module StoreSystem

def current_store
@current_store ||= Store.find_by_host(request.host) or raise Store::UndefinedError
@current_store ||= (get_store_from_request || get_store_from_session || false)
end

end

def current_store=(new_store)
session[:store_id] = new_store.nil? ? nil : new_store.id
@current_store = new_store || :false
end

def get_store_from_session
self.current_store = Store.find(session[:store_id]) if session[:store_id]
end

def get_store_from_request
self.current_store = Store.find_by_param(params[:store_name])
end

def get_store_and_products
yield
# raise Store::UndefinedError unless @store = current_store
@store = current_store
@taxonomies = @store.taxonomies.find(:all, :include => {:root => :children})
@products = @store.products.find(:all)
end

def get_store_for_owner
@store = current_user.store if current_user.has_role?("admin")
self.current_store = @store
end

def get_store_products
@products = @store.products
end
end
end
85 changes: 51 additions & 34 deletions multi_store_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,77 @@

class MultiStoreExtension < Spree::Extension
version "1.0"
description "Implements multistore functionality"
url "http://yourwebsite.com/stores"
description "Adds multistore functionality"
url "http://yourwebsite.com/my_store"

define_routes do |map|
# map.namespace :admin do |admin|
# admin.resources :whatever
# end
# map.root :controller => "stores", :action => "index"
map.resources :stores
map.resources :stores
map.connect '/:store_name', :controller => 'products', :action => 'index', :id => :store_name
end

def activate

ApplicationController.class_eval do
include StoreSystem
User.class_eval do
belongs_to :store
end

Product.class_eval do
belongs_to :store
end

Taxonomy.class_eval do
belongs_to :store
end

ApplicationController.class_eval do
include MultiStore::StoreSystem

ProductsController.class_eval do
before_filter :get_store_and_products, :only => :index
before_filter :get_store_and_product, :only => :show

def get_store_and_products
@store = current_store
@products = @store.products
end

def get_store_and_product
@store = current_store
@product = @store.product.find(params[:id])
def instantiate_controller_and_action_names
@current_action = action_name
@current_controller = controller_name
end
end

ProductsController.class_eval do
around_filter :get_store_and_products

# rescue_from Store::UndefinedError do |exception|
# redirect_to stores_path
# end

end

Product.class_eval do
belongs_to :store
end

UsersController.class_eval do
before_filter :add_store_fields, :only => :new
before_filter :add_store, :only => [ :create ]

def add_store
store = @user.store.build(params[:store])
end


def add_store_fields
@extension_partials << 'store'
end

def create
@user = User.new(params[:user])
@store = @user.build_store(params[:store])
respond_to do |format|
if @user.save
# create an admin role and and assign the admin user to that role
@user.roles << Role.find_by_name('admin')
self.current_store = @store
format.html { redirect_to products_path}
else
format.html { render :action => "new" }
end
end
end
end

User.class_eval do
belongs_to :store
Admin::BaseController.class_eval do
before_filter :get_store_for_owner
end


Admin::ProductsController.class_eval do
before_filter :get_store_products
end

end

def deactivate
Expand Down

0 comments on commit 08622c6

Please sign in to comment.