Permalink
Browse files

Use new Signup model in AccountsController [#40]

  • Loading branch information...
1 parent c652f33 commit 82e269dc3f871b44db262b7ad4250f303994d9f4 @seven1m seven1m committed Oct 25, 2013
@@ -19,45 +19,22 @@ def new
elsif params[:birthday]
render action: 'new_by_birthday'
elsif Setting.get(:features, :sign_up)
- @person = Person.new
+ @signup = Signup.new
else
render text: I18n.t('pages.not_found'), layout: true, status: :not_found
end
end
def create
- if params[:person] and Setting.get(:features, :sign_up) and not bot?
- if params[:person][:email].to_s.any?
- if Person.find_by_email(params[:person][:email])
- params[:email] = params[:person][:email]
- create_by_email
- else
- attributes = {can_sign_in: false, full_access: false, visible_to_everyone: false}
- attributes.merge! params[:person].reject { |k, v| !%w(email first_name last_name gender birthday).include?(k) }
- @person = Person.new(attributes)
- if @person.adult?
- if @person.save
- @person.family = Family.create(name: @person.name, last_name: @person.last_name)
- if Setting.get(:features, :sign_up_approval_email).to_s.any?
- @person.save
- Notifier.pending_sign_up(@person).deliver
- render text: t('accounts.pending_approval'), layout: true
- else
- @person.update_attributes!(can_sign_in: true, full_access: true, visible_to_everyone: true, visible_on_printed_directory: true)
- params[:email] = @person.email
- create_by_email
- end
- else
- render action: 'new'
- end
- else
- @person.errors.add(:base, t('accounts.must_be_of_age', years: Setting.get(:system, :adult_age)))
- render action: 'new'
- end
+ if params[:signup]
+ @signup = Signup.new(params[:signup])
+ if @signup.save
+ if @signup.verification_sent?
+ render text: t('accounts.verification_email_sent'), layout: true
+ elsif @signup.approval_sent?
+ render text: t('accounts.pending_approval'), layout: true
end
else
- @person = Person.new
- @person.errors.add(:email, :invalid)
render action: 'new'
end
elsif params[:name].to_s.any? and params[:email].to_s.any? and params[:phone].to_s.any? and params[:birthday].to_s.any? and params[:notes].to_s.any?
@@ -67,7 +44,7 @@ def create
elsif params[:email].to_s.any?
create_by_email
else
- @person = Person.new
+ @signup = Signup.new
flash[:warning] = t('accounts.fill_required_fields')
render action: 'new'
end
@@ -115,7 +92,7 @@ def create_by_mobile
end
else
flash[:warning] = t('accounts.mobile_number_not_found')
- @person = Person.new
+ @signup = Signup.new
render action: 'new'
end
end
@@ -126,7 +103,7 @@ def create_by_birthday
render text: t('accounts.submission_will_be_reviewed'), layout: true
else
flash[:warning] = t('accounts.fill_required_fields')
- @person = Person.new
+ @signup = Signup.new
render action: 'new'
end
end
View
@@ -1,14 +1,15 @@
require 'active_model'
class Signup
+ include ActiveModel::Naming
include ActiveModel::Validations
PARAMS = [:email, :password, :password_confirmation, :first_name, :last_name, :gender, :birthday, :a_phone_number]
attr_accessor *PARAMS
attr_accessor :family, :person
- validates :email, :first_name, :last_name, :gender, :birthday, presence: true
+ validates :email, :first_name, :last_name, :birthday, presence: true
validate :validate_adult
validate :validate_not_a_bot
validate :validate_sign_up_allowed
@@ -36,6 +37,14 @@ def save!
save
end
+ def verification_sent?
+ !!@verification_sent
+ end
+
+ def approval_sent?
+ !!@approval_sent
+ end
+
protected
def validate_existing
@@ -71,6 +80,7 @@ def create_person
def deliver_signup_approval
Notifier.pending_sign_up(@person).deliver
+ @approval_sent = true
end
def full_access?
@@ -80,6 +90,7 @@ def full_access?
def create_and_deliver_verification
verification = Verification.create!(email: @person.email)
Notifier.email_verification(verification).deliver
+ @verification_sent = true
end
def sign_up_approval_required?
@@ -4,15 +4,15 @@
<% if Setting.get(:features, :sign_up) %>
<h2><%= t('accounts.create') %></h2>
- <%= form_for @person, url: account_path, method: 'post' do |form| %>
+ <%= form_for @signup, as: :signup, url: account_path, method: 'post' do |form| %>
<%= error_messages_for(form) %>
<p>
<%= form.label :email, t('people.email') %> <%= form.text_field :email %><br/>
<%= form.label :first_name, t('accounts.first_name') %> <%= form.text_field :first_name %><br/>
<%= form.label :last_name, t('accounts.last_name') %> <%= form.text_field :last_name %><br/>
<%= form.label :gender, t('accounts.gender') %> <%= form.select :gender, [[t('search.male'), 'Male'], [t('search.female'), 'Female']] %><br/>
<%= form.label :birthday, t('accounts.birthday') %> <%= form.date_field :birthday %><br/>
- <%= text_field_tag :a_phone_number, nil, id: 'dummy_phone' %>
+ <%= form.text_field :a_phone_number, id: 'dummy_phone' %>
<%= form.submit t('accounts.create') %>
</p>
<% end %>
@@ -88,11 +88,7 @@ def setup
context 'sign up feature disabled' do
setup do
Setting.set(1, 'Features', 'Sign Up', false)
- post :create, {person: {email: 'rick@example.com'}}
- end
-
- should 'set flash warning about required fields' do
- assert_match /required/i, flash[:warning]
+ post :create, {signup: {email: 'rick@example.com'}}
end
should 'render new template again' do
@@ -108,7 +104,7 @@ def setup
context 'spam sign up (honeypot a_phone_number field has text)' do
setup do
@count_was = Person.count
- post :create, {person: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}, a_phone_number: '1234567890'}
+ post :create, {signup: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980', a_phone_number: '1234567890'}}
end
should 'render new template' do
@@ -128,7 +124,8 @@ def setup
context 'user is an adult' do
setup do
- post :create, {person: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}}
+ post :create, {signup: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}}
+ assert assigns[:signup].errors.empty?
@person = Person.last
end
@@ -158,7 +155,7 @@ def setup
setup do
ActionMailer::Base.deliveries.clear
@count_was = Person.count
- post :create, {person: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: Date.today.to_s}}
+ post :create, {signup: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: Date.today.to_s}}
end
should 'not send email' do
@@ -174,15 +171,16 @@ def setup
end
should 'add an error to the record' do
- assert assigns[:person].errors[:base]
+ assert assigns[:signup].errors[:base]
end
end
end
context 'sign up approval required' do
setup do
Setting.set(1, 'Features', 'Sign Up Approval Email', 'admin@example.com')
- post :create, {person: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}}
+ post :create, {signup: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}}
+ assert assigns[:signup].errors.empty?
@person = Person.last
end
@@ -194,10 +192,10 @@ def setup
assert_equal 'rick@example.com', @person.email
end
- should 'create a new family' do
- assert @person.family
- assert_equal @person.name, @person.family.name
- end
+ should 'create a new family' do
+ assert @person.family
+ assert_equal @person.name, @person.family.name
+ end
should 'set can_sign_in=false' do
refute @person.can_sign_in?
@@ -212,7 +210,7 @@ def setup
context 'sign up with existing user email' do
setup do
@existing = FactoryGirl.create(:person, email: 'rick@example.com')
- post :create, {person: {email: 'rick@example.com'}}
+ post :create, {signup: {email: 'rick@example.com', first_name: 'Rick', last_name: 'Smith', birthday: '4/1/1980'}}
end
should 'send email verification email' do
@@ -226,15 +224,15 @@ def setup
context 'sign up missing name' do
setup do
- post :create, {person: {email: 'rick@example.com', birthday: '4/1/1980'}}
+ post :create, {signup: {email: 'rick@example.com', birthday: '4/1/1980'}}
end
should 'render the new template again' do
assert_template :new
end
- should 'fail to save the person' do
- assert assigns['person'].errors.any?
+ should 'fail to save the signup' do
+ assert assigns['signup'].errors.any?
end
end
end
@@ -653,11 +651,11 @@ def setup
should "create account with birthday in american date format" do
Setting.set(1, 'Features', 'Sign Up', true)
Setting.set(1, 'Formats', 'Date', '%m/%d/%Y')
- post :create, {person: {email: 'bob@example.com',
- first_name: 'Bob',
- last_name: 'Morgan',
- gender: 'Male',
- birthday: '01/02/1980'}}
+ post :create, {signup: {email: 'bob@example.com',
+ first_name: 'Bob',
+ last_name: 'Morgan',
+ gender: 'Male',
+ birthday: '01/02/1980'}}
assert_response :success
assert bob = Person.find_by_email('bob@example.com')
assert_equal '01/02/1980', bob.birthday.strftime('%m/%d/%Y')
@@ -666,11 +664,11 @@ def setup
should "create account with birthday in european date format" do
Setting.set(1, 'Features', 'Sign Up', true)
Setting.set(1, 'Formats', 'Date', '%d/%m/%Y')
- post :create, {person: {email: 'bob@example.com',
- first_name: 'Bob',
- last_name: 'Morgan',
- gender: 'Male',
- birthday: '02/01/1980'}}
+ post :create, {signup: {email: 'bob@example.com',
+ first_name: 'Bob',
+ last_name: 'Morgan',
+ gender: 'Male',
+ birthday: '02/01/1980'}}
assert_response :success
assert bob = Person.find_by_email('bob@example.com')
assert_equal 'Jan 02, 1980', bob.birthday.strftime('%b %d, %Y')
View
@@ -82,6 +82,18 @@ class SignupTest < ActiveSupport::TestCase
should 'deliver email verification email to user' do
assert_equal ['Verify Email'], Notifier.deliveries.map(&:subject)
end
+
+ context '#verification_sent?' do
+ should 'return true' do
+ assert_equal true, @signup.verification_sent?
+ end
+ end
+
+ context '#approval_sent?' do
+ should 'return false' do
+ assert_equal false, @signup.approval_sent?
+ end
+ end
end
end
@@ -203,6 +215,18 @@ class SignupTest < ActiveSupport::TestCase
assert_equal true, @return
end
+ context '#verification_sent?' do
+ should 'return false' do
+ assert_equal false, @signup.verification_sent?
+ end
+ end
+
+ context '#approval_sent?' do
+ should 'return true' do
+ assert_equal true, @signup.approval_sent?
+ end
+ end
+
context 'created person' do
setup do
@person = @signup.person

0 comments on commit 82e269d

Please sign in to comment.