Permalink
Browse files

Sets the default_county_id config value

  • Loading branch information...
1 parent bf4b99a commit 62b64f3f4bad104426e5acd3059edac5ba3222d6 @mscottford mscottford committed Feb 8, 2013
View
30 app/models/spree/tenant.rb
@@ -31,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
View
9 lib/spree/spree_landlord/tenant_seeder.rb
@@ -8,6 +8,7 @@ def initialize(tenant)
def seed
seed_roles
seed_countries
+ set_default_country_id
seed_states
seed_zones
seed_zone_members
@@ -261,6 +262,14 @@ def seed_countries
end
end
+ def set_default_country_id
+ old_current_tenant_id = Spree::Tenant.current_tenant_id
+ Spree::Tenant.set_current_tenant(@tenant)
+ united_states = Spree::Country.where(iso: 'US').first
+ Spree::Config[:default_country_id] = united_states.id
+ Spree::Tenant.set_current_tenant(old_current_tenant_id)
+ end
+
def seed_states
states_data = [
{"name"=>"Alabama", "country_iso"=>"US", "abbr"=>"AL"},
View
15 spec/features/states_spec.rb
@@ -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
View
6 spec/lib/spree/spree_landlord/tenant_seeder_spec.rb
@@ -79,6 +79,12 @@ def load_seed_data(seed_file)
end
end
+ it 'sets default_country_id preference to a tenanted country' do
+ Spree::Tenant.set_current_tenant(tenant)
+ united_states = Spree::Country.where(iso: 'US').first
+ expect(Spree::Config[:default_country_id]).to eq(united_states.id)
+ end
+
it 'generates all states in spree_core' do
united_states = Spree::Country.where(:iso => 'US').first
View
11 spec/models/spree/tenant_spec.rb
@@ -94,6 +94,7 @@ def assert_requires_attribute(attribute_name)
context 'when current thread has no tenant id' do
it 'returns the master tenant id' do
tenant = Spree::Tenant.create(shortname: 'test', domain: 'test.dev')
+ Thread.current[:tenant_id] = nil
Spree::Tenant.should_receive(:master).and_return(tenant)
Spree::Tenant.current_tenant_id.should == tenant.id
@@ -129,6 +130,16 @@ def assert_requires_attribute(attribute_name)
end
end
+ context 'with nil' do
+ it 'set the tenant id to the id of the master tenant' do
+ Thread.current[:tenant_id].should == nil
+
+ Spree::Tenant.set_current_tenant(nil)
+
+ Thread.current[:tenant_id].should == Spree::Tenant.master.id
+ end
+ end
+
context 'with something else' do
it 'raises an exception' do
expect {

0 comments on commit 62b64f3

Please sign in to comment.