Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added overlay for templates. removed old generator to avoid naming co…

…nflict with rails_datamapper gem
  • Loading branch information...
commit cf71c320aa601a540257152bd4fa8ae9ee1b71d1 1 parent 7b7ff7b
Christian Meier authored September 17, 2009

Showing 26 changed files with 409 additions and 240 deletions. Show diff stats Hide diff stats

  1. 6  generate.sh
  2. 12  generators/datamapper_controller/datamapper_controller_generator.rb
  3. 14  generators/datamapper_model/datamapper_model_generator.rb
  4. 13  generators/datamapper_model/templates/model.rb
  5. 0  generators/{dm_model → datamapper_model}/templates/unit_test.rb
  6. 12  generators/dm_install/dm_install_generator.rb
  7. 42  generators/dm_install/templates/datamapper.rake
  8. 10  generators/dm_migration/dm_migration_generator.rb
  9. 16  generators/dm_migration/templates/migration.rb
  10. 27  generators/dm_model/dm_model_generator.rb
  11. 14  generators/dm_model/templates/model.rb
  12. 47  generators/overlay.rb
  13. 8  generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
  14. 0  generators/{rspec_dm_controller → rspec_datamapper_controller}/templates/controller.rb
  15. 12  generators/{rspec_dm_controller → rspec_datamapper_controller}/templates/controller_spec.rb
  16. 0  generators/{rspec_dm_controller → rspec_datamapper_controller}/templates/helper.rb
  17. 0  generators/{rspec_dm_controller → rspec_datamapper_controller}/templates/helper_spec.rb
  18. 13  generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
  19. 7  generators/{rspec_dm_model → rspec_datamapper_model}/templates/model.rb
  20. 5  generators/{rspec_dm_model → rspec_datamapper_model}/templates/model_spec.rb
  21. 16  generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
  22. 90  generators/rspec_datamapper_scaffold/templates/controller.rb
  23. 174  generators/rspec_datamapper_scaffold/templates/controller_spec.rb
  24. 7  generators/rspec_default_values.rb
  25. 71  generators/rspec_dm_controller/rspec_dm_controller_generator.rb
  26. 33  generators/rspec_dm_model/rspec_dm_model_generator.rb
6  generate.sh
... ...
@@ -0,0 +1,6 @@
  1
+script/generate datamapper_model datamapper_model name:string
  2
+script/generate rspec_datamapper_model rspec_datamapper_model name:string
  3
+script/generate datamapper_controller datamapper_controller index
  4
+script/generate rspec_datamapper_controller rspec_datamapper_controller index
  5
+#script/generate datamapper_scaffold datamapper_scaffold name:string
  6
+script/generate rspec_datamapper_scaffold rspec_datamapper_scaffold name:string
12  generators/datamapper_controller/datamapper_controller_generator.rb
... ...
@@ -0,0 +1,12 @@
  1
+require File.dirname(__FILE__) + '/../overlay'
  2
+
  3
+# this controller is just for completeness but does not offer
  4
+# any further functionality to the underlying controller
  5
+class DatamapperControllerGenerator < ControllerGenerator
  6
+
  7
+  def manifest
  8
+    overlay_dirs << File.join(self.class.lookup("controller").path, 'templates')
  9
+    super
  10
+  end
  11
+
  12
+end
14  generators/datamapper_model/datamapper_model_generator.rb
... ...
@@ -0,0 +1,14 @@
  1
+require 'rails_generator/generators/components/model/model_generator'
  2
+require 'active_record'
  3
+require File.dirname(__FILE__) + '/../overlay'
  4
+
  5
+
  6
+class DatamapperModelGenerator <ModelGenerator
  7
+
  8
+  def manifest
  9
+    overlay_dirs << File.join(self.class.lookup("model").path, 'templates')
  10
+    overlay_dirs << source_root
  11
+    super
  12
+  end
  13
+
  14
+end
13  generators/datamapper_model/templates/model.rb
... ...
@@ -0,0 +1,13 @@
  1
+class <%= class_name %>
  2
+  include DataMapper::Resource
  3
+
  4
+  property :id, Serial
  5
+<% Array(attributes).each do |attribute| -%>
  6
+  property :<%= attribute.name %>, <%= attribute.type.to_s.capitalize %>, :nullable => false <% if attribute.type == :string or attribute.type == :text or attribute.type == :slug -%>, :format => /^[^<'&">]*$/<% if attribute.type == :string or attribute.type == :slug %>, :length => 255<% end -%><% end -%>
  7
+
  8
+<% end -%>
  9
+<% unless options[:skip_timestamps] -%>
  10
+  timestamps :at
  11
+<% end -%>
  12
+
  13
+end
0  generators/dm_model/templates/unit_test.rb → generators/datamapper_model/templates/unit_test.rb
File renamed without changes
12  generators/dm_install/dm_install_generator.rb
... ...
@@ -1,12 +0,0 @@
1  
-require 'rails_generator/base'
2  
-
3  
-class DmInstallGenerator < Rails::Generator::Base
4  
-
5  
-  def manifest
6  
-    record do |m|
7  
-      m.directory "lib/tasks"
8  
-      m.template "datamapper.rake",      "lib/tasks/datamapper.rake"
9  
-    end
10  
-  end
11  
-
12  
-end
42  generators/dm_install/templates/datamapper.rake
... ...
@@ -1,42 +0,0 @@
1  
-namespace :db do
2  
-
3  
-  desc "Perform automigration"
4  
-  task :automigrate => :environment do
5  
-    FileList["app/models/**/*.rb"].each do |model|
6  
-      load model
7  
-    end
8  
-    ::DataMapper.auto_migrate!
9  
-  end
10  
-
11  
-  desc "Perform non destructive automigration"
12  
-  task :autoupgrade => :environment do
13  
-    FileList["app/models/**/*.rb"].each do |model|
14  
-      load model
15  
-    end
16  
-    ::DataMapper.auto_upgrade!
17  
-  end
18  
-
19  
-  namespace :migrate do
20  
-    task :load => :environment do
21  
-      gem 'dm-migrations'
22  
-      FileList["db/migrations/*.rb"].each do |migration|
23  
-        load migration
24  
-      end
25  
-    end
26  
-
27  
-    desc "Migrate up using migrations"
28  
-    task :up, :version, :needs => :load do |t, args|
29  
-      version = args[:version]
30  
-      migrate_up!(version)
31  
-    end
32  
-
33  
-    desc "Migrate down using migrations"
34  
-    task :down, :version, :needs => :load do |t, args|
35  
-      version = args[:version]
36  
-      migrate_down!(version)
37  
-    end
38  
-  end
39  
-
40  
-  desc "Migrate the database to the latest version"
41  
-  task :migrate => 'db:migrate:up'
42  
-end
10  generators/dm_migration/dm_migration_generator.rb
... ...
@@ -1,10 +0,0 @@
1  
-require 'rails_generator/generators/components/migration/migration_generator'
2  
-
3  
-class DmMigrationGenerator < Rails::Generator::NamedBase
4  
-
5  
-  def manifest
6  
-    record do |m|
7  
-      m.migration_template "migration.rb", "db/migrate"
8  
-    end
9  
-  end
10  
-end
16  generators/dm_migration/templates/migration.rb
... ...
@@ -1,16 +0,0 @@
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
27  generators/dm_model/dm_model_generator.rb
... ...
@@ -1,27 +0,0 @@
1  
-require 'rails_generator/generators/components/model/model_generator'
2  
-require 'active_record'
3  
-
4  
-class DmModelGenerator <ModelGenerator
5  
-
6  
-  def manifest
7  
-    record do |m|
8  
-      # Check for class naming collisions.
9  
-      m.class_collisions class_path, class_name, "#{class_name}Test"
10  
-
11  
-      # Model, test, and fixture directories.
12  
-      m.directory File.join('app/models', class_path)
13  
-      m.directory File.join('test/unit', class_path)
14  
-
15  
-      # Model class, unit test, and fixtures.
16  
-      m.template 'model.rb',      File.join('app/models', class_path, "#{file_name}.rb")
17  
-      m.template 'unit_test.rb',  File.join('test/unit', class_path, "#{file_name}_test.rb")
18  
-
19  
-      unless options[:skip_migration]
20  
-        m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
21  
-          :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
22  
-        }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
23  
-      end
24  
-    end
25  
-  end
26  
-
27  
-end
14  generators/dm_model/templates/model.rb
... ...
@@ -1,14 +0,0 @@
1  
-class <%= class_name %>
2  
-  include DataMapper::Resource
3  
-  property :id, Serial
4  
-
5  
-<% for attribute in attributes -%>
6  
-  property :<%= attribute.name %>, <%= attribute.type.to_s.capitalize %>, :nullable => false 
7  
-
8  
-<% end -%>
9  
-<% unless options[:skip_timestamps] %>
10  
-  property :created_at, DateTime, :nullable => false
11  
-  property :updated_at, DateTime, :nullable => false
12  
-
13  
-<% end -%>
14  
-end
47  generators/overlay.rb
... ...
@@ -0,0 +1,47 @@
  1
+require 'rails_generator/base'
  2
+
  3
+# this extension allows to reuse generators from other gems by defining
  4
+# a search path (list of overlay directories) for template files. these
  5
+# overlay directories have preference to the rails template search algorithm.
  6
+# the overlay directories can be also given via command line options.
  7
+
  8
+Rails::Generator::NamedBase.class_eval do
  9
+
  10
+  def overlay_dirs
  11
+    options[:overlay_dirs] ||= []
  12
+  end
  13
+
  14
+  def add_options!(opt)
  15
+    super
  16
+    opt.on("--overlay-dir DIR",
  17
+           "overlay") do |v|
  18
+      overlay_dirs << v
  19
+    end
  20
+  end
  21
+
  22
+  def source_path(relative_source)
  23
+    # Check whether we're referring to another generator's file.
  24
+    name, path = relative_source.split(':', 2)
  25
+
  26
+    # first check if the template can be found with in any of the overlay directories
  27
+    if dirs = options[:overlay_dirs]
  28
+      file = path.nil? ? name : path
  29
+      dirs.reverse.each do |dir|
  30
+        if (f = File.join(dir, file)) and File.exists?(f)
  31
+          return f
  32
+        end
  33
+      end
  34
+    end
  35
+
  36
+    # If not, return the full path to our source file.
  37
+    if path.nil?
  38
+      File.join(source_root, name)
  39
+
  40
+      # Otherwise, ask our referral for the file.
  41
+    else
  42
+      # FIXME: this is broken, though almost always true.  Others'
  43
+      # source_root are not necessarily the templates dir.
  44
+      File.join(self.class.lookup(name).path, 'templates', path)
  45
+    end
  46
+  end
  47
+end
8  generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
... ...
@@ -0,0 +1,8 @@
  1
+require File.dirname(__FILE__) + '/../overlay'
  2
+
  3
+class RspecDatamapperControllerGenerator < RspecControllerGenerator
  4
+  def manifest
  5
+    overlay_dirs << File.join(self.class.lookup("rspec_controller").path, 'templates')
  6
+    super
  7
+  end
  8
+end
0  ...ators/rspec_dm_controller/templates/controller.rb → ...pec_datamapper_controller/templates/controller.rb
File renamed without changes
12  .../rspec_dm_controller/templates/controller_spec.rb → ...atamapper_controller/templates/controller_spec.rb
@@ -125,19 +125,19 @@ def user.groups
125 125
 
126 126
       it "updates the requested <%= file_name %>" do
127 127
         <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
128  
-        mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
  128
+        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
129 129
         mock_<%= file_name %>.should_receive(:dirty?)
130 130
         put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
131 131
       end
132 132
 
133 133
       it "exposes the requested <%= file_name %> as @<%= file_name %>" do
134  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => true))
  134
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
135 135
         put :update, :id => "1"
136 136
         assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
137 137
       end
138 138
 
139 139
       it "redirects to the <%= file_name %>" do
140  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => true))
  140
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
141 141
         put :update, :id => "1"
142 142
         response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
143 143
       end
@@ -148,20 +148,20 @@ def user.groups
148 148
 
149 149
       it "updates the requested <%= file_name %>" do
150 150
         <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
151  
-        mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
  151
+        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
152 152
         mock_<%= file_name %>.should_receive(:dirty?)
153 153
         put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
154 154
       end
155 155
 
156 156
       it "exposes the <%= file_name %> as @<%= file_name %>" do
157  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => false))
  157
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
158 158
         mock_<%= file_name %>.should_receive(:dirty?)
159 159
         put :update, :id => "1"
160 160
         assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
161 161
       end
162 162
 
163 163
       it "re-renders the 'edit' template" do
164  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => false, :dirty? => true))
  164
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
165 165
         put :update, :id => "1"
166 166
         response.should render_template('edit')
167 167
       end
0  generators/rspec_dm_controller/templates/helper.rb → ...s/rspec_datamapper_controller/templates/helper.rb
File renamed without changes
0  ...tors/rspec_dm_controller/templates/helper_spec.rb → ...ec_datamapper_controller/templates/helper_spec.rb
File renamed without changes
13  generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
... ...
@@ -0,0 +1,13 @@
  1
+require 'rails_generator/generators/components/model/model_generator'
  2
+require 'active_record'
  3
+require File.dirname(__FILE__) + '/../overlay'
  4
+
  5
+class RspecDatamapperModelGenerator < RspecModelGenerator
  6
+
  7
+  def manifest
  8
+    overlay_dirs << File.join(self.class.lookup("datamapper_model").path, 'templates')
  9
+    overlay_dirs << File.join(self.class.lookup("rspec_model").path, 'templates')
  10
+    super
  11
+  end
  12
+
  13
+end
7  generators/rspec_dm_model/templates/model.rb → generators/rspec_datamapper_model/templates/model.rb
@@ -7,7 +7,7 @@ class <%= class_name %>
7 7
   property :id, Serial
8 8
 
9 9
 <% for attribute in attributes -%>
10  
-  property :<%= attribute.name %>, <%= attribute.type.to_s.camelize %>, :nullable => false <% if attribute.type == :string or attribute.type == :text -%>, :format => /^[^<'&">]*$/<% if attribute.type == :string %>, :length => 255<% end -%><% end -%>
  10
+  property :<%= attribute.name %>, <%= attribute.type.to_s.camelize %>, :nullable => false <% if attribute.type == :string or attribute.type == :text or attribute.type == :slug -%>, :format => /^[^<'&">]*$/<% if attribute.type == :string or attribute.type == :slug %>, :length => 255<% end -%><% end -%>
11 11
 
12 12
 <% end -%>
13 13
 <% unless options[:skip_timestamps] %>
@@ -15,9 +15,4 @@ class <%= class_name %>
15 15
 
16 16
 <% end -%>
17 17
 
18  
-<% if options[:ixtlan] -%>
19  
-   def to_s
20  
-     "<%= class_name %>(#{attribute_get(:id)})"
21  
-   end
22  
-<% end -%>
23 18
 end
5  generators/rspec_dm_model/templates/model_spec.rb → ...rs/rspec_datamapper_model/templates/model_spec.rb 100644 → 100755
@@ -9,13 +9,16 @@
9 9
     }
10 10
   end
11 11
 
  12
+  it "should create a new instance given valid attributes" do
  13
+    <%= class_name %>.create(@valid_attributes)
  14
+  end
12 15
 <% attributes.each do |attribute| -%>
13 16
   it "should require <%= attribute.name %>" do
14 17
     <%= singular_name %> = <%= class_name %>.create(@valid_attributes.merge(:<%= attribute.name %> => nil))
15 18
     <%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
16 19
   end
17 20
 
18  
-<% if attribute.type == :string or attribute.type == :text -%>
  21
+<% if attribute.type == :string or attribute.type == :text  or attribute.type == :slug -%>
19 22
   it 'should not match <%= attribute.name %>' do
20 23
     <%= singular_name %> = <%= class_name %>.create(@valid_attributes.merge(:<%= attribute.name %> => "<script" ))
21 24
     <%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
16  generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
... ...
@@ -0,0 +1,16 @@
  1
+require File.dirname(__FILE__) + '/../overlay'
  2
+require File.dirname(__FILE__) + '/../rspec_default_values'
  3
+
  4
+class RspecDatamapperScaffoldGenerator < RspecScaffoldGenerator
  5
+
  6
+  def manifest
  7
+    overlay_dirs << File.join(self.class.lookup("scaffold").path, 'templates')
  8
+    overlay_dirs << File.join(self.class.lookup("rspec_scaffold").path, 'templates')
  9
+    overlay_dirs << File.join(self.class.lookup("datamapper_model").path, 'templates')
  10
+    overlay_dirs << File.join(self.class.lookup("rspec_model").path, 'templates')
  11
+    overlay_dirs << source_root
  12
+
  13
+    super
  14
+  end
  15
+
  16
+end
90  generators/rspec_datamapper_scaffold/templates/controller.rb
... ...
@@ -0,0 +1,90 @@
  1
+class <%= controller_class_name %>Controller < ApplicationController
  2
+
  3
+  # GET /<%= table_name %>
  4
+  # GET /<%= table_name %>.xml
  5
+  def index
  6
+    @<%= table_name %> = <%= class_name %>.all()
  7
+
  8
+    respond_to do |format|
  9
+      format.html
  10
+      format.xml  { render :xml => @<%= table_name %> }
  11
+    end
  12
+  end
  13
+
  14
+  # GET /<%= table_name %>/1
  15
+  # GET /<%= table_name %>/1.xml
  16
+  def show
  17
+    @<%= file_name %> = <%= class_name %>.get!(params[:id])
  18
+
  19
+    respond_to do |format|
  20
+      format.html # show.html.erb
  21
+      format.xml  { render :xml => @<%= file_name %> }
  22
+    end
  23
+  end
  24
+
  25
+  # GET /<%= table_name %>/new
  26
+  # GET /<%= table_name %>/new.xml
  27
+  def new
  28
+    @<%= file_name %> = <%= class_name %>.new
  29
+
  30
+    respond_to do |format|
  31
+      format.html # new.html.erb
  32
+      format.xml  { render :xml => @<%= file_name %> }
  33
+    end
  34
+  end
  35
+
  36
+  # GET /<%= table_name %>/1/edit
  37
+  def edit
  38
+    @<%= file_name %> = <%= class_name %>.get!(params[:id])
  39
+  end
  40
+
  41
+  # POST /<%= table_name %>
  42
+  # POST /<%= table_name %>.xml
  43
+  def create
  44
+    @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
  45
+
  46
+    respond_to do |format|
  47
+      if @<%= file_name %>.save
  48
+        flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_created')<% else -%>'<%= class_name %> was successfully created.'<% end -%>
  49
+
  50
+        format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
  51
+        format.xml  { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
  52
+      else
  53
+        format.html { render :action => "new" }
  54
+        format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
  55
+      end
  56
+    end
  57
+  end
  58
+
  59
+  # PUT /<%= table_name %>/1
  60
+  # PUT /<%= table_name %>/1.xml
  61
+  def update
  62
+    @<%= file_name %> = <%= class_name %>.get!(params[:id])
  63
+
  64
+    respond_to do |format|
  65
+      if @<%= file_name %>.update(params[:<%= file_name %>]) or not @<%= file_name %>.dirty?
  66
+        flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_updated')<% else -%>'<%= class_name %> was successfully updated.'<% end -%>
  67
+
  68
+        format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
  69
+        format.xml  { head :ok }
  70
+      else
  71
+        format.html { render :action => "edit" }
  72
+        format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
  73
+      end
  74
+    end
  75
+  end
  76
+
  77
+  # DELETE /<%= table_name %>/1
  78
+  # DELETE /<%= table_name %>/1.xml
  79
+  def destroy
  80
+    @<%= file_name %> = <%= class_name %>.get(params[:id])
  81
+    @<%= file_name %>.destroy if @<%= file_name %>
  82
+
  83
+    respond_to do |format|
  84
+      flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_deleted')<% else -%>'<%= class_name %> was successfully deleted.'<% end -%>
  85
+
  86
+      format.html { redirect_to(<%= table_name %>_url) }
  87
+      format.xml  { head :ok }
  88
+    end
  89
+  end
  90
+end
174  generators/rspec_datamapper_scaffold/templates/controller_spec.rb
... ...
@@ -0,0 +1,174 @@
  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 "GET index" do
  10
+
  11
+    it "exposes all <%= table_name %> as @<%= table_name %>" do
  12
+      <%= class_name %>.should_receive(:all).and_return([mock_<%= file_name %>])
  13
+      get :index
  14
+      assigns[:<%= table_name %>].should == [mock_<%= file_name %>]
  15
+    end
  16
+
  17
+    describe "with mime type of xml" do
  18
+
  19
+      it "renders all <%= table_name.pluralize %> as xml" do
  20
+        <%= class_name %>.should_receive(:all).and_return(<%= file_name.pluralize %> = mock("Array of <%= class_name.pluralize %>"))
  21
+        <%= file_name.pluralize %>.should_receive(:to_xml).and_return("generated XML")
  22
+        get :index, :format => 'xml'
  23
+        response.body.should == "generated XML"
  24
+      end
  25
+
  26
+    end
  27
+
  28
+  end
  29
+
  30
+  describe "GET show" do
  31
+
  32
+    it "exposes the requested <%= file_name %> as @<%= file_name %>" do
  33
+      <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
  34
+      get :show, :id => "37"
  35
+      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
  36
+    end
  37
+
  38
+    describe "with mime type of xml" do
  39
+
  40
+      it "renders the requested <%= file_name %> as xml" do
  41
+        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
  42
+        mock_<%= file_name %>.should_receive(:to_xml).and_return("generated XML")
  43
+        get :show, :id => "37", :format => 'xml'
  44
+        response.body.should == "generated XML"
  45
+      end
  46
+
  47
+    end
  48
+
  49
+  end
  50
+
  51
+  describe "GET new" do
  52
+
  53
+    it "exposes a new <%= file_name %> as @<%= file_name %>" do
  54
+      <%= class_name %>.should_receive(:new).and_return(mock_<%= file_name %>)
  55
+      get :new
  56
+      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
  57
+    end
  58
+
  59
+  end
  60
+
  61
+  describe "GET edit" do
  62
+
  63
+    it "exposes the requested <%= file_name %> as @<%= file_name %>" do
  64
+      <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
  65
+      get :edit, :id => "37"
  66
+      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
  67
+    end
  68
+
  69
+  end
  70
+
  71
+  describe "POST create" do
  72
+
  73
+    describe "with valid params" do
  74
+
  75
+      it "exposes a newly created <%= file_name %> as @<%= file_name %>" do
  76
+        <%= class_name %>.should_receive(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => true))
  77
+        post :create, :<%= file_name %> => {:these => 'params'}
  78
+        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
  79
+      end
  80
+
  81
+      it "redirects to the created <%= file_name %>" do
  82
+        <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => true))
  83
+        post :create, :<%= file_name %> => {}
  84
+        response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
  85
+      end
  86
+
  87
+    end
  88
+
  89
+    describe "with invalid params" do
  90
+
  91
+      it "exposes a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
  92
+        <%= class_name %>.stub!(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => false))
  93
+        post :create, :<%= file_name %> => {:these => 'params'}
  94
+        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
  95
+      end
  96
+
  97
+      it "re-renders the 'new' template" do
  98
+        <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => false))
  99
+        post :create, :<%= file_name %> => {}
  100
+        response.should render_template('new')
  101
+      end
  102
+
  103
+    end
  104
+
  105
+  end
  106
+
  107
+  describe "PUT udpate" do
  108
+
  109
+    describe "with valid params" do
  110
+
  111
+      it "updates the requested <%= file_name %>" do
  112
+        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
  113
+        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
  114
+        mock_<%= file_name %>.should_receive(:dirty?)
  115
+        put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
  116
+      end
  117
+
  118
+      it "exposes the requested <%= file_name %> as @<%= file_name %>" do
  119
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
  120
+        put :update, :id => "1"
  121
+        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
  122
+      end
  123
+
  124
+      it "redirects to the <%= file_name %>" do
  125
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
  126
+        put :update, :id => "1"
  127
+        response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
  128
+      end
  129
+
  130
+    end
  131
+
  132
+    describe "with invalid params" do
  133
+
  134
+      it "updates the requested <%= file_name %>" do
  135
+        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
  136
+        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
  137
+        mock_<%= file_name %>.should_receive(:dirty?)
  138
+        put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
  139
+      end
  140
+
  141
+      it "exposes the <%= file_name %> as @<%= file_name %>" do
  142
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
  143
+        mock_<%= file_name %>.should_receive(:dirty?)
  144
+        put :update, :id => "1"
  145
+        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
  146
+      end
  147
+
  148
+      it "re-renders the 'edit' template" do
  149
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
  150
+        put :update, :id => "1"
  151
+        response.should render_template('edit')
  152
+      end
  153
+
  154
+    end
  155
+
  156
+  end
  157
+
  158
+  describe "DELETE destroy" do
  159
+
  160
+    it "destroys the requested <%= file_name %>" do
  161
+      <%= class_name %>.should_receive(:get).with("37").and_return(mock_<%= file_name %>)
  162
+      mock_<%= file_name %>.should_receive(:destroy)
  163
+      delete :destroy, :id => "37"
  164
+    end
  165
+
  166
+    it "redirects to the <%= table_name %> list" do
  167
+      <%= class_name %>.should_receive(:get).with("1").and_return(mock_<%= file_name %>(:destroy => true))
  168
+      delete :destroy, :id => "1"
  169
+      response.should redirect_to(<%= table_name %>_url)
  170
+    end
  171
+
  172
+  end
  173
+
  174
+end
7  generators/rspec_default_values.rb
... ...
@@ -1,3 +1,6 @@
  1
+require 'rails_generator/generated_attribute'
  2
+require 'active_record'
  3
+
1 4
 module Rails
2 5
   module Generator
3 6
     class GeneratedAttribute
@@ -6,14 +9,14 @@ def default_value
6 9
           when :int, :integer               then "1"
7 10
           when :float                       then "1.5"
8 11
           when :decimal, :big_decimal       then "9.99"
9  
-          when :date_time, :datetime, 
  12
+          when :date_time, :datetime,
10 13
                           :timestamp, :time then "Time.now"
11 14
           when :date                        then "Date.today"
12 15
           when :string, :text               then "\"value for #{@name}\""
13 16
           when :boolean                     then "false"
14 17
           else
15 18
             ""
16  
-        end      
  19
+        end
17 20
       end
18 21
     end
19 22
   end
71  generators/rspec_dm_controller/rspec_dm_controller_generator.rb
... ...
@@ -1,71 +0,0 @@
1  
-class RspecDmControllerGenerator < Rails::Generator::NamedBase
2  
-
3  
-  attr_reader   :controller_name,
4  
-                :controller_class_path,
5  
-                :controller_file_path,
6  
-                :controller_class_nesting,
7  
-                :controller_class_nesting_depth,
8  
-                :controller_class_name,
9  
-                :controller_singular_name,
10  
-                :controller_plural_name
11  
-  alias_method  :controller_file_name,  :controller_singular_name
12  
-  alias_method  :controller_table_name, :controller_plural_name
13  
-
14  
-  def initialize(runtime_args, runtime_options = {})
15  
-    super
16  
-
17  
-    @controller_name = @name.pluralize
18  
-
19  
-    base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
20  
-    @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
21  
-
22  
-    if @controller_class_nesting.empty?
23  
-      @controller_class_name = @controller_class_name_without_nesting
24  
-    else
25  
-      @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
26  
-    end
27  
-  end
28  
-
29  
-  def manifest
30  
-    record do |m|
31  
-      # Check for class naming collisions.
32  
-      m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
33  
-      m.class_collisions(class_name)
34  
-      
35  
-      # Controller, helper, views, and test directories.
36  
-      m.directory(File.join('app/controllers', controller_class_path))
37  
-      m.directory(File.join('app/helpers', controller_class_path))
38  
-      m.directory(File.join('spec/helpers', controller_class_path))
39  
-      
40  
-      m.template 'controller_spec.rb',
41  
-         File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
42  
-      
43  
-      m.template 'rspec_scaffold:routing_spec.rb',
44  
-        File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb")
45  
-      
46  
-      m.template 'rspec_scaffold:helper_spec.rb',
47  
-        File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
48  
-
49  
-      m.template('controller.rb', 
50  
-                 File.join('app/controllers', 
51  
-                           controller_class_path, 
52  
-                           "#{controller_file_name}_controller.rb"))
53  
-
54  
-      m.template('helper.rb',          
55  
-                 File.join('app/helpers',     
56  
-                           controller_class_path, "#{controller_file_name}_helper.rb"))
57  
-      
58  
-      m.route_resources controller_file_name
59  
-    end
60  
-  end
61  
-
62  
-  protected
63  
-  
64  
-  def banner
65  
-    "Usage: #{$0} rspec_dm_controller ModelName"
66  
-  end
67  
-  
68  
-  def model_name
69  
-    class_name.demodulize
70  
-  end
71  
-end
33  generators/rspec_dm_model/rspec_dm_model_generator.rb
... ...
@@ -1,33 +0,0 @@
1  
-require 'rails_generator/generators/components/model/model_generator'
2  
-require 'active_record'
3  
-require File.dirname(__FILE__) + '/../rspec_default_values'
4  
-
5  
-class RspecDmModelGenerator <ModelGenerator
6  
-
7  
-  def manifest
8  
-    record do |m|
9  
-
10  
-      # Check for class naming collisions.
11  
-      #m.class_collisions class_path, class_name
12  
-
13  
-      # Model, spec, and fixture directories.
14  
-      m.directory File.join('app/models', class_path)
15  
-      m.directory File.join('spec/models', class_path)
16  
-
17  
-      # Model class, spec and fixtures.
18  
-      m.template 'model.rb',      File.join('app/models', 
19  
-                                            class_path, 
20  
-                                            "#{file_name}.rb")
21  
-      m.template 'model_spec.rb', File.join('spec/models', 
22  
-                                            class_path, 
23  
-                                            "#{file_name}_spec.rb")
24  
-    end
25  
-
26  
-#     unless options[:skip_migration]
27  
-#       m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
28  
-#         :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
29  
-#       }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
30  
-#     end
31  
-  end
32  
-
33  
-end

0 notes on commit cf71c32

Please sign in to comment.
Something went wrong with that request. Please try again.