mirror of
https://github.com/TracksApp/tracks.git
synced 2026-02-04 15:01:48 +01:00
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
This commit is contained in:
parent
04bd157857
commit
78a89ccf3b
7 changed files with 33 additions and 34 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
5
tracks/lib/name_part_finder.rb
Normal file
5
tracks/lib/name_part_finder.rb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
module NamePartFinder
|
||||
def find_by_namepart(namepart)
|
||||
find_by_name(namepart) || find(:first, :conditions => ["name LIKE ?", namepart + '%'])
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue