Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

make it a gem. added integration test

  • Loading branch information...
commit 04ee14a450d1efa5d6dacd235dd43481262e54c0 1 parent 04d07bf
Christian Meier authored October 14, 2010
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+*.gemspec.pom
20  MIT-LICENSE
... ...
@@ -0,0 +1,20 @@
  1
+Copyright (c) 2010 Kristian Meier
  2
+
  3
+Permission is hereby granted, free of charge, to any person obtaining
  4
+a copy of this software and associated documentation files (the
  5
+"Software"), to deal in the Software without restriction, including
  6
+without limitation the rights to use, copy, modify, merge, publish,
  7
+distribute, sublicense, and/or sell copies of the Software, and to
  8
+permit persons to whom the Software is furnished to do so, subject to
  9
+the following conditions:
  10
+
  11
+The above copyright notice and this permission notice shall be
  12
+included in all copies or substantial portions of the Software.
  13
+
  14
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55  Rakefile
... ...
@@ -0,0 +1,55 @@
  1
+# -*- ruby -*-
  2
+
  3
+require 'rubygems'
  4
+
  5
+require 'spec'
  6
+require 'spec/rake/spectask'
  7
+
  8
+BUILD_DIR = 'target'
  9
+
  10
+desc 'clean up'
  11
+task :clean do
  12
+  FileUtils.rm_rf(BUILD_DIR)
  13
+end
  14
+
  15
+desc 'Package as a gem.'
  16
+task :package do
  17
+  require 'fileutils'
  18
+  gemspec = Dir['*.gemspec'].first
  19
+  Kernel.system("#{RUBY} -S gem build #{gemspec}")
  20
+  FileUtils.mkdir_p(BUILD_DIR)
  21
+  gem = Dir['*.gem'].first
  22
+  FileUtils.mv(gem, File.join(BUILD_DIR,"#{gem}"))
  23
+  puts File.join(BUILD_DIR,"#{gem}")
  24
+end
  25
+
  26
+desc 'Install the package as a gem.'
  27
+task :install => [:package] do
  28
+  gem = Dir[File.join(BUILD_DIR, '*.gem')].first
  29
+  extra = ENV['GEM_HOME'].nil? && ENV['GEM_PATH'].nil? ? "--user-install" : ""
  30
+  Kernel.system("#{RUBY} -S gem install --local #{gem} --no-ri --no-rdoc #{extra}")
  31
+end
  32
+
  33
+desc 'Run specifications'
  34
+Spec::Rake::SpecTask.new(:spec) do |t|
  35
+  if File.exists?(File.join('spec','spec.opts'))
  36
+    t.spec_opts << '--options' << File.join('spec','spec.opts')
  37
+  end
  38
+  t.spec_files = Dir.glob(File.join('spec','**','*_spec.rb'))
  39
+end
  40
+
  41
+desc 'generate rails using all generators and run the specs'
  42
+task :"integration-test" => [:spec, :install] do
  43
+  ruby = RUBY_PLATFORM =~ /java/ ? 'jruby' : 'ruby'
  44
+  directory = File.join('target', 'app')
  45
+  rails_version = '3.0.0'
  46
+  rails_template = 'integration-test.rb'
  47
+  command = "-S rails _#{rails_version}_ new #{directory} -f #{rails_template ? '-m' : ''} #{rails_template}"
  48
+  unless system("#{ruby} #{command}")
  49
+      puts
  50
+      puts "error in: #{@ruby} #{command}"
  51
+      exit 1
  52
+    end
  53
+end
  54
+
  55
+# vim: syntax=Ruby
36  integration-test.rb
... ...
@@ -0,0 +1,36 @@
  1
+apply File.join(File.dirname(__FILE__), 'roo-template.rb')
  2
+
  3
+def rake(*args)
  4
+  ruby = (defined? JRUBY_VERSION) ? "jruby" : "ruby"
  5
+  run "#{ruby} -S rake #{args.join(' ')}"
  6
+end
  7
+
  8
+def roo_generate(*args)
  9
+  generate *args
  10
+  rake "db:migrate"
  11
+end
  12
+
  13
+roo_generate 'roo:new_model', 'user'
  14
+
  15
+roo_generate 'roo:add_field', 'user', "name:string"
  16
+
  17
+roo_generate 'roo:rename_field', 'user', "name", "login"
  18
+
  19
+roo_generate 'roo:change_field', 'user', "login:string", "presence:true", "max:124"
  20
+
  21
+roo_generate 'roo:change_field', 'user', "login:string", "presence:true", "min:12", "max:224"
  22
+
  23
+roo_generate 'roo:change_field', 'user', "login:string", "presence:true", "min:2", "max:24", "default:bka"
  24
+
  25
+roo_generate 'roo:change_field', 'user', "login:string", "presence:true", "min:2", "max:24", "default:bka"
  26
+
  27
+roo_generate 'roo:change_field', 'user', "login:string", "presence:true", "min:2", "max:24", "default:bka", "unique:true", "format:[a-z]*"
  28
+
  29
+roo_generate 'roo:add_field', 'user', "name:string"
  30
+
  31
+roo_generate 'roo:remove_field', 'user', "name"
  32
+
  33
+rake "db:migrate VERSION=0"
  34
+
  35
+rake "db:migrate"
  36
+
0  lib/roo/USAGE → lib/generators/USAGE
File renamed without changes
0  lib/roo/active_record/validations_file.rb → lib/generators/active_record/validations_file.rb
File renamed without changes
7  lib/roo/base.rb → lib/generators/roo.rb
... ...
@@ -1,6 +1,7 @@
  1
+require 'rails/generators'
1 2
 require 'rails/generators/active_record'
2 3
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/active_record/validations_file'
  4
+require 'generators/active_record/validations_file'
4 5
 module Roo
5 6
   module Generators
6 7
     class Base < ::ActiveRecord::Generators::Base
@@ -11,7 +12,7 @@ def name # set alias so NamedBase uses the model as its name
11 12
       end
12 13
 
13 14
       def self.parameters_argument
14  
-        argument :parameters, :type => :hash, :default => {}, :banner => "index:name index:true unique:true min:123 max:128 nullable:false default:blablub"
  15
+        argument :parameters, :type => :hash, :default => {}, :banner => "index:name index:true unique:true min:123 max:128 presence:false default:blablub"
15 16
       end
16 17
 
17 18
       def self.migration_exists?(dirname, file_name)
@@ -89,7 +90,7 @@ def init_new_column_options(field)
89 90
         @new_column_options = {}
90 91
         @parameters ||= {}
91 92
         @new_column_options[:limit] = @parameters["max"] if @parameters["max"] && @parameters["max"].to_i > 0
92  
-        @new_column_options[:default] = @parameters["default"] if @parameters["default"] && @parameters["default"] != ''
  93
+        @new_column_options[:default] = @parameters["default"]if @parameters["default"] && @parameters["default"] != ''
93 94
         @new_column_options[:null] = "true" != @parameters["presence"] 
94 95
         #TODO precision + scale for decimal
95 96
       end
4  lib/roo/add_field/add_field_generator.rb → lib/generators/roo/add_field/add_field_generator.rb
... ...
@@ -1,7 +1,7 @@
1 1
 require 'rails/generators/active_record'
2 2
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
4  
-require 'roo/active_record/validations_file'
  3
+require 'generators/roo'
  4
+#require 'genrators/active_record/validations_file'
5 5
 
6 6
 module Roo
7 7
   module Generators
2  lib/roo/change_field/change_field_generator.rb → ...rators/roo/change_field/change_field_generator.rb
... ...
@@ -1,6 +1,6 @@
1 1
 require 'rails/generators/active_record'
2 2
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
  3
+require 'generators/roo'
4 4
 
5 5
 module Roo
6 6
   module Generators
2  lib/roo/new_model/new_model_generator.rb → lib/generators/roo/new_model/new_model_generator.rb
... ...
@@ -1,6 +1,6 @@
1 1
 require 'rails/generators/active_record'
2 2
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
  3
+require 'generators/roo'
4 4
 require 'rails/generators/test_unit'
5 5
 
6 6
 module Roo
0  lib/roo/new_model/templates/migration.rb → lib/generators/roo/new_model/templates/migration.rb
File renamed without changes
0  lib/roo/new_model/templates/model.rb → lib/generators/roo/new_model/templates/model.rb
File renamed without changes
0  lib/roo/new_model/templates/module.rb → lib/generators/roo/new_model/templates/module.rb
File renamed without changes
2  lib/roo/remove_field/remove_field_generator.rb → ...rators/roo/remove_field/remove_field_generator.rb
... ...
@@ -1,6 +1,6 @@
1 1
 require 'rails/generators/active_record'
2 2
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
  3
+require 'generators/roo'
4 4
 
5 5
 module Roo
6 6
   module Generators
2  lib/roo/rename_field/rename_field_generator.rb → ...rators/roo/rename_field/rename_field_generator.rb
... ...
@@ -1,6 +1,6 @@
1 1
 require 'rails/generators/active_record'
2 2
 require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
  3
+require 'generators/roo'
4 4
 
5 5
 module Roo
6 6
   module Generators
0  lib/roo/rename_field/templates/migration.rb → ...enerators/roo/rename_field/templates/migration.rb
File renamed without changes
4  lib/roo/templates/migration.rb → lib/generators/templates/migration.rb
... ...
@@ -1,7 +1,7 @@
1 1
 class <%= migration_class_name %> < ActiveRecord::Migration
2 2
   def self.up
3 3
   <%- if migration_action -%>
4  
-    <%= migration_action %>_column :<%= table_name %>, :<%= new_field.name %><%- if migration_action != 'remove' -%>, :<%= new_field.type %><%- end -%><%- new_column_options.each do |k,v| -%>, :<%= k %> => <%= v %><%- end -%>
  4
+    <%= migration_action %>_column :<%= table_name %>, :<%= new_field.name %><%- if migration_action != 'remove' -%>, :<%= new_field.type %><%- end -%><%- new_column_options.each do |k,v| -%>, :<%= k %> => <%= v.inspect %><%- end -%>
5 5
   <%- end -%>
6 6
   <%- if old_index_options -%>
7 7
 
@@ -16,7 +16,7 @@ def self.up
16 16
 
17 17
   def self.down
18 18
   <%- if migration_alternate_action -%>
19  
-    <%= migration_alternate_action %>_column :<%= table_name %>, :<%= new_field.name %><%- if migration_alternate_action != 'remove' -%>, :<%= old_field.type %><%- end -%><%- (old_column_options || {}).each do |k,v| -%>, :<%= k %> => <%= v %><%- end -%>
  19
+    <%= migration_alternate_action %>_column :<%= table_name %>, :<%= new_field.name %><%- if migration_alternate_action != 'remove' -%>, :<%= old_field.type %><%- end -%><%- (old_column_options || {}).each do |k,v| -%>, :<%= k %> => <%= v.inspect %><%- end -%>
20 20
   <%- end -%>
21 21
   <%- if new_index_options -%>
22 22
 
19  lib/roo/new_model/new_model_generator.rb-
... ...
@@ -1,19 +0,0 @@
1  
-require 'rails/generators/active_record'
2  
-require 'rails/generators/active_record/migration/migration_generator'
3  
-require 'roo/base'
4  
-
5  
-module Roo
6  
-  module Generators
7  
-    class NewModelGenerator < Roo::Generators::Base
8  
-
9  
-      hook_for :orm, :as => :model
10  
-      source_root(File.join(File.dirname(__FILE__), "templates"))
11  
-
12  
-      class_option :validation,  :type => :boolean
13  
-      
14  
-      def create_migration_file
15  
-        create_file File.join('app', 'models', class_path, file_name) + "_validation.rb", "module #{class_name}Validation; end"
16  
-      end
17  
-    end
18  
-  end
19  
-end
25  rails-roo.gemspec
... ...
@@ -0,0 +1,25 @@
  1
+# create by maven - leave it as is
  2
+Gem::Specification.new do |s|
  3
+  s.name = 'rails-roo'
  4
+  s.version = '0.1.0'
  5
+
  6
+  s.summary = 'roo like generators for rails'
  7
+  s.description = 'this is inspired by www.springsource.org/roo which allows to manage models and their views during the whole lifecycle of the related classes/files.'
  8
+  s.homepage = 'http://github.com/mkristian/rails-roo'
  9
+
  10
+  s.authors = ['mkristian']
  11
+  s.email = ['m.kristian@web.de']
  12
+
  13
+  s.files = Dir['MIT-LICENSE']
  14
+  s.licenses << 'MIT-LICENSE'
  15
+#  s.files += Dir['History.txt']
  16
+  s.files += Dir['README.textile']
  17
+#  s.extra_rdoc_files = ['History.txt','README.textile']
  18
+  s.rdoc_options = ['--main','README.textile']
  19
+  s.files += Dir['lib/**/*']
  20
+  s.files += Dir['spec/**/*']
  21
+  s.test_files += Dir['spec/**/*_spec.rb']
  22
+  s.add_dependency 'rails', '~> 3.0.0'
  23
+  s.add_development_dependency 'rspec', '~> 1.3.0'
  24
+  s.add_development_dependency 'rake', '0.8.7'
  25
+end
25  rails3-default-template.rb
... ...
@@ -0,0 +1,25 @@
  1
+db_gem_line = "gem '#{gem_for_database}'"
  2
+db_gem_line << ", :require => '#{require_for_database}'" if require_for_database
  3
+db_gem_regexp = Regexp::quote(db_gem_line).gsub("'", "['\"]")
  4
+jdbc_db = case options[:database]
  5
+          when /postgresql/
  6
+            "postgres"
  7
+          when /mysql2/
  8
+            "mysql"
  9
+          when /mysql|sqlite3/
  10
+            options[:database]
  11
+          end
  12
+jdbc_gem_line = "\n  gem 'jdbc-#{jdbc_db}', :require => false" if jdbc_db
  13
+
  14
+gsub_file "Gemfile", /^#{db_gem_regexp}\w*$/, <<DB
  15
+if defined?(JRUBY_VERSION)
  16
+  gem 'activerecord-jdbc-adapter'#{jdbc_gem_line}
  17
+else
  18
+  #{db_gem_line}
  19
+end
  20
+DB
  21
+
  22
+# Avoid MySQL2 option for Rails 3
  23
+if options[:database] =~ /mysql/
  24
+  gsub_file "config/database.yml", /mysql2/, 'mysql'
  25
+end
4  roo-template.rb
... ...
@@ -0,0 +1,4 @@
  1
+if defined? JRUBY_VERSION
  2
+  apply File.join(File.dirname(__FILE__), 'rails3-default-template.rb')
  3
+end
  4
+gem 'rails-roo'
0  lib/roo/roo_generator.rb → roo_generator.rb
File renamed without changes

0 notes on commit 04ee14a

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