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
This commit is contained in:
Jean-Philippe Lang 2009-02-15 13:33:09 +00:00
parent 13e2c727cf
commit 4601ed2f3a
17 changed files with 51 additions and 39 deletions

View File

@ -54,7 +54,7 @@ class DocumentsController < ApplicationController
end end
def edit def edit
@categories = Enumeration::get_values('DCAT') @categories = Enumeration.document_categories
if request.post? and @document.update_attributes(params[:document]) if request.post? and @document.update_attributes(params[:document])
flash[:notice] = l(:notice_successful_update) flash[:notice] = l(:notice_successful_update)
redirect_to :action => 'show', :id => @document redirect_to :action => 'show', :id => @document

View File

@ -85,7 +85,7 @@ class EnumerationsController < ApplicationController
redirect_to :action => 'index' redirect_to :action => 'index'
end end
end end
@enumerations = Enumeration.get_values(@enumeration.opt) - [@enumeration] @enumerations = Enumeration.values(@enumeration.opt) - [@enumeration]
#rescue #rescue
# flash[:error] = 'Unable to delete enumeration' # flash[:error] = 'Unable to delete enumeration'
# redirect_to :action => 'index' # redirect_to :action => 'index'

View File

@ -100,7 +100,7 @@ class IssuesController < ApplicationController
@journals.reverse! if User.current.wants_comments_in_reverse_order? @journals.reverse! if User.current.wants_comments_in_reverse_order?
@allowed_statuses = @issue.new_statuses_allowed_to(User.current) @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
@edit_allowed = User.current.allowed_to?(:edit_issues, @project) @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
@priorities = Enumeration::get_values('IPRI') @priorities = Enumeration.priorities
@time_entry = TimeEntry.new @time_entry = TimeEntry.new
respond_to do |format| respond_to do |format|
format.html { render :template => 'issues/show.rhtml' } format.html { render :template => 'issues/show.rhtml' }
@ -153,7 +153,7 @@ class IssuesController < ApplicationController
return return
end end
end end
@priorities = Enumeration::get_values('IPRI') @priorities = Enumeration.priorities
render :layout => !request.xhr? render :layout => !request.xhr?
end end
@ -163,7 +163,7 @@ class IssuesController < ApplicationController
def edit def edit
@allowed_statuses = @issue.new_statuses_allowed_to(User.current) @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) @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
@time_entry = TimeEntry.new @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) @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
end end
@priorities = Enumeration.get_values('IPRI').reverse @priorities = Enumeration.priorities.reverse
@statuses = IssueStatus.find(:all, :order => 'position') @statuses = IssueStatus.find(:all, :order => 'position')
@back = request.env['HTTP_REFERER'] @back = request.env['HTTP_REFERER']

View File

@ -37,7 +37,7 @@ class ReportsController < ApplicationController
render :template => "reports/issue_report_details" render :template => "reports/issue_report_details"
when "priority" when "priority"
@field = "priority_id" @field = "priority_id"
@rows = Enumeration::get_values('IPRI') @rows = Enumeration.priorities
@data = issues_by_priority @data = issues_by_priority
@report_title = l(:field_priority) @report_title = l(:field_priority)
render :template => "reports/issue_report_details" render :template => "reports/issue_report_details"
@ -68,7 +68,7 @@ class ReportsController < ApplicationController
else else
@trackers = @project.trackers @trackers = @project.trackers
@versions = @project.versions.sort @versions = @project.versions.sort
@priorities = Enumeration::get_values('IPRI') @priorities = Enumeration.priorities
@categories = @project.issue_categories @categories = @project.issue_categories
@assignees = @project.members.collect { |m| m.user } @assignees = @project.members.collect { |m| m.user }
@authors = @project.members.collect { |m| m.user } @authors = @project.members.collect { |m| m.user }

View File

@ -27,7 +27,7 @@ module TimelogHelper
end end
def activity_collection_for_select_options def activity_collection_for_select_options
activities = Enumeration::get_values('ACTI') activities = Enumeration.activities
collection = [] collection = []
collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default) collection << [ "--- #{l(:actionview_instancetag_blank_option)} ---", '' ] unless activities.detect(&:is_default)
activities.each { |a| collection << [a.name, a.id] } activities.each { |a| collection << [a.name, a.id] }

View File

@ -31,7 +31,7 @@ class Document < ActiveRecord::Base
def after_initialize def after_initialize
if new_record? if new_record?
self.category ||= Enumeration.default('DCAT') self.category ||= Enumeration.document_categories.default
end end
end end
end end

View File

@ -26,17 +26,29 @@ class Enumeration < ActiveRecord::Base
# Single table inheritance would be an option # Single table inheritance would be an option
OPTIONS = { OPTIONS = {
"IPRI" => {:label => :enumeration_issue_priorities, :model => Issue, :foreign_key => :priority_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}, "DCAT" => {:label => :enumeration_doc_categories, :model => Document, :foreign_key => :category_id, :scope => :document_categories},
"ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id} "ACTI" => {:label => :enumeration_activities, :model => TimeEntry, :foreign_key => :activity_id, :scope => :activities}
}.freeze }.freeze
def self.get_values(option) # Creates a named scope for each type of value. The scope has a +default+ method
find(:all, :conditions => {:opt => option}, :order => 'position') # 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 end
def self.default(option) named_scope :values, lambda {|opt| { :conditions => { :opt => opt }, :order => 'position' } } do
find(:first, :conditions => {:opt => option, :is_default => true}, :order => 'position') def default
find(:first, :conditions => { :is_default => true })
end
end end
def option_name def option_name

View File

@ -63,7 +63,7 @@ class Issue < ActiveRecord::Base
if new_record? if new_record?
# set default values for new records only # set default values for new records only
self.status ||= IssueStatus.default self.status ||= IssueStatus.default
self.priority ||= Enumeration.default('IPRI') self.priority ||= Enumeration.priorities.default
end end
end end

View File

@ -37,7 +37,7 @@ class TimeEntry < ActiveRecord::Base
def after_initialize def after_initialize
if new_record? && self.activity.nil? if new_record? && self.activity.nil?
if default_activity = Enumeration.default('ACTI') if default_activity = Enumeration.activities.default
self.activity_id = default_activity.id self.activity_id = default_activity.id
end end
end end

View File

@ -2,7 +2,7 @@
<div class="box"> <div class="box">
<!--[form:document]--> <!--[form:document]-->
<p><label for="document_category_id"><%=l(:field_category)%></label> <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> <p><label for="document_title"><%=l(:field_title)%> <span class="required">*</span></label>
<%= text_field 'document', 'title', :size => 60 %></p> <%= text_field 'document', 'title', :size => 60 %></p>

View File

@ -3,7 +3,7 @@
<% Enumeration::OPTIONS.each do |option, params| %> <% Enumeration::OPTIONS.each do |option, params| %>
<h3><%= l(params[:label]) %></h3> <h3><%= l(params[:label]) %></h3>
<% enumerations = Enumeration.get_values(option) %> <% enumerations = Enumeration.values(option) %>
<% if enumerations.any? %> <% if enumerations.any? %>
<table class="list"> <table class="list">
<% enumerations.each do |enumeration| %> <% enumerations.each do |enumeration| %>

View File

@ -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> <%= select_tag('status_id', "<option value=\"\">#{l(:label_no_change_option)}</option>" + options_from_collection_for_select(@available_statuses, :id, :name)) %></label>
<% end %> <% end %>
<label><%= l(:field_priority) %>: <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) %>: <label><%= l(:field_category) %>:
<%= select_tag('category_id', content_tag('option', l(:label_no_change_option), :value => '') + <%= select_tag('category_id', content_tag('option', l(:label_no_change_option), :value => '') +
content_tag('option', l(:label_none), :value => 'none') + content_tag('option', l(:label_none), :value => 'none') +

View File

@ -40,7 +40,7 @@ task :migrate_from_mantis => :environment do
90 => closed_status # closed 90 => closed_status # closed
} }
priorities = Enumeration.get_values('IPRI') priorities = Enumeration.priorities
DEFAULT_PRIORITY = priorities[2] DEFAULT_PRIORITY = priorities[2]
PRIORITY_MAPPING = {10 => priorities[1], # none PRIORITY_MAPPING = {10 => priorities[1], # none
20 => priorities[1], # low 20 => priorities[1], # low

View File

@ -37,7 +37,7 @@ namespace :redmine do
'closed' => closed_status 'closed' => closed_status
} }
priorities = Enumeration.get_values('IPRI') priorities = Enumeration.priorities
DEFAULT_PRIORITY = priorities[0] DEFAULT_PRIORITY = priorities[0]
PRIORITY_MAPPING = {'lowest' => priorities[0], PRIORITY_MAPPING = {'lowest' => priorities[0],
'low' => priorities[0], 'low' => priorities[0],

View File

@ -640,7 +640,7 @@ class IssuesControllerTest < Test::Unit::TestCase
:id => 1, :id => 1,
:issue => { :status_id => 2, :assigned_to_id => 3 }, :issue => { :status_id => 2, :assigned_to_id => 3 },
:notes => 'Assigned to dlopper', :notes => 'Assigned to dlopper',
:time_entry => { :hours => '', :comments => '', :activity_id => Enumeration.get_values('ACTI').first } :time_entry => { :hours => '', :comments => '', :activity_id => Enumeration.activities.first }
end end
assert_redirected_to :action => 'show', :id => '1' assert_redirected_to :action => 'show', :id => '1'
issue.reload issue.reload
@ -676,7 +676,7 @@ class IssuesControllerTest < Test::Unit::TestCase
post :edit, post :edit,
:id => 1, :id => 1,
:notes => '2.5 hours added', :notes => '2.5 hours added',
:time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.get_values('ACTI').first } :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.activities.first }
end end
assert_redirected_to :action => 'show', :id => '1' assert_redirected_to :action => 'show', :id => '1'

View File

@ -38,7 +38,7 @@ class EnumerationTest < Test::Unit::TestCase
end end
def test_default def test_default
e = Enumeration.default('IPRI') e = Enumeration.priorities.default
assert e.is_a?(Enumeration) assert e.is_a?(Enumeration)
assert e.is_default? assert e.is_default?
assert_equal 'Normal', e.name assert_equal 'Normal', e.name
@ -47,31 +47,31 @@ class EnumerationTest < Test::Unit::TestCase
def test_create def test_create
e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => false) e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => false)
assert e.save assert e.save
assert_equal 'Normal', Enumeration.default('IPRI').name assert_equal 'Normal', Enumeration.priorities.default.name
end end
def test_create_as_default def test_create_as_default
e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => true) e = Enumeration.new(:opt => 'IPRI', :name => 'Very urgent', :is_default => true)
assert e.save assert e.save
assert_equal e, Enumeration.default('IPRI') assert_equal e, Enumeration.priorities.default
end end
def test_update_default def test_update_default
e = Enumeration.default('IPRI') e = Enumeration.priorities.default
e.update_attributes(:name => 'Changed', :is_default => true) e.update_attributes(:name => 'Changed', :is_default => true)
assert_equal e, Enumeration.default('IPRI') assert_equal e, Enumeration.priorities.default
end end
def test_update_default_to_non_default def test_update_default_to_non_default
e = Enumeration.default('IPRI') e = Enumeration.priorities.default
e.update_attributes(:name => 'Changed', :is_default => false) e.update_attributes(:name => 'Changed', :is_default => false)
assert_nil Enumeration.default('IPRI') assert_nil Enumeration.priorities.default
end end
def test_change_default def test_change_default
e = Enumeration.find_by_name('Urgent') e = Enumeration.find_by_name('Urgent')
e.update_attributes(:name => 'Urgent', :is_default => true) e.update_attributes(:name => 'Urgent', :is_default => true)
assert_equal e, Enumeration.default('IPRI') assert_equal e, Enumeration.priorities.default
end end
def test_destroy_with_reassign def test_destroy_with_reassign

View File

@ -27,14 +27,14 @@ class IssueTest < Test::Unit::TestCase
:time_entries :time_entries
def test_create def test_create
issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30') issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'test_create', :description => 'IssueTest#test_create', :estimated_hours => '1:30')
assert issue.save assert issue.save
issue.reload issue.reload
assert_equal 1.5, issue.estimated_hours assert_equal 1.5, issue.estimated_hours
end end
def test_create_minimal def test_create_minimal
issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'test_create') issue = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'test_create')
assert issue.save assert issue.save
assert issue.description.nil? assert issue.description.nil?
end end
@ -110,7 +110,7 @@ class IssueTest < Test::Unit::TestCase
end end
def test_category_based_assignment def test_category_based_assignment
issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1) issue = Issue.create(:project_id => 1, :tracker_id => 1, :author_id => 3, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'Assignment test', :description => 'Assignment test', :category_id => 1)
assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to assert_equal IssueCategory.find(1).assigned_to, issue.assigned_to
end end
@ -126,7 +126,7 @@ class IssueTest < Test::Unit::TestCase
def test_should_close_duplicates def test_should_close_duplicates
# Create 3 issues # Create 3 issues
issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'Duplicates test', :description => 'Duplicates test')
assert issue1.save assert issue1.save
issue2 = issue1.clone issue2 = issue1.clone
assert issue2.save assert issue2.save
@ -153,7 +153,7 @@ class IssueTest < Test::Unit::TestCase
def test_should_not_close_duplicated_issue def test_should_not_close_duplicated_issue
# Create 3 issues # Create 3 issues
issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.get_values('IPRI').first, :subject => 'Duplicates test', :description => 'Duplicates test') issue1 = Issue.new(:project_id => 1, :tracker_id => 1, :author_id => 1, :status_id => 1, :priority => Enumeration.priorities.first, :subject => 'Duplicates test', :description => 'Duplicates test')
assert issue1.save assert issue1.save
issue2 = issue1.clone issue2 = issue1.clone
assert issue2.save assert issue2.save