From e414cf56a6b7b50ed354f6275d6c4efff8feeec3 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 20 Jun 2011 16:33:53 -0300 Subject: [PATCH] Fixing partial for class --- app/helpers/application_helper.rb | 45 +++++++++++++++++++++++++++++++-------------- test/unit/application_helper_test.rb | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 7fa9664..b7b8cbd 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -255,30 +255,47 @@ module ApplicationHelper concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), { :class => 'button-bar' }.merge(options)), block.binding) end - def partial_for_class(klass) - if klass.nil? - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' - end + VIEW_EXTENSIONS = %w[.rhtml .html.erb] + def partial_for_class_in_view_path(klass, view_path) + return nil if klass.nil? name = klass.name.underscore - if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) - name + + search_name = String.new(name) + if search_name.include?("/") + search_name.gsub!(/(\/)([^\/]*)$/,'\1_\2') + name = File.join(params[:controller], name) else - partial_for_class(klass.superclass) + search_name = "_" + search_name + end + + VIEW_EXTENSIONS.each do |ext| + return name if File.exists?(File.join(view_path, params[:controller], search_name+ext)) end + + partial_for_class_in_view_path(klass.superclass, view_path) end - def partial_for_task_class(klass, action) - if klass.nil? - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' + def partial_for_class(klass) + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? + name = klass.name.underscore + @controller.view_paths.each do |view_path| + partial = partial_for_class_in_view_path(klass, view_path) + return partial if partial end + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' + end + + def partial_for_task_class(klass, action) + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? + name = "#{klass.name.underscore}_#{action.to_s}" - if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) - name - else - partial_for_task_class(klass.superclass, action) + VIEW_EXTENSIONS.each do |ext| + return name if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], '_'+name+ext)) end + + partial_for_task_class(klass.superclass, action) end def user diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 58452a7..9b1baf7 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -8,16 +8,49 @@ class ApplicationHelperTest < Test::Unit::TestCase self.stubs(:session).returns({}) end - should 'calculate correctly partial for object' do + should 'calculate correctly partial for models' do + p1 = 'path1/' + p2 = 'path2/' + @controller = mock() + @controller.stubs(:view_paths).returns([p1,p2]) + self.stubs(:params).returns({:controller => 'test'}) - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_float.rhtml").returns(false) - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_numeric.rhtml").returns(true).times(2) - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_runtime_error.rhtml").returns(true).at_least_once + File.expects(:exists?).with(p1+"test/_integer.rhtml").returns(true) + + File.expects(:exists?).with(p1+"test/_float.rhtml").returns(false) + File.expects(:exists?).with(p1+"test/_float.html.erb").returns(false) + File.expects(:exists?).with(p2+"test/_float.rhtml").returns(false) + File.expects(:exists?).with(p2+"test/_float.html.erb").returns(false) + File.expects(:exists?).with(p1+"test/_numeric.rhtml").returns(false) + File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false) + File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false) + File.expects(:exists?).with(p1+"test/_numeric.html.erb").returns(false) + File.expects(:exists?).with(p2+"test/_numeric.rhtml").returns(true) + + File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false) + File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false) + File.expects(:exists?).with(p2+"test/_object.rhtml").returns(false) + File.expects(:exists?).with(p2+"test/_object.html.erb").returns(false) + assert_equal 'integer', partial_for_class(Integer) assert_equal 'numeric', partial_for_class(Float) - assert_equal 'numeric', partial_for_class(Numeric) - assert_equal 'runtime_error', partial_for_class(RuntimeError) + assert_raises ArgumentError do + partial_for_class(Object) + end + end + + should 'calculate correctly partial for namespaced models' do + p = 'path/' + @controller = mock() + @controller.stubs(:view_paths).returns([p]) + self.stubs(:params).returns({:controller => 'test'}) + + class School; class Project; end; end + + File.expects(:exists?).with(p+"test/application_helper_test/school/_project.rhtml").returns(true) + + assert_equal 'test/application_helper_test/school/project', partial_for_class(School::Project) end should 'give error when there is no partial for class' do -- libgit2 0.21.2