Forum: Ruby on Rails Ecommerce site w/multiple subdomains

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8117d5394c8e517529feed4b66ae3075?d=identicon&s=25 Wayne Molina (waynem)
on 2008-12-01 20:28
(Received via mailing list)
My company is looking at redoing our legacy Classic ASP storefront,
and I'm thinking about doing it in Rails since the alternative is to
use a PHP-based solution called Magento that, while it looks nice, is
insanely abstract to the point of making me go blind (it uses a
database design principle called Entity-Attribute-Value or EAV which
basically is to keep database tables like hashmaps that can contain an
infinite number of values).

The principle requirement of this site is to be able to filter certain
products based on the subdomain, since we want to have different
"brands".  The products aren't specific to this subdomain, but the
subdomain only shows a subset of products.

For example:

www.mysite.com: displays all products with categories/subcategories
(i.e. no filtering)
furniture.mysite.com: displays only products/subcategories with a
parent category of furniture
green.mysite.com: displays only products/categories/subcategories with
a (boolean) flag recycled = true

How would I go about doing this?  The only way I can think would be to
have separate controllers for each "store", for example:

# Main store i.e. no subdomain
class StoreController < ApplicationController
  def index
    @products = Products.find(:all)
  end
end

# furniture.mysite.com
class FurnitureStoreController < ApplicationController
  def index
    @products = Products.find_all_by_category('Furniture')
  end
end

# green.mysite.com
class GreenStoreController < ApplicationController
  def index
    @products = Products.find_all_by_recycled(true)
  end
end

But that seems rather unweildy.  This is a critical business
requirement since we want to create different stores with their own
unique look and feel, but the actual data displayed comes from one
master database and needs to be filtered appropriately based on the
store the user is browsing.

Another critical requirement we have is that each individual product
can have up to four prices, and the price is chosen based on the
"store" the user is viewing it on (e.g. Product A is $15.00 on the
mysite.com, but $12.00 on furniture.mysite.com).  I'm not sure what
the best way to tackle that problem would be.

Any assistance with either or both of these issues would be greatly
appreciated!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-01 21:05
(Received via mailing list)
On Dec 1, 7:27 pm, Wayne M <wayne.mol...@gmail.com> wrote:
>
> But that seems rather unweildy.  This is a critical business
> requirement since we want to create different stores with their own
> unique look and feel, but the actual data displayed comes from one
> master database and needs to be filtered appropriately based on the
> store the user is browsing.
>
Just the first thing that springs to mind.

in your product class do

named_scope :green, ...
named_scope :recycled, ...

def self.scope_for_subdomain(subdomain)
  send case subdomain
  when 'green' then :green
  when 'recycled' then :recycled
  else
    :all
  end
end

and then your index action is

Product.scope_for_subdomain(request.subdomains.first).find :all
You could also just use anonymous scopes

Depending on what's in your app it might be better to pull the
selecting of the scope out into a before filter.

> Another critical requirement we have is that each individual product
> can have up to four prices, and the price is chosen based on the
> "store" the user is viewing it on (e.g. Product A is $15.00 on the
> mysite.com, but $12.00 on furniture.mysite.com).  I'm not sure what
> the best way to tackle that problem would be.
>
Again a random thought: products have a base price, and you have a
price_overrides table (products has_many price_overrides etc...)
Assuming you have Store objects describing the stores and a filter
that sets the current store you could have a method in your product
class similar to

def overriden_price(store)
  override = price_overrides.find_by_store_id store
  override ? override.price : self.price
end

and when you want to display the price to a user you just do

product.overriden_price(@current_store)

Fred
This topic is locked and can not be replied to.