Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…nflict with rails_datamapper gem
  • Loading branch information...
commit cf71c320aa601a540257152bd4fa8ae9ee1b71d1 1 parent 7b7ff7b
@mkristian authored
Showing with 409 additions and 240 deletions.
  1. +6 −0 generate.sh
  2. +12 −0 generators/datamapper_controller/datamapper_controller_generator.rb
  3. +14 −0 generators/datamapper_model/datamapper_model_generator.rb
  4. +13 −0 generators/datamapper_model/templates/model.rb
  5. 0  generators/{dm_model → datamapper_model}/templates/unit_test.rb
  6. +0 −12 generators/dm_install/dm_install_generator.rb
  7. +0 −42 generators/dm_install/templates/datamapper.rake
  8. +0 −10 generators/dm_migration/dm_migration_generator.rb
  9. +0 −16 generators/dm_migration/templates/migration.rb
  10. +0 −27 generators/dm_model/dm_model_generator.rb
  11. +0 −14 generators/dm_model/templates/model.rb
  12. +47 −0 generators/overlay.rb
  13. +8 −0 generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
  14. 0  generators/{rspec_dm_controller → rspec_datamapper_controller}/templates/controller.rb
  15. +6 −6 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 −0 generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
  19. +1 −6 generators/{rspec_dm_model → rspec_datamapper_model}/templates/model.rb
  20. +4 −1 generators/{rspec_dm_model → rspec_datamapper_model}/templates/model_spec.rb
  21. +16 −0 generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
  22. +90 −0 generators/rspec_datamapper_scaffold/templates/controller.rb
  23. +174 −0 generators/rspec_datamapper_scaffold/templates/controller_spec.rb
  24. +5 −2 generators/rspec_default_values.rb
  25. +0 −71 generators/rspec_dm_controller/rspec_dm_controller_generator.rb
  26. +0 −33 generators/rspec_dm_model/rspec_dm_model_generator.rb
View
6 generate.sh
@@ -0,0 +1,6 @@
+script/generate datamapper_model datamapper_model name:string
+script/generate rspec_datamapper_model rspec_datamapper_model name:string
+script/generate datamapper_controller datamapper_controller index
+script/generate rspec_datamapper_controller rspec_datamapper_controller index
+#script/generate datamapper_scaffold datamapper_scaffold name:string
+script/generate rspec_datamapper_scaffold rspec_datamapper_scaffold name:string
View
12 generators/datamapper_controller/datamapper_controller_generator.rb
@@ -0,0 +1,12 @@
+require File.dirname(__FILE__) + '/../overlay'
+
+# this controller is just for completeness but does not offer
+# any further functionality to the underlying controller
+class DatamapperControllerGenerator < ControllerGenerator
+
+ def manifest
+ overlay_dirs << File.join(self.class.lookup("controller").path, 'templates')
+ super
+ end
+
+end
View
14 generators/datamapper_model/datamapper_model_generator.rb
@@ -0,0 +1,14 @@
+require 'rails_generator/generators/components/model/model_generator'
+require 'active_record'
+require File.dirname(__FILE__) + '/../overlay'
+
+
+class DatamapperModelGenerator <ModelGenerator
+
+ def manifest
+ overlay_dirs << File.join(self.class.lookup("model").path, 'templates')
+ overlay_dirs << source_root
+ super
+ end
+
+end
View
13 generators/datamapper_model/templates/model.rb
@@ -0,0 +1,13 @@
+class <%= class_name %>
+ include DataMapper::Resource
+
+ property :id, Serial
+<% Array(attributes).each do |attribute| -%>
+ 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 -%>
+
+<% end -%>
+<% unless options[:skip_timestamps] -%>
+ timestamps :at
+<% end -%>
+
+end
View
0  generators/dm_model/templates/unit_test.rb → ...s/datamapper_model/templates/unit_test.rb
File renamed without changes
View
12 generators/dm_install/dm_install_generator.rb
@@ -1,12 +0,0 @@
-require 'rails_generator/base'
-
-class DmInstallGenerator < Rails::Generator::Base
-
- def manifest
- record do |m|
- m.directory "lib/tasks"
- m.template "datamapper.rake", "lib/tasks/datamapper.rake"
- end
- end
-
-end
View
42 generators/dm_install/templates/datamapper.rake
@@ -1,42 +0,0 @@
-namespace :db do
-
- desc "Perform automigration"
- task :automigrate => :environment do
- FileList["app/models/**/*.rb"].each do |model|
- load model
- end
- ::DataMapper.auto_migrate!
- end
-
- desc "Perform non destructive automigration"
- task :autoupgrade => :environment do
- FileList["app/models/**/*.rb"].each do |model|
- load model
- end
- ::DataMapper.auto_upgrade!
- end
-
- namespace :migrate do
- task :load => :environment do
- gem 'dm-migrations'
- FileList["db/migrations/*.rb"].each do |migration|
- load migration
- end
- end
-
- desc "Migrate up using migrations"
- task :up, :version, :needs => :load do |t, args|
- version = args[:version]
- migrate_up!(version)
- end
-
- desc "Migrate down using migrations"
- task :down, :version, :needs => :load do |t, args|
- version = args[:version]
- migrate_down!(version)
- end
- end
-
- desc "Migrate the database to the latest version"
- task :migrate => 'db:migrate:up'
-end
View
10 generators/dm_migration/dm_migration_generator.rb
@@ -1,10 +0,0 @@
-require 'rails_generator/generators/components/migration/migration_generator'
-
-class DmMigrationGenerator < Rails::Generator::NamedBase
-
- def manifest
- record do |m|
- m.migration_template "migration.rb", "db/migrate"
- end
- end
-end
View
16 generators/dm_migration/templates/migration.rb
@@ -1,16 +0,0 @@
-class <%= migration_name %> < ActiveRecord::Migration
- def self.up
- create_table :<%= table_name %> do |t|
-<% for attribute in attributes -%>
- t.<%= attribute.type %> :<%= attribute.name %>
-<% end -%>
-<% unless options[:skip_timestamps] %>
- t.timestamps
-<% end -%>
- end
- end
-
- def self.down
- drop_table :<%= table_name %>
- end
-end
View
27 generators/dm_model/dm_model_generator.rb
@@ -1,27 +0,0 @@
-require 'rails_generator/generators/components/model/model_generator'
-require 'active_record'
-
-class DmModelGenerator <ModelGenerator
-
- def manifest
- record do |m|
- # Check for class naming collisions.
- m.class_collisions class_path, class_name, "#{class_name}Test"
-
- # Model, test, and fixture directories.
- m.directory File.join('app/models', class_path)
- m.directory File.join('test/unit', class_path)
-
- # Model class, unit test, and fixtures.
- m.template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
- m.template 'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
-
- unless options[:skip_migration]
- m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
- :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
- }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
- end
- end
- end
-
-end
View
14 generators/dm_model/templates/model.rb
@@ -1,14 +0,0 @@
-class <%= class_name %>
- include DataMapper::Resource
- property :id, Serial
-
-<% for attribute in attributes -%>
- property :<%= attribute.name %>, <%= attribute.type.to_s.capitalize %>, :nullable => false
-
-<% end -%>
-<% unless options[:skip_timestamps] %>
- property :created_at, DateTime, :nullable => false
- property :updated_at, DateTime, :nullable => false
-
-<% end -%>
-end
View
47 generators/overlay.rb
@@ -0,0 +1,47 @@
+require 'rails_generator/base'
+
+# this extension allows to reuse generators from other gems by defining
+# a search path (list of overlay directories) for template files. these
+# overlay directories have preference to the rails template search algorithm.
+# the overlay directories can be also given via command line options.
+
+Rails::Generator::NamedBase.class_eval do
+
+ def overlay_dirs
+ options[:overlay_dirs] ||= []
+ end
+
+ def add_options!(opt)
+ super
+ opt.on("--overlay-dir DIR",
+ "overlay") do |v|
+ overlay_dirs << v
+ end
+ end
+
+ def source_path(relative_source)
+ # Check whether we're referring to another generator's file.
+ name, path = relative_source.split(':', 2)
+
+ # first check if the template can be found with in any of the overlay directories
+ if dirs = options[:overlay_dirs]
+ file = path.nil? ? name : path
+ dirs.reverse.each do |dir|
+ if (f = File.join(dir, file)) and File.exists?(f)
+ return f
+ end
+ end
+ end
+
+ # If not, return the full path to our source file.
+ if path.nil?
+ File.join(source_root, name)
+
+ # Otherwise, ask our referral for the file.
+ else
+ # FIXME: this is broken, though almost always true. Others'
+ # source_root are not necessarily the templates dir.
+ File.join(self.class.lookup(name).path, 'templates', path)
+ end
+ end
+end
View
8 generators/rspec_datamapper_controller/rspec_datamapper_controller_generator.rb
@@ -0,0 +1,8 @@
+require File.dirname(__FILE__) + '/../overlay'
+
+class RspecDatamapperControllerGenerator < RspecControllerGenerator
+ def manifest
+ overlay_dirs << File.join(self.class.lookup("rspec_controller").path, 'templates')
+ super
+ end
+end
View
0  ...pec_dm_controller/templates/controller.rb → ...mapper_controller/templates/controller.rb
File renamed without changes
View
12 ...m_controller/templates/controller_spec.rb → ...r_controller/templates/controller_spec.rb
@@ -125,19 +125,19 @@ def user.groups
it "updates the requested <%= file_name %>" do
<%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
- mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
+ mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
mock_<%= file_name %>.should_receive(:dirty?)
put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
end
it "exposes the requested <%= file_name %> as @<%= file_name %>" do
- <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => true))
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
put :update, :id => "1"
assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
end
it "redirects to the <%= file_name %>" do
- <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => true))
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
put :update, :id => "1"
response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
end
@@ -148,20 +148,20 @@ def user.groups
it "updates the requested <%= file_name %>" do
<%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
- mock_<%= file_name %>.should_receive(:update_attributes).with({'these' => 'params'})
+ mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
mock_<%= file_name %>.should_receive(:dirty?)
put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
end
it "exposes the <%= file_name %> as @<%= file_name %>" do
- <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => false))
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
mock_<%= file_name %>.should_receive(:dirty?)
put :update, :id => "1"
assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
end
it "re-renders the 'edit' template" do
- <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update_attributes => false, :dirty? => true))
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
put :update, :id => "1"
response.should render_template('edit')
end
View
0  ...s/rspec_dm_controller/templates/helper.rb → ...datamapper_controller/templates/helper.rb
File renamed without changes
View
0  ...ec_dm_controller/templates/helper_spec.rb → ...apper_controller/templates/helper_spec.rb
File renamed without changes
View
13 generators/rspec_datamapper_model/rspec_datamapper_model_generator.rb
@@ -0,0 +1,13 @@
+require 'rails_generator/generators/components/model/model_generator'
+require 'active_record'
+require File.dirname(__FILE__) + '/../overlay'
+
+class RspecDatamapperModelGenerator < RspecModelGenerator
+
+ def manifest
+ overlay_dirs << File.join(self.class.lookup("datamapper_model").path, 'templates')
+ overlay_dirs << File.join(self.class.lookup("rspec_model").path, 'templates')
+ super
+ end
+
+end
View
7 generators/rspec_dm_model/templates/model.rb → ...rspec_datamapper_model/templates/model.rb
@@ -7,7 +7,7 @@ class <%= class_name %>
property :id, Serial
<% for attribute in attributes -%>
- 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 -%>
+ 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 -%>
<% end -%>
<% unless options[:skip_timestamps] %>
@@ -15,9 +15,4 @@ class <%= class_name %>
<% end -%>
-<% if options[:ixtlan] -%>
- def to_s
- "<%= class_name %>(#{attribute_get(:id)})"
- end
-<% end -%>
end
View
5 ...rs/rspec_dm_model/templates/model_spec.rb → ..._datamapper_model/templates/model_spec.rb 100644 → 100755
@@ -9,13 +9,16 @@
}
end
+ it "should create a new instance given valid attributes" do
+ <%= class_name %>.create(@valid_attributes)
+ end
<% attributes.each do |attribute| -%>
it "should require <%= attribute.name %>" do
<%= singular_name %> = <%= class_name %>.create(@valid_attributes.merge(:<%= attribute.name %> => nil))
<%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
end
-<% if attribute.type == :string or attribute.type == :text -%>
+<% if attribute.type == :string or attribute.type == :text or attribute.type == :slug -%>
it 'should not match <%= attribute.name %>' do
<%= singular_name %> = <%= class_name %>.create(@valid_attributes.merge(:<%= attribute.name %> => "<script" ))
<%= singular_name %>.errors.on(:<%= attribute.name %>).should_not == nil
View
16 generators/rspec_datamapper_scaffold/rspec_datamapper_scaffold_generator.rb
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/../overlay'
+require File.dirname(__FILE__) + '/../rspec_default_values'
+
+class RspecDatamapperScaffoldGenerator < RspecScaffoldGenerator
+
+ def manifest
+ overlay_dirs << File.join(self.class.lookup("scaffold").path, 'templates')
+ overlay_dirs << File.join(self.class.lookup("rspec_scaffold").path, 'templates')
+ overlay_dirs << File.join(self.class.lookup("datamapper_model").path, 'templates')
+ overlay_dirs << File.join(self.class.lookup("rspec_model").path, 'templates')
+ overlay_dirs << source_root
+
+ super
+ end
+
+end
View
90 generators/rspec_datamapper_scaffold/templates/controller.rb
@@ -0,0 +1,90 @@
+class <%= controller_class_name %>Controller < ApplicationController
+
+ # GET /<%= table_name %>
+ # GET /<%= table_name %>.xml
+ def index
+ @<%= table_name %> = <%= class_name %>.all()
+
+ respond_to do |format|
+ format.html
+ format.xml { render :xml => @<%= table_name %> }
+ end
+ end
+
+ # GET /<%= table_name %>/1
+ # GET /<%= table_name %>/1.xml
+ def show
+ @<%= file_name %> = <%= class_name %>.get!(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.xml { render :xml => @<%= file_name %> }
+ end
+ end
+
+ # GET /<%= table_name %>/new
+ # GET /<%= table_name %>/new.xml
+ def new
+ @<%= file_name %> = <%= class_name %>.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.xml { render :xml => @<%= file_name %> }
+ end
+ end
+
+ # GET /<%= table_name %>/1/edit
+ def edit
+ @<%= file_name %> = <%= class_name %>.get!(params[:id])
+ end
+
+ # POST /<%= table_name %>
+ # POST /<%= table_name %>.xml
+ def create
+ @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
+
+ respond_to do |format|
+ if @<%= file_name %>.save
+ flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_created')<% else -%>'<%= class_name %> was successfully created.'<% end -%>
+
+ format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
+ 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
+
+ # PUT /<%= table_name %>/1
+ # PUT /<%= table_name %>/1.xml
+ def update
+ @<%= file_name %> = <%= class_name %>.get!(params[:id])
+
+ respond_to do |format|
+ if @<%= file_name %>.update(params[:<%= file_name %>]) or not @<%= file_name %>.dirty?
+ flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_updated')<% else -%>'<%= class_name %> was successfully updated.'<% end -%>
+
+ format.html { redirect_to(<%= file_name %>_url(@<%= file_name %>.id)) }
+ format.xml { head :ok }
+ else
+ format.html { render :action => "edit" }
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /<%= table_name %>/1
+ # DELETE /<%= table_name %>/1.xml
+ def destroy
+ @<%= file_name %> = <%= class_name %>.get(params[:id])
+ @<%= file_name %>.destroy if @<%= file_name %>
+
+ respond_to do |format|
+ flash[:notice] = <% if options[:i18n] -%>t('<%= plural_name %>.<%= singular_name %>_deleted')<% else -%>'<%= class_name %> was successfully deleted.'<% end -%>
+
+ format.html { redirect_to(<%= table_name %>_url) }
+ format.xml { head :ok }
+ end
+ end
+end
View
174 generators/rspec_datamapper_scaffold/templates/controller_spec.rb
@@ -0,0 +1,174 @@
+require File.expand_path(File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../spec_helper')
+
+describe <%= controller_class_name %>Controller do
+
+ def mock_<%= file_name %>(stubs={})
+ @mock_<%= file_name %> ||= mock_model(<%= class_name %>, stubs)
+ end
+
+ describe "GET index" do
+
+ it "exposes all <%= table_name %> as @<%= table_name %>" do
+ <%= class_name %>.should_receive(:all).and_return([mock_<%= file_name %>])
+ get :index
+ assigns[:<%= table_name %>].should == [mock_<%= file_name %>]
+ end
+
+ describe "with mime type of xml" do
+
+ it "renders all <%= table_name.pluralize %> as xml" do
+ <%= class_name %>.should_receive(:all).and_return(<%= file_name.pluralize %> = mock("Array of <%= class_name.pluralize %>"))
+ <%= file_name.pluralize %>.should_receive(:to_xml).and_return("generated XML")
+ get :index, :format => 'xml'
+ response.body.should == "generated XML"
+ end
+
+ end
+
+ end
+
+ describe "GET show" do
+
+ it "exposes the requested <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
+ get :show, :id => "37"
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
+ end
+
+ describe "with mime type of xml" do
+
+ it "renders the requested <%= file_name %> as xml" do
+ <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
+ mock_<%= file_name %>.should_receive(:to_xml).and_return("generated XML")
+ get :show, :id => "37", :format => 'xml'
+ response.body.should == "generated XML"
+ end
+
+ end
+
+ end
+
+ describe "GET new" do
+
+ it "exposes a new <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.should_receive(:new).and_return(mock_<%= file_name %>)
+ get :new
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
+ end
+
+ end
+
+ describe "GET edit" do
+
+ it "exposes the requested <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
+ get :edit, :id => "37"
+ assigns[:<%= file_name %>].should equal(mock_<%= file_name %>)
+ end
+
+ end
+
+ describe "POST create" do
+
+ describe "with valid params" do
+
+ it "exposes a newly created <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.should_receive(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => true))
+ post :create, :<%= file_name %> => {:these => 'params'}
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
+ end
+
+ it "redirects to the created <%= file_name %>" do
+ <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => true))
+ post :create, :<%= file_name %> => {}
+ response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
+ end
+
+ end
+
+ describe "with invalid params" do
+
+ it "exposes a newly created but unsaved <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.stub!(:new).with({'these' => 'params'}).and_return(mock_<%= file_name %>(:save => false))
+ post :create, :<%= file_name %> => {:these => 'params'}
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
+ end
+
+ it "re-renders the 'new' template" do
+ <%= class_name %>.stub!(:new).and_return(mock_<%= file_name %>(:save => false))
+ post :create, :<%= file_name %> => {}
+ response.should render_template('new')
+ end
+
+ end
+
+ end
+
+ describe "PUT udpate" do
+
+ describe "with valid params" do
+
+ it "updates the requested <%= file_name %>" do
+ <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
+ mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
+ mock_<%= file_name %>.should_receive(:dirty?)
+ put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
+ end
+
+ it "exposes the requested <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
+ put :update, :id => "1"
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
+ end
+
+ it "redirects to the <%= file_name %>" do
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => true))
+ put :update, :id => "1"
+ response.should redirect_to(<%= table_name.singularize %>_url(mock_<%= file_name %>))
+ end
+
+ end
+
+ describe "with invalid params" do
+
+ it "updates the requested <%= file_name %>" do
+ <%= class_name %>.should_receive(:get!).with("37").and_return(mock_<%= file_name %>)
+ mock_<%= file_name %>.should_receive(:update).with({'these' => 'params'})
+ mock_<%= file_name %>.should_receive(:dirty?)
+ put :update, :id => "37", :<%= file_name %> => {:these => 'params'}
+ end
+
+ it "exposes the <%= file_name %> as @<%= file_name %>" do
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false))
+ mock_<%= file_name %>.should_receive(:dirty?)
+ put :update, :id => "1"
+ assigns(:<%= file_name %>).should equal(mock_<%= file_name %>)
+ end
+
+ it "re-renders the 'edit' template" do
+ <%= class_name %>.stub!(:get!).and_return(mock_<%= file_name %>(:update => false, :dirty? => true))
+ put :update, :id => "1"
+ response.should render_template('edit')
+ end
+
+ end
+
+ end
+
+ describe "DELETE destroy" do
+
+ it "destroys the requested <%= file_name %>" do
+ <%= class_name %>.should_receive(:get).with("37").and_return(mock_<%= file_name %>)
+ mock_<%= file_name %>.should_receive(:destroy)
+ delete :destroy, :id => "37"
+ end
+
+ it "redirects to the <%= table_name %> list" do
+ <%= class_name %>.should_receive(:get).with("1").and_return(mock_<%= file_name %>(:destroy => true))
+ delete :destroy, :id => "1"
+ response.should redirect_to(<%= table_name %>_url)
+ end
+
+ end
+
+end
View
7 generators/rspec_default_values.rb
@@ -1,3 +1,6 @@
+require 'rails_generator/generated_attribute'
+require 'active_record'
+
module Rails
module Generator
class GeneratedAttribute
@@ -6,14 +9,14 @@ def default_value
when :int, :integer then "1"
when :float then "1.5"
when :decimal, :big_decimal then "9.99"
- when :date_time, :datetime,
+ when :date_time, :datetime,
:timestamp, :time then "Time.now"
when :date then "Date.today"
when :string, :text then "\"value for #{@name}\""
when :boolean then "false"
else
""
- end
+ end
end
end
end
View
71 generators/rspec_dm_controller/rspec_dm_controller_generator.rb
@@ -1,71 +0,0 @@
-class RspecDmControllerGenerator < Rails::Generator::NamedBase
-
- attr_reader :controller_name,
- :controller_class_path,
- :controller_file_path,
- :controller_class_nesting,
- :controller_class_nesting_depth,
- :controller_class_name,
- :controller_singular_name,
- :controller_plural_name
- alias_method :controller_file_name, :controller_singular_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_singular_name, @controller_plural_name = inflect_names(base_name)
-
- 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_name}Controller", "#{controller_class_name}Helper")
- m.class_collisions(class_name)
-
- # Controller, helper, views, and test directories.
- m.directory(File.join('app/controllers', controller_class_path))
- m.directory(File.join('app/helpers', controller_class_path))
- m.directory(File.join('spec/helpers', controller_class_path))
-
- m.template 'controller_spec.rb',
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_controller_spec.rb")
-
- m.template 'rspec_scaffold:routing_spec.rb',
- File.join('spec/controllers', controller_class_path, "#{controller_file_name}_routing_spec.rb")
-
- m.template 'rspec_scaffold:helper_spec.rb',
- File.join('spec/helpers', class_path, "#{controller_file_name}_helper_spec.rb")
-
- m.template('controller.rb',
- File.join('app/controllers',
- controller_class_path,
- "#{controller_file_name}_controller.rb"))
-
- m.template('helper.rb',
- File.join('app/helpers',
- controller_class_path, "#{controller_file_name}_helper.rb"))
-
- m.route_resources controller_file_name
- end
- end
-
- protected
-
- def banner
- "Usage: #{$0} rspec_dm_controller ModelName"
- end
-
- def model_name
- class_name.demodulize
- end
-end
View
33 generators/rspec_dm_model/rspec_dm_model_generator.rb
@@ -1,33 +0,0 @@
-require 'rails_generator/generators/components/model/model_generator'
-require 'active_record'
-require File.dirname(__FILE__) + '/../rspec_default_values'
-
-class RspecDmModelGenerator <ModelGenerator
-
- def manifest
- record do |m|
-
- # Check for class naming collisions.
- #m.class_collisions class_path, class_name
-
- # Model, spec, and fixture directories.
- m.directory File.join('app/models', class_path)
- m.directory File.join('spec/models', class_path)
-
- # Model class, spec and fixtures.
- m.template 'model.rb', File.join('app/models',
- class_path,
- "#{file_name}.rb")
- m.template 'model_spec.rb', File.join('spec/models',
- class_path,
- "#{file_name}_spec.rb")
- end
-
-# unless options[:skip_migration]
-# m.migration_template 'model:migration.rb', 'db/migrate', :assigns => {
-# :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}"
-# }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
-# end
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.