Commit 4ecfeb625d4ddb8a58b1357a4a3b0d460689099e
Exists in
master
and in
29 other branches
Merge commit 'refs/merge-requests/17' of git://gitorious.org/noosfero/noosfero i…
…nto merge-requests/17
Showing
2 changed files
with
70 additions
and
20 deletions
Show diff stats
app/helpers/application_helper.rb
... | ... | @@ -255,30 +255,47 @@ module ApplicationHelper |
255 | 255 | concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), { :class => 'button-bar' }.merge(options)), block.binding) |
256 | 256 | end |
257 | 257 | |
258 | - def partial_for_class(klass) | |
259 | - if klass.nil? | |
260 | - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | |
261 | - end | |
258 | + VIEW_EXTENSIONS = %w[.rhtml .html.erb] | |
262 | 259 | |
260 | + def partial_for_class_in_view_path(klass, view_path) | |
261 | + return nil if klass.nil? | |
263 | 262 | name = klass.name.underscore |
264 | - if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) | |
265 | - name | |
263 | + | |
264 | + search_name = String.new(name) | |
265 | + if search_name.include?("/") | |
266 | + search_name.gsub!(/(\/)([^\/]*)$/,'\1_\2') | |
267 | + name = File.join(params[:controller], name) | |
266 | 268 | else |
267 | - partial_for_class(klass.superclass) | |
269 | + search_name = "_" + search_name | |
270 | + end | |
271 | + | |
272 | + VIEW_EXTENSIONS.each do |ext| | |
273 | + return name if File.exists?(File.join(view_path, params[:controller], search_name+ext)) | |
268 | 274 | end |
275 | + | |
276 | + partial_for_class_in_view_path(klass.superclass, view_path) | |
269 | 277 | end |
270 | 278 | |
271 | - def partial_for_task_class(klass, action) | |
272 | - if klass.nil? | |
273 | - raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | |
279 | + def partial_for_class(klass) | |
280 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | |
281 | + name = klass.name.underscore | |
282 | + @controller.view_paths.each do |view_path| | |
283 | + partial = partial_for_class_in_view_path(klass, view_path) | |
284 | + return partial if partial | |
274 | 285 | end |
275 | 286 | |
287 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' | |
288 | + end | |
289 | + | |
290 | + def partial_for_task_class(klass, action) | |
291 | + raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?' if klass.nil? | |
292 | + | |
276 | 293 | name = "#{klass.name.underscore}_#{action.to_s}" |
277 | - if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml")) | |
278 | - name | |
279 | - else | |
280 | - partial_for_task_class(klass.superclass, action) | |
294 | + VIEW_EXTENSIONS.each do |ext| | |
295 | + return name if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], '_'+name+ext)) | |
281 | 296 | end |
297 | + | |
298 | + partial_for_task_class(klass.superclass, action) | |
282 | 299 | end |
283 | 300 | |
284 | 301 | def user | ... | ... |
test/unit/application_helper_test.rb
... | ... | @@ -8,16 +8,49 @@ class ApplicationHelperTest < Test::Unit::TestCase |
8 | 8 | self.stubs(:session).returns({}) |
9 | 9 | end |
10 | 10 | |
11 | - should 'calculate correctly partial for object' do | |
11 | + should 'calculate correctly partial for models' do | |
12 | + p1 = 'path1/' | |
13 | + p2 = 'path2/' | |
14 | + @controller = mock() | |
15 | + @controller.stubs(:view_paths).returns([p1,p2]) | |
16 | + | |
12 | 17 | self.stubs(:params).returns({:controller => 'test'}) |
13 | 18 | |
14 | - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_float.rhtml").returns(false) | |
15 | - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_numeric.rhtml").returns(true).times(2) | |
16 | - File.expects(:exists?).with("#{RAILS_ROOT}/app/views/test/_runtime_error.rhtml").returns(true).at_least_once | |
19 | + File.expects(:exists?).with(p1+"test/_integer.rhtml").returns(true) | |
20 | + | |
21 | + File.expects(:exists?).with(p1+"test/_float.rhtml").returns(false) | |
22 | + File.expects(:exists?).with(p1+"test/_float.html.erb").returns(false) | |
23 | + File.expects(:exists?).with(p2+"test/_float.rhtml").returns(false) | |
24 | + File.expects(:exists?).with(p2+"test/_float.html.erb").returns(false) | |
25 | + File.expects(:exists?).with(p1+"test/_numeric.rhtml").returns(false) | |
26 | + File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false) | |
27 | + File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false) | |
28 | + File.expects(:exists?).with(p1+"test/_numeric.html.erb").returns(false) | |
29 | + File.expects(:exists?).with(p2+"test/_numeric.rhtml").returns(true) | |
30 | + | |
31 | + File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false) | |
32 | + File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false) | |
33 | + File.expects(:exists?).with(p2+"test/_object.rhtml").returns(false) | |
34 | + File.expects(:exists?).with(p2+"test/_object.html.erb").returns(false) | |
17 | 35 | |
36 | + assert_equal 'integer', partial_for_class(Integer) | |
18 | 37 | assert_equal 'numeric', partial_for_class(Float) |
19 | - assert_equal 'numeric', partial_for_class(Numeric) | |
20 | - assert_equal 'runtime_error', partial_for_class(RuntimeError) | |
38 | + assert_raises ArgumentError do | |
39 | + partial_for_class(Object) | |
40 | + end | |
41 | + end | |
42 | + | |
43 | + should 'calculate correctly partial for namespaced models' do | |
44 | + p = 'path/' | |
45 | + @controller = mock() | |
46 | + @controller.stubs(:view_paths).returns([p]) | |
47 | + self.stubs(:params).returns({:controller => 'test'}) | |
48 | + | |
49 | + class School; class Project; end; end | |
50 | + | |
51 | + File.expects(:exists?).with(p+"test/application_helper_test/school/_project.rhtml").returns(true) | |
52 | + | |
53 | + assert_equal 'test/application_helper_test/school/project', partial_for_class(School::Project) | |
21 | 54 | end |
22 | 55 | |
23 | 56 | should 'give error when there is no partial for class' do | ... | ... |