Skip to content

Commit

Permalink
fix how alternate layouts are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
adamlogic committed Feb 25, 2010
1 parent fb6c234 commit 4bd247d
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ you embed a "mini app" inside of your main app.

Just throw your mockups into app/views/mockups, then navigate to /mockups in your app. Just like any other
Rails view, you can use partials, helpers, etc. If you want to use a layout other than your application
default, tack it on as an additional file extension (example.html.erb.alternate).
default, nest your mockup in a subdirectory with the same name as the layout (see examples).

== Can I see an example?

Expand Down
21 changes: 7 additions & 14 deletions app/controllers/mockups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class MockupsController < ApplicationController
LAYOUTS = Dir.glob(File.join(Rails.root, 'app', 'views', 'layouts', '*.html.*')).map do |file|
File.basename(file).split('.').first
end

def frameset
render :layout => false
Expand Down Expand Up @@ -26,24 +29,14 @@ def show
session[:template_name] = params[:template_name]
session[:parent_dir] = params[:parent_dir]
template_path = File.join(['mockups', params[:parent_dir], params[:template_name]].compact)
render :template => template_path, :layout => extract_layout(template_path)
render :template => template_path, :layout => determine_layout
end

private

def extract_layout(template_path)
full_path = File.join(Rails.root, 'app', 'views', template_path)
located_files = Dir.glob("#{full_path}*")

return true if located_files.empty?

filename_components = File.basename(located_files[0]).split('.')

if filename_components.length < 4
true
else
filename_components.last
end
def determine_layout
directory = params[:parent_dir].to_s.downcase
LAYOUTS.include?(directory) ? directory : true
end

end
6 changes: 3 additions & 3 deletions generators/showoff/showoff_generator.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class ShowoffGenerator < Rails::Generator::Base
def manifest
record do |m|
m.directory 'app/views/mockups/sample_section'
m.directory 'app/views/mockups/basic'

m.file 'sample_mockup.html.erb', 'app/views/mockups/sample_mockup.html.erb'
m.file 'alternate_layout.html.erb.alternate', 'app/views/mockups/alternate_layout.html.erb.alternate'
m.file 'sample_nested.html.erb', 'app/views/mockups/sample_section/sample_nested.html.erb'
m.file 'alternate_layout.html.erb', 'app/views/mockups/basic/alternate_layout.html.erb'
m.file 'showoff.css', 'public/stylesheets/showoff.css'
end
end
Expand Down
10 changes: 10 additions & 0 deletions generators/showoff/templates/alternate_layout.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h1>Nesting & Alternate Layout Example</h1>

<p>
You can add subdirectories within app/views/mockups to organize your mockups (only one level deep).
</p>

<p>
If the subdirectory name matches a layout file, that layout would be used.
In this example, if you had a layout named "basic", it would be used instead of the default.
</p>

This file was deleted.

5 changes: 0 additions & 5 deletions generators/showoff/templates/sample_nested.html.erb

This file was deleted.

0 comments on commit 4bd247d

Please sign in to comment.