forked from norman/haml-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Norman Clarke
committed
Nov 7, 2008
0 parents
commit 4c8cf96
Showing
23 changed files
with
646 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2008 [name of plugin creator] | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
h1. Haml Scaffold | ||
|
||
A collection of hacks to the Rails scaffold generator, to make it output | ||
templates using HAML rather than ERB. You may like some of it, and may hate | ||
other parts. You're free to use it under the terms of the MIT license if the | ||
parts you like outweigh the parts you hate. | ||
|
||
This scaffold generator does the same thing as the default Rails scaffold | ||
generator, with a few differences. | ||
|
||
h2. Differences from Rails Scaffolding: | ||
|
||
* HAML not ERB. | ||
|
||
h3. Controller | ||
|
||
* Loads object with a before_filter to be DRYer. | ||
* Adds placeholder permission checking before_filters on all actions. | ||
* "Destroy" method handles error conditions. | ||
* Actions are alphabetized for more obvious consistency. | ||
|
||
h3. Controller Test | ||
|
||
* Tests error conditions, not just the "happy-path." | ||
* Has 100% code coverage with RCov. | ||
* Simplifies test method names and alphabetizes them for more obvious consistency. | ||
* Uses some very simple mocking with mocha to limit calls to the DB. | ||
|
||
h3. Views | ||
|
||
* Have cleaner, more semantic XHTML. | ||
* Are broken up into a couple of partials to be DRYer. | ||
* Use will_paginate. | ||
|
||
h3. Misc | ||
|
||
* Doesn't generate a layout or CSS file. | ||
|
||
h2. Samples | ||
|
||
"View them here.":http://github.com/norman/haml-scaffold/tree/master/samples | ||
|
||
h2. Installation | ||
|
||
There are three ways you can install this generator: | ||
|
||
h3. Gem | ||
|
||
@sudo gem install norman-haml_scaffold@ | ||
|
||
- or - | ||
|
||
<code> | ||
<pre> | ||
git clone git://github.com/norman/haml-scaffold.git | ||
cd haml-scaffold | ||
gem build haml_scaffold.gemspec | ||
sudo gem install haml_scaffold-*.gem | ||
</pre> | ||
</code> | ||
|
||
h3. Manual | ||
|
||
Download the tarball from the Github repository and unarchive it in ~/.rails/generators. | ||
|
||
h3. Plugin | ||
|
||
@./script/plugin install git://github.com/norman/haml-scaffold.git@ | ||
|
||
|
||
|
||
h2. Dependencies | ||
|
||
The generated code will depend on: | ||
|
||
* "will_paginate":http://github.com/mislav/will_paginate/ | ||
* "mocha":http://mocha.rubyforge.org/ | ||
|
||
You'll need to add the gems and or requires to your config/environment.rb | ||
manually. | ||
|
||
h2. Other stuff you might be interested in: | ||
|
||
* "RSpec HAML scaffold generator":http://github.com/dfischer/rspec-haml-scaffold-generator | ||
* "Randomba scaffold":https://github.com/norman/randomba-scaffold/tree (like this one, but ERB) | ||
|
||
h2. Author | ||
|
||
"Norman Clarke":mailto:[email protected] | ||
|
||
This work is derived from code in "Ruby on Rails":http://rubyonrails.org/ and | ||
is released under its same license, the MIT License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
class HamlScaffoldGenerator < Rails::Generator::NamedBase | ||
default_options :skip_timestamps => false, :skip_migration => false | ||
|
||
attr_reader :controller_name, | ||
:controller_class_path, | ||
:controller_file_path, | ||
:controller_class_nesting, | ||
:controller_class_nesting_depth, | ||
:controller_class_name, | ||
:controller_underscore_name, | ||
:controller_singular_name, | ||
:controller_plural_name | ||
alias_method :controller_file_name, :controller_underscore_name | ||
alias_method :controller_table_name, :controller_plural_name | ||
|
||
def initialize(runtime_args, runtime_options = {}) | ||
super | ||
|
||
@controller_name = @name.pluralize | ||
|
||
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) | ||
@controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name) | ||
@controller_singular_name=base_name.singularize | ||
if @controller_class_nesting.empty? | ||
@controller_class_name = @controller_class_name_without_nesting | ||
else | ||
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" | ||
end | ||
end | ||
|
||
def manifest | ||
record do |m| | ||
|
||
# Check for class naming collisions. | ||
m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper") | ||
m.class_collisions(class_path, "#{class_name}") | ||
|
||
# Controller, helper, views, test and stylesheets directories. | ||
m.directory(File.join('app/models', class_path)) | ||
m.directory(File.join('app/controllers', controller_class_path)) | ||
m.directory(File.join('app/helpers', controller_class_path)) | ||
m.directory(File.join('app/views', controller_class_path, controller_file_name)) | ||
m.directory(File.join('test/functional', controller_class_path)) | ||
m.directory(File.join('test/unit', class_path)) | ||
|
||
for action in scaffold_views | ||
m.template( | ||
"view_#{action}.html.erb", | ||
File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.haml") | ||
) | ||
end | ||
|
||
m.template( | ||
"_form.html.erb", | ||
File.join('app/views', controller_class_path, controller_file_name, "_form.html.haml") | ||
) | ||
|
||
m.template( | ||
"_object.html.erb", | ||
File.join('app/views', controller_class_path, controller_file_name, "_#{name}.html.haml") | ||
) | ||
|
||
m.template( | ||
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb") | ||
) | ||
|
||
m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")) | ||
m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb")) | ||
|
||
m.route_resources controller_file_name | ||
|
||
m.dependency 'model', [name] + @args, :collision => :skip | ||
end | ||
end | ||
|
||
protected | ||
# Override with your own usage banner. | ||
def banner | ||
"Usage: #{$0} scaffold ModelName [field:type, field:type]" | ||
end | ||
|
||
def add_options!(opt) | ||
opt.separator '' | ||
opt.separator 'Options:' | ||
opt.on("--skip-timestamps", | ||
"Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v } | ||
opt.on("--skip-migration", | ||
"Don't generate a migration file for this model") { |v| options[:skip_migration] = v } | ||
end | ||
|
||
def scaffold_views | ||
%w[ index show new edit ] | ||
end | ||
|
||
def model_name | ||
class_name.demodulize | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
- form_for(<%= singular_name %>) do |f| | ||
= f.error_messages | ||
<% for attribute in attributes -%> | ||
%p | ||
= f.label :<%= attribute.name %> | ||
%br | ||
= f.<%= attribute.field_type %> :<%= attribute.name %> | ||
<% end -%> | ||
%p | ||
= f.submit "Submit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
= content_tag_for :div, <%= singular_name %> do | ||
%ul | ||
<% for attribute in attributes -%> | ||
%li | ||
%strong <%= attribute.column.human_name %>: | ||
=h <%= singular_name %>.<%= attribute.name %> | ||
<% end -%> | ||
= link_to 'Show', <%= singular_name %> | ||
= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) | ||
= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
class <%= controller_class_name %>Controller < ApplicationController | ||
before_filter :find_<%= file_name %> | ||
|
||
<%= file_name.pluralize.upcase %>_PER_PAGE = 20 | ||
def create | ||
@<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>]) | ||
respond_to do |format| | ||
if @<%= file_name %>.save | ||
flash[:notice] = '<%= class_name %> was successfully created.' | ||
format.html { redirect_to @<%= file_name %> } | ||
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> } | ||
else | ||
format.html { render :action => "new" } | ||
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
def destroy | ||
respond_to do |format| | ||
if @<%= file_name %>.destroy | ||
flash[:notice] = '<%= class_name %> was successfully destroyed.' | ||
format.html { redirect_to <%= file_name.pluralize %>_path } | ||
format.xml { head :ok } | ||
else | ||
flash[:error] = '<%= class_name %> could not be destroyed.' | ||
format.html { redirect_to @<%= file_name %> } | ||
format.xml { head :unprocessable_entity } | ||
end | ||
end | ||
end | ||
def index | ||
@<%= table_name %> = <%= class_name %>.paginate(:page => params[:page], :per_page => <%= file_name.pluralize.upcase %>_PER_PAGE) | ||
respond_to do |format| | ||
format.html | ||
format.xml { render :xml => @<%= table_name %> } | ||
end | ||
end | ||
def edit | ||
end | ||
def new | ||
@<%= file_name %> = <%= class_name %>.new | ||
respond_to do |format| | ||
format.html | ||
format.xml { render :xml => @<%= file_name %> } | ||
end | ||
end | ||
def show | ||
respond_to do |format| | ||
format.html | ||
format.xml { render :xml => @<%= file_name %> } | ||
end | ||
end | ||
def update | ||
respond_to do |format| | ||
if @<%= file_name %>.update_attributes(params[:<%= file_name %>]) | ||
flash[:notice] = '<%= class_name %> was successfully updated.' | ||
format.html { redirect_to @<%= file_name %> } | ||
format.xml { head :ok } | ||
else | ||
format.html { render :action => "edit" } | ||
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_<%= file_name %> | ||
@<%= file_name %> = <%= class_name %>.find(params[:id]) if params[:id] | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'test_helper' | ||
|
||
class <%= controller_class_name %>ControllerTest < ActionController::TestCase | ||
def test_create | ||
<%= class_name %>.any_instance.expects(:save).returns(true) | ||
post :create, :<%= file_name %> => { } | ||
assert_response :redirect | ||
end | ||
|
||
def test_create_with_failure | ||
<%= class_name %>.any_instance.expects(:save).returns(false) | ||
post :create, :<%= file_name %> => { } | ||
assert_template "new" | ||
end | ||
|
||
def test_destroy | ||
<%= class_name %>.any_instance.expects(:destroy).returns(true) | ||
delete :destroy, :id => <%= table_name %>(:one).to_param | ||
assert_not_nil flash[:notice] | ||
assert_response :redirect | ||
end | ||
|
||
def test_destroy_with_failure | ||
<%= class_name %>.any_instance.expects(:destroy).returns(false) | ||
delete :destroy, :id => <%= table_name %>(:one).to_param | ||
assert_not_nil flash[:error] | ||
assert_response :redirect | ||
end | ||
|
||
def test_edit | ||
get :edit, :id => <%= table_name %>(:one).to_param | ||
assert_response :success | ||
end | ||
|
||
def test_index | ||
get :index | ||
assert_response :success | ||
assert_not_nil assigns(:<%= table_name %>) | ||
end | ||
|
||
def test_new | ||
get :new | ||
assert_response :success | ||
end | ||
|
||
def test_show | ||
get :show, :id => <%= table_name %>(:one).to_param | ||
assert_response :success | ||
end | ||
|
||
def test_update | ||
<%= class_name %>.any_instance.expects(:save).returns(true) | ||
put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { } | ||
assert_response :redirect | ||
end | ||
|
||
def test_update_with_failure | ||
<%= class_name %>.any_instance.expects(:save).returns(false) | ||
put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { } | ||
assert_template "edit" | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module <%= controller_class_name %>Helper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
%h2 Editing <%= singular_name %> | ||
= render :partial => "form", :locals => {:<%= singular_name %> => @<%= singular_name %>} | ||
%ul | ||
%li= link_to 'Show', @<%= singular_name %> | ||
%li= link_to 'Back', <%= plural_name %>_path |
Oops, something went wrong.