Skip to content

Commit

Permalink
Use different class name conventions for layous, generate better view…
Browse files Browse the repository at this point in the history
…/template for layouts
  • Loading branch information
Joe Ferris committed Nov 11, 2009
1 parent 4075d5d commit df7d232
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 35 deletions.
10 changes: 9 additions & 1 deletion lib/effigy/rails/template_handler.rb
Expand Up @@ -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
Expand Down
42 changes: 32 additions & 10 deletions rails/generators/effigy_view/effigy_view_generator.rb
Expand Up @@ -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

Expand Down
7 changes: 7 additions & 0 deletions rails/generators/effigy_view/templates/layout_template.erb
@@ -0,0 +1,7 @@
<html>
<body>
<h1><%= view_class_name %></h1>
<p>Edit me at <%= template_path %></p>
<p>Edit my view at <%= view_path %></p>
</body>
</html>
13 changes: 13 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion spec/effigy/rails/template_handler_spec.rb
Expand Up @@ -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
Expand Down
105 changes: 82 additions & 23 deletions 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)
Expand Down Expand Up @@ -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("<html>")
template_path.should contain("<body>")
template_path.should contain("<p>Edit me at #{relative_template_path}</p>")
template_path.should contain("<p>Edit my view at #{relative_view_path}</p>")
template_path.should contain("</body>")
template_path.should contain("</html>")
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

0 comments on commit df7d232

Please sign in to comment.