diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-02-15 13:33:09 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2009-02-15 13:33:09 +0000 |
commit | 4601ed2f3aebc545024287e318494ce32bc5803f (patch) | |
tree | 40f4bda265bbc92f9ddaed1bca6deb7769f2ccae /app/models/enumeration.rb | |
parent | 13e2c727cfa7b3a9e3e9bd0563cda0a15ee3892d (diff) | |
download | redmine-4601ed2f3aebc545024287e318494ce32bc5803f.tar.gz redmine-4601ed2f3aebc545024287e318494ce32bc5803f.zip |
Replaces Enumeration.get_values and Enumeration.default with named scopes.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@2472 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/enumeration.rb')
-rw-r--r-- | app/models/enumeration.rb | 26 |
1 files changed, 19 insertions, 7 deletions
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 |