Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* cleaned up the generators

* simplified the overlay api

* moved overlay and default values to lib so other generators can use them too
  • Loading branch information...
commit 27e855f0a6b4e5d223c66949c3b78e4ad4e10730 1 parent a49a4c3
Christian Meier authored October 01, 2009

Showing 30 changed files with 294 additions and 419 deletions. Show diff stats Hide diff stats

  1. 32  Manifest.txt
  2. 4  Rakefile
  3. 12  generators/datamapper_controller/datamapper_controller_generator.rb
  4. 12  generators/datamapper_install/datamapper_install_generator.rb
  5. 42  generators/datamapper_install/templates/datamapper.rake
  6. 12  generators/datamapper_model/datamapper_model_generator.rb
  7. 3  generators/datamapper_model/templates/model.rb
  8. 8  generators/datamapper_model/templates/unit_test.rb
  9. 18  generators/datamapper_rspec_model/datamapper_rspec_model_generator.rb
  10. 2  generators/{rspec_datamapper_model → datamapper_rspec_model}/templates/model_spec.rb
  11. 20  generators/datamapper_rspec_scaffold/datamapper_rspec_scaffold_generator.rb
  12. 5  generators/{rspec_datamapper_scaffold → datamapper_rspec_scaffold}/templates/controller_spec.rb
  13. 31  generators/datamapper_scaffold/datamapper_scaffold_generator.rb
  14. 2  generators/{rspec_datamapper_scaffold → datamapper_scaffold}/templates/controller.rb
  15. 49  generators/datamapper_scaffold/templates/functional_test.rb
  16. 18  generators/datamapper_scaffold/templates/view_edit.html.erb
  17. 24  generators/datamapper_scaffold/templates/view_index.html.erb
  18. 17  generators/datamapper_scaffold/templates/view_new.html.erb
  19. 10  generators/datamapper_scaffold/templates/view_show.html.erb
  20. 8  generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
  21. 111  generators/rspec_datamapper_controller/templates/controller.rb
  22. 189  generators/rspec_datamapper_controller/templates/controller_spec.rb
  23. 2  generators/rspec_datamapper_controller/templates/helper.rb
  24. 11  generators/rspec_datamapper_controller/templates/helper_spec.rb
  25. 13  generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
  26. 18  generators/rspec_datamapper_model/templates/model.rb
  27. 16  generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
  28. 2  lib/datamapper4rails.rb
  29. 22  {generators → lib/datamapper4rails}/overlay.rb
  30. 0  {generators → lib/datamapper4rails}/rspec_default_values.rb
32  Manifest.txt
@@ -3,31 +3,33 @@ Manifest.txt
3 3
 README.txt
4 4
 Rakefile
5 5
 generate.sh
6  
-generators/datamapper_controller/datamapper_controller_generator.rb
  6
+generators/datamapper_install/datamapper_install_generator.rb
  7
+generators/datamapper_install/templates/datamapper.rake
7 8
 generators/datamapper_model/datamapper_model_generator.rb
8 9
 generators/datamapper_model/templates/model.rb
9  
-generators/datamapper_model/templates/unit_test.rb
10  
-generators/overlay.rb
11  
-generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
12  
-generators/rspec_datamapper_controller/templates/controller.rb
13  
-generators/rspec_datamapper_controller/templates/controller_spec.rb
14  
-generators/rspec_datamapper_controller/templates/helper.rb
15  
-generators/rspec_datamapper_controller/templates/helper_spec.rb
16  
-generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
17  
-generators/rspec_datamapper_model/templates/model.rb
18  
-generators/rspec_datamapper_model/templates/model_spec.rb
19  
-generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
20  
-generators/rspec_datamapper_scaffold/templates/controller.rb
21  
-generators/rspec_datamapper_scaffold/templates/controller_spec.rb
22  
-generators/rspec_default_values.rb
  10
+generators/datamapper_rspec_model/datamapper_rspec_model_generator.rb
  11
+generators/datamapper_rspec_model/templates/model_spec.rb
  12
+generators/datamapper_rspec_scaffold/datamapper_rspec_scaffold_generator.rb
  13
+generators/datamapper_rspec_scaffold/templates/controller_spec.rb
  14
+generators/datamapper_scaffold/datamapper_scaffold_generator.rb
  15
+generators/datamapper_scaffold/templates/controller.rb
  16
+generators/datamapper_scaffold/templates/functional_test.rb
  17
+generators/datamapper_scaffold/templates/view_edit.html.erb
  18
+generators/datamapper_scaffold/templates/view_index.html.erb
  19
+generators/datamapper_scaffold/templates/view_new.html.erb
  20
+generators/datamapper_scaffold/templates/view_show.html.erb
23 21
 lib/datamapper4rails.rb
24 22
 lib/datamapper4rails/adapters/base_adapter.rb
25 23
 lib/datamapper4rails/adapters/restful_adapter.rb
  24
+lib/datamapper4rails/database_config.rb-
26 25
 lib/datamapper4rails/datamapper_store.rb
27 26
 lib/datamapper4rails/identity_maps.rb
  27
+lib/datamapper4rails/overlay.rb
28 28
 lib/datamapper4rails/preload_models.rb
29 29
 lib/datamapper4rails/restful_transactions.rb
  30
+lib/datamapper4rails/rspec_default_values.rb
30 31
 lib/datamapper4rails/version.rb
  32
+lib/datamapper_rails_templates.rb
31 33
 spec/datamapper_store_spec.rb
32 34
 spec/restful_adapter_spec.rb
33 35
 spec/spec.opts
4  Rakefile
@@ -9,10 +9,10 @@ require 'pathname'
9 9
 
10 10
 require './lib/datamapper4rails/version.rb'
11 11
 
12  
-Hoe.new('datamapper4rail', Datamapper4rails::VERSION) do |p|
  12
+Hoe.new('datamapper4rails', Datamapper4rails::VERSION) do |p|
13 13
   p.rubyforge_name = 'datamapper4rail'
14 14
   p.developer('mkristian', 'm.kristian@web.de')
15  
-  p.extra_deps = ['slf4r', 'rack_datamapper', 'rails_datamapper']
  15
+  p.extra_deps = ['rack_datamapper', 'rails_datamapper']
16 16
   p.remote_rdoc_dir = '' # Release to root
17 17
 end
18 18
 
12  generators/datamapper_controller/datamapper_controller_generator.rb
... ...
@@ -1,12 +0,0 @@
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
12  generators/datamapper_install/datamapper_install_generator.rb
... ...
@@ -0,0 +1,12 @@
  1
+require 'rails_generator/base'
  2
+
  3
+class DatamapperInstallGenerator < 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/datamapper_install/templates/datamapper.rake
... ...
@@ -0,0 +1,42 @@
  1
+namespace :dm 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
+      require 'dm-migrations/migration_runner'
  22
+      FileList['db/migrate/*.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 => 'dm:migrate:up'
  42
+end
12  generators/datamapper_model/datamapper_model_generator.rb
... ...
@@ -1,14 +1,18 @@
1 1
 require 'rails_generator/generators/components/model/model_generator'
2 2
 require 'active_record'
3  
-require File.dirname(__FILE__) + '/../overlay'
  3
+require 'datamapper4rails/overlay'
4 4
 
5 5
 
6  
-class DatamapperModelGenerator <ModelGenerator
  6
+class DatamapperModelGenerator < ModelGenerator
7 7
 
8 8
   def manifest
9  
-    overlay_dirs << File.join(self.class.lookup("model").path, 'templates')
10  
-    overlay_dirs << source_root
  9
+    overlay_dirs.add_generator("model")
11 10
     super
12 11
   end
13 12
 
  13
+  def add_options!(opt)
  14
+    super
  15
+    opt.on("--skip-timestamps",
  16
+           "Don't add timestamps for this model") { |v| options[:skip_timestamps] = v }
  17
+  end
14 18
 end
3  generators/datamapper_model/templates/model.rb
@@ -2,8 +2,9 @@ class <%= class_name %>
2 2
   include DataMapper::Resource
3 3
 
4 4
   property :id, Serial
  5
+
5 6
 <% 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
+  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
 
8 9
 <% end -%>
9 10
 <% unless options[:skip_timestamps] -%>
8  generators/datamapper_model/templates/unit_test.rb
... ...
@@ -1,8 +0,0 @@
1  
-require 'test_helper'
2  
-
3  
-class <%= class_name %>Test < ActiveSupport::TestCase
4  
-  # Replace this with your real tests.
5  
-  def test_truth
6  
-    assert true
7  
-  end
8  
-end
18  generators/datamapper_rspec_model/datamapper_rspec_model_generator.rb
... ...
@@ -0,0 +1,18 @@
  1
+require 'rails_generator/generators/components/model/model_generator'
  2
+require 'active_record'
  3
+require 'datamapper4rails/overlay'
  4
+
  5
+class DatamapperRspecModelGenerator < RspecModelGenerator
  6
+
  7
+  def manifest
  8
+    overlay_dirs.add_generator("datamapper_model")
  9
+    overlay_dirs.add_generator("rspec_model")
  10
+    super
  11
+  end
  12
+
  13
+  def add_options!(opt)
  14
+    super
  15
+    opt.on("--skip-timestamps",
  16
+           "Don't add timestamps for this model") { |v| options[:skip_timestamps] = v }
  17
+  end
  18
+end
2  ...rs/rspec_datamapper_model/templates/model_spec.rb → ...rs/datamapper_rspec_model/templates/model_spec.rb
@@ -18,7 +18,7 @@
18 18
     <%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
19 19
   end
20 20
 
21  
-<% if attribute.type == :string or attribute.type == :text  or attribute.type == :slug -%>
  21
+<% if [:string, :text, :slug].member? attribute.type -%>
22 22
   it 'should not match <%= attribute.name %>' do
23 23
     <%= singular_name %> = <%= class_name %>.create(@valid_attributes.merge(:<%= attribute.name %> => "<script" ))
24 24
     <%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
20  generators/datamapper_rspec_scaffold/datamapper_rspec_scaffold_generator.rb
... ...
@@ -0,0 +1,20 @@
  1
+require 'datamapper4rails/overlay'
  2
+require 'datamapper4rails/rspec_default_values'
  3
+
  4
+class DatamapperRspecScaffoldGenerator < RspecScaffoldGenerator
  5
+
  6
+  def manifest
  7
+    overlay_dirs.add_generator("datamapper_rspec_model")
  8
+    overlay_dirs.add_generator("datamapper_model")
  9
+    overlay_dirs.add_generator("datamapper_scaffold")
  10
+    overlay_dirs.add_generator("rspec_scaffold")
  11
+    overlay_dirs.add_generator("scaffold")
  12
+    super
  13
+  end
  14
+
  15
+  def add_options!(opt)
  16
+    super
  17
+    opt.on("--skip-timestamps",
  18
+           "Don't add timestamps for this model") { |v| options[:skip_timestamps] = v }
  19
+  end
  20
+end
5  ..._datamapper_scaffold/templates/controller_spec.rb → ...apper_rspec_scaffold/templates/controller_spec.rb
@@ -111,7 +111,6 @@ def mock_<%= file_name %>(stubs={})
111 111
       it "updates the requested <%= file_name %>" do
112 112
         <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
113 113
         mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
114  
-        mock_<%= file_name %>.should_receive(:dirty?)
115 114
         put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
116 115
       end
117 116
 
@@ -134,19 +133,17 @@ def mock_<%= file_name %>(stubs={})
134 133
       it "updates the requested <%= file_name %>" do
135 134
         <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
136 135
         mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
137  
-        mock_<%= file_name %>.should_receive(:dirty?)
138 136
         put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
139 137
       end
140 138
 
141 139
       it "exposes the <%= file_name %> as @<%= file_name %>" do
142 140
         <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
143  
-        mock_<%= file_name %>.should_receive(:dirty?)
144 141
         put :update, :id => "1"
145 142
         assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
146 143
       end
147 144
 
148 145
       it "re-renders the 'edit' template" do
149  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
  146
+        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
150 147
         put :update, :id => "1"
151 148
         response.should render_template('edit')
152 149
       end
31  generators/datamapper_scaffold/datamapper_scaffold_generator.rb
... ...
@@ -0,0 +1,31 @@
  1
+require 'datamapper4rails/overlay'
  2
+require 'datamapper4rails/rspec_default_values'
  3
+
  4
+class DatamapperScaffoldGenerator < ScaffoldGenerator
  5
+
  6
+  def manifest
  7
+    overlay_dirs.add_generator("datamapper_model")
  8
+    overlay_dirs.add_generator("scaffold")
  9
+
  10
+    logger.warn
  11
+    logger.warn
  12
+    logger.warn
  13
+    logger.warn
  14
+    logger.warn "           WARNING"
  15
+    logger.warn
  16
+    logger.warn "fixtures with datamapper do not work"
  17
+    logger.warn "and so functional tests do not work"
  18
+    logger.warn
  19
+    logger.warn
  20
+    logger.warn
  21
+    logger.warn
  22
+    
  23
+    super
  24
+  end
  25
+
  26
+  def add_options!(opt)
  27
+    super
  28
+    opt.on("--skip-timestamps",
  29
+           "Don't add timestamps for this model") { |v| options[:skip_timestamps] = v }
  30
+  end
  31
+end
2  ...rspec_datamapper_scaffold/templates/controller.rb → ...ators/datamapper_scaffold/templates/controller.rb
@@ -62,7 +62,7 @@ def update
62 62
     @<%= file_name %> = <%= class_name %>.get!(params[:id])
63 63
 
64 64
     respond_to do |format|
65  
-      if @<%= file_name %>.update(params[:<%= file_name %>]) or not @<%= file_name %>.dirty?
  65
+      if @<%= file_name %>.update(params[:<%= file_name %>])
66 66
         flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_updated')<% else -%>'<%= class_name %> was successfully updated.'<% end -%>
67 67
 
68 68
         format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
49  generators/datamapper_scaffold/templates/functional_test.rb
... ...
@@ -0,0 +1,49 @@
  1
+require 'test_helper'
  2
+
  3
+class <%= controller_class_name %>ControllerTest < ActionController::TestCase
  4
+  test "should get index" do
  5
+    get :index
  6
+    assert_response :success
  7
+    assert_not_nil assigns(:<%= table_name %>)
  8
+  end
  9
+
  10
+  test "should get new" do
  11
+    get :new
  12
+    assert_response :success
  13
+  end
  14
+
  15
+  test "should create <%= file_name %>" do
  16
+    assert_difference('<%= class_name %>.all.size') do
  17
+      post :create, :<%= file_name %> => {
  18
+<% attributes.each_with_index do |attribute, attribute_index| -%>
  19
+      :<%= attribute.name %> => <%= attribute.default_value %><%= attribute_index == attributes.length - 1 ? '' : ','%>
  20
+<% end -%>
  21
+ }
  22
+    end
  23
+
  24
+    assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>).key)
  25
+  end
  26
+
  27
+  test "should show <%= file_name %>" do
  28
+    get :show, :id => <%= table_name %>(:one).to_param
  29
+    assert_response :success
  30
+  end
  31
+
  32
+  test "should get edit" do
  33
+    get :edit, :id => <%= table_name %>(:one).to_param
  34
+    assert_response :success
  35
+  end
  36
+
  37
+  test "should update <%= file_name %>" do
  38
+    put :update, :id => <%= table_name %>(:one).to_param, :<%= file_name %> => { }
  39
+    assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>).key)
  40
+  end
  41
+
  42
+  test "should destroy <%= file_name %>" do
  43
+    assert_difference('<%= class_name %>.all.size', -1) do
  44
+      delete :destroy, :id => <%= table_name %>(:one).to_param
  45
+    end
  46
+
  47
+    assert_redirected_to <%= table_name %>_path
  48
+  end
  49
+end
18  generators/datamapper_scaffold/templates/view_edit.html.erb
... ...
@@ -0,0 +1,18 @@
  1
+<h1>Editing <%= singular_name %></h1>
  2
+
  3
+<%% form_for(:<%= singular_name %>, :url => <%= singular_name %>_path(@<%= singular_name %>.key), :html => {:method => :put}) do |f| %>
  4
+  <%%= f.error_messages %>
  5
+
  6
+<% for attribute in attributes -%>
  7
+  <p>
  8
+    <%%= f.label :<%= attribute.name %> %><br />
  9
+    <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
  10
+  </p>
  11
+<% end -%>
  12
+  <p>
  13
+    <%%= f.submit 'Update' %>
  14
+  </p>
  15
+<%% end %>
  16
+
  17
+<%%= link_to 'Show', <%= singular_name %>_path(@<%= singular_name %>.key) %> |
  18
+<%%= link_to 'Back', <%= plural_name %>_path %>
24  generators/datamapper_scaffold/templates/view_index.html.erb
... ...
@@ -0,0 +1,24 @@
  1
+<h1>Listing <%= plural_name %></h1>
  2
+
  3
+<table>
  4
+  <tr>
  5
+<% for attribute in attributes -%>
  6
+    <th><%= attribute.column.human_name %></th>
  7
+<% end -%>
  8
+  </tr>
  9
+
  10
+<%% @<%= plural_name %>.each do |<%= singular_name %>| %>
  11
+  <tr>
  12
+<% for attribute in attributes -%>
  13
+    <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
  14
+<% end -%>
  15
+    <td><%%= link_to 'Show', <%= singular_name %>_path(<%= singular_name %>.key) %></td>
  16
+    <td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>.key) %></td>
  17
+    <td><%%= link_to 'Destroy', <%= singular_name %>_path(<%= singular_name %>.key), :confirm => 'Are you sure?', :method => :delete %></td>
  18
+  </tr>
  19
+<%% end %>
  20
+</table>
  21
+
  22
+<br />
  23
+
  24
+<%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>
17  generators/datamapper_scaffold/templates/view_new.html.erb
... ...
@@ -0,0 +1,17 @@
  1
+<h1>New <%= singular_name %></h1>
  2
+
  3
+<%% form_for(@<%= singular_name %>) do |f| %>
  4
+  <%%= f.error_messages %>
  5
+
  6
+<% for attribute in attributes -%>
  7
+  <p>
  8
+    <%%= f.label :<%= attribute.name %> %><br />
  9
+    <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
  10
+  </p>
  11
+<% end -%>
  12
+  <p>
  13
+    <%%= f.submit 'Create' %>
  14
+  </p>
  15
+<%% end %>
  16
+
  17
+<%%= link_to 'Back', <%= plural_name %>_path %>
10  generators/datamapper_scaffold/templates/view_show.html.erb
... ...
@@ -0,0 +1,10 @@
  1
+<% for attribute in attributes -%>
  2
+<p>
  3
+  <b><%= attribute.column.human_name %>:</b>
  4
+  <%%=h @<%= singular_name %>.<%= attribute.name %> %>
  5
+</p>
  6
+
  7
+<% end -%>
  8
+
  9
+<%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>.key) %> |
  10
+<%%= link_to 'Back', <%= plural_name %>_path %>
8  generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
... ...
@@ -1,8 +0,0 @@
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
111  generators/rspec_datamapper_controller/templates/controller.rb
... ...
@@ -1,111 +0,0 @@
1  
-class <%= controller_class_name %>Controller < ApplicationController
2  
-  # GET /<%= table_name %>
3  
-  # GET /<%= table_name %>.xml
4  
-  def index
5  
-<% if options[:ixtlan] -%>
6  
-    @<%= table_name %> = <%= class_name %>.all(@find_all_args)
7  
-<% else -%>
8  
-    @<%= table_name %> = <%= class_name %>.all()
9  
-<% end -%>
10  
-
11  
-    respond_to do |format|
12  
-      format.html # index.html.erb
13  
-      format.xml  { render :xml => @<%= table_name %> }
14  
-    end
15  
-  end
16  
-
17  
-  # GET /<%= table_name %>/1
18  
-  # GET /<%= table_name %>/1.xml
19  
-  def show
20  
-    @<%= file_name %> = <%= class_name %>.get!(params[:id])
21  
-
22  
-    respond_to do |format|
23  
-      format.html # show.html.erb
24  
-      format.xml  { render :xml => @<%= file_name %> }
25  
-    end
26  
-  end
27  
-
28  
-  # GET /<%= table_name %>/new
29  
-  # GET /<%= table_name %>/new.xml
30  
-  def new
31  
-    @<%= file_name %> = <%= class_name %>.new
32  
-
33  
-    respond_to do |format|
34  
-      format.html # new.html.erb
35  
-      format.xml  { render :xml => @<%= file_name %> }
36  
-    end
37  
-  end
38  
-
39  
-  # GET /<%= table_name %>/1/edit
40  
-  def edit
41  
-    @<%= file_name %> = <%= class_name %>.get!(params[:id])
42  
-  end
43  
-
44  
-  # POST /<%= table_name %>
45  
-  # POST /<%= table_name %>.xml
46  
-  def create
47  
-    @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
48  
-
49  
-    respond_to do |format|
50  
-      if @<%= file_name %>.save
51  
-        flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_created')<% else -%>'<%= class_name %> was successfully created.'<% end -%>
52  
-
53  
-        format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
54  
-        format.xml  { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
55  
-      else
56  
-        format.html { render :action => "new" }
57  
-        format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
58  
-      end
59  
-    end
60  
-  end
61  
-
62  
-  # PUT /<%= table_name %>/1
63  
-  # PUT /<%= table_name %>/1.xml
64  
-  def update
65  
-    @<%= file_name %> = <%= class_name %>.get!(params[:id])
66  
-
67  
-    respond_to do |format|
68  
-      if @<%= file_name %>.update_attributes(params[:<%= file_name %>]) or not @<%= file_name %>.dirty?
69  
-        flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_updated')<% else -%>'<%= class_name %> was successfully updated.'<% end -%>
70  
-
71  
-        format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
72  
-        format.xml  { head :ok }
73  
-      else
74  
-        format.html { render :action => "edit" }
75  
-        format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
76  
-      end
77  
-    end
78  
-  end
79  
-
80  
-  # DELETE /<%= table_name %>/1
81  
-  # DELETE /<%= table_name %>/1.xml
82  
-  def destroy
83  
-    @<%= file_name %> = <%= class_name %>.get(params[:id])
84  
-    @<%= file_name %>.destroy if @<%= file_name %>
85  
-
86  
-    respond_to do |format|
87  
-      flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_deleted')<% else -%>'<%= class_name %> was successfully deleted.'<% end -%>
88  
-
89  
-      format.html { redirect_to(<%= table_name %>_url) }
90  
-      format.xml  { head :ok }
91  
-    end
92  
-  end
93  
-
94  
-<% if options[:ixtlan] -%>
95  
-  private
96  
-
97  
-  def audit
98  
-    if @<%= file_name %>
99  
-      @<%= file_name %>.to_s
100  
-    elsif @<%= table_name %>
101  
-<% if options[:ixtlan] -%>
102  
-      "<%= controller_class_name %>[#{@<%= table_name %>.size};#{@field}:#{@direction}]"
103  
-<% else -%>
104  
-      "<%= controller_class_name %>[#{@<%= table_name %>.size}]"
105  
-<% end -%>
106  
-    else
107  
-      ""
108  
-    end
109  
-  end
110  
-<% end -%>
111  
-end
189  generators/rspec_datamapper_controller/templates/controller_spec.rb
... ...
@@ -1,189 +0,0 @@
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  
-<% if options[:add_guard] -%>
10  
-  before(:each) do
11  
-    user = User.new(:id => 1)
12  
-    def user.groups
13  
-      [Group.new(:name => "root")]
14  
-    end
15  
-    controller.send(:current_user=, user)
16  
-<% if options[:ixtlan] -%>
17  
-    mock_configuration = mock_model(Configuration,{})
18  
-    Configuration.should_receive(:instance).any_number_of_times.and_return(mock_configuration)
19  
-    mock_configuration.should_receive(:session_idle_timeout).any_number_of_times.and_return(1)
20  
-<% end -%>
21  
-  end
22  
-<% end -%>
23  
-
24  
-  describe "GET index" do
25  
-
26  
-    it "exposes all <%= table_name.pluralize %> as @<%= table_name.pluralize %>" do
27  
-      <%= class_name %>.should_receive(:all).and_return([mock_<%= file_name %>])
28  
-      get :index
29  
-      assigns[:<%= table_name %>].should == [mock_<%= file_name %>]
30  
-    end
31  
-
32  
-    describe "with mime type of xml" do
33  
-  
34  
-      it "renders all <%= table_name.pluralize %> as xml" do
35  
-        <%= class_name %>.should_receive(:all).and_return(<%= file_name.pluralize %> = mock("Array of <%= class_name.pluralize %>"))
36  
-        <%= file_name.pluralize %>.should_receive(:to_xml).and_return("generated XML")
37  
-        get :index, :format => 'xml'
38  
-        response.body.should == "generated XML"
39  
-      end
40  
-    
41  
-    end
42  
-
43  
-  end
44  
-
45  
-  describe "GET show" do
46  
-
47  
-    it "exposes the requested <%= file_name %> as @<%= file_name %>" do
48  
-      <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
49  
-      get :show, :id => "37"
50  
-      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
51  
-    end
52  
-    
53  
-    describe "with mime type of xml" do
54  
-
55  
-      it "renders the requested <%= file_name %> as xml" do
56  
-        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
57  
-        mock_<%= file_name %>.should_receive(:to_xml).and_return("generated XML")
58  
-        get :show, :id => "37", :format => 'xml'
59  
-        response.body.should == "generated XML"
60  
-      end
61  
-
62  
-    end
63  
-    
64  
-  end
65  
-
66  
-  describe "GET new" do
67  
-  
68  
-    it "exposes a new <%= file_name %> as @<%= file_name %>" do
69  
-      <%= class_name %>.should_receive(:new).and_return(mock_<%= file_name %>)
70  
-      get :new
71  
-      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
72  
-    end
73  
-
74  
-  end
75  
-
76  
-  describe "GET edit" do
77  
-  
78  
-    it "exposes the requested <%= file_name %> as @<%= file_name %>" do
79  
-      <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
80  
-      get :edit, :id => "37"
81  
-      assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
82  
-    end
83  
-
84  
-  end
85  
-
86  
-  describe "POST create" do
87  
-
88  
-    describe "with valid params" do
89  
-      
90  
-      it "exposes a newly created <%= file_name %> as @<%= file_name %>" do
91  
-        <%= class_name %>.should_receive(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => true))
92  
-        post :create, :<%= file_name %> => {:these => 'params'}
93  
-        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
94  
-      end
95  
-
96  
-      it "redirects to the created <%= file_name %>" do
97  
-        <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => true))
98  
-        post :create, :<%= file_name %> => {}
99  
-        response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
100  
-      end
101  
-      
102  
-    end
103  
-    
104  
-    describe "with invalid params" do
105  
-
106  
-      it "exposes a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
107  
-        <%= class_name %>.stub!(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => false))
108  
-        post :create, :<%= file_name %> => {:these => 'params'}
109  
-        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
110  
-      end
111  
-
112  
-      it "re-renders the 'new' template" do
113  
-        <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => false))
114  
-        post :create, :<%= file_name %> => {}
115  
-        response.should render_template('new')
116  
-      end
117  
-      
118  
-    end
119  
-    
120  
-  end
121  
-
122  
-  describe "PUT udpate" do
123  
-
124  
-    describe "with valid params" do
125  
-
126  
-      it "updates the requested <%= file_name %>" do
127  
-        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
128  
-        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
129  
-        mock_<%= file_name %>.should_receive(:dirty?)
130  
-        put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
131  
-      end
132  
-
133  
-      it "exposes the requested <%= file_name %> as @<%= file_name %>" do
134  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
135  
-        put :update, :id => "1"
136  
-        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
137  
-      end
138  
-
139  
-      it "redirects to the <%= file_name %>" do
140  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
141  
-        put :update, :id => "1"
142  
-        response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
143  
-      end
144  
-
145  
-    end
146  
-    
147  
-    describe "with invalid params" do
148  
-
149  
-      it "updates the requested <%= file_name %>" do
150  
-        <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
151  
-        mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
152  
-        mock_<%= file_name %>.should_receive(:dirty?)
153  
-        put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
154  
-      end
155  
-
156  
-      it "exposes the <%= file_name %> as @<%= file_name %>" do
157  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
158  
-        mock_<%= file_name %>.should_receive(:dirty?)
159  
-        put :update, :id => "1"
160  
-        assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
161  
-      end
162  
-
163  
-      it "re-renders the 'edit' template" do
164  
-        <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
165  
-        put :update, :id => "1"
166  
-        response.should render_template('edit')
167  
-      end
168  
-
169  
-    end
170  
-
171  
-  end
172  
-
173  
-  describe "DELETE destroy" do
174  
-
175  
-    it "destroys the requested <%= file_name %>" do
176  
-      <%= class_name %>.should_receive(:get).with("37").and_return(mock_<%= file_name %>)
177  
-      mock_<%= file_name %>.should_receive(:destroy)
178  
-      delete :destroy, :id => "37"
179  
-    end
180  
-  
181  
-    it "redirects to the <%= table_name %> list" do
182  
-      <%= class_name %>.should_receive(:get).with("1").and_return(mock_<%= file_name %>(:destroy => true))
183  
-      delete :destroy, :id => "1"
184  
-      response.should redirect_to(<%= table_name %>_url)
185  
-    end
186  
-
187  
-  end
188  
-
189  
-end
2  generators/rspec_datamapper_controller/templates/helper.rb
... ...
@@ -1,2 +0,0 @@
1  
-module <%= controller_class_name %>Helper
2  
-end
11  generators/rspec_datamapper_controller/templates/helper_spec.rb
... ...
@@ -1,11 +0,0 @@
1  
-require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
2  
-require 'active_record'
3  
-
4  
-describe <%= controller_class_name %>Helper do
5  
-  before(:each) do
6  
-   include <%= controller_class_name %>Helper
7  
-  end
8  
-
9  
-  it "should be valid" do
10  
-  end
11  
-end
13  generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
... ...
@@ -1,13 +0,0 @@
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
18  generators/rspec_datamapper_model/templates/model.rb
... ...
@@ -1,18 +0,0 @@
1  
-class <%= class_name %>
2  
-  include DataMapper::Resource
3  
-<% if options[:add_constraints] -%>
4  
-  include DataMapper::Constraints
5  
-<% end -%>
6  
-
7  
-  property :id, Serial
8  
-
9  
-<% for attribute in attributes -%>
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  
-
12  
-<% end -%>
13  
-<% unless options[:skip_timestamps] %>
14  
-  timestamps :at
15  
-
16  
-<% end -%>
17  
-
18  
-end
16  generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
... ...
@@ -1,16 +0,0 @@
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
2  lib/datamapper4rails.rb
... ...
@@ -1,5 +1,5 @@
1  
-#require 'datamapper4rails/database_config'
2 1
 require 'rails_datamapper'
  2
+require 'datamapper4rails/datamapper_store'
3 3
 
4 4
 # keep this here until rails_datamapper has it included
5 5
 module DataMapper
22  generators/overlay.rb → lib/datamapper4rails/overlay.rb
@@ -7,11 +7,25 @@
7 7
 
8 8
 Rails::Generator::NamedBase.class_eval do
9 9
 
  10
+  def overlay_array
  11
+    a = []
  12
+    def a.lookup(clazz)
  13
+      @clazz = clazz
  14
+    end
  15
+    a.lookup(self.class)
  16
+    def a.add_generator(generator_name, prepend = false)
  17
+      path = File.join(@clazz.lookup(generator_name).path, 'templates')
  18
+      push(path) unless member? path
  19
+    end
  20
+    a
  21
+  end
  22
+
10 23
   def overlay_dirs
11  
-    options[:overlay_dirs] ||= []
  24
+    options[:overlay_dirs] ||= overlay_array
12 25
   end
13 26
 
14 27
   def add_options!(opt)
  28
+    # TODO this seems not to work anymore !!
15 29
     super
16 30
     opt.on("--overlay-dir DIR",
17 31
            "overlay") do |v|
@@ -25,9 +39,13 @@ def source_path(relative_source)
25 39
 
26 40
     # first check if the template can be found with in any of the overlay directories
27 41
     if dirs = options[:overlay_dirs]
  42
+      generator_name = self.class.name.underscore.sub(/_generator/, '')
  43
+      dirs.insert(0, File.join(self.class.lookup(generator_name).path, 'templates'))
  44
+      
28 45
       file = path.nil? ? name : path
29  
-      dirs.reverse.each do |dir|
  46
+      dirs.each do |dir|
30 47
         if (f = File.join(dir, file)) and File.exists?(f)
  48
+          logger.overlay f
31 49
           return f
32 50
         end
33 51
       end
0  generators/rspec_default_values.rb → lib/datamapper4rails/rspec_default_values.rb
File renamed without changes

0 notes on commit 27e855f

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