Skip to content

Commit

Permalink
Added find_by_param methods and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jseifer committed Mar 16, 2008
1 parent f11cc96 commit fd735cb
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
2 changes: 2 additions & 0 deletions init.rb
@@ -0,0 +1,2 @@
require 'find_by_param'
ActiveRecord::Base.send(:extend, FindByParam::ClassMethods)
27 changes: 27 additions & 0 deletions lib/find_by_param.rb
@@ -0,0 +1,27 @@
module FindByParam
module ClassMethods
def define_find_param(param, options={})
param = param.to_s
options[:raise_on_not_found] ||= false
if column_names.include?(param)
write_inheritable_attribute :find_parameter, param
write_inheritable_attribute :find_param_options, options
bl = lambda do |args|
results = send("find_by_#{read_inheritable_attribute(:find_parameter)}", *args)
raise ActiveRecord::RecordNotFound if options[:raise_on_not_found] && (results.nil? or (results.is_a?(Array) && results.empty?))
return results
end
self.class.send(:define_method, 'find_by_param', &bl)
else
raise StandardError
end
self.send(:include, FindByParam::InstanceMethods)
end
end

module InstanceMethods
def to_param
self.send(self.class.read_inheritable_attribute(:find_parameter))
end
end
end
33 changes: 33 additions & 0 deletions test/find_by_param_test.rb
@@ -0,0 +1,33 @@
require File.join(File.dirname(__FILE__), 'test_helper')

class FindByParamTest < Test::Unit::TestCase
def setup
BlogPost.create(:slug => 'adam-west', :title => 'Adam West')
BlogPost.create(:slug => 'burt-ward', :title => 'Burt Ward')
BlogPost.send(:define_find_param, 'slug')
end

def teardown
BlogPost.delete_all
end

def test_plugin_loaded_correctly
assert BlogPost.respond_to?(:find_by_param)
end

def test_returns_valid_data
bp = BlogPost.find(:first, :conditions => 'slug = "adam-west"')
assert_equal BlogPost.find_by_param('adam-west'), bp
end

def test_can_define_find_parameter
BlogPost.send('define_find_param', 'title')
bp = BlogPost.find(:first, :conditions => {:slug => 'adam-west'})
assert_equal BlogPost.find_by_param('Adam West'), bp
end

def test_correctly_goes_to_param
bp = BlogPost.find(:first, :conditions => {:slug => 'adam-west'})
assert_equal bp.to_param, 'adam-west'
end
end
32 changes: 32 additions & 0 deletions test/test_helper.rb
@@ -0,0 +1,32 @@
$LOAD_PATH.unshift 'lib/'

require 'rubygems'
require 'multi_rails_init'
require 'action_controller/test_process'
require 'test/unit'
require 'find_by_param'

begin
require 'redgreen'
rescue LoadError
nil
end

RAILS_ROOT = '.' unless defined? RAILS_ROOT
RAILS_ENV = 'test' unless defined? RAILS_ENV


ActiveRecord::Base.send(:extend, FindByParam::ClassMethods)
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define(:version => 1) do
create_table :blog_posts do |t|
t.column :slug, :string
t.column :title, :string
end
end

class BlogPost < ActiveRecord::Base
define_find_param :slug
end

0 comments on commit fd735cb

Please sign in to comment.