Better settings coupling with spec coverage

This commit is contained in:
2015-02-20 11:51:35 +01:00
parent adeedb2f1b
commit cde551dc7f
6 changed files with 102 additions and 19 deletions
+9 -12
View File
@@ -6,13 +6,13 @@ class SupplierEmployeesSettings
@employee_ids = supplier.employee_ids || []
end
def for_employee(employee_id)
employee_id = employee_id.id if employee_id.is_a?(Employee)
if employee_ids.include? employee_id
SupplierEmployeeSettings.new(self, employee_id, dictionary[employee_id])
def for_employee(employee)
if employee_ids.include? employee.id
settings = SupplierEmployeeSettings.new(self, employee, dictionary[employee.id])
else
SupplierEmployeeSettings::NullObject.new
settings = SupplierEmployeeSettings::NullObject.new
end
employee.settings = settings
end
def [](val)
@@ -48,7 +48,7 @@ class SupplierEmployeesSettings
class SupplierEmployeeSettings
delegate :as_json, :to_json, to: :settings
attr_reader :id, :settings, :all_employees_settings
attr_reader :employee, :settings, :all_employees_settings
class NullObject < ::NullObject
::Employee::DEFAULT_SETTINGS.each do |attribute, value|
if value == true or value == false
@@ -58,9 +58,9 @@ class SupplierEmployeesSettings
end
end
def initialize(all_employees_settings, employee_id, settings = {})
def initialize(all_employees_settings, employee, settings = {})
@all_employees_settings = all_employees_settings
@id = employee_id
@employee = employee
@settings = Employee.default_settings.merge(settings || {})
end
@@ -70,10 +70,7 @@ class SupplierEmployeesSettings
def set(attribute, value, persist: false)
settings[attribute] = value
all_employees_settings[id][attribute] = value
if employee = all_employees_settings.supplier.employees.find{|e| e.id == id}
employee.public_send("#{attribute}=", value)
end
all_employees_settings[employee.id][attribute] = value
persist! if persist
self
end