diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/document.rb | 2 | ||||
-rw-r--r-- | app/models/enumeration.rb | 26 | ||||
-rw-r--r-- | app/models/issue.rb | 2 | ||||
-rw-r--r-- | app/models/time_entry.rb | 2 |
4 files changed, 22 insertions, 10 deletions
diff --git a/app/models/document.rb b/app/models/document.rb index 2ec99fe07..f78c15e98 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -31,7 +31,7 @@ class Document < ActiveRecord::Base def after_initialize if new_record? - self.category ||= Enumeration.default('DCAT') + self.category ||= Enumeration.document_categories.default end end end diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index e4b080be1..d466940b7 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -26,17 +26,29 @@ class Enumeration < ActiveRecord::Base # Single table inheritance would be an option OPTIONS = { - "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id}, - "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_id}, - "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id} + "IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_id, :scope => :priorities}, + "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_id, :scope => :document_categories}, + "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id, :scope => :activities} }.freeze - def self.get_values(option) - find(:all, :conditions => {:opt => option}, :order => 'position') + # Creates a named scope for each type of value. The scope has a +default+ method + # that returns the default value, or nil if no value is set as default. + # Example: + # Enumeration.priorities + # Enumeration.priorities.default + OPTIONS.each do |k, v| + next unless v[:scope] + named_scope v[:scope], :conditions => { :opt => k }, :order => 'position' do + def default + find(:first, :conditions => { :is_default => true }) + end + end end - def self.default(option) - find(:first, :conditions => {:opt => option, :is_default => true}, :order => 'position') + named_scope :values, lambda {|opt| { :conditions => { :opt => opt }, :order => 'position' } } do + def default + find(:first, :conditions => { :is_default => true }) + end end def option_name diff --git a/app/models/issue.rb b/app/models/issue.rb index 618c5597d..46a28708d 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -63,7 +63,7 @@ class Issue < ActiveRecord::Base if new_record? # set default values for new records only self.status ||= IssueStatus.default - self.priority ||= Enumeration.default('IPRI') + self.priority ||= Enumeration.priorities.default end end diff --git a/app/models/time_entry.rb b/app/models/time_entry.rb index f10b179d1..4cb76441b 100644 --- a/app/models/time_entry.rb +++ b/app/models/time_entry.rb @@ -37,7 +37,7 @@ class TimeEntry < ActiveRecord::Base def after_initialize if new_record? && self.activity.nil? - if default_activity = Enumeration.default('ACTI') + if default_activity = Enumeration.activities.default self.activity_id = default_activity.id end end |