Permalink
Browse files

Added find_by_param methods and tests

  • Loading branch information...
1 parent f11cc96 commit fd735cb1f252398397c8ed08a229db79da51684e @jseifer committed Mar 16, 2008
Showing with 94 additions and 0 deletions.
  1. +2 −0 init.rb
  2. +27 −0 lib/find_by_param.rb
  3. +33 −0 test/find_by_param_test.rb
  4. +32 −0 test/test_helper.rb
View
@@ -0,0 +1,2 @@
+require 'find_by_param'
+ActiveRecord::Base.send(:extend, FindByParam::ClassMethods)
View
@@ -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
@@ -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
View
@@ -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.