End of day commit

This commit is contained in:
2015-02-18 22:36:47 +01:00
parent 82670f271b
commit c1858455e9
68 changed files with 633 additions and 145 deletions
+11
View File
@@ -0,0 +1,11 @@
class Ability
include CanCan::Ability
def initialize(settings)
settings ||= SupplierEmployeesSettings.new(Supplier.new).for_employee(nil)
if settings.manager?
can :manage, :all
else
can :read, :all
end
end
end
+46
View File
@@ -0,0 +1,46 @@
class NewSupplier
include ActiveAttr::Model
attr_accessor :current_employee
attribute :supplier_name
attribute :email
attribute :password
attribute :password_confirmation
without_current_supplier = { unless: 'current_employee.present?' }
validates :supplier_name, presence: true
validates :email, email: without_current_supplier
validates :password, presence: without_current_supplier, confirmation: without_current_supplier
validate :supplier_name_uniqueness
validate :employee_uniqueness
def save
if valid?
persist!
true
else
false
end
end
private
def persist!
employee = Employee.new unconfirmed_email: email, password: password, password_confirmation: password_confirmation
raise "Cannot create employee with #{attributes.inspect}" unless employee.save
supplier = Supplier.create name: supplier_name
supplier.add_manager employee
end
def employee_uniqueness
return if current_employee.present?
errors.add :email, :taken if Employee.count_by_email(email) > 0
end
def supplier_name_uniqueness
return unless current_employee.present? and supplier_name.present?
current_suppliers = current_employee.suppliers
errors.add :base, 'You already have a supplier with that name' if current_suppliers.map(&:name).include? supplier_name
end
end
+23
View File
@@ -0,0 +1,23 @@
NullObject = Naught.build do |config|
config.black_hole
config.define_explicit_conversions
config.define_implicit_conversions
end
class NullObject
def presence
nil
end
def present?
false
end
def blank?
true
end
def empty?
true
end
def any?
false
end
end