Skip to content

Commit

Permalink
Changed subdomain resolution. Added specs for controller extensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwinM committed Jan 4, 2014
1 parent 5eead22 commit 0ee4514
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 3 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down
1 change: 1 addition & 0 deletions acts_as_tenant.gemspec
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_tenant/controller_extensions.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_tenant/version.rb
@@ -1,3 +1,3 @@
module ActsAsTenant
VERSION = "0.3.1"
VERSION = "0.3.2"
end
1 change: 1 addition & 0 deletions spec/acts_as_tenant/model_extensions_spec.rb
Expand Up @@ -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|
Expand Down
33 changes: 33 additions & 0 deletions 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
32 changes: 32 additions & 0 deletions 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
13 changes: 12 additions & 1 deletion spec/spec_helper.rb
Expand Up @@ -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"))
Expand All @@ -22,5 +24,14 @@
DatabaseCleaner.clean
ActsAsTenant.current_tenant = nil
end

config.infer_base_class_for_anonymous_controllers = true
end

end
# 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'

0 comments on commit 0ee4514

Please sign in to comment.