adding devise + create account model and bind it to site
This commit is contained in:
parent
0db6b9625f
commit
30789cbbc3
@ -1,17 +1,23 @@
|
||||
class Account
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
|
||||
# include MongoMapper::Document
|
||||
|
||||
## Attributes
|
||||
# key :name
|
||||
# key :locale
|
||||
# timestamps!
|
||||
|
||||
# Include default devise modules. Others available are:
|
||||
# :http_authenticatable, :token_authenticatable, :lockable, :timeoutable and :activatable
|
||||
# devise :registerable, :authenticatable, :confirmable, :recoverable,
|
||||
# :rememberable, :trackable, :validatable
|
||||
# devise modules
|
||||
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
|
||||
|
||||
# Setup accessible (or protected) attributes for your model
|
||||
# attr_accessible :email, :password, :password_confirmation
|
||||
# attr_accessible :email, :password, :password_confirmation # TODO
|
||||
|
||||
## attributes ##
|
||||
field :name
|
||||
field :locale, :default => 'en'
|
||||
|
||||
## validations ##
|
||||
validates_presence_of :name
|
||||
|
||||
## associations ##
|
||||
|
||||
def sites
|
||||
Site.where({ :account_ids => self._id })
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -6,6 +6,7 @@ class Site
|
||||
field :name
|
||||
field :subdomain, :type => String
|
||||
field :domains, :type => Array, :default => []
|
||||
field :account_ids, :type => Array, :default => []
|
||||
|
||||
## validations ##
|
||||
validates_presence_of :name, :subdomain
|
||||
@ -25,6 +26,14 @@ class Site
|
||||
add_dirty_methods :domains
|
||||
|
||||
## methods ##
|
||||
|
||||
def accounts
|
||||
Account.criteria.in(:_id => self.account_ids)
|
||||
end
|
||||
|
||||
def accounts=(models_or_ids)
|
||||
self.account_ids = [*models_or_ids].collect { |object| object.respond_to?(:to_i) ? object : object.id }.uniq
|
||||
end
|
||||
|
||||
def add_subdomain_to_domains
|
||||
self.domains ||= []
|
||||
|
12
app/views/devise/confirmations/new.html.erb
Normal file
12
app/views/devise/confirmations/new.html.erb
Normal file
@ -0,0 +1,12 @@
|
||||
<h2>Resend confirmation instructions</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => confirmation_path(resource_name)) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Resend confirmation instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
@ -0,0 +1,5 @@
|
||||
<p>Welcome <%= @resource.email %>!</p>
|
||||
|
||||
<p>You can confirm your account through the link below:</p>
|
||||
|
||||
<p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %></p>
|
@ -0,0 +1,8 @@
|
||||
<p>Hello <%= @resource.email %>!</p>
|
||||
|
||||
<p>Someone has requested a link to change your password, and you can do this through the link below.</p>
|
||||
|
||||
<p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @resource.reset_password_token) %></p>
|
||||
|
||||
<p>If you didn't request this, please ignore this email.</p>
|
||||
<p>Your password won't change until you access the link above and create a new one.</p>
|
7
app/views/devise/mailer/unlock_instructions.html.erb
Normal file
7
app/views/devise/mailer/unlock_instructions.html.erb
Normal file
@ -0,0 +1,7 @@
|
||||
<p>Hello <%= @resource.email %>!</p>
|
||||
|
||||
<p>Your account has been locked due to an excessive amount of unsuccessful sign in attempts.</p>
|
||||
|
||||
<p>Click the link below to unlock your account:</p>
|
||||
|
||||
<p><%= link_to 'Unlock my account', unlock_url(@resource, :unlock_token => @resource.unlock_token) %></p>
|
16
app/views/devise/passwords/edit.html.erb
Normal file
16
app/views/devise/passwords/edit.html.erb
Normal file
@ -0,0 +1,16 @@
|
||||
<h2>Change your password</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
<%= f.hidden_field :reset_password_token %>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.submit "Change my password" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
12
app/views/devise/passwords/new.html.erb
Normal file
12
app/views/devise/passwords/new.html.erb
Normal file
@ -0,0 +1,12 @@
|
||||
<h2>Forgot your password?</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => password_path(resource_name)) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Send me reset password instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
25
app/views/devise/registrations/edit.html.erb
Normal file
25
app/views/devise/registrations/edit.html.erb
Normal file
@ -0,0 +1,25 @@
|
||||
<h2>Edit <%= resource_name.to_s.humanize %></h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i></p>
|
||||
<p><%= f.password_field :current_password %></p>
|
||||
|
||||
<p><%= f.submit "Update" %></p>
|
||||
<% end %>
|
||||
|
||||
<h3>Cancel my account</h3>
|
||||
|
||||
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
|
||||
|
||||
<%= link_to "Back", :back %>
|
17
app/views/devise/registrations/new.html.erb
Normal file
17
app/views/devise/registrations/new.html.erb
Normal file
@ -0,0 +1,17 @@
|
||||
<h2>Sign up</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => registration_path(resource_name)) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.submit "Sign up" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
17
app/views/devise/sessions/new.html.erb
Normal file
17
app/views/devise/sessions/new.html.erb
Normal file
@ -0,0 +1,17 @@
|
||||
<h2>Sign in</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => session_path(resource_name)) do |f| %>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<% if devise_mapping.rememberable? -%>
|
||||
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
|
||||
<% end -%>
|
||||
|
||||
<p><%= f.submit "Sign in" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
19
app/views/devise/shared/_links.erb
Normal file
19
app/views/devise/shared/_links.erb
Normal file
@ -0,0 +1,19 @@
|
||||
<%- if controller_name != 'sessions' %>
|
||||
<%= link_to "Sign in", new_session_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
|
||||
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
|
||||
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
|
||||
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.lockable? && controller_name != 'unlocks' %>
|
||||
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
|
||||
<% end -%>
|
12
app/views/devise/unlocks/new.html.erb
Normal file
12
app/views/devise/unlocks/new.html.erb
Normal file
@ -0,0 +1,12 @@
|
||||
<h2>Resend unlock instructions</h2>
|
||||
|
||||
<%= form_for(resource_name, resource, :url => unlock_path(resource_name)) do |f| %>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.submit "Resend unlock instructions" %></p>
|
||||
<% end %>
|
||||
|
||||
<%= render :partial => "devise/shared/links" %>
|
@ -84,7 +84,6 @@ Devise.setup do |config|
|
||||
# ==> General configuration
|
||||
# Load and configure the ORM. Supports :active_record (default), :mongoid
|
||||
# (requires mongo_ext installed) and :data_mapper (experimental).
|
||||
# require 'devise/orm/active_record'
|
||||
require 'devise/orm/mongoid'
|
||||
|
||||
# Turn scoped views on. Before rendering "sessions/new", it will first check for
|
||||
|
@ -2,4 +2,7 @@ require 'lib/locomotive.rb'
|
||||
|
||||
Locomotive.configure do |config|
|
||||
config.default_domain = 'example.com'
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: embed it in Locomotive
|
||||
ActionMailer::Base.default_url_options[:host] = Locomotive.config.default_domain + (Rails.env.development? ? ':3000' : '')
|
@ -2,6 +2,7 @@ Locomotive::Application.routes.draw do |map|
|
||||
|
||||
constraints(Locomotive::Routing::DefaultConstraint) do
|
||||
root :to => 'home#show'
|
||||
devise_for :accounts
|
||||
end
|
||||
|
||||
match '/' => 'pages#show'
|
||||
|
@ -7,4 +7,5 @@
|
||||
# Mayor.create(:name => 'Daley', :city => cities.first)
|
||||
|
||||
|
||||
Site.create! :name => 'Locomotive test website', :subdomain => 'test'
|
||||
Site.create! :name => 'Locomotive test website', :subdomain => 'test'
|
||||
Account.create :name => 'Admin', :email => 'admin@locomotiveapp.org', :password => 'locomotive', :password_confirmation => 'locomotive'
|
||||
|
@ -5,13 +5,12 @@ Factory.define :site do |s|
|
||||
s.created_at Time.now
|
||||
end
|
||||
|
||||
## Accounts ##
|
||||
# Factory.define :account do |a|
|
||||
# a.name 'Bart Simpson'
|
||||
# a.email 'bart@fox.com'
|
||||
# a.password 'easyone'
|
||||
# a.password_confirmation 'easyone'
|
||||
# a.locale 'en'
|
||||
# end
|
||||
# Accounts ##
|
||||
Factory.define :account do |a|
|
||||
a.name 'Bart Simpson'
|
||||
a.email 'bart@simpson.net'
|
||||
a.password 'easyone'
|
||||
a.password_confirmation 'easyone'
|
||||
a.locale 'en'
|
||||
end
|
||||
|
||||
## Site ##
|
||||
|
40
spec/models/account_spec.rb
Normal file
40
spec/models/account_spec.rb
Normal file
@ -0,0 +1,40 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Account do
|
||||
|
||||
it 'should have a valid factory' do
|
||||
Factory.build(:account).should be_valid
|
||||
end
|
||||
|
||||
## Validations ##
|
||||
|
||||
%w{name email password}.each do |attr|
|
||||
it "should validate presence of #{attr}" do
|
||||
account = Factory.build(:account, attr.to_sym => nil)
|
||||
account.should_not be_valid
|
||||
account.errors[attr.to_sym].should include("can't be blank")
|
||||
end
|
||||
end
|
||||
|
||||
it "should have a default locale" do
|
||||
account = Factory.build(:account, :locale => nil)
|
||||
account.should be_valid
|
||||
account.locale.should == 'en'
|
||||
end
|
||||
|
||||
it "should validate uniqueness of email" do
|
||||
Factory(:account)
|
||||
(account = Factory.build(:account)).should_not be_valid
|
||||
account.errors[:email].should == ["is already taken"]
|
||||
end
|
||||
|
||||
## Associations ##
|
||||
|
||||
it 'should own many sites' do
|
||||
account = Factory(:account)
|
||||
site_1 = Factory(:site, :accounts => account)
|
||||
site_2 = Factory(:site, :subdomain => 'foo', :accounts => account)
|
||||
account.sites.should == [site_1, site_2]
|
||||
end
|
||||
|
||||
end
|
@ -80,6 +80,16 @@ describe Site do
|
||||
sites.should be_empty
|
||||
end
|
||||
|
||||
## Associations ##
|
||||
|
||||
it 'should have many accounts' do
|
||||
account_1 = Factory(:account)
|
||||
account_2 = Factory(:account, :name => 'homer', :email => 'homer@simpson.net')
|
||||
site = Factory(:site, :accounts => [account_1, account_2, account_1])
|
||||
site.account_ids.should == [account_1.id, account_2.id]
|
||||
site.accounts.should == [account_1, account_2]
|
||||
end
|
||||
|
||||
## Methods ##
|
||||
|
||||
it 'should return domains without subdomain' do
|
||||
@ -87,4 +97,5 @@ describe Site do
|
||||
site.domains.should == %w{www.acme.net www.acme.com acme.example.com}
|
||||
site.domains_without_subdomain.should == %w{www.acme.net www.acme.com}
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user