From 78a89ccf3b654091832ba1772bf3d4cf2d3b16ad Mon Sep 17 00:00:00 2001 From: lukemelia Date: Thu, 16 Nov 2006 05:33:25 +0000 Subject: [PATCH] Refactor acts_as_namepart_finder to a module, as suggested in the review of Tracks on The Rails Way. http://www.therailsway.com/2006/11/15/tracks-part-1/ git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@346 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/models/context.rb | 2 +- tracks/app/models/project.rb | 2 +- tracks/config/environment.rb.tmpl | 3 +-- tracks/lib/acts_as_namepart_finder.rb | 30 --------------------------- tracks/lib/name_part_finder.rb | 5 +++++ tracks/test/unit/context_test.rb | 13 ++++++++++++ tracks/test/unit/project_test.rb | 12 +++++++++++ 7 files changed, 33 insertions(+), 34 deletions(-) delete mode 100644 tracks/lib/acts_as_namepart_finder.rb create mode 100644 tracks/lib/name_part_finder.rb diff --git a/tracks/app/models/context.rb b/tracks/app/models/context.rb index 8e4a2911..94c2983f 100644 --- a/tracks/app/models/context.rb +++ b/tracks/app/models/context.rb @@ -4,7 +4,7 @@ class Context < ActiveRecord::Base belongs_to :user acts_as_list :scope => :user - acts_as_namepart_finder + extend NamePartFinder acts_as_todo_container :find_todos_include => :project attr_protected :user diff --git a/tracks/app/models/project.rb b/tracks/app/models/project.rb index 6a963a54..0dd50a5f 100644 --- a/tracks/app/models/project.rb +++ b/tracks/app/models/project.rb @@ -12,7 +12,7 @@ class Project < ActiveRecord::Base acts_as_list :scope => :user acts_as_state_machine :initial => :active, :column => 'state' - acts_as_namepart_finder + extend NamePartFinder acts_as_todo_container :find_todos_include => :context state :active diff --git a/tracks/config/environment.rb.tmpl b/tracks/config/environment.rb.tmpl index ec658d9d..2741e3eb 100644 --- a/tracks/config/environment.rb.tmpl +++ b/tracks/config/environment.rb.tmpl @@ -62,12 +62,11 @@ SALT = "change-me" # If you choose ldap, see the additional configuration options further down. AUTHENTICATION_SCHEMES = ['database'] -require 'acts_as_namepart_finder' +require 'name_part_finder' require 'acts_as_todo_container' require 'config' ActiveRecord::Base.class_eval do - include Tracks::Acts::NamepartFinder include Tracks::Acts::TodoContainer end diff --git a/tracks/lib/acts_as_namepart_finder.rb b/tracks/lib/acts_as_namepart_finder.rb deleted file mode 100644 index b3ea045e..00000000 --- a/tracks/lib/acts_as_namepart_finder.rb +++ /dev/null @@ -1,30 +0,0 @@ -module Tracks - module Acts #:nodoc: - module NamepartFinder #:nodoc: - - # This act provides the capabilities for finding a name that equals or starts with a given string - - def self.included(base) #:nodoc: - base.extend ActMacro - end - - module ActMacro - def acts_as_namepart_finder - self.extend(ClassMethods) - end - end - - module ClassMethods - - def find_by_namepart(namepart) - entity = find(:first, :conditions => ['name = ?', namepart]) - if (entity.nil?) - entity = find :first, :conditions => ["name LIKE ?", namepart + '%'] - end - entity - end - end - - end - end -end diff --git a/tracks/lib/name_part_finder.rb b/tracks/lib/name_part_finder.rb new file mode 100644 index 00000000..79d66338 --- /dev/null +++ b/tracks/lib/name_part_finder.rb @@ -0,0 +1,5 @@ +module NamePartFinder + def find_by_namepart(namepart) + find_by_name(namepart) || find(:first, :conditions => ["name LIKE ?", namepart + '%']) + end +end \ No newline at end of file diff --git a/tracks/test/unit/context_test.rb b/tracks/test/unit/context_test.rb index 56828a93..bfc8b8f3 100644 --- a/tracks/test/unit/context_test.rb +++ b/tracks/test/unit/context_test.rb @@ -38,4 +38,17 @@ class ContextTest < Test::Unit::TestCase assert_equal 1, newcontext.errors.count assert_equal "cannot contain the slash ('/') character", newcontext.errors.on(:name) end + + def test_find_by_namepart_with_exact_match + c = Context.find_by_namepart('agenda') + assert_not_nil c + assert_equal @agenda.id, c.id + end + + def test_find_by_namepart_with_starts_with + c = Context.find_by_namepart('agen') + assert_not_nil c + assert_equal @agenda.id, c.id + end + end diff --git a/tracks/test/unit/project_test.rb b/tracks/test/unit/project_test.rb index b04e5795..de004b51 100644 --- a/tracks/test/unit/project_test.rb +++ b/tracks/test/unit/project_test.rb @@ -61,5 +61,17 @@ class ProjectTest < Test::Unit::TestCase assert_equal :completed, @timemachine.current_state assert @timemachine.completed? end + + def test_find_project_by_namepart_with_exact_match + p = Project.find_by_namepart('Build a working time machine') + assert_not_nil p + assert_equal @timemachine.id, p.id + end + + def test_find_project_by_namepart_with_starts_with + p = Project.find_by_namepart('Build a') + assert_not_nil p + assert_equal @timemachine.id, p.id + end end