summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2009-02-15 13:33:09 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2009-02-15 13:33:09 +0000
commit4601ed2f3aebc545024287e318494ce32bc5803f (patch)
tree40f4bda265bbc92f9ddaed1bca6deb7769f2ccae /app
parent13e2c727cfa7b3a9e3e9bd0563cda0a15ee3892d (diff)
downloadredmine-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')
-rw-r--r--app/controllers/documents_controller.rb2
-rw-r--r--app/controllers/enumerations_controller.rb2
-rw-r--r--app/controllers/issues_controller.rb8
-rw-r--r--app/controllers/reports_controller.rb4
-rw-r--r--app/helpers/timelog_helper.rb2
-rw-r--r--app/models/document.rb2
-rw-r--r--app/models/enumeration.rb26
-rw-r--r--app/models/issue.rb2
-rw-r--r--app/models/time_entry.rb2
-rw-r--r--app/views/documents/_form.rhtml2
-rw-r--r--app/views/enumerations/list.rhtml2
-rw-r--r--app/views/issues/bulk_edit.rhtml2
12 files changed, 34 insertions, 22 deletions
diff --git a/app/controllers/documents_controller.rb b/app/controllers/documents_controller.rb
index 2d1c414c9..965614149 100644
--- a/app/controllers/documents_controller.rb
+++ b/app/controllers/documents_controller.rb
@@ -54,7 +54,7 @@ class DocumentsController < ApplicationController
end
def edit
- @categories = Enumeration::get_values('DCAT')
+ @categories = Enumeration.document_categories
if request.post? and @document.update_attributes(params[:document])
flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'show', :id => @document
diff --git a/app/controllers/enumerations_controller.rb b/app/controllers/enumerations_controller.rb
index 50521bab8..6e136fba4 100644
--- a/app/controllers/enumerations_controller.rb
+++ b/app/controllers/enumerations_controller.rb
@@ -85,7 +85,7 @@ class EnumerationsController < ApplicationController
redirect_to :action => 'index'
end
end
- @enumerations = Enumeration.get_values(@enumeration.opt) - [@enumeration]
+ @enumerations = Enumeration.values(@enumeration.opt) - [@enumeration]
#rescue
# flash[:error] = 'Unable to delete enumeration'
# redirect_to :action => 'index'
diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb
index 557347bba..5efa1d64c 100644
--- a/app/controllers/issues_controller.rb
+++ b/app/controllers/issues_controller.rb
@@ -100,7 +100,7 @@ class IssuesController < ApplicationController
@journals.reverse! if User.current.wants_comments_in_reverse_order?
@allowed_statuses = @issue.new_statuses_allowed_to(User.current)
@edit_allowed = User.current.allowed_to?(:edit_issues, @project)
- @priorities = Enumeration::get_values('IPRI')
+ @priorities = Enumeration.priorities
@time_entry = TimeEntry.new
respond_to do |format|
format.html { render :template => 'issues/show.rhtml' }
@@ -153,7 +153,7 @@ class IssuesController < ApplicationController
return
end
end
- @priorities = Enumeration::get_values('IPRI')
+ @priorities = Enumeration.priorities
render :layout => !request.xhr?
end
@@ -163,7 +163,7 @@ class IssuesController < ApplicationController
def edit
@allowed_statuses = @issue.new_statuses_allowed_to(User.current)
- @priorities = Enumeration::get_values('IPRI')
+ @priorities = Enumeration.priorities
@edit_allowed = User.current.allowed_to?(:edit_issues, @project)
@time_entry = TimeEntry.new
@@ -403,7 +403,7 @@ class IssuesController < ApplicationController
@assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
end
- @priorities = Enumeration.get_values('IPRI').reverse
+ @priorities = Enumeration.priorities.reverse
@statuses = IssueStatus.find(:all, :order => 'position')
@back = request.env['HTTP_REFERER']
diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb
index bfe515778..f4594139f 100644
--- a/app/controllers/reports_controller.rb
+++ b/app/controllers/reports_controller.rb
@@ -37,7 +37,7 @@ class ReportsController < ApplicationController
render :template => "reports/issue_report_details"
when "priority"
@field = "priority_id"
- @rows = Enumeration::get_values('IPRI')
+ @rows = Enumeration.priorities
@data = issues_by_priority
@report_title = l(:field_priority)
render :template => "reports/issue_report_details"
@@ -68,7 +68,7 @@ class ReportsController < ApplicationController
else
@trackers = @project.trackers
@versions = @project.versions.sort
- @priorities = Enumeration::get_values('IPRI')
+ @priorities = Enumeration.priorities
@categories = @project.issue_categories
@assignees = @project.members.collect { |m| m.user }
@authors = @project.members.collect { |m| m.user }
diff --git a/app/helpers/timelog_helper.rb b/app/helpers/timelog_helper.rb
index f068b4db8..f43bce0bc 100644
--- a/app/helpers/timelog_helper.rb
+++ b/app/helpers/timelog_helper.rb
@@ -27,7 +27,7 @@ module TimelogHelper
end
def activity_collection_for_select_options
- activities = Enumeration::get_values('ACTI')
+ activities = Enumeration.activities
collection = []
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
activities.each { |a| collection << [a.name, a.id] }
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
diff --git a/app/views/documents/_form.rhtml b/app/views/documents/_form.rhtml
index d45e295b0..12a16f5d7 100644
--- a/app/views/documents/_form.rhtml
+++ b/app/views/documents/_form.rhtml
@@ -2,7 +2,7 @@
<div class="box">
<!--[form:document]-->
<p><label for="document_category_id"><%=l(:field_category)%></label>
-<%= select('document', 'category_id', Enumeration.get_values('DCAT').collect {|c| [c.name, c.id]}) %></p>
+<%= select('document', 'category_id', Enumeration.document_categories.collect {|c| [c.name, c.id]}) %></p>
<p><label for="document_title"><%=l(:field_title)%> <span class="required">*</span></label>
<%= text_field 'document', 'title', :size => 60 %></p>
diff --git a/app/views/enumerations/list.rhtml b/app/views/enumerations/list.rhtml
index 7f3886b44..c4a9af27b 100644
--- a/app/views/enumerations/list.rhtml
+++ b/app/views/enumerations/list.rhtml
@@ -3,7 +3,7 @@
<% Enumeration::OPTIONS.each do |option, params| %>
<h3><%= l(params[:label]) %></h3>
-<% enumerations = Enumeration.get_values(option) %>
+<% enumerations = Enumeration.values(option) %>
<% if enumerations.any? %>
<table class="list">
<% enumerations.each do |enumeration| %>
diff --git a/app/views/issues/bulk_edit.rhtml b/app/views/issues/bulk_edit.rhtml
index acda6a65f..214116868 100644
--- a/app/views/issues/bulk_edit.rhtml
+++ b/app/views/issues/bulk_edit.rhtml
@@ -13,7 +13,7 @@
<%= select_tag('status_id', "<option value=\"\">#{l(:label_no_change_option)}</option>" + options_from_collection_for_select(@available_statuses, :id, :name)) %></label>
<% end %>
<label><%= l(:field_priority) %>:
-<%= select_tag('priority_id', "<option value=\"\">#{l(:label_no_change_option)}</option>" + options_from_collection_for_select(Enumeration.get_values('IPRI'), :id, :name)) %></label>
+<%= select_tag('priority_id', "<option value=\"\">#{l(:label_no_change_option)}</option>" + options_from_collection_for_select(Enumeration.priorities, :id, :name)) %></label>
<label><%= l(:field_category) %>:
<%= select_tag('category_id', content_tag('option', l(:label_no_change_option), :value => '') +
content_tag('option', l(:label_none), :value => 'none') +