From 08622c6d3f27e3f0e760dad28270469576929a4c Mon Sep 17 00:00:00 2001 From: Bobby Santiago Date: Thu, 9 Oct 2008 11:23:55 +0800 Subject: [PATCH] Basic store access. Associations between stores and taxonomies, products. --- README.markdown | 6 +- app/controllers/stores_controller.rb | 4 +- app/models/store.rb | 13 ++- app/views/admin/overview/index.html.haml | 4 + app/views/products/index.html.haml | 12 +++ app/views/stores/show.html.haml | 3 - app/views/users/_store.html.haml | 5 -- .../20081008030311_add_store_to_taxonomies.rb | 10 +++ lib/store_system.rb | 39 ++++++++- multi_store_extension.rb | 85 +++++++++++-------- 10 files changed, 125 insertions(+), 56 deletions(-) create mode 100644 app/views/admin/overview/index.html.haml create mode 100644 app/views/products/index.html.haml delete mode 100644 app/views/stores/show.html.haml create mode 100644 db/migrate/20081008030311_add_store_to_taxonomies.rb diff --git a/README.markdown b/README.markdown index 3d4c623..338be57 100644 --- a/README.markdown +++ b/README.markdown @@ -6,6 +6,10 @@ Individual stores are accessed like this:
 http://example.com/store1 and http://example.com/store2
 
-A partial is added to the users/new form for the store info (views use HAML/SASS) +Admin URL: +
+http://example.com/admin
+
+Store will be read via a current_store method using session[:store_id], similar to current_user diff --git a/app/controllers/stores_controller.rb b/app/controllers/stores_controller.rb index 9f04e76..7a03587 100644 --- a/app/controllers/stores_controller.rb +++ b/app/controllers/stores_controller.rb @@ -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 diff --git a/app/models/store.rb b/app/models/store.rb index 19de981..01a896c 100644 --- a/app/models/store.rb +++ b/app/models/store.rb @@ -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) diff --git a/app/views/admin/overview/index.html.haml b/app/views/admin/overview/index.html.haml new file mode 100644 index 0000000..4303970 --- /dev/null +++ b/app/views/admin/overview/index.html.haml @@ -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. diff --git a/app/views/products/index.html.haml b/app/views/products/index.html.haml new file mode 100644 index 0000000..60db9df --- /dev/null +++ b/app/views/products/index.html.haml @@ -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? \ No newline at end of file diff --git a/app/views/stores/show.html.haml b/app/views/stores/show.html.haml deleted file mode 100644 index 7308ff2..0000000 --- a/app/views/stores/show.html.haml +++ /dev/null @@ -1,3 +0,0 @@ -%h1= @store.name -- for product in @store.products - %p= product.name \ No newline at end of file diff --git a/app/views/users/_store.html.haml b/app/views/users/_store.html.haml index b7a36a7..f9c34ca 100644 --- a/app/views/users/_store.html.haml +++ b/app/views/users/_store.html.haml @@ -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 diff --git a/db/migrate/20081008030311_add_store_to_taxonomies.rb b/db/migrate/20081008030311_add_store_to_taxonomies.rb new file mode 100644 index 0000000..13c15c1 --- /dev/null +++ b/db/migrate/20081008030311_add_store_to_taxonomies.rb @@ -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 diff --git a/lib/store_system.rb b/lib/store_system.rb index 5ab7838..5aa0f12 100644 --- a/lib/store_system.rb +++ b/lib/store_system.rb @@ -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 \ No newline at end of file diff --git a/multi_store_extension.rb b/multi_store_extension.rb index 7b9d3c5..9d98df3 100644 --- a/multi_store_extension.rb +++ b/multi_store_extension.rb @@ -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