Skip to content

Commit

Permalink
moved subdomain reference from name:string to account_id:integer, thi…
Browse files Browse the repository at this point in the history
…s could pose a speed issue later on. Also created the necessary indexes.
  • Loading branch information
berislavbabic committed Sep 30, 2010
1 parent c2ad8d8 commit 01de78a
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 25 deletions.
8 changes: 4 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ class ApplicationController < ActionController::Base


def current_subdomain
if request.subdomain.present? && request.subdomain != "www"
current_subdomain = request.subdomain
if request.subdomains.first.present? && request.subdomains.first != "www"
current_subdomain = Subdomain.find_by_name(request.subdomains.first)
else
current_subdomain = nil
end
return current_subdomain
end
end

def check_my_subdomain(subdomain)
if subdomain != current_subdomain
if subdomain != current_subdomain.name
redirect_to "/opps" , :alert => "Sorry, resource is not part of your subdomain"
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HomeController < ApplicationController
def index
if current_user
subdomain = current_user.subdomain_name
subdomain = current_user.subdomain.name
sign_out(current_user)
redirect_to new_user_session_url( :subdomain => subdomain), :notice => "Please sign-in to your subdomain account"
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class SitesController < ApplicationController

def index
@site = Site.find_by_name(current_subdomain)
@site = Site.find_by_name(current_subdomain.name)
end

def opps
@site = Site.find_by_name(current_subdomain)
@site = Site.find_by_name(current_subdomain.name)
end

end
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class UsersController < ApplicationController

def index
@users = current_subdomain.nil? ? User.all : User.where(:subdomain_name => current_subdomain)
@users = current_subdomain.nil? ? User.all : current_subdomain.users
end

def show
@user = User.find(params[:id])
if !current_subdomain.nil?
check_my_subdomain(@user.subdomain_name)
check_my_subdomain(@user.subdomain.name)
end
end

Expand Down
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
module ApplicationHelper

end
2 changes: 1 addition & 1 deletion app/models/subdomain.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Subdomain < ActiveRecord::Base
has_many :users, :primary_key => "name", :foreign_key => "subdomain_name"
has_many :users
has_friendly_id :name, :use_slug => true, :strip_non_ascii => true
validates_uniqueness_of :name, :case_sensitive => false
validates_presence_of :name
Expand Down
12 changes: 5 additions & 7 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
class User < ActiveRecord::Base
belongs_to :subdomain, :primary_key => "name", :foreign_key => "subdomain_name"
belongs_to :subdomain
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :name, :subdomain_name
validates_uniqueness_of :email, :case_sensitive => false
attr_accessor :subdomain_name
attr_accessible :name, :subdomain_name, :email, :password, :password_confirmation, :remember_me
# has_friendly_id :subdomain_name, :use_slug => true, :strip_non_ascii => true
after_create :create_subdomain
before_create :create_subdomain

private

def create_subdomain
subdomain = Subdomain.find_by_name(self.subdomain_name)
if !subdomain
Subdomain.create! :name => self.subdomain_name, :user_id => self.id
end
self.subdomain = Subdomain.find_by_name(self.subdomain_name)
self.subdomain ||= Subdomain.create!(:name => self.subdomain_name, :user_id => self.id)
end
end
3 changes: 1 addition & 2 deletions app/views/devise/registrations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>

<p><%= f.label :subdomain %> :
<%= f.hidden_field :subdomain_name %><%= resource.subdomain_name %></p>
<p><%= f.hidden_field :subdomain_name, :value => resource.subdomain.name %></p>

<p><%= f.label :name %><br />
<%= f.text_field :name %></p>
Expand Down
5 changes: 2 additions & 3 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :subdomain %>
<% if current_subdomain %>
: <%= f.hidden_field :subdomain_name, :value => current_subdomain %> <%= current_subdomain%></p>

<%= f.hidden_field :subdomain_name, :value => current_subdomain.name %>
<% else%>
<p><%= f.label :subdomain %>
<br /><%= f.text_field :subdomain_name%></p>
<% end %>

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20100808194405_devise_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ def self.up
def self.down
drop_table :users
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20100930104337_migrate_subdomain_to_integer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class MigrateSubdomainToInteger < ActiveRecord::Migration
def self.up
remove_column :users, :subdomain_name
add_column :users, :subdomain_id, :integer
add_index :users, :subdomain_id
add_index :subdomains, :name
end

def self.down
add_column :users, :subdomain_name, :limit => 40
remove_column :users, :subdomain_id
remove_index :users, :subdomain_id
remove_index :subdomains, :name
end
end
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100808194652) do
ActiveRecord::Schema.define(:version => 20100930104337) do

create_table "slugs", :force => true do |t|
t.string "name"
Expand All @@ -31,6 +31,8 @@
t.datetime "updated_at"
end

add_index "subdomains", ["name"], :name => "index_subdomains_on_name"

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
Expand All @@ -44,14 +46,15 @@
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "name"
t.string "subdomain_name", :limit => 40
t.string "loginable_type", :limit => 40
t.integer "loginable_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "subdomain_id"
end

add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
add_index "users", ["subdomain_id"], :name => "index_users_on_subdomain_id"

end

0 comments on commit 01de78a

Please sign in to comment.