Created
May 18, 2015 07:34
-
-
Save novohispano/166b3d79d26af9d94abc to your computer and use it in GitHub Desktop.
Multitenant Storedom
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item < ActiveRecord::Base | |
has_many :order_items | |
has_many :orders, through: :order_items | |
belongs_to :store | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stores::ItemsController < Stores::StoresController | |
def index | |
@items = current_store.items | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stores::OrdersController < Stores::StoresController | |
def index | |
@orders = current_store.orders | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<h1>Welcome to <%= current_store.name %></h1> | |
<% @orders.each do |order| %> | |
<div class="col-sm-12"> | |
<h2>Order <%= order.id %></h2> | |
<h5>Client: <%= order.user.name %></h5> | |
<h5>Order Items</h5> | |
<% order.items.each_with_index do |item, index| %> | |
<p> | |
<%= "#{index + 1}. #{item.name}" %> | |
</p> | |
<% end %> | |
</div> | |
<% end %> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
resources :items, only: [:index, :show] | |
resources :orders, only: [:index, :show] | |
resources :users, only: [:index, :show] | |
namespace :stores, path: ':store', as: :store do | |
resources :items, only: [:index, :show] | |
resources :orders, only: [:index, :show] | |
end | |
root 'stores#index' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Store < ActiveRecord::Base | |
has_many :items | |
has_many :orders | |
validates :url, uniqueness: true, presence: true | |
validates :name, presence: true | |
before_validation :generate_url | |
def generate_url | |
self.url = name.parameterize | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StoresController < ApplicationController | |
def index | |
@stores = Store.all | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Stores::StoresController < ApplicationController | |
before_action :store_not_found | |
helper_method :current_store | |
def current_store | |
@current_store ||= Store.find_by(url: params[:store]) | |
end | |
def store_not_found | |
redirect_to root_path unless current_store | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<h1>Welcome to <%= current_store.name %></h1> | |
<% @items.each do |item| %> | |
<div class="col-sm-3"> | |
<h5><%= item.name %></h5> | |
<%= link_to(image_tag(item.image_url), item_path(item)) %> | |
<p> | |
<%= item.description %> | |
</p> | |
</div> | |
<% end %> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment