Better settings coupling with spec coverage
This commit is contained in:
+15
-6
@@ -1,13 +1,19 @@
|
||||
class Employee
|
||||
include SimplyStored::Couch
|
||||
include ActiveModel::SerializerSupport
|
||||
attr_reader :settings
|
||||
DEFAULT_SETTINGS = {
|
||||
'manager' => false,
|
||||
'active' => true,
|
||||
'color' => '#3a87ad'
|
||||
}
|
||||
attr_accessor *DEFAULT_SETTINGS.keys
|
||||
attr_reader :settings
|
||||
DEFAULT_SETTINGS.each do |attribute, default_value|
|
||||
define_method(attribute) { settings.public_send attribute }
|
||||
define_method("#{attribute}=") { |value| settings.set attribute, value }
|
||||
if default_value == true or default_value == false # boolean
|
||||
define_method(:"#{attribute}?"){ public_send attribute }
|
||||
end
|
||||
end
|
||||
|
||||
view :by_confirmation_token, key: :confirmation_token # devise confirmable
|
||||
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :confirmable
|
||||
@@ -35,10 +41,13 @@ class Employee
|
||||
|
||||
def enrich_with_settings(settings)
|
||||
@settings = settings
|
||||
settings.settings.each do |attribute, value|
|
||||
public_send "#{attribute}=", value
|
||||
end
|
||||
settings
|
||||
end
|
||||
alias_method :settings=, :enrich_with_settings
|
||||
|
||||
alias_method :orig_save, :save
|
||||
def save(*args)
|
||||
settings.persist!
|
||||
orig_save(*args)
|
||||
end
|
||||
|
||||
def settings
|
||||
|
||||
@@ -45,7 +45,7 @@ class Supplier
|
||||
|
||||
alias_method :non_enriced_employees, :employees
|
||||
def employees
|
||||
non_enriced_employees.tap { |es| es.each{ |e| e.enrich_with_settings(settings_for(e) ) }}
|
||||
@cached_enriched_employees ||= non_enriced_employees.tap { |es| es.each{ |e| e.enrich_with_settings(settings_for(e) ) }}
|
||||
end
|
||||
|
||||
after_create :add_section_on_create
|
||||
@@ -94,6 +94,14 @@ class Supplier
|
||||
@active_lists
|
||||
end
|
||||
|
||||
before_method :reload do
|
||||
@employees = nil
|
||||
@cached_enriched_employees = nil
|
||||
@employee_settings = nil
|
||||
@active_lists = nil
|
||||
@active_orders = nil
|
||||
end
|
||||
|
||||
|
||||
# Return the currently active tables for the supplier
|
||||
def active_tables(options = {})
|
||||
|
||||
Reference in New Issue
Block a user