require 'spec_helper' describe Employee do context 'class methods' do describe '.count_by_email' do it 'is zero without employees' do described_class.count_by_email('admin@example.com').should be_zero end it 'is one when an email already exists' do create :employee, email: 'alreadyexisting@example.com' described_class.count_by_email('alreadyexisting@example.com').should be 1 end end end describe 'settings' do let(:supplier){ build :supplier } let(:employee){ create :employee} before do supplier.add_employee employee employee.enrich_with_settings supplier.settings_for(employee) end it 'responds to defaults without full initialization (yes this test is needed)' do Employee.new.manager?.should be false end it 'responds to defaults' do employee.manager?.should be false employee.active?.should be true employee.color.should == '#3a87ad' end it 'Sets the values non persisted on the supplier' do employee.manager = true supplier.employee_settings_storage[employee.id]['manager'].should be true supplier.reload supplier.employee_settings_storage.should_not be_present end it 'persists settings of the supplier on save' do employee.manager = true employee.save supplier.reload supplier.employee_settings_storage[employee.id]['manager'].should be true end it 'does not non existant suppliers on save' do employee.manager = true ->{ employee.save }.should_not change{ Supplier.count } end it 'sets attributes on the employee if fetched through the supplier' do settings = supplier.settings_for employee settings.is_manager! employee.manager?.should be true end it 'Implements settings on employees' do supplier.update employee_settings_storage: {employee.id => { 'manager' => true }} supplier.reload # Without the acessor employees on the supplier, the employee and therefore its settings are not linked to the supplier linked_employee = supplier.employees.find{|e| e.id == employee.id } linked_employee.should be_manager end end end