@@ -35,19 +35,19 class Enumeration < ActiveRecord::Base | |||||
35 | validates_uniqueness_of :name, :scope => [:type, :project_id] |
|
35 | validates_uniqueness_of :name, :scope => [:type, :project_id] | |
36 | validates_length_of :name, :maximum => 30 |
|
36 | validates_length_of :name, :maximum => 30 | |
37 |
|
37 | |||
38 |
scope :shared, |
|
38 | scope :shared, where(:project_id => nil) | |
39 |
scope :active, |
|
39 | scope :active, where(:active => true) | |
40 |
scope :named, lambda {|arg| |
|
40 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} | |
41 |
|
41 | |||
42 | def self.default |
|
42 | def self.default | |
43 | # Creates a fake default scope so Enumeration.default will check |
|
43 | # Creates a fake default scope so Enumeration.default will check | |
44 | # it's type. STI subclasses will automatically add their own |
|
44 | # it's type. STI subclasses will automatically add their own | |
45 | # types to the finder. |
|
45 | # types to the finder. | |
46 | if self.descends_from_active_record? |
|
46 | if self.descends_from_active_record? | |
47 |
|
|
47 | where(:is_default => true, :type => 'Enumeration').first | |
48 | else |
|
48 | else | |
49 | # STI classes are |
|
49 | # STI classes are | |
50 | find(:first, :conditions => { :is_default => true }) |
|
50 | where(:is_default => true).first | |
51 | end |
|
51 | end | |
52 | end |
|
52 | end | |
53 |
|
53 | |||
@@ -58,7 +58,7 class Enumeration < ActiveRecord::Base | |||||
58 |
|
58 | |||
59 | def check_default |
|
59 | def check_default | |
60 | if is_default? && is_default_changed? |
|
60 | if is_default? && is_default_changed? | |
61 |
Enumeration.update_all( |
|
61 | Enumeration.update_all({:is_default => false}, {:type => type}) | |
62 | end |
|
62 | end | |
63 | end |
|
63 | end | |
64 |
|
64 |
@@ -20,6 +20,19 require File.expand_path('../../test_helper', __FILE__) | |||||
20 | class IssuePriorityTest < ActiveSupport::TestCase |
|
20 | class IssuePriorityTest < ActiveSupport::TestCase | |
21 | fixtures :enumerations, :issues |
|
21 | fixtures :enumerations, :issues | |
22 |
|
22 | |||
|
23 | def test_named_scope | |||
|
24 | assert_equal Enumeration.find_by_name('Normal'), Enumeration.named('normal').first | |||
|
25 | end | |||
|
26 | ||||
|
27 | def test_default_should_return_the_default_priority | |||
|
28 | assert_equal Enumeration.find_by_name('Normal'), IssuePriority.default | |||
|
29 | end | |||
|
30 | ||||
|
31 | def test_default_should_return_nil_when_no_default_priority | |||
|
32 | IssuePriority.update_all :is_default => false | |||
|
33 | assert_nil IssuePriority.default | |||
|
34 | end | |||
|
35 | ||||
23 | def test_should_be_an_enumeration |
|
36 | def test_should_be_an_enumeration | |
24 | assert IssuePriority.ancestors.include?(Enumeration) |
|
37 | assert IssuePriority.ancestors.include?(Enumeration) | |
25 | end |
|
38 | end |
General Comments 0
You need to be logged in to leave comments.
Login now