dramsay-simple_scaffold 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Doug Ramsay
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,18 @@
1
+ # Simple Scaffold Generator
2
+
3
+ A customized, simpler version of the rspec_scaffold generator.
4
+
5
+ The generator does not include respond_to blocks in the controller and skips controller routing specs as well as view specs.
6
+
7
+ ## Installation
8
+
9
+ gem install dramsay-simple_scaffold -s http://gems.github.com
10
+
11
+ ## Usage
12
+
13
+ # From root of your Rails app
14
+ script/generate simple_scaffold Post title:string body:text
15
+
16
+ ## Copyright
17
+
18
+ Copyright (c) 2009 Doug Ramsay. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "simple_scaffold"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "doug@intridea.com"
10
+ gem.homepage = "http://github.com/dramsay/simple_scaffold"
11
+ gem.authors = ["Doug Ramsay"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "simple_scaffold #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 0
4
+ :patch: 1
@@ -0,0 +1,109 @@
1
+ module Rails
2
+ module Generator
3
+ class GeneratedAttribute
4
+ def default_value
5
+ @default_value ||= case type
6
+ when :int, :integer then "1"
7
+ when :float then "1.5"
8
+ when :decimal then "9.99"
9
+ when :datetime, :timestamp, :time then "Time.now"
10
+ when :date then "Date.today"
11
+ when :string, :text then "\"value for #{@name}\""
12
+ when :boolean then "false"
13
+ else
14
+ ""
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ class SimpleScaffoldGenerator < Rails::Generator::NamedBase
22
+ default_options :skip_timestamps => false, :skip_migration => false
23
+
24
+ attr_reader :controller_name,
25
+ :controller_class_path,
26
+ :controller_file_path,
27
+ :controller_class_nesting,
28
+ :controller_class_nesting_depth,
29
+ :controller_class_name,
30
+ :controller_underscore_name,
31
+ :controller_singular_name,
32
+ :controller_plural_name
33
+ alias_method :controller_file_name, :controller_underscore_name
34
+ alias_method :controller_table_name, :controller_plural_name
35
+
36
+ def initialize(runtime_args, runtime_options = {})
37
+ super
38
+
39
+ if @name == @name.pluralize && !options[:force_plural]
40
+ logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
41
+ @name = @name.singularize
42
+ end
43
+
44
+ @controller_name = @name.pluralize
45
+
46
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
47
+ @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
48
+ @controller_singular_name=base_name.singularize
49
+ if @controller_class_nesting.empty?
50
+ @controller_class_name = @controller_class_name_without_nesting
51
+ else
52
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
53
+ end
54
+ end
55
+
56
+ def manifest
57
+ record do |m|
58
+ # Check for class naming collisions.
59
+ m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
60
+ m.class_collisions(class_name)
61
+
62
+ # Directories for templates
63
+ m.directory(File.join('app/models', class_path))
64
+ m.directory(File.join('app/controllers', controller_class_path))
65
+ m.directory(File.join('spec/controllers', controller_class_path))
66
+ m.directory(File.join('spec/models', class_path))
67
+
68
+ m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
69
+ m.template 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
70
+ m.template 'controller_spec.rb', File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
71
+ m.template 'model_spec.rb', File.join('spec/models', class_path, "#{file_name}_spec.rb")
72
+
73
+ unless options[:skip_migration]
74
+ m.migration_template(
75
+ 'model:migration.rb', 'db/migrate',
76
+ :assigns => {
77
+ :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}",
78
+ :attributes => attributes
79
+ },
80
+ :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
81
+ )
82
+ end
83
+
84
+ m.route_resources controller_file_name
85
+
86
+ end
87
+ end
88
+
89
+ protected
90
+ # Override with your own usage banner.
91
+ def banner
92
+ "Usage: #{$0} simple_scaffold ModelName [field:type, field:type]"
93
+ end
94
+
95
+ def add_options!(opt)
96
+ opt.separator ''
97
+ opt.separator 'Options:'
98
+ opt.on("--skip-timestamps",
99
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
100
+ opt.on("--skip-migration",
101
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
102
+ opt.on("--force-plural",
103
+ "Forces the generation of a plural ModelName") { |v| options[:force_plural] = v }
104
+ end
105
+
106
+ def model_name
107
+ class_name.demodulize
108
+ end
109
+ end
@@ -0,0 +1,47 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+ def index
4
+ @<%= table_name %> = <%= class_name %>.find(:all)
5
+ end
6
+
7
+ def show
8
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
9
+ end
10
+
11
+ def new
12
+ @<%= file_name %> = <%= class_name %>.new
13
+ end
14
+
15
+ def edit
16
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
17
+ end
18
+
19
+ def create
20
+ @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
21
+
22
+ if @<%= file_name %>.save
23
+ flash[:notice] = '<%= class_name %> was successfully created.'
24
+ redirect_to(@<%= file_name %>)
25
+ else
26
+ render :action => "new"
27
+ end
28
+ end
29
+
30
+ def update
31
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
32
+
33
+ if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
34
+ flash[:notice] = '<%= class_name %> was successfully updated.'
35
+ redirect_to(@<%= file_name %>)
36
+ else
37
+ render :action => "edit"
38
+ end
39
+ end
40
+
41
+ def destroy
42
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
43
+ @<%= file_name %>.destroy
44
+
45
+ redirect_to(<%= table_name %>_url)
46
+ end
47
+ end
@@ -0,0 +1,149 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2
+
3
+ describe <%= controller_class_name %>Controller do
4
+
5
+ def mock_<%= file_name %>(stubs={})
6
+ @mock_<%= file_name %> ||= mock_model(<%= class_name %>, stubs)
7
+ end
8
+
9
+ describe "responding to GET index" do
10
+
11
+ it "should expose all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
12
+ <%= class_name %>.should_receive(:find).with(:all).and_return([mock_<%= file_name %>])
13
+ get :index
14
+ assigns[:<%= table_name %>].should == [mock_<%= file_name %>]
15
+ end
16
+
17
+ end
18
+
19
+ describe "responding to GET show" do
20
+
21
+ it "should expose the requested <%= file_name %> as @<%= file_name %>" do
22
+ <%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
23
+ get :show, :id => "37"
24
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
25
+ end
26
+
27
+ end
28
+
29
+ describe "responding to GET new" do
30
+
31
+ it "should expose a new <%= file_name %> as @<%= file_name %>" do
32
+ <%= class_name %>.should_receive(:new).and_return(mock_<%= file_name %>)
33
+ get :new
34
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
35
+ end
36
+
37
+ end
38
+
39
+ describe "responding to GET edit" do
40
+
41
+ it "should expose the requested <%= file_name %> as @<%= file_name %>" do
42
+ <%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
43
+ get :edit, :id => "37"
44
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
45
+ end
46
+
47
+ end
48
+
49
+ describe "responding to POST create" do
50
+
51
+ describe "with valid params" do
52
+
53
+ it "should expose a newly created <%= file_name %> as @<%= file_name %>" do
54
+ <%= class_name %>.should_receive(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => true))
55
+ post :create, :<%= file_name %> => {:these => 'params'}
56
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
57
+ end
58
+
59
+ it "should redirect to the created <%= file_name %>" do
60
+ <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => true))
61
+ post :create, :<%= file_name %> => {}
62
+ response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
63
+ end
64
+
65
+ end
66
+
67
+ describe "with invalid params" do
68
+
69
+ it "should expose a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
70
+ <%= class_name %>.stub!(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => false))
71
+ post :create, :<%= file_name %> => {:these => 'params'}
72
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
73
+ end
74
+
75
+ it "should re-render the 'new' template" do
76
+ <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => false))
77
+ post :create, :<%= file_name %> => {}
78
+ response.should render_template('new')
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ describe "responding to PUT udpate" do
86
+
87
+ describe "with valid params" do
88
+
89
+ it "should update the requested <%= file_name %>" do
90
+ <%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
91
+ mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
92
+ put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
93
+ end
94
+
95
+ it "should expose the requested <%= file_name %> as @<%= file_name %>" do
96
+ <%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>(:update_attributes => true))
97
+ put :update, :id => "1"
98
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
99
+ end
100
+
101
+ it "should redirect to the <%= file_name %>" do
102
+ <%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>(:update_attributes => true))
103
+ put :update, :id => "1"
104
+ response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
105
+ end
106
+
107
+ end
108
+
109
+ describe "with invalid params" do
110
+
111
+ it "should update the requested <%= file_name %>" do
112
+ <%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
113
+ mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
114
+ put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
115
+ end
116
+
117
+ it "should expose the <%= file_name %> as @<%= file_name %>" do
118
+ <%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>(:update_attributes => false))
119
+ put :update, :id => "1"
120
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
121
+ end
122
+
123
+ it "should re-render the 'edit' template" do
124
+ <%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>(:update_attributes => false))
125
+ put :update, :id => "1"
126
+ response.should render_template('edit')
127
+ end
128
+
129
+ end
130
+
131
+ end
132
+
133
+ describe "responding to DELETE destroy" do
134
+
135
+ it "should destroy the requested <%= file_name %>" do
136
+ <%= class_name %>.should_receive(:find).with("37").and_return(mock_<%= file_name %>)
137
+ mock_<%= file_name %>.should_receive(:destroy)
138
+ delete :destroy, :id => "37"
139
+ end
140
+
141
+ it "should redirect to the <%= table_name %> list" do
142
+ <%= class_name %>.stub!(:find).and_return(mock_<%= file_name %>(:destroy => true))
143
+ delete :destroy, :id => "1"
144
+ response.should redirect_to(<%= table_name %>_url)
145
+ end
146
+
147
+ end
148
+
149
+ end
@@ -0,0 +1,16 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ <% for attribute in attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %>
6
+ <% end -%>
7
+ <% unless options[:skip_timestamps] %>
8
+ t.timestamps
9
+ <% end -%>
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table :<%= table_name %>
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <% attributes.select(&:reference?).each do |attribute| -%>
3
+ belongs_to :<%= attribute.name %>
4
+ <% end -%>
5
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2
+
3
+ describe <%= class_name %> do
4
+ before(:each) do
5
+ @valid_attributes = {
6
+ <% attributes.each_with_index do |attribute, attribute_index| -%>
7
+ :<%= attribute.name %> => <%= attribute.default_value %><%= attribute_index == attributes.length - 1 ? '' : ','%>
8
+ <% end -%>
9
+ }
10
+ end
11
+
12
+ it "should create a new instance given valid attributes" do
13
+ <%= class_name %>.create!(@valid_attributes)
14
+ end
15
+ end
File without changes
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class SimpleScaffoldTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'simple_scaffold'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dramsay-simple_scaffold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Doug Ramsay
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dougramsay@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.markdown
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - Rakefile
30
+ - VERSION.yml
31
+ - lib/simple_scaffold.rb
32
+ - test/simple_scaffold_test.rb
33
+ - test/test_helper.rb
34
+ - generators/simple_scaffold/simple_scaffold_generator.rb
35
+ - generators/simple_scaffold/templates/controller.rb
36
+ - generators/simple_scaffold/templates/controller_spec.rb
37
+ - generators/simple_scaffold/templates/migration.rb
38
+ - generators/simple_scaffold/templates/model.rb
39
+ - generators/simple_scaffold/templates/model_spec.rb
40
+ - README.markdown
41
+ has_rdoc: true
42
+ homepage: http://github.com/dramsay/simple_scaffold
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: A customized, simpler version of the rspec_scaffold generator
67
+ test_files:
68
+ - test/simple_scaffold_test.rb
69
+ - test/test_helper.rb