FellyTone84
FellyTone84

Reputation: 695

How to invite new users to a specific subdomain in rails

I like how Turbinehq allows you to create an admin account and a subdomain for your company all in one step. Testing it out, I discovered that after one creates a subdomain for their company, they can then invite users via email. Users who act on the invite automatically become part of the same company.

I'd like to emulate this process in rails. I tried this starter app, but it's not restrictive enough. The first question I have deals with how one would design the form below:

TurbineHQ's account creation view

Upvotes: 1

Views: 710

Answers (1)

Julio Betta
Julio Betta

Reputation: 2295

I had the same issue couple days ago. I've found a solution that works fine!

# models/company.rb
class Company < ActiveRecord::Base
  has_many :users, :dependent => :destroy

  validates :subdomain, :presence   => true,
                        :uniqueness => { :case_sensitive => false },
                        :length     => { :within => 4..20 },
                        :format     => { :with => /^[a-z0-9]+$/i }

  attr_accessible :name, :subdomain

end

# ======================================================================================

# models/user.rb
class User < ActiveRecord::Base
  before_create :create_company
  belongs_to :company

  validates :subdomain, :on         => :create,
                        :presence   => true,
                        :length     => { :within => 4..20 },
                        :format     => { :with => /^[a-z0-9]+$/i }

  validates_presence_of  :nome

  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable,
         :authentication_keys => [:subdomain]

  attr_accessor   :subdomain # VIRTUAL ATTRIBUTE
  attr_accessible :name, :email, :subdomain, :password, :password_confirmation,
                  :remember_me, :loginable_token

  private

    def create_company
     self.company = Company.create!(:subdomain => self.subdomain)
    end

end

# ======================================================================================

# example of registration form
= simple_form_for(resource, :as => resource_name, :url =>    registration_path(resource_name)) do |f|
  = devise_error_messages!

  %fieldset
    .clearfix= f.input :name,       :required => true
    .clearfix= f.input :email,      :required => true
    .clearfix= f.input :subdomain,  :required => true
    .clearfix= f.input :password,   :required => true, :input_html => {:minlength => 6}
    .clearfix= f.input :password_confirmation, :input_html => {:minlength => 6}

    .form-actions
      = f.submit t('labels.signup'), :class => 'btn btn-success'
    %p= render "links"

Hope it helps..

Upvotes: 1

Related Questions