diff --git a/lib/effigy/rails/template_handler.rb b/lib/effigy/rails/template_handler.rb index c65bbec..9c47674 100644 --- a/lib/effigy/rails/template_handler.rb +++ b/lib/effigy/rails/template_handler.rb @@ -32,7 +32,15 @@ def load_view_class end def view_class_name - [base_path, view_name, 'view'].join('_').camelize + view_class_components.join('_').camelize.sub(/^Layouts/, '') + end + + def view_class_components + [base_path, view_name, layout? ? 'layout' : 'view'] + end + + def layout? + base_path =~ /^layouts/ end def template_source diff --git a/rails/generators/effigy_view/effigy_view_generator.rb b/rails/generators/effigy_view/effigy_view_generator.rb index bee19c5..7dc96a7 100644 --- a/rails/generators/effigy_view/effigy_view_generator.rb +++ b/rails/generators/effigy_view/effigy_view_generator.rb @@ -39,20 +39,42 @@ def effigy_views(view_names) end def effigy_view(view_name) - view_path = File.join('app', 'views', @controller_path, "#{view_name}.html.effigy") - template_path = File.join('app', 'templates', @controller_path, "#{view_name}.html") - view_class_name = "#{@controller_path.camelize}#{view_name.camelize}View".sub(/^::/, '') + view_path = File.join('app', 'views', @controller_path, "#{view_name}.html.effigy") + template_path = File.join('app', 'templates', @controller_path, "#{view_name}.html") + assigns = { :view_class_name => view_class_name(view_name), + :template_path => template_path, + :view_path => view_path } - template 'view.erb', + template template_for('view'), view_path, - :assigns => { :view_class_name => view_class_name, - :template_path => template_path } + :assigns => assigns - template 'template.erb', + template template_for('template'), template_path, - :assigns => { :view_class_name => view_class_name, - :template_path => template_path, - :view_path => view_path } + :assigns => assigns + end + + private + + def view_class_name(view_name) + prefix = "#{@controller_path.camelize}#{view_name.camelize}" + if layout? + "#{prefix}Layout".sub(/^::Layouts(::)?/, '') + else + "#{prefix}View".sub(/^::/, '') + end + end + + def template_for(file) + if layout? + "layout_#{file}.erb" + else + "#{file}.erb" + end + end + + def layout? + @controller_path =~ /^\/layouts/ end end diff --git a/rails/generators/effigy_view/templates/layout_template.erb b/rails/generators/effigy_view/templates/layout_template.erb new file mode 100644 index 0000000..ea00a56 --- /dev/null +++ b/rails/generators/effigy_view/templates/layout_template.erb @@ -0,0 +1,7 @@ + + +

<%= view_class_name %>

+

Edit me at <%= template_path %>

+

Edit my view at <%= view_path %>

+ + diff --git a/rails/generators/effigy_view/templates/layout_view.erb b/rails/generators/effigy_view/templates/layout_view.erb new file mode 100644 index 0000000..dab918f --- /dev/null +++ b/rails/generators/effigy_view/templates/layout_view.erb @@ -0,0 +1,13 @@ +class <%= view_class_name %> < Rails::Effigy::View + private + def transform + # Apply transformations to the template file here: + # text('h1', 'Hello') + # Assigns from the action are available: + # text('h1', @post.title) + # Transformations will be applied to to the template file: + # <%= template_path %> + # See the documentation for more information. + html('body', content_for(:layout)) + end +end diff --git a/spec/effigy/rails/template_handler_spec.rb b/spec/effigy/rails/template_handler_spec.rb index bf3df97..1bb7cf6 100644 --- a/spec/effigy/rails/template_handler_spec.rb +++ b/spec/effigy/rails/template_handler_spec.rb @@ -32,7 +32,7 @@ def transform HTML create_rails_file 'app/views/layouts/application.html.effigy', <<-RUBY - class LayoutsApplicationView < Effigy::Rails::View + class ApplicationLayout < Effigy::Rails::View def transform html('body', content_for(:layout)) end diff --git a/spec/rails/generators/effigy_view_spec.rb b/spec/rails/generators/effigy_view_spec.rb index 22c0437..1c18795 100644 --- a/spec/rails/generators/effigy_view_spec.rb +++ b/spec/rails/generators/effigy_view_spec.rb @@ -1,18 +1,44 @@ require 'spec_helper' -describe "script/generate effigy_view users create" do - before do - @controller_name = 'users' - @view_name = 'create' - @view_class_name = 'UsersCreateView' +module TemplateMatchers + def contain(expected_text) + simple_matcher("contain the following lines:\n#{expected_text}") do |path, matcher| + if File.exist?(path) + actual_text = IO.read(path) + if actual_text.include?(expected_text) + true + else + matcher.failure_message = + "Expected to get the following text:\n#{expected_text}\nBut got:\n#{actual_text}" + false + end + else + matcher.failure_message = "File does not exist" + false + end + end + end + + def rails_command(command) FileUtils.cd RAILS_ROOT do - command = "script/generate effigy_view --backtrace #{@controller_name} #{@view_name} 2>&1" - output = `#{command}` + output = `#{command} 2>&1` unless $? == 0 violated "Command failed: #{command}\n#{output}" end end end +end + +describe "script/generate effigy_view users create" do + + include TemplateMatchers + + before do + @controller_name = 'users' + @view_name = 'create' + @view_class_name = 'UsersCreateView' + rails_command "script/generate effigy_view --backtrace #{@controller_name} #{@view_name} 2>&1" + end after do FileUtils.rm_f(view_path) @@ -49,22 +75,55 @@ def relative_template_path File.join('app', 'templates', @controller_name, "#{@view_name}.html") end - def contain(expected_text) - simple_matcher("contain the following lines:\n#{expected_text}") do |path, matcher| - if File.exist?(path) - actual_text = IO.read(path) - if actual_text.include?(expected_text) - true - else - matcher.failure_message = - "Expected to get the following text:\n#{expected_text}\nBut got:\n#{actual_text}" - false - end - else - matcher.failure_message = "File does not exist" - false - end - end +end + +describe "script/generate effigy_view layouts narrow" do + + include TemplateMatchers + + before do + @layout_name = 'narrow' + @layout_class_name = 'NarrowLayout' + rails_command "script/generate effigy_view --backtrace layouts #{@layout_name} 2>&1" + end + + after do + FileUtils.rm_f(view_path) + FileUtils.rm_f(template_path) + end + + it "should create a view file" do + view_path.should contain("class #{@layout_class_name} < Rails::Effigy::View") + view_path.should contain("private") + view_path.should contain("def transform") + view_path.should contain("html('body', content_for(:layout))") + view_path.should contain(relative_template_path) + view_path.should contain("end\nend") + end + + it "should create a template file" do + template_path.should contain("") + template_path.should contain("") + template_path.should contain("

Edit me at #{relative_template_path}

") + template_path.should contain("

Edit my view at #{relative_view_path}

") + template_path.should contain("") + template_path.should contain("") + end + + def view_path + File.join(RAILS_ROOT, relative_view_path) + end + + def relative_view_path + File.join('app', 'views', 'layouts', "#{@layout_name}.html.effigy") + end + + def template_path + File.join(RAILS_ROOT, relative_template_path) + end + + def relative_template_path + File.join('app', 'templates', 'layouts', "#{@layout_name}.html") end end