Skip to content

Commit

Permalink
Seperating my plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
zachinglis committed Apr 17, 2008
0 parents commit a227131
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
39 changes: 39 additions & 0 deletions README
@@ -0,0 +1,39 @@
Dashboard Location
============================----version 1 beta
by Zach Inglis // zachinglis.com

I was wanting to implement Basecamp-like urls to a project I was working on and found that account_location was only half of my soloution while dashboard_location was
only 3 quarters. Attached is a project-agnostic(no setting variables, do not worry) library for Rails.

It is not the cleanest of libraries yet but I intend on improving it. It is only 1 day old so far.

In controllers such as sessions_controller where you want people to be using the subdomain, use:
before_filter :should_add_subdomain_for_home_if_logged_in
before_filter :protect_controller_if_no_dashboard

For controllers that should not be accessed through a subdomain, such as content controllers, use:
before_filter :redirect_dashboard_calls

That is it! Just use methods like logged_in anywhere you like and it will work as you hope.

Credits:

While I hacked the hell out of it, credits are still due.

David Heinemeier Hansson - for account_location - http://dev.rubyonrails.org/svn/rails/plugins/account_location/
Derek Haynes - for his modifications - http://cleanair.highgroove.com/articles/2006/08/14/simplied-subdomain-authentication-in-ruby-on-rails

Installation:

Drag the dashboard_location.rb to your lib folder in your Rails directory.

Perform: ruby ./script/generate model Home permalink:string name:string

I use this in conjunction with Technoweenies permalink_fu to automatically generate the permalink. If you want to add the following code, else skip it:
has_permalink :name

Add this to your signup process. Ryan address how to do similiar things here: http://railscasts.com/episodes/75

Cheers, hope your coding time is easier. Any suggestions or code changes you want to suggest, please email me by going to zachinglis.com/contact!

Released under the MIT License as is account_dashboard
95 changes: 95 additions & 0 deletions lib/dashboard_location.rb
@@ -0,0 +1,95 @@
module DashboardLocation
def self.included(controller)
controller.helper_method(:dashboard_domain, :dashboard_subdomain,
:dashboard_host, :dashboard_url,
:current_dashboard, :current_subscription,
:clean_root_url, :clean_dashboard_url)
end

protected
def default_dashboard_subdomain
current_dashboard.permalink if current_dashboard
end

def home_url(dashboard_subdomain = default_dashboard_subdomain, use_ssl = request.ssl?)
(use_ssl ? "https://" : "http://") + dashboard_host(dashboard_subdomain)
end

def dashboard_host(dashboard_subdomain = default_dashboard_subdomain)
dashboard_subdomain + "." + dashboard_domain
end

def dashboard_domain
dashboard_domain = ""
dashboard_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
dashboard_domain << request.domain + request.port_string
dashboard_domain
end

def dashboard_subdomain
request.subdomains.first
end

def current_dashboard
Home.find_by_permalink(dashboard_subdomain)
end

def ensure_current_dashboard
return true if current_dashboard
flash[:notice] = "Sorry, but that is an invalid home."
redirect_to root_url and return false
end

def should_add_subdomain_for_home_if_logged_in
redirect_to clean_dashboard_url if request.subdomains.empty? && logged_in?
end

def should_redirect_if_no_subdomain
return unless request.subdomains.empty?
redirect_to logged_in? ? clean_dashboard_url : root_url
end

def redirect_dashboard_calls
return if request.subdomains.empty?
if !logged_in? && !dashboard_subdomain.nil? && !current_dashboard.nil?
flash[:notice] = "Please login to access your account"
redirect_to login_url
elsif logged_in? && current_user.home.permalink != dashboard_subdomain
flash[:notice] = "Please only access your own home panel."
redirect_to clean_dashboard_url(current_user.home.permalink)
elsif dashboard_subdomain && current_dashboard.nil?
flash[:notice] = "We can't find where you are looking for."
redirect_to clean_root_url
elsif logged_in? && (request.path.nil? || request.path == "/")
redirect_to_dashboard_if_logged_in
end
end

def redirect_to_dashboard_if_logged_in
redirect_to dashboard_url if logged_in?
end

def protect_controller_if_no_dashboard
redirect_to clean_dashboard_url if dashboard_subdomain.nil?
end

def account_domain
account_domain = ""
account_domain << request.subdomains[1..-1].join(".") + "." if request.subdomains.size > 1
account_domain << request.domain + request.port_string
end

def clean_dashboard_url(permalink=nil)
home_permalink = request.subdomains.empty? ? current_user.home.permalink : request.subdomains.first
home_permalink = permalink unless permalink.nil? # overwrite fu
"#{protocol}#{home_permalink}.#{account_domain}"
end

def clean_root_url
"#{protocol}#{account_domain}"
end

def protocol
request.ssl? ? "https://" : "http://"
end
end

0 comments on commit a227131

Please sign in to comment.