Permalink
Browse files

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

…nflict with rails_datamapper gem
  • Loading branch information...
1 parent 7b7ff7b commit cf71c320aa601a540257152bd4fa8ae9ee1b71d1 @mkristian committed Sep 16, 2009
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -7,17 +7,12 @@ 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] %>
timestamps :at
<% end -%>
-<% if options[:ixtlan] -%>
- def to_s
- "<%= class_name %>(#{attribute_get(:id)})"
- end
-<% end -%>
end
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit cf71c32

Please sign in to comment.