Permalink
Browse files

No longer trying to find a record if the lookup key is blank. Verifie…

…d that load_model works under ruby 1.9
  • Loading branch information...
1 parent 275fd96 commit a42b5ce2d7dfd8ea7445553825de392a49cd50ec Justin Knowlden committed Jun 3, 2009
View
@@ -165,9 +165,7 @@ def load_specified_models
self.class.loaders.each do |loader|
if loader.action_allowed?(action_name)
obj = loader.load_model(self)
- if obj.nil? && loader.action_required?(action_name)
- raise RequiredRecordNotFound
- end
+ raise RequiredRecordNotFound if obj.nil? && loader.action_required?(action_name)
instance_variable_set(loader.assigns_to, obj)
end
end
@@ -32,6 +32,10 @@ def parse_required_actions(actions)
def parameter_value(controller) controller.params[parameter_key]; end
def stringify_array(value) Array(value).map(&:to_s); end
+
+ def retrieve_association_through(controller, instance_variable_name)
+ controller.instance_variable_get(instance_variable_name).send(@association)
+ end
end # ModelLoader
class AssociativeModelLoader < ModelLoader #:nodoc
@@ -48,7 +52,7 @@ def initialize(name, opts={})
def load_model(controller)
begin
lookup = parameter_value(controller)
- source(controller).send("find_by_#{foreign_key}", lookup)
+ source(controller).send("find_by_#{foreign_key}", lookup) unless lookup.blank?
rescue ActiveRecord::StatementInvalid
nil
end
@@ -67,7 +71,7 @@ def initialize(name, opts={})
end
private
def source(controller)
- controller.instance_variable_get(load_through).send(@association)
+ retrieve_association_through(controller, load_through)
end
end # ThroughModelLoader
@@ -79,7 +83,7 @@ def initialize(name, opts={})
end
def load_model(controller)
- controller.instance_variable_get(@load_from).send(@association)
+ retrieve_association_through(controller, @load_from)
end
end # FromModelLoader
View
@@ -1,12 +1,12 @@
Gem::Specification.new do |s|
- s.name = "load_model"
- s.version = "0.2.1"
- s.date = "2009-03-24"
+ s.name = "thumblemonks-load_model"
+ s.version = "0.2.2"
+ s.date = "2009-06-03"
s.summary = "Rails Controller plugin that provides easy and useful macros for tying models and requests together"
s.email = %w[gus@gusg.us gabriel.gironda@gmail.com]
s.homepage = "http://github.com/thumblemonks/load_model"
s.description = "Rails Controller plugin that provides easy and useful macros for tying models and requests together"
- s.authors = %w[Justin\ Knowlden Gabriel\ Gironda]
+ s.authors = %w[Justin\ Knowlden Gabriel\ Gironda Dan\ Hodos]
s.rubyforge_project = %q{load_model}
@@ -34,53 +34,57 @@ def setup
end # does not match existing record
end # when parameter
- def test_should_find_record_with_alternate_id_as_expected_param_key
- alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
- get :index, :alternate_id => alt.alternate_id
- assert_equal alt.id, assigns(:alternate).id
- assert_equal alt.alternate_id, assigns(:alternate).alternate_id
- end
-
- def test_should_find_nothing_when_alternate_id_does_not_match_record
- alt = Alternate.create!(:name => 'Alternate', :alternate_id => 99)
- get :index, :alternate_id => 100
- assert_nil assigns(:alternate)
- end
-
- def test_should_find_chameleon_in_user_table
+ should "find chameleon in users" do
get :index, :id => @foo.id
assert_equal @foo.id, assigns(:chameleon).id
end
- def test_should_not_find_chameleon_in_user_table_with_nonexistent_id
+ should "not find chameleon in users with nonexistent id" do
get :index, :id => (@foo.id + 1)
assert_nil assigns(:chameleon)
end
- def test_should_find_flamingo_in_user_table_even_when_class_name_is_constant
- get :index, :id => @foo.id
- assert_equal @foo.id, assigns(:flamingo).id
+ should "not find record if key value is not an integer" do
+ get :index, :id => 'abc'
+ assert_nil assigns(:user)
end
- def test_should_not_find_flamingo_in_user_table_when_class_name_is_constant
- get :index, :id => (@foo.id + 1)
- assert_nil assigns(:flamingo)
- end
+ context "when class name is constant" do
+ should "find flamingo in user table" do
+ get :index, :id => @foo.id
+ assert_equal @foo.id, assigns(:flamingo).id
+ end
- def test_should_find_tucan_in_users_with_alternate_class_and_key
- alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
- get :index, :alternate_id => alt.alternate_id
- assert_equal alt.id, assigns(:tucan).id
- end
+ should "not find flamingo in user table" do
+ get :index, :id => (@foo.id + 1)
+ assert_nil assigns(:flamingo)
+ end
+ end # when class name is constant
- def test_should_not_find_tucan_in_users_with_alternate_class_and_key
- alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
- get :index, :alternate_id => (alt.alternate_id + 1)
- assert_nil assigns(:tucan)
- end
+ context "for alternate" do
+ context "with existing id" do
+ setup do
+ @alt = Alternate.create!(:name => 'Alternate', :alternate_id => 100)
+ get :index, :alternate_id => @alt.alternate_id
+ end
- def test_should_not_find_record_if_key_value_is_not_an_integer
- get :index, :id => 'abc'
- assert_nil assigns(:user)
- end
+ should_assign_to(:alternate) { @alt }
+ should_assign_to(:tucan) { @alt }
+ end
+
+ context "with non-existant id" do
+ setup { get :index, :alternate_id => 99 }
+ should_not_assign_to(:alternate)
+ should_not_assign_to(:tucan)
+ end
+ end # with alternate class and key
+
+ context "when id is nil for users" do
+ setup do
+ User.expects(:find_by_id).never
+ get :index, :id => ''
+ end
+
+ should_not_assign_to(:user)
+ end # when parameter value is nil
end
@@ -67,4 +67,14 @@ def setup
end
end # has nonexistant records for required action
end # show with alternative post via weird_id
+
+ context "when parameter value is nil" do
+ setup do
+ Post.expects(:find_by_id).never
+ get :index, :user_id => @user.id, :id => ''
+ end
+
+ should_assign_to(:user) { @user }
+ should_not_assign_to(:post)
+ end # when parameter value is nil
end
@@ -6,11 +6,10 @@
Rails::Initializer.run do |config|
config.gem 'thoughtbot-shoulda', :lib => 'shoulda/rails', :source => 'http://gems.github.com'
+ config.gem 'mocha'
config.action_controller.session = {
:session_key => '_load_model_session',
:secret => '9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd9908cd'
}
end
-
-require 'ruby-debug'
View
@@ -4,9 +4,11 @@ def require_local_lib(pattern)
ENV["RAILS_ENV"] = "test"
ENV["RAILS_ROOT"] = File.expand_path(File.join(File.dirname(__FILE__), '..', 'test', 'rails'))
+
require File.expand_path(File.join(ENV["RAILS_ROOT"], 'config', 'environment'))
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib', 'load_model'))
require 'test_help'
+
load(File.join(ENV["RAILS_ROOT"], "db", "schema.rb"))
# Models

0 comments on commit a42b5ce

Please sign in to comment.