diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ef7ddd..ea97f73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +0.3.2 +----- +* correctly support nested models with has_many :through (thx dexion) +* Support 'www.subdomain.example.com' (thx wtfiwtz) +* Support setting `tenant_id` on scoped models if the `tenant_id` is nil (thx Matt Wilson) + 0.3.1 ----- * Added support for Rails 4 diff --git a/acts_as_tenant.gemspec b/acts_as_tenant.gemspec index b1ba27e..82ce1f0 100644 --- a/acts_as_tenant.gemspec +++ b/acts_as_tenant.gemspec @@ -24,6 +24,7 @@ Gem::Specification.new do |s| #s.add_dependency('request_store', '>= 1.0.5') s.add_development_dependency('rspec') + s.add_development_dependency('rspec-rails') s.add_development_dependency('database_cleaner') s.add_development_dependency('sqlite3') s.add_development_dependency('debugger') diff --git a/lib/acts_as_tenant/controller_extensions.rb b/lib/acts_as_tenant/controller_extensions.rb index 4642641..0e9f05b 100644 --- a/lib/acts_as_tenant/controller_extensions.rb +++ b/lib/acts_as_tenant/controller_extensions.rb @@ -18,7 +18,7 @@ def set_current_tenant_by_subdomain(tenant = :account, column = :subdomain ) private def find_tenant_by_subdomain - ActsAsTenant.current_tenant = tenant_class.where(tenant_column => request.subdomains.first).first + ActsAsTenant.current_tenant = tenant_class.where(tenant_column => request.subdomains.last).first end def current_tenant diff --git a/lib/acts_as_tenant/version.rb b/lib/acts_as_tenant/version.rb index 0d63df2..7d1aa3b 100644 --- a/lib/acts_as_tenant/version.rb +++ b/lib/acts_as_tenant/version.rb @@ -1,3 +1,3 @@ module ActsAsTenant - VERSION = "0.3.1" + VERSION = "0.3.2" end diff --git a/spec/acts_as_tenant/model_extensions_spec.rb b/spec/acts_as_tenant/model_extensions_spec.rb index 5bf824d..1177bb2 100644 --- a/spec/acts_as_tenant/model_extensions_spec.rb +++ b/spec/acts_as_tenant/model_extensions_spec.rb @@ -4,6 +4,7 @@ ActiveRecord::Schema.define(:version => 1) do create_table :accounts, :force => true do |t| t.column :name, :string + t.column :subdomain, :string end create_table :projects, :force => true do |t| diff --git a/spec/acts_as_tenant/tenant_by_filter_spec.rb b/spec/acts_as_tenant/tenant_by_filter_spec.rb new file mode 100644 index 0000000..cf18f7e --- /dev/null +++ b/spec/acts_as_tenant/tenant_by_filter_spec.rb @@ -0,0 +1,33 @@ +require "spec_helper" + +#Setup test specific ApplicationController +class Account + attr_accessor :name +end + +class ApplicationController2 < ActionController::Base + include Rails.application.routes.url_helpers + set_current_tenant_through_filter + before_filter :your_method_that_finds_the_current_tenant + + def your_method_that_finds_the_current_tenant + current_account = Account.new + current_account.name = 'account1' + set_current_tenant(current_account) + end + +end + +# Start testing +describe ApplicationController2, :type => :controller do + controller do + def index + render :text => "custom called" + end + end + + it 'Finds the correct tenant using the filter command' do + get :index + ActsAsTenant.current_tenant.name.should eq 'account1' + end +end \ No newline at end of file diff --git a/spec/acts_as_tenant/tenant_by_subdomain_spec.rb b/spec/acts_as_tenant/tenant_by_subdomain_spec.rb new file mode 100644 index 0000000..6ecfea1 --- /dev/null +++ b/spec/acts_as_tenant/tenant_by_subdomain_spec.rb @@ -0,0 +1,32 @@ +require "spec_helper" + +#Setup test specific ApplicationController +class Account; end # this is so the spec will work in isolation + +class ApplicationController < ActionController::Base + include Rails.application.routes.url_helpers + set_current_tenant_by_subdomain +end + +# Start testing +describe ApplicationController, :type => :controller do + controller do + def index + render :text => "custom called" + end + end + + it 'Finds the correct tenant with a subdomain.example.com' do + @request.host = "account1.example.com" + Account.should_receive(:where).with({subdomain: 'account1'}) {['account1']} + get :index + ActsAsTenant.current_tenant.should eq 'account1' + end + + it 'Finds the correct tenant with a www.subdomain.example.com' do + @request.host = "www.account1.example.com" + Account.should_receive(:where).with({subdomain: 'account1'}) {['account1']} + get :index + ActsAsTenant.current_tenant.should eq 'account1' + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 249ac83..eeb032f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -3,6 +3,8 @@ require 'database_cleaner' require 'acts_as_tenant' +require 'rspec/rails' +require 'rails' config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml'))) ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log")) @@ -22,5 +24,14 @@ DatabaseCleaner.clean ActsAsTenant.current_tenant = nil end + + config.infer_base_class_for_anonymous_controllers = true +end -end \ No newline at end of file +# Setup a test app +module Rollcall + class Application < Rails::Application; end +end + +Rollcall::Application.config.secret_token = '1234567890123456789012345678901234567890' +Rollcall::Application.config.secret_key_base = '1234567890123456789012345678901234567890' \ No newline at end of file