Permalink
Browse files

Merge pull request #8 from railsdog/topic/seeds-edge

Forward ports hybridindie/spree_landlord#19 to master
  • Loading branch information...
2 parents b79eb25 + ea570e4 commit 52178ce21d11907baea25a431de65303261558f3 @jsqu99 jsqu99 committed Feb 8, 2013
View
@@ -5,6 +5,8 @@ class Tenant < ActiveRecord::Base
before_validation :downcase_shortname
before_validation :ensure_name_is_present
+ after_create :seed_tenant
+
['domain', 'shortname'].each do |attrib|
validates attrib.to_sym, uniqueness: true, presence: true
end
@@ -29,17 +31,25 @@ def current_tenant_id
end
def set_current_tenant( tenant )
- # able to handle tenant obj or tenant_id
- case tenant
- when Tenant then tenant_id = tenant.id
- when Integer then
- tenant_id = tenant
- unless Spree::Tenant.exists?(tenant_id)
- raise Spree::SpreeLandlord::TenantNotFound, "No tenant found with id = #{tenant_id}"
- end
- else
- raise ArgumentError, "invalid tenant object or id"
- end # case
+ tenant_id = nil
+
+ if tenant.present?
+ # able to handle tenant obj or tenant_id
+ case tenant
+ when Tenant then tenant_id = tenant.id
+ when Integer then
+ tenant_id = tenant
+ unless Spree::Tenant.exists?(tenant_id)
+ raise Spree::SpreeLandlord::TenantNotFound, "No tenant found with id = #{tenant_id}"
+ end
+ else
+ raise ArgumentError, "invalid tenant object or id"
+ end # case
+ else
+ tenant_id = Spree::Tenant.master.id
+ end
+
+ raise "Invalid tenant id" if tenant_id.nil?
Thread.current[:tenant_id] = tenant_id
end
@@ -56,5 +66,9 @@ def ensure_name_is_present
self.name ||= self.shortname.humanize.titleize if self.shortname.present?
end
end
+
+ def seed_tenant
+ Spree::SpreeLandlord::TenantSeeder.new(self).seed
+ end
end
end
@@ -0,0 +1,9 @@
+Spree::Zone.class_eval do
+ # ugly hack to remove the uniqueness validator on name
+ _validate_callbacks.reject! do |callback|
+ callback.raw_filter.kind_of?(ActiveRecord::Validations::UniquenessValidator) &&
+ callback.raw_filter.attributes == [:name]
+ end
+
+ validates :name, :uniqueness => { :scope => :tenant_id }
+end
@@ -10,4 +10,4 @@ def change
Spree::SpreeLandlord::TenantBootstrapper.new.run
end
-end
+end
@@ -4,11 +4,19 @@ class TenantBootstrapper
attr_reader :first_tenant
def run
+ clear_column_cache
create_first_tenant
create_tenants_admin_role
notify
end
+ def clear_column_cache
+ Spree::Tenant.connection.schema_cache.clear!
+ Spree::Landlord.model_names.each do |model|
+ model.reset_column_information
+ end
+ end
+
def create_first_tenant
@first_tenant = Spree::Tenant.new
@first_tenant.domain = "#{dns_friendly_app_name}.dev"

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -100,7 +100,14 @@ namespace :db do
Spree::SpreeLandlord::TenantMigrator.new.move_unassigned_to_master
end
- task :migrate => :reset_column_information
+ task :migrate => :reset_column_information do
+ # clear tables that are seeded by spree_core
+ Spree::Country.delete_all
+ Spree::State.delete_all
+ Spree::Zone.delete_all
+ Spree::Role.delete_all
+ Spree::ZoneMember.delete_all
+ end
task :reset_column_information do
ActiveRecord::Base.send(:subclasses).each do |model|
@@ -1,8 +1,8 @@
require 'spec_helper'
describe 'full domain routing' do
- let!(:apples_tenant) { FactoryGirl.create(:tenant, :shortname => 'apples', :domain => 'apples.com', name: "Apple") }
- let!(:oranges_tenant) { FactoryGirl.create(:tenant, :shortname => 'oranges', :domain => 'oranges.com', name: "Orange") }
+ let!(:apples_tenant) { Spree::Tenant.create!(:shortname => 'apples', :domain => 'apples.com', :name => 'Apple') }
+ let!(:oranges_tenant) { Spree::Tenant.create!(:shortname => 'oranges', :domain => 'oranges.com', :name => 'Orange') }
let!(:apple) {
Spree::Tenant.set_current_tenant(apples_tenant)
@@ -1,8 +1,8 @@
require 'spec_helper'
describe 'preferences' do
- let!(:apples_tenant) { FactoryGirl.create(:tenant, :shortname => 'apples', :domain => 'apples.com') }
- let!(:oranges_tenant) { FactoryGirl.create(:tenant, :shortname => 'oranges', :domain => 'oranges.com') }
+ let!(:apples_tenant) { Spree::Tenant.create!(:shortname => 'apples', :domain => 'apples.com') }
+ let!(:oranges_tenant) { Spree::Tenant.create!(:shortname => 'oranges', :domain => 'oranges.com') }
let!(:apples_tenant_site_name) {
Spree::Tenant.set_current_tenant(apples_tenant)
@@ -2,9 +2,9 @@
describe 'subdomain routing' do
let!(:master_tenant) { Spree::Tenant.master }
- let!(:apples_tenant) { FactoryGirl.create(:tenant, :shortname => 'apples') }
- let!(:oranges_tenant) { FactoryGirl.create(:tenant, :shortname => 'oranges') }
- let!(:mixed_tenant) { FactoryGirl.create(:tenant, :shortname => 'Mixed') }
+ let!(:apples_tenant) { Spree::Tenant.create!(:shortname => 'apples', :domain => 'apples.dev') }
+ let!(:oranges_tenant) { Spree::Tenant.create!(:shortname => 'oranges', :domain => 'oranges.dev') }
+ let!(:mixed_tenant) { Spree::Tenant.create!(:shortname => 'Mixed', :domain => 'mixed.dev') }
let!(:apple) {
Spree::Tenant.set_current_tenant(apples_tenant)
@@ -1,8 +1,8 @@
require 'spec_helper'
describe 'preferences' do
- let!(:alpha_tenant) { FactoryGirl.create(:tenant, shortname: 'alpha') }
- let!(:beta_tenant) { FactoryGirl.create(:tenant, shortname: 'beta') }
+ let!(:alpha_tenant) { Spree::Tenant.create!(:shortname => 'alpha', :domain => 'alpha.dev') }
+ let!(:beta_tenant) { Spree::Tenant.create!(:shortname => 'beta', :domain => 'beta.dev') }
let!(:super_admin) {
Spree::User.create!(email: 'super@example.com', password: 'spree123')
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+describe 'States' do
+ extend AuthorizationHelpers::Request
+ stub_authorization!
+
+ let!(:alpha_tenant) { Spree::Tenant.create!(:shortname => 'alpha', :domain => 'alpha.dev') }
+
+ it 'can view states without crashing' do
+ visit 'http://alpha.dev/admin'
+ click_link 'Configuration'
+ click_link 'States'
+ expect(page).to have_content('Alabama')
+ end
+end
@@ -0,0 +1,100 @@
+require 'spec_helper'
+
+describe 'Tenant checkout process' do
+ let!(:alpha_tenant) { Spree::Tenant.create!(shortname: 'alpha', domain: 'alpha.dev') }
+ let!(:beta_tenant) { Spree::Tenant.create!(shortname: 'beta', domain: 'beta.dev') }
+
+ let!(:alpha_product) {
+ Spree::Tenant.set_current_tenant(alpha_tenant)
+ Spree::Product.create!(name: 'Alpha Product', price: 9.99, available_on: 1.day.ago)
+ }
+
+ let!(:beta_product) {
+ Spree::Tenant.set_current_tenant(beta_tenant)
+ Spree::Product.create!(name: 'Beta Product', price: 12.25, available_on: 1.day.ago)
+ }
+
+ let!(:alpha_shipping) {
+ Spree::Tenant.set_current_tenant(alpha_tenant)
+ Spree::ShippingMethod.create!(
+ name: 'Free Shipping',
+ zone: Spree::Zone.where(name: 'North America').first,
+ calculator_type: 'Spree::Calculator::FlatRate'
+ ).tap do |s|
+ s.calculator = Spree::Calculator::FlatRate.create!({calculable: s}, without_protection: true)
+ s.save!
+ end
+ }
+
+ let!(:beta_shipping) {
+ Spree::Tenant.set_current_tenant(beta_tenant)
+ Spree::ShippingMethod.create!(
+ name: 'Free Shipping',
+ zone: Spree::Zone.where(name: 'North America').first,
+ calculator_type: 'Spree::Calculator::FlatRate'
+ ).tap do |s|
+ s.calculator = Spree::Calculator::FlatRate.create!({calculable: s}, without_protection: true)
+ s.save!
+ end
+ }
+
+ let!(:alpha_payment_method) {
+ Spree::Tenant.set_current_tenant(alpha_tenant)
+ Spree::PaymentMethod::Check.create!(
+ name: 'Default',
+ environment: 'test',
+ active: true)
+ }
+
+ let!(:beta_payment_method) {
+ Spree::Tenant.set_current_tenant(beta_tenant)
+ Spree::PaymentMethod::Check.create!(
+ name: 'Default',
+ environment: 'test',
+ active: true)
+ }
+
+ it 'purchases a product from the alpha tenant' do
+ visit 'http://alpha.dev'
+ click_on 'Alpha Product'
+ click_on 'Add To Cart'
+ click_on 'Checkout'
+ fill_in 'order_email', with: 'naruto@uzamaki.jp'
+ click_on 'Continue'
+ fill_in 'First Name', with: 'Naruto'
+ fill_in 'Last Name', with: 'Uzamaki'
+ fill_in 'Street Address', with: '126 Ramen Way'
+ fill_in 'City', with: 'Konohama'
+ fill_in 'order_bill_address_attributes_state_name', with: 'Virginia'
+ select 'United States', from: 'Country'
+ fill_in 'Zip', with: '23229'
+ fill_in 'Phone', with: '8885551212'
+ check 'Use Billing Address'
+ click_on 'Save and Continue'
+ click_on 'Save and Continue'
+ click_on 'Save and Continue'
+ expect(page).to have_content('Your order has been processed successfully')
+ end
+
+ it 'purchases a product from the beta tenant' do
+ visit 'http://beta.dev'
+ click_on 'Beta Product'
+ click_on 'Add To Cart'
+ click_on 'Checkout'
+ fill_in 'order_email', with: 'naruto@uzamaki.jp'
+ click_on 'Continue'
+ fill_in 'First Name', with: 'Naruto'
+ fill_in 'Last Name', with: 'Uzamaki'
+ fill_in 'Street Address', with: '126 Ramen Way'
+ fill_in 'City', with: 'Konohama'
+ fill_in 'order_bill_address_attributes_state_name', with: 'Virginia'
+ select 'United States', from: 'Country'
+ fill_in 'Zip', with: '23229'
+ fill_in 'Phone', with: '8885551212'
+ check 'Use Billing Address'
+ click_on 'Save and Continue'
+ click_on 'Save and Continue'
+ click_on 'Save and Continue'
+ expect(page).to have_content('Your order has been processed successfully')
+ end
+end
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+describe 'Zones' do
+ extend AuthorizationHelpers::Request
+ stub_authorization!
+
+ let!(:alpha_tenant) { Spree::Tenant.create!(:shortname => 'alpha', :domain => 'alpha.dev') }
+ let!(:beta_tenant) { Spree::Tenant.create!(:shortname => 'beta', :domain => 'beta.dev') }
+
+ it 'permits creating two zones with the same name on different tenants' do
+ visit 'http://alpha.dev/admin'
+ click_link 'Configuration'
+ click_link 'Zones'
+ click_link 'New Zone'
+ fill_in 'Name', with: 'Test'
+ click_button 'Create'
+ expect(page).to have_content('Zone "Test" has been successfully created')
+
+ visit 'http://beta.dev/admin'
+ click_link 'Configuration'
+ click_link 'Zones'
+ click_link 'New Zone'
+ fill_in 'Name', with: 'Test'
+ click_button 'Create'
+ expect(page).to have_content('Zone "Test" has been successfully created')
+ end
+end
@@ -5,9 +5,9 @@
describe '#find_target_tenant' do
subject(:finder) { Spree::SpreeLandlord::TenantFinder.new }
before { Spree::Tenant.destroy_all }
- let!(:master) { FactoryGirl.create(:tenant, :shortname => 'master', :domain => 'master.com') }
- let!(:apples) { FactoryGirl.create(:tenant, :shortname => 'apples', :domain => 'apples.com') }
- let!(:oranges) { FactoryGirl.create(:tenant, :shortname => 'oranges', :domain => 'oranges.com') }
+ let!(:master) { Spree::Tenant.create!(:shortname => 'master', :domain => 'master.com') }
+ let!(:apples) { Spree::Tenant.create!(:shortname => 'apples', :domain => 'apples.com') }
+ let!(:oranges) { Spree::Tenant.create!(:shortname => 'oranges', :domain => 'oranges.com') }
def request(uri)
env = Rack::MockRequest.env_for(uri)
Oops, something went wrong.

0 comments on commit 52178ce

Please sign in to comment.