Permalink
Browse files

Merge branch 'rails' of github.com:winton/gem_template

Conflicts:
	README.md
	Rakefile
	bin/gem_template
	lib/gem_template.rb
	rails/init.rb
	require.rb
	spec/spec_helper.rb
  • Loading branch information...
2 parents b828d39 + ceab4b3 commit c32690c783a3e936201ecd3255f5c53c7e59866b @winton committed Dec 5, 2010
View
@@ -1,6 +1,5 @@
.DS_Store
*.gem
-*.gemspec
coverage
pkg
spec/config/database.yml
View
@@ -1,4 +1,4 @@
-Copyright (c) 2009 Winton Welsh
+Copyright (c) 2010 Winton Welsh
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
@@ -15,4 +15,4 @@ 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.
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -14,7 +14,7 @@ Define the model
----------------
<pre>
-class Article < ActiveRecord::Base
+class Article &lt; ActiveRecord::Base
also_migrate :article_archives, :ignore => 'moved_at', :indexes => 'id'
end
</pre>
View
107 Rakefile
@@ -1,2 +1,105 @@
-require "#{File.dirname(__FILE__)}/require"
-Require.rakefile!
+require File.dirname(__FILE__) + '/lib/also_migrate/gems'
+
+AlsoMigrate::Gems.require(:rake)
+
+require 'rake'
+require 'rake/gempackagetask'
+require 'spec/rake/spectask'
+
+def gemspec
+ @gemspec ||= begin
+ file = File.expand_path('../also_migrate.gemspec', __FILE__)
+ eval(File.read(file), binding, file)
+ end
+end
+
+if defined?(Rake::GemPackageTask)
+ Rake::GemPackageTask.new(gemspec) do |pkg|
+ pkg.gem_spec = gemspec
+ end
+ task :gem => :gemspec
+end
+
+if defined?(Spec::Rake::SpecTask)
+ desc "Run specs"
+ Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+ t.spec_opts = %w(-fs --color)
+ t.warning = true
+ end
+ task :spec
+end
+
+namespace :gems do
+ desc "Install gems (DEV=1|0 DOCS=1|0 SUDO=1|0)"
+ task :install do
+ file = File.dirname(__FILE__) + '/gems'
+ sudo = (ENV['SUDO'] ||= '0').to_i
+ docs = (ENV['DOCS'] ||= '0').to_i
+ sudo = sudo == 1 ? 'sudo' : ''
+ docs = docs == 1 ? '' : '--no-ri --no-rdoc'
+ gems = []
+
+ if File.exists?(file)
+ File.open(file, 'r') do |f|
+ gems = f.readlines.collect do |line|
+ line.split(' ')
+ end
+ end
+ else
+ gems = AlsoMigrate::Gems::TYPES[:gemspec]
+ gems = AlsoMigrate::Gems::TYPES[:gemspec_dev] if ENV['DEV'] == '1'
+ gems.collect! do |g|
+ [ g.to_s, AlsoMigrate::Gems::VERSIONS[g] ]
+ end
+ end
+
+ gems.each do |(name, version)|
+ if Gem.source_index.find_name(name, version).empty?
+ version = version ? "-v #{version}" : ''
+ system "#{sudo} gem install #{name} #{version} #{docs}"
+ else
+ puts "already installed: #{name} #{version}"
+ end
+ end
+ end
+end
+
+desc "Install gem locally"
+task :install => :package do
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
+end
+
+desc "Validate the gemspec"
+task :gemspec do
+ gemspec.validate
+end
+
+task :package => :gemspec
+task :default => :spec
+
+# DELETE AFTER USING
+desc "Rename project"
+task :rename do
+ name = ENV['NAME'] || File.basename(Dir.pwd)
+ camelize = lambda do |str|
+ str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ end
+ dir = Dir['**/also_migrate*']
+ begin
+ from = dir.pop
+ if from
+ to = from.split('/')
+ to[-1].gsub!('also_migrate', name)
+ FileUtils.mv(from, to.join('/'))
+ end
+ end while dir.length > 0
+ Dir["**/*"].each do |path|
+ if File.file?(path)
+ `sed -i '' 's/also_migrate/#{name}/g' #{path}`
+ `sed -i '' 's/AlsoMigrate/#{camelize.call(name)}/g' #{path}`
+ no_space = File.read(path).gsub(/\s+\z/, '')
+ File.open(path, 'w') { |f| f.write(no_space) }
+ end
+ end
+end
View
@@ -0,0 +1,29 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib/', __FILE__)
+$:.unshift lib unless $:.include?(lib)
+
+require 'also_migrate/gems'
+require 'also_migrate/version'
+
+Gem::Specification.new do |s|
+ s.name = "also_migrate"
+ s.version = AlsoMigrate::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Winton Welsh"]
+ s.email = ["mail@wintoni.us"]
+ s.homepage = "http://github.com/winton/also_migrate"
+ s.summary = ""
+ s.description = ""
+
+ AlsoMigrate::Gems::TYPES[:gemspec].each do |g|
+ s.add_dependency g.to_s, AlsoMigrate::Gems::VERSIONS[g]
+ end
+
+ AlsoMigrate::Gems::TYPES[:gemspec_dev].each do |g|
+ s.add_development_dependency g.to_s, AlsoMigrate::Gems::VERSIONS[g]
+ end
+
+ s.files = Dir.glob("{bin,lib}/**/*") + %w(LICENSE README.md)
+ s.executables = Dir.glob("{bin}/*").collect { |f| File.basename(f) }
+ s.require_path = 'lib'
+end
View
@@ -1 +1 @@
-require File.dirname(__FILE__) + "/rails/init"
+require File.dirname(__FILE__) + "/rails/init"
View
@@ -1,5 +1,19 @@
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.lib!
+require File.dirname(__FILE__) + '/also_migrate/gems'
+
+AlsoMigrate::Gems.require(:lib)
+
+$:.unshift File.dirname(__FILE__) + '/also_migrate'
+
+require 'base'
+require 'migration'
+require 'migrator'
+require 'version'
+
+module AlsoMigrate
+ class <<self
+ attr_accessor :classes
+ end
+end
ActiveRecord::Base.send(:include, AlsoMigrate::Base)
ActiveRecord::Migrator.send(:include, AlsoMigrate::Migrator)
View
@@ -24,6 +24,8 @@ class <<self
attr_accessor :also_migrate_config
end
end
+ ::AlsoMigrate.classes ||= []
+ ::AlsoMigrate.classes << self
end
end
end
View
@@ -0,0 +1,44 @@
+unless defined?(AlsoMigrate::Gems)
+
+ require 'rubygems'
+
+ module AlsoMigrate
+ class Gems
+
+ VERSIONS = {
+ :active_wrapper => '=0.3.4',
+ :rake => '=0.8.7',
+ :rspec => '=1.3.1'
+ }
+
+ TYPES = {
+ :gemspec => [],
+ :gemspec_dev => [ :rspec ],
+ :lib => [],
+ :rake => [ :rake, :rspec ],
+ :spec => [ :active_wrapper, :rspec ],
+ :spec_rake => [ :active_wrapper ]
+ }
+
+ class <<self
+
+ def lockfile
+ file = File.expand_path('../../../gems', __FILE__)
+ unless File.exists?(file)
+ File.open(file, 'w') do |f|
+ Gem.loaded_specs.each do |key, value|
+ f.puts "#{key} #{value.version.version}"
+ end
+ end
+ end
+ end
+
+ def require(type=nil)
+ (TYPES[type] || TYPES.values.flatten.compact).each do |name|
+ gem name.to_s, VERSIONS[name]
+ end
+ end
+ end
+ end
+ end
+end
@@ -33,28 +33,29 @@ def method_missing_with_also_migrate(method, *arguments, &block)
table_name = ActiveRecord::Migrator.proper_table_name(args[0])
# Find models
- Object.subclasses_of(ActiveRecord::Base).each do |klass|
- if klass.respond_to?(:also_migrate_config)
- next unless klass.table_name == table_name && !klass.also_migrate_config.nil?
- klass.also_migrate_config.each do |config|
- options = config[:options]
- tables = config[:tables]
-
- # Don't change ignored columns
- options[:ignore].each do |column|
- next if args.include?(column) || args.include?(column.intern)
- end
+ if ::AlsoMigrate.classes
+ ::AlsoMigrate.classes.uniq.each do |klass|
+ if klass.also_migrate_config
+ klass.also_migrate_config.each do |config|
+ options = config[:options]
+ tables = config[:tables]
+
+ # Don't change ignored columns
+ options[:ignore].each do |column|
+ next if args.include?(column) || args.include?(column.intern)
+ end
- # Run migration
- config[:tables].each do |table|
- if method == :create_table
- ActiveRecord::Migrator::AlsoMigrate.create_tables(klass)
- elsif method == :add_index && !options[:indexes].nil?
- next
- elsif connection.table_exists?(table)
- args[0] = table
- args[1] = table if method == :rename_table
- connection.send(method, *args, &block)
+ # Run migration
+ config[:tables].each do |table|
+ if method == :create_table
+ ActiveRecord::Migrator::AlsoMigrate.create_tables(klass)
+ elsif method == :add_index && !options[:indexes].nil?
+ next
+ elsif connection.table_exists?(table)
+ args[0] = table
+ args[1] = table if method == :rename_table
+ connection.send(method, *args, &block)
+ end
end
end
end
@@ -14,9 +14,11 @@ def self.included(base)
module InstanceMethods
def migrate_with_also_migrate
- Object.subclasses_of(ActiveRecord::Base).each do |klass|
- if klass.respond_to?(:also_migrate_config)
- AlsoMigrate.create_tables(klass)
+ if ::AlsoMigrate.classes
+ ::AlsoMigrate.classes.uniq.each do |klass|
+ if klass.respond_to?(:also_migrate_config)
+ AlsoMigrate.create_tables(klass)
+ end
end
end
rescue Exception => e
@@ -74,6 +76,45 @@ def create_tables(klass)
end
end
end
+
+ def indexed_columns(table_name)
+ # MySQL
+ if connection.class.to_s.include?('Mysql')
+ index_query = "SHOW INDEX FROM #{table_name}"
+ connection.select_all(index_query).collect do |r|
+ r["Column_name"]
+ end
+ # PostgreSQL
+ # http://stackoverflow.com/questions/2204058/show-which-columns-an-index-is-on-in-postgresql/2213199
+ elsif connection.class.to_s.include?('PostgreSQL')
+ index_query = <<-SQL
+ select
+ t.relname as table_name,
+ i.relname as index_name,
+ a.attname as column_name
+ from
+ pg_class t,
+ pg_class i,
+ pg_index ix,
+ pg_attribute a
+ where
+ t.oid = ix.indrelid
+ and i.oid = ix.indexrelid
+ and a.attrelid = t.oid
+ and a.attnum = ANY(ix.indkey)
+ and t.relkind = 'r'
+ and t.relname = '#{table_name}'
+ order by
+ t.relname,
+ i.relname
+ SQL
+ connection.select_all(index_query).collect do |r|
+ r["column_name"]
+ end
+ else
+ raise 'AlsoMigrate does not support this database adapter'
+ end
+ end
end
end
end
@@ -0,0 +1,3 @@
+module AlsoMigrate
+ VERSION = "0.1.1" unless defined?(::AlsoMigrate::VERSION)
+end
View
@@ -1,6 +0,0 @@
- SQL (0.2ms) SET SQL_AUTO_IS_NULL=0
- User Load (59.6ms) SELECT * FROM `users` ORDER BY users.id DESC LIMIT 1
- User Columns (21.6ms) SHOW FIELDS FROM `users`
- SQL (0.3ms) SET SQL_AUTO_IS_NULL=0
- SQL (0.3ms) SHOW TABLES
- User Columns (13.0ms) SHOW FIELDS FROM `users`
View
@@ -1,4 +1,3 @@
-require File.expand_path("#{File.dirname(__FILE__)}/../require")
-Require.rails_init!
+require File.expand_path(File.dirname(__FILE__) + '/../lib/also_migrate')
$rails_rake_task = false # hack hack, hackety hack, f my life, hackety hack
Oops, something went wrong.

0 comments on commit c32690c

Please sign in to comment.