Skip to content
Browse files

make it a gem. added integration test

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

0 comments on commit 04ee14a

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