diff options
author | Seb Bacon <seb.bacon@gmail.com> | 2011-07-12 14:36:55 +0100 |
---|---|---|
committer | Seb Bacon <seb.bacon@gmail.com> | 2011-07-12 14:36:55 +0100 |
commit | 8ca2f1fca08b8d645040bba2d83b77a889067b23 (patch) | |
tree | 9fe956d70154e4c162454e099a331f4e0386293e /app/models/info_request.rb | |
parent | 15973386cb4f412cf439414ef908d4594c897f24 (diff) |
Refactor custom states loading (use class methods where appropriate, simplify main code at the cost (small) of test legibility)
Diffstat (limited to 'app/models/info_request.rb')
-rw-r--r-- | app/models/info_request.rb | 34 |
1 files changed, 13 insertions, 21 deletions
diff --git a/app/models/info_request.rb b/app/models/info_request.rb index c51e0c546..f7a8f58a2 100644 --- a/app/models/info_request.rb +++ b/app/models/info_request.rb @@ -80,7 +80,7 @@ class InfoRequest < ActiveRecord::Base 'blackhole' # just dump them ] - def enumerate_states + def self.enumerate_states states = [ 'waiting_response', 'waiting_clarification', @@ -96,15 +96,25 @@ class InfoRequest < ActiveRecord::Base ] if @@custom_states_loaded - states += self.theme_extra_states + states += InfoRequest.theme_extra_states end states end def must_be_valid_state errors.add(:described_state, "is not a valid state") if - !self.enumerate_states.include? described_state + !InfoRequest.enumerate_states.include? described_state end + + @@custom_states_loaded = false + begin + if !ENV["RAILS_ENV"] == "test" + include InfoRequestCustomStates + @@custom_states_loaded = true + end + rescue NameError + end + # only check on create, so existing models with mixed case are allowed def validate_on_create if !self.title.nil? && !MySociety::Validate.uses_mixed_capitals(self.title, 10) @@ -121,7 +131,6 @@ class InfoRequest < ActiveRecord::Base OLD_AGE_IN_DAYS = 21.days def after_initialize - self.load_custom_states if self.described_state.nil? self.described_state = 'waiting_response' end @@ -131,23 +140,6 @@ class InfoRequest < ActiveRecord::Base end end - def load_custom_states - @@custom_states_loaded = false - if !ENV["RAILS_ENV"] == "test" - load_custom_states! - end - end - - def load_custom_states! - begin - # InfoRequestCustomStates may be `require`d in a theme - # plugin, or by a test - InfoRequest.send(:include, InfoRequestCustomStates) - @@custom_states_loaded = true - rescue NameError - end - end - def visible_comments self.comments.find(:all, :conditions => 'visible') end |