Permalink
Browse files

Add subdomain_account_options cattr_accessor to store options

  • Loading branch information...
1 parent 792f920 commit ffef6c3fe964171c0d4139523b0241a8152ce991 @shuber committed Jan 9, 2009
Showing with 28 additions and 22 deletions.
  1. +1 −0 CHANGELOG
  2. +18 −14 README.markdown
  3. +9 −8 lib/huberry/subdomain_account/controller_methods.rb
View
@@ -4,6 +4,7 @@
* Convert to soft tabs
* Convert tests to soft tabs
* Update CHANGELOG to present tense
+ * Add subdomain_account_options cattr_accessor to store options
2008-06-12 - Sean Huber (shuber@huberry.com)
* Update README
View
@@ -1,5 +1,5 @@
-Huberry::SubdomainAccount
-=========================
+subdomain\_account
+==================
A rails plugin that handles subdomain accounts
@@ -10,27 +10,31 @@ Installation
script/plugin install git://github.com/shuber/subdomain_account.git
-Example
--------
+Usage
+-----
+
+Simply add `before_filter :subdomain_account_required` for any actions that require a subdomain. A `:model` will then be searched
+by `:field` for the current subdomain. The result of that query is stored in a controller instance method called `current_account`.
+In the example below, the `current_account` would be set to the result of `Account.find_by_subdomain(request.subdomains.first)`.
+The `:model` and `:field` options are customizable, see below.
class ProjectsController < ApplicationController
before_filter :subdomain_account_required
-
+
def index
render :text => current_account.subdomain
end
end
+By default the `:model` to use for looking up records is `Account` and the `:field` to search by is `:subdomain`. You can change
+this by modifying the controller's `subdomain_account_options` attribute like so:
-Controller Methods
-------------------
-
- # Returns the current account or nil if one is not found
- current_account
-
- # A before filter to require a subdomain - renders a 404 if one is not found
- # (overwrite subdomain_account_not_found to change this behavior)
- subdomain_account_required
+ class ApplicationController < ActionController::Base
+ self.subdomain_account_options.merge!(:model => Company, :field => :username)
+ end
+
+When a subdomain is required but the `current_account` was not found, the controller calls the `subdomain_account_not_found` method
+which simply renders a 404. You can overwrite this method to change this behavior.
Request Methods
@@ -6,9 +6,11 @@ def self.extended(base)
include InstanceMethods
include AuthenticationPatch if included_modules.collect(&:to_s).include? 'Huberry::Authentication::ControllerMethods::InstanceMethods'
- cattr_accessor :subdomain_account_model, :subdomain_field
- self.subdomain_account_model = 'Account'
- self.subdomain_field = :subdomain
+ cattr_accessor :subdomain_account_options
+ self.subdomain_account_options = {
+ :model => 'Account',
+ :field => :subdomain
+ }
attr_accessor :current_account
helper_method :current_account
@@ -19,8 +21,8 @@ module InstanceMethods
protected
def find_current_account(force_query = false)
if @queried_for_current_account.nil? || force_query
- account_class = self.class.subdomain_account_model.to_s.constantize
- self.current_account = account_class.send "find_by_#{self.class.subdomain_field}", request.subdomain
+ account_class = self.class.subdomain_account_options[:model].to_s.constantize
+ self.current_account = account_class.send "find_by_#{self.class.subdomain_account_options[:field]}", request.subdomains.first
end
self.current_account
end
@@ -30,7 +32,6 @@ def subdomain_account_not_found
format.html { render :file => "#{RAILS_ROOT}/public/404.html", :status => 404 }
format.all { render :nothing => true, :status => 404 }
end
- false
end
def subdomain_account_required
@@ -43,8 +44,8 @@ module AuthenticationPatch
def find_current_user(force_query = false)
if @queried_for_current_user.nil? || force_query
@queried_for_current_user = true
- users = self.class.authentication_model.to_s.underscore.pluralize
- self.current_user = find_current_account.send(users).find(session[:user_id]) rescue nil
+ underscored_authentication_model = self.class.authentication_model.to_s.underscore
+ self.current_user = find_current_account.send(underscored_authentication_model.pluralize.to_sym).find(session["#{underscored_authentication_model}_id".to_sym]) rescue nil
end
self.current_user
end

0 comments on commit ffef6c3

Please sign in to comment.