Skip to content

Instantly share code, notes, and snippets.

@RemiBa
Created November 8, 2012 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RemiBa/4037771 to your computer and use it in GitHub Desktop.
Save RemiBa/4037771 to your computer and use it in GitHub Desktop.
Test data wrong
<%= form_for @customer do |c|%>
<% if @customer.errors.any?%>
<div id="form_errors">
<ul>
<% @customer.errors.full_messages.each do |msg| %>
<li>
<%= msg %>
</li>
<%end%>
</ul>
</div>
<% end %>
<p>
<%= c.label :id%>
<%= c.text_field :id%>
</p>
<p>
<%= c.label :name %>
<%= c.text_field :name%>
</p>
<h3>Please ad a valid e-mail to receive your credentials:</h3>
<p>
<%= c.label :email%>
<%= c.text_field :email%>
</p>
<p>
<%= c.label :email_confirmation %>
<%= c.text_field :email_confirmation %>
</p>
<p class="button">
<%= c.submit "Create account"%>
</p>
<% end %>
require 'bcrypt'
class Customer < ActiveRecord::Base
include BCrypt
attr_accessible :email
attr_accessor :is_signup
validates :email, :presence => true, :confirmation => true, :format => {:with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/}, :uniqueness => true
def self.authenticate(login, password)
#1. Find an authentication with this login and password
#From the first 7 digits of the login, I can find the customer + authentication
customerid = login[0,7]
customer = Customer.where('id = ? OR email = ?', customerid, login).first
if customer
if customer.authentication && Password.new(customer.authentication.password) == password
return customer
else
return nil
end
else
return nil
end
end
def init_reset()
self.reset_token = nil
self.reset_token_init = nil
if self.save
return true
else
return false
end
end
def create_authentication
authentication = Authentication.new
login = self.id.to_s << self.zipcode.to_s
randPassword = RandomPasswordGenerator.generate(8, :skip_symbols => true)
authentication.new_authentication(login,self.id, randPassword)
contactperson = ContactPerson.find(self.contact_person_id)
if contactperson
#Contactperson found, add to this customer
if self.authentication = authentication
#Authentication saved!
#Send mail with information to the customer
#Mail includes: login, password
CustomerMailer.signup_succeeded(self, randPassword).deliver
#Send mail with information to the contact_person
#Mail includes: customer_name, customer_number, customer_email, customer_login
CustomerMailer.signup_contactInform(self, contactperson).deliver
success = [:success => true,:message => "Successfully signed up! Please view your e-mail for further instructions."]
return success
else
success = [:success => false,:message => "Error saving your authentciation, contact the administrator."]
return success
end
else
success = [:success => false,:message => "Error finding your contactperson, contact the administrator."]
return success
end
end
def fill_standard(id, name, email, confirmation)
self.id = id
self.name = name
self.email = email
self.email_confirmation = confirmation
end
def send_reset_email
randPassword = RandomPasswordGenerator.generate(8, :skip_symbols => true)
self.reset_token = randPassword
self.reset_token_init = Time.zone.now
save!
CustomerMailer.reset_email(self).deliver
end
has_one :authentication
belongs_to :contact_person
belongs_to :country
accepts_nested_attributes_for :authentication
end
def create
@customer = Customer.new()
@customer.fill_standard(params[:customer][:id],params[:customer][:name], params[:customer][:email],params[:customer][:email_confirmation])
dbCustomer = Customer.where("id = ?", @customer.id).first
if dbCustomer
#CUSTOMERNUMBER CORRECT
#Check if the input is matching!
if @customer.id == dbCustomer.id && @customer.name == dbCustomer.name
#INPUT IS MATCHING
#Check if the customer already has an account
auth = Authentication.where('customer_id = ? ', dbCustomer.id).first
if auth.class != Authentication
#TEST
#let the customer create his authentication
#Customer doesn't have an account yet!
#Update the customer with an e-mail:
unless dbCustomer.update_email(@customer.email, @customer.email_confirmation)
@customer = dbCustomer
@customer.is_signup = true
render :action => :edit
return
end
success = dbCustomer.create_authentication
if success[0][:success]
#AUTHENTICATION CREATED
flash[:notice] = success[0][:message]
redirect_to login_path
else
#Error creating authentication
flash[:notice] = success[0][:message]
redirect_to login_path
end
else
#User has already an account
flash[:notice] = "You already have an account."
redirect_to login_path
end
else
#INPUT MATCHING FAILED
#Give message there is no such customer in the database
flash[:notice] = "The customer with this specifications was not found... (" << params.to_s << ")"
redirect_to signup_path
end
else
#CUSTOMERNUMBER IS NOT CORRECT
#Give message there is no such customer in the database
flash[:notice] = "The customer with this specifications was not found... (" << params.to_s << ")"
redirect_to signup_path
end
end
def update
dbCustomer = Customer.find_by_id(params[:id])
@customer = dbCustomer
if dbCustomer.authentication
#Customer already exists
else
@customer = Customer.new()
@customer.fill_standard(params[:customer][:id],params[:customer][:name], params[:customer][:email],params[:customer][:email_confirmation])
#NO AUTHENTICATION YET SO PLEASE MAKE THE AUTHENTICATION!
dbCustomer.email = params[:customer][:email]
dbCustomer.email_confirmation = params[:customer][:email_confirmation]
unless dbCustomer.save
render :action => :edit
return
end
success = dbCustomer.create_authentication
if success[0][:success]
#AUTHENTICATION CREATED
flash[:notice] = success[0][:message]
redirect_to login_path
else
#Error creating authentication
flash[:notice] = success[0][:message]
redirect_to signup_path
end
end
end
def edit
@customer ||= Customer.find(session[:customer_id]) if session[:customer_id]
if @customer
if @customer.authentication.login_count == nil
@customer.is_signup = false
end
else
@customer = Customer.new
@customer.is_signup = true
end
end
require 'spec_helper'
describe "customers" do
let(:dbCustomer) {FactoryGirl.create(:signedup_customer)}
describe "signup" do
it "has right data" do
visit signup_path
fill_in :id, :with => dbCustomer.id
fill_in :name, :with => dbCustomer.name
fill_in :email, :with => dbCustomer.email
fill_in :email_confirmation, :with => dbCustomer.email
click_button "Create account"
#current_path.should eq(login_path)
page.should have_content("Forgot")
end
end
FactoryGirl.define do
factory :signedup_customer, class: Customer do
id 2110001
name "Customername"
email "my.email@company.be"
address_1 "address1"
address_2 "address2"
zipcode 62
city "Bragha"
currency "PLN"
country_id "PL"
contact_person_id "AZU"
reset_token nil
reset_token_init nil
end
end
1) customers signup has right data
←[31mFailure/Error:←[0m ←[31mpage.should have_content("Forgot")←[0m
←[31mexpected there to be content "Forgot" in "Customer portal\n\t\n\t\tHome\n\t\t\n\t\tThe customer with this specificati
ons was not found... ({\"utf8\"=>\"√\", \"customer\"=>{\"id\"=>\"my.email@company.be\", \"name\"=>\"\", \"email\"=>\"\", \"email_co
nfirmation\"=>\"\"}, \"commit\"=>\"Create account\", \"action\"=>\"create\", \"controller\"=>\"customers\"})\n\t\n\t\t\n\t\n\t\n\t\t\t\t\tP
lease sign up to access the customer portal\n\t\t\t\n \t\n \t\tId\n \t\t\n \t\n \t\tName\n \t\t\n \tPlease ad a valid e-mail to rece
ive your credentials:\n \t\n \t\tEmail\n \t\t\n \t\n \t\tEmail confirmation\n \t\t\n \t\n \t\t\n \n\n\t\n\t\n\t\t\n\t\n\t\n\t\n"←[
0m
←[36m # ./spec/requests/customers_spec.rb:14:in `block (3 levels) in <top (required)>'←[0m
@RemiBa
Copy link
Author

RemiBa commented Nov 8, 2012

Why is in my customer the id=my.email@company.be?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment