Fix employee adding

This commit is contained in:
2015-02-27 11:09:16 +01:00
parent 6e805fed3e
commit bfc0f336d1
13 changed files with 137 additions and 21 deletions
+2 -2
View File
@@ -107,8 +107,8 @@ end
group :test do group :test do
#gem 'capybara-webkit' #, '~>0.14.2' # version 1.1.0 does not yet compile in mavericks #gem 'capybara-webkit' #, '~>0.14.2' # version 1.1.0 does not yet compile in mavericks
gem 'selenium-webdriver' gem 'selenium-webdriver', '~> 2.45.0.dev3'
#gem 'poltergeist' gem 'poltergeist'
gem 'capybara-screenshot' gem 'capybara-screenshot'
gem 'turnip' gem 'turnip'
gem 'rspec-its' gem 'rspec-its'
+11 -4
View File
@@ -135,6 +135,7 @@ GEM
ffi (~> 1.0, >= 1.0.11) ffi (~> 1.0, >= 1.0.11)
climate_control (0.0.3) climate_control (0.0.3)
activesupport (>= 3.0) activesupport (>= 3.0)
cliver (0.3.2)
cocaine (0.5.5) cocaine (0.5.5)
climate_control (>= 0.0.3, < 1.0) climate_control (>= 0.0.3, < 1.0)
coderay (1.1.0) coderay (1.1.0)
@@ -300,6 +301,11 @@ GEM
activesupport (>= 3.0.0) activesupport (>= 3.0.0)
cocaine (~> 0.5.3) cocaine (~> 0.5.3)
mime-types mime-types
poltergeist (1.6.0)
capybara (~> 2.1)
cliver (~> 0.3.1)
multi_json (~> 1.0)
websocket-driver (>= 0.2.0)
pry (0.10.1) pry (0.10.1)
coderay (~> 1.1.0) coderay (~> 1.1.0)
method_source (~> 0.8.1) method_source (~> 0.8.1)
@@ -371,14 +377,14 @@ GEM
ruby-progressbar (1.7.1) ruby-progressbar (1.7.1)
rubyzip (1.1.7) rubyzip (1.1.7)
safe_yaml (1.0.4) safe_yaml (1.0.4)
sass (3.4.12) sass (3.4.13)
sass-rails (5.0.1) sass-rails (5.0.1)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
sass (~> 3.1) sass (~> 3.1)
sprockets (>= 2.8, < 4.0) sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
tilt (~> 1.1) tilt (~> 1.1)
selenium-webdriver (2.44.0) selenium-webdriver (2.45.0.dev3)
childprocess (~> 0.5) childprocess (~> 0.5)
multi_json (~> 1.0) multi_json (~> 1.0)
rubyzip (~> 1.0) rubyzip (~> 1.0)
@@ -398,7 +404,7 @@ GEM
railties (>= 3.1, < 5.0) railties (>= 3.1, < 5.0)
slim (~> 3.0) slim (~> 3.0)
slop (3.6.0) slop (3.6.0)
spring (1.3.2) spring (1.3.3)
spring-commands-rspec (1.0.4) spring-commands-rspec (1.0.4)
spring (>= 0.9.1) spring (>= 0.9.1)
sprockets (2.12.3) sprockets (2.12.3)
@@ -488,6 +494,7 @@ DEPENDENCIES
omniauth-facebook omniauth-facebook
omniauth-instagram omniauth-instagram
paperclip paperclip
poltergeist
pry-rails pry-rails
quiet_assets quiet_assets
rack-cors rack-cors
@@ -496,7 +503,7 @@ DEPENDENCIES
rspec-its rspec-its
rspec-rails rspec-rails
sass-rails (~> 5.0.0) sass-rails (~> 5.0.0)
selenium-webdriver selenium-webdriver (~> 2.45.0.dev3)
simplecov simplecov
simply_stored! simply_stored!
slim-rails slim-rails
@@ -30,7 +30,7 @@ module Suppliers
raise CanCan::AccessDenied unless current_employee.active? raise CanCan::AccessDenied unless current_employee.active?
@current_ability = ::Ability.new( current_employee ) @current_ability = ::Ability.new( current_employee )
Array.wrap(after_authentication_hooks).each do |hook| Array.wrap(after_authentication_hooks).each do |hook|
next if hook[:options][:only].present? && !Array.wrap(hook[:options][:only].include?(action_name.to_sym)) next if hook[:options][:only].present? && !Array.wrap(hook[:options][:only]).include?(action_name.to_sym)
instance_eval &hook[:block] instance_eval &hook[:block]
end end
end end
@@ -20,10 +20,23 @@ module Suppliers
# POST /employees # POST /employees
# POST /employees.json # POST /employees.json
def create def create
@employee = Employee.find_by_email(employee_params[:email]) if employee_params[:email].present? valid = false
if existing_employee = Employee.find_by_email(employee_params[:email])
@employee = existing_employee
if valid = @employee.valid?
current_supplier.add_employee @employee
@employee.update_attributes employee_params
end
else
@employee = Employee.new(employee_params)
@employee.password = SecureRandom.hex(8)
if valid = @employee.save
current_supplier.add_employee @employee
@employee.update_attributes employee_params # needed for supplier specific settings
end
end
if @employee.save if valid
current_supplier.add_employee @employee unless current_supplier.employee_ids.include? @employee.id # already linked
render json: @employee, serializer: Suppliers::EmployeeSerializer, status: :created render json: @employee, serializer: Suppliers::EmployeeSerializer, status: :created
else else
render json: {errors: @employee.errors}, status: :unprocessable_entity render json: {errors: @employee.errors}, status: :unprocessable_entity
+7 -1
View File
@@ -67,11 +67,17 @@ class Supplier
def add_manager(employee) def add_manager(employee)
add_employee employee unless employee_ids.include? employee.id add_employee employee unless employee_ids.include? employee.id
settings = employee_settings.for_employee(employee) settings = settings_for(employee)
settings.is_manager! settings.is_manager!
save and employee save and employee
end end
after_method :add_employee do |result, employee|
@employee_settings = nil
employee.settings = settings_for(employee)
result
end
def employee_settings def employee_settings
@employee_settings ||= SupplierEmployeesSettings.new(self) @employee_settings ||= SupplierEmployeesSettings.new(self)
end end
@@ -112,6 +112,10 @@ class SupplierEmployeesSettings
all_employees_settings.supplier.save all_employees_settings.supplier.save
end end
def supplier
all_employees_settings.supplier
end
# Parse a method name to its underlying operations # Parse a method name to its underlying operations
# settings.is_manager? # settings.is_manager?
# is a getter for the attribute manager # is a getter for the attribute manager
+3 -2
View File
@@ -8,7 +8,7 @@ module MethodPrependAndAppend
def before_method(m, &blk) def before_method(m, &blk)
alias_method :"#{m}_before_extending_before", m alias_method :"#{m}_before_extending_before", m
define_method m do |*args| define_method m do |*args|
instance_eval(&blk) instance_exec(*args, &blk)
send :"#{m}_before_extending_before", *args send :"#{m}_before_extending_before", *args
end end
end end
@@ -17,7 +17,8 @@ module MethodPrependAndAppend
alias_method :"#{m}_before_extending_after", m alias_method :"#{m}_before_extending_after", m
define_method m do |*args| define_method m do |*args|
result = send :"#{m}_before_extending_after", *args result = send :"#{m}_before_extending_after", *args
instance_eval(result, &blk) args.unshift result
instance_exec(*args, &blk)
end end
end end
end end
+34 -1
View File
@@ -1,5 +1,38 @@
Feature: Manage employees Feature: Manage employees
@javascript
Scenario: create an not yet existing extra employee
Given there is a confirmed and open supplier
And I am signed in as supplier
When the supplier employee visits the 'employees' path
And I click on selector '.new-employee-button'
And the supplier emloyee sets the 'name' to 'New employee' in the employee popup
And the supplier emloyee sets the 'email' to 'new-employee@example.com' in the employee popup
And I click on selector '.form-row.manager .switch'
And I click on selector '.modal-confirm'
And I wait 1 second
Then the new employee should be added to the supplier
@javascript
Scenario: adding an already existing employee that was firstly added as manager without manager and active
Given there is a confirmed and open supplier
And I am signed in as supplier
When the supplier employee visits the 'employees' path
And I click on selector '.new-employee-button'
And the supplier emloyee sets the 'name' to 'Existing employee' in the employee popup
And the supplier emloyee sets the 'email' to 'existing-employee@example.com' in the employee popup
And I click on selector '.form-row.manager .switch'
And I click on selector '.modal-confirm'
And the supplier employee clicks on the existing employee table destroy button
And I click on selector '.modal-confirm'
And I click on selector '.new-employee-button'
And the supplier emloyee sets the 'name' to 'Existing employee readded' in the employee popup
And the supplier emloyee sets the 'email' to 'existing-employee@example.com' in the employee popup
And I click on selector '.form-row.active .switch'
And I click on selector '.modal-confirm'
And I wait 1 second
Then the new employee should be re-added to the supplier as non manager non active
@javascript @javascript
Scenario: a just signed up manager can manage employees Scenario: a just signed up manager can manage employees
Given there is a confirmed and open supplier Given there is a confirmed and open supplier
@@ -7,7 +40,7 @@ Feature: Manage employees
And I am signed in as supplier And I am signed in as supplier
When the supplier employee visits the 'employees' path When the supplier employee visits the 'employees' path
And the supplier employee clicks on the other employee table edit button And the supplier employee clicks on the other employee table edit button
And the supplier emloyee sets the 'name' to 'Edited employee' And the supplier emloyee sets the 'name' to 'Edited employee' in the employee popup
And the supplier emloyee clicks the active switch to deactivate the employee And the supplier emloyee clicks the active switch to deactivate the employee
And the supplier employee clicks the confirm modal button And the supplier employee clicks the confirm modal button
Then the other employee should no longer be active for the current supplier Then the other employee should no longer be active for the current supplier
@@ -8,7 +8,13 @@ step "the supplier employee clicks on the other employee table edit button" do
all('tr').last.find('.table-edit').click all('tr').last.find('.table-edit').click
end end
step "the supplier emloyee sets the :attribute to :value" do |attribute, value| step "the supplier employee clicks on the existing employee table destroy button" do
find 'table.table'
sleep 0.2
all('tr').last.find('.table-destroy').click
end
step "the supplier emloyee sets the :attribute to :value in the employee popup" do |attribute, value|
js_set_field ".form-row.#{attribute} input", value js_set_field ".form-row.#{attribute} input", value
end end
@@ -21,3 +27,24 @@ step "the other employee should no longer be active for the current supplier" do
@supplier.reload @supplier.reload
@supplier.settings_for(@other_employee).active?.should be false @supplier.settings_for(@other_employee).active?.should be false
end end
step "the new employee should be added to the supplier" do
@new_employee = Employee.find_by_email('new-employee@example.com')
@new_employee.should be_present
@new_employee.name.should eq 'New employee'
@supplier.reload
settings = @supplier.settings_for @new_employee
settings.is_manager?.should be true
settings.is_active?.should be true
end
step "the new employee should be re-added to the supplier as non manager non active" do
# Non active for a non default setting change to be recorded
@existing_employee = Employee.find_by_email('existing-employee@example.com')
@existing_employee.should be_present
@existing_employee.name.should eq 'Existing employee readded'
@supplier.reload
settings = @supplier.settings_for @existing_employee
settings.is_manager?.should be false
settings.is_active?.should be false
end
+1 -1
View File
@@ -1,7 +1,7 @@
FactoryGirl.define do FactoryGirl.define do
factory :employee do factory :employee do
sequence(:name){|i| "Employee #{i}"} sequence(:name){|i| "Employee #{i}"}
email 'employee@mozo.bar' sequence(:email){ |i| "employee#{i}@mozo.bar" }
password 'secret' password 'secret'
trait :confirmed do trait :confirmed do
+22 -1
View File
@@ -124,9 +124,30 @@ describe Supplier do
settings = supplier.employee_settings.for_employee(employee) settings = supplier.employee_settings.for_employee(employee)
settings.manager?.should be true settings.manager?.should be true
reloaded_supplier = Supplier.find(supplier.id) reloaded_supplier = Supplier.find(supplier.id)
settings = reloaded_supplier.employee_settings.for_employee(employee) settings = reloaded_supplier.settings_for(employee)
settings.manager?.should be true
end
it "Adds another manager (second appears to work different as the first)" do
supplier.add_manager employee
new_employee = create :employee, email: 'new-employee@example.com'
supplier.add_manager new_employee
reloaded_supplier = Supplier.find(supplier.id)
settings = reloaded_supplier.settings_for(new_employee)
settings.manager?.should be true settings.manager?.should be true
end end
end end
describe '#add_employee' do
it 'resets the settings to the supplier settings' do
employee = create :employee
expect( employee.settings ).not_to be_present
expect( employee.settings.supplier ).not_to be_present
supplier.add_employee employee
new_supplier_referenced = (employee.settings.supplier == supplier)
new_supplier_referenced.should be true
end
end
end end
+6 -4
View File
@@ -10,7 +10,7 @@ require 'turnip/capybara'
require 'in_memory_q_counter' require 'in_memory_q_counter'
require 'capybara-screenshot/rspec' require 'capybara-screenshot/rspec'
require 'webmock/rspec' require 'webmock/rspec'
#require 'capybara/poltergeist' require 'capybara/poltergeist'
# Requires supporting ruby files with custom matchers and macros, etc, # Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories. # in spec/support/ and its subdirectories.
@@ -21,9 +21,9 @@ Dir.glob("spec/acceptance_steps/**/*steps.rb") { |f| load f, true }
I18n.locale =I18n.default_locale I18n.locale =I18n.default_locale
Devise.stretches = 1 Devise.stretches = 1
#Capybara.javascript_driver = :webkit #Capybara.javascript_driver = :webkit
#Capybara.javascript_driver = :poltergeist Capybara.javascript_driver = :poltergeist
Capybara.javascript_driver = :selenium #Capybara.javascript_driver = :selenium
Capybara.default_wait_time = 4 # ember needs more time than the default of 2 Capybara.default_wait_time = 3 # ember needs more time than the default of 2
Capybara::Screenshot.webkit_options = { width: 1024, height: 768 } Capybara::Screenshot.webkit_options = { width: 1024, height: 768 }
WebMock.disable_net_connect!(allow_localhost: true) WebMock.disable_net_connect!(allow_localhost: true)
@@ -169,12 +169,14 @@ RSpec.configure do |config|
end end
config.after :suite do config.after :suite do
=begin
rspec_outfile = Rails.root.join('coverage/rspec_results.html') rspec_outfile = Rails.root.join('coverage/rspec_results.html')
result = File.read rspec_outfile result = File.read rspec_outfile
replacement = %|<body><div><a href="index.html" style="padding:4px 8px;background-color:#393;color:white;font-weight:bold;border:2px #050 outset">Coverage</a></div>| replacement = %|<body><div><a href="index.html" style="padding:4px 8px;background-color:#393;color:white;font-weight:bold;border:2px #050 outset">Coverage</a></div>|
result.gsub! /<body>/, replacement result.gsub! /<body>/, replacement
File.open(rspec_outfile, 'w'){|f| f.puts result} File.open(rspec_outfile, 'w'){|f| f.puts result}
`open #{rspec_outfile}` `open #{rspec_outfile}`
=end
end end
# If true, the base class of anonymous controllers will be inferred # If true, the base class of anonymous controllers will be inferred
+2
View File
@@ -4,6 +4,8 @@ Release
Supplier Supplier
-------- --------
- Fix employee creation
- Fix add manager functionality
- Link employee to orders - Link employee to orders
- Employee personal schedule - Employee personal schedule
- english emails - english emails