├── init.rb
├── Gemfile
├── .gitignore
├── lib
├── slim_migrations
│ └── version.rb
├── tasks
│ └── slim_migrations_tasks.rake
├── slim_migrations.rb
└── rails
│ └── generators
│ └── active_record
│ └── migration
│ ├── migration_generator.rb
│ └── templates
│ └── migration.rb
├── slim_migrations.gemspec
├── Rakefile
├── MIT-LICENSE
├── README.rdoc
└── Gemfile.lock
/init.rb:
--------------------------------------------------------------------------------
1 | require 'slim_migrations'
2 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "http://rubygems.org"
2 |
3 | gemspec
4 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .bundle/
2 | log/*.log
3 | pkg/
4 | *~
5 | *.swp
6 | *.swo
7 |
--------------------------------------------------------------------------------
/lib/slim_migrations/version.rb:
--------------------------------------------------------------------------------
1 | module SlimMigrations
2 | VERSION = '3.2.1'
3 | end
4 |
--------------------------------------------------------------------------------
/slim_migrations.gemspec:
--------------------------------------------------------------------------------
1 | # -*- encoding: utf-8 -*-
2 | require 'rubygems' unless defined? Gem
3 | require File.dirname(__FILE__) + '/lib/slim_migrations/version'
4 |
5 | Gem::Specification.new do |s|
6 | s.name = 'slim_migrations'
7 | s.summary = 'Let\'s you write even slimmer migrations.'
8 | s.description = 'Let\'s you write even slimmer migrations.'
9 | s.author = 'Jan Lelis'
10 | s.email = 'mail@janlelis.de'
11 | s.homepage = 'https://github.com/janlelis/slim_migrations'
12 | s.files = Dir['lib/**/*'] + ['MIT-LICENSE', 'Rakefile', 'README.rdoc']
13 | s.version = SlimMigrations::VERSION
14 | s.add_dependency 'rails', '~> 3.2.0'
15 | end
16 |
--------------------------------------------------------------------------------
/lib/tasks/slim_migrations_tasks.rake:
--------------------------------------------------------------------------------
1 | namespace :slim_migrations do
2 | desc "Update all current old-style migrations to slim migration syntax"
3 | task :update_syntax do
4 | Dir["db/migrate/[0-9]*_*.rb"].each{ |migration_path|
5 | migration_content = File.read migration_path
6 | if migration_content =~ /^[^#]*class\s+[A-Za-z0-9_]+\s*<\s*ActiveRecord::Migration/
7 | puts "Upgrading: #{migration_path}"
8 | migration_content.gsub! /class\s+[A-Za-z0-9_]+\s*<\s*ActiveRecord::Migration/, 'migration do'
9 | migration_content.gsub! /self\.(up|down)/, '\1'
10 | File.open(migration_path, 'w') do |file| file.write migration_content end
11 | end
12 | }
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/lib/slim_migrations.rb:
--------------------------------------------------------------------------------
1 | require 'slim_migrations/version'
2 |
3 | module SlimMigrations
4 | class Railtie < Rails::Railtie
5 | rake_tasks do
6 | load "tasks/slim_migrations_tasks.rake"
7 | end
8 | end
9 | end
10 |
11 | module Kernel
12 | private
13 |
14 | # initialize migrations with migration do instead of class SomeMigration < ActiveRecord::Migration
15 | def migration(&block)
16 | if caller[0].rindex(/\/(?:[0-9]+)_([_a-z0-9]*).rb:\d+(?::in `.*')?$/)
17 | m = Object.const_set $1.camelize, Class.new(ActiveRecord::Migration)
18 | m.class_eval(&block) # 3.1
19 | else
20 | raise ArgumentError, "Could not create migration at: #{caller[0]}"
21 | end
22 | end
23 | end
24 |
--------------------------------------------------------------------------------
/lib/rails/generators/active_record/migration/migration_generator.rb:
--------------------------------------------------------------------------------
1 | require 'rails/generators/active_record'
2 |
3 | module ActiveRecord
4 | module Generators
5 | class MigrationGenerator < Base
6 | argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
7 |
8 | def create_migration_file
9 | set_local_assigns!
10 | migration_template "migration.rb", "db/migrate/#{file_name}.rb"
11 | end
12 |
13 | protected
14 | attr_reader :migration_action
15 |
16 | def set_local_assigns!
17 | if file_name =~ /^(add|remove)_.*_(?:to|from)_(.*)/
18 | @migration_action = $1
19 | @table_name = $2.pluralize
20 | end
21 | end
22 |
23 | end
24 | end
25 | end
26 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env rake
2 | begin
3 | require 'bundler/setup'
4 | rescue LoadError
5 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6 | end
7 |
8 | # gem stuff
9 | GEMSPEC = 'slim_migrations.gemspec'
10 |
11 | def gemspec
12 | @gemspec ||= eval(File.read(GEMSPEC), binding, GEMSPEC)
13 | end
14 |
15 | desc "Build the gem"
16 | task :gem => :gemspec do
17 | sh "gem build #{GEMSPEC}"
18 | FileUtils.mkdir_p 'pkg'
19 | FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
20 | end
21 |
22 | desc "Install the gem locally"
23 | task :install => :gem do
24 | sh %{gem install pkg/#{gemspec.name}-#{gemspec.version} --no-rdoc --no-ri}
25 | end
26 |
27 | desc "Generate the gemspec"
28 | task :generate do
29 | puts gemspec.to_ruby
30 | end
31 |
32 | desc "Validate the gemspec"
33 | task :gemspec do
34 | gemspec.validate
35 | end
36 |
--------------------------------------------------------------------------------
/MIT-LICENSE:
--------------------------------------------------------------------------------
1 | Copyright 2011-2012 Jan Lelis
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.
21 |
--------------------------------------------------------------------------------
/lib/rails/generators/active_record/migration/templates/migration.rb:
--------------------------------------------------------------------------------
1 | migration do
2 | <%- if migration_action == 'add' -%>
3 | def change
4 | <% attributes.each do |attribute| -%>
5 | add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
6 | <%- if attribute.has_index? -%>
7 | add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
8 | <%- end %>
9 | <%- end -%>
10 | end
11 | <%- else -%>
12 | def up
13 | <% attributes.each do |attribute| -%>
14 | <%- if migration_action -%>
15 | <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
16 | <% if attribute.has_index? && migration_action == 'add' %>
17 | add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
18 | <% end -%>
19 | <%- end -%>
20 | <%- end -%>
21 | end
22 |
23 | def down
24 | <% attributes.reverse.each do |attribute| -%>
25 | <%- if migration_action -%>
26 | <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><%= attribute.inject_options %><% end %>
27 | <%- end -%>
28 | <%- end -%>
29 | end
30 | <%- end -%>
31 | end
32 |
--------------------------------------------------------------------------------
/README.rdoc:
--------------------------------------------------------------------------------
1 | Please note: This project is archived and does not work with recent versions of Rails anymore.
2 |
3 | === SlimMigrations
4 |
5 | Turns
6 |
7 | class AddWebsiteToUser < ActiveRecord::Migration
8 | def self.up
9 | add_column :users, :website, :string
10 | end
11 |
12 | def self.down
13 | remove_column :users, :website
14 | end
15 | end
16 |
17 | into
18 |
19 | migration do
20 | def up
21 | add_column :users, :website, :string
22 | end
23 |
24 | def down
25 | remove_column :users, :website
26 | end
27 | end
28 |
29 | === Extras
30 |
31 | * Modifies the rails generator to use the slim syntax
32 | * Helper task for converting existing migrations: rake slim_migrations:update_syntax
33 |
34 | === Install
35 |
36 | ==== Rails 3.2
37 |
38 | # in your Gemfile
39 | gem 'slim_migrations', '~> 3.2.1'
40 |
41 | ==== Rails 3.1
42 |
43 | # as plugin:
44 | rails plugin install git://github.com/janlelis/slim_migrations.git -r 3.1
45 | # or in your Gemfile:
46 | gem 'slim_migrations', '~> 3.1.1'
47 |
48 | ==== Rails 3.0
49 |
50 | # as plugin:
51 | rails plugin install git://github.com/janlelis/slim_migrations.git -r 3.0
52 | # or in your Gemfile:
53 | gem 'slim_migrations', '~> 3.0.1'
54 |
55 | ==== Rails 2.3
56 |
57 | # as plugin:
58 | script/plugin install git://github.com/janlelis/slim_migrations.git -r 2.3
59 | # or in your config/environment.rb
60 | config.gem 'slim_migrations', :version => '~> 2.3.1'
61 |
62 | === Credits
63 |
64 | Blog post: https://rbjl.janlelis.com/53-three-little-tips-for-slimmer-rails-migrations
65 |
66 | Thanks to contributions from: Manuel Meurer
67 |
68 | MIT-LICENSE, J-_-L
69 |
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | slim_migrations (3.2.1)
5 | rails (~> 3.2.0)
6 |
7 | GEM
8 | remote: http://rubygems.org/
9 | specs:
10 | actionmailer (3.2.1)
11 | actionpack (= 3.2.1)
12 | mail (~> 2.4.0)
13 | actionpack (3.2.1)
14 | activemodel (= 3.2.1)
15 | activesupport (= 3.2.1)
16 | builder (~> 3.0.0)
17 | erubis (~> 2.7.0)
18 | journey (~> 1.0.1)
19 | rack (~> 1.4.0)
20 | rack-cache (~> 1.1)
21 | rack-test (~> 0.6.1)
22 | sprockets (~> 2.1.2)
23 | activemodel (3.2.1)
24 | activesupport (= 3.2.1)
25 | builder (~> 3.0.0)
26 | activerecord (3.2.1)
27 | activemodel (= 3.2.1)
28 | activesupport (= 3.2.1)
29 | arel (~> 3.0.0)
30 | tzinfo (~> 0.3.29)
31 | activeresource (3.2.1)
32 | activemodel (= 3.2.1)
33 | activesupport (= 3.2.1)
34 | activesupport (3.2.1)
35 | i18n (~> 0.6)
36 | multi_json (~> 1.0)
37 | arel (3.0.0)
38 | builder (3.0.0)
39 | erubis (2.7.0)
40 | hike (1.2.1)
41 | i18n (0.6.0)
42 | journey (1.0.1)
43 | json (1.6.5)
44 | mail (2.4.1)
45 | i18n (>= 0.4.0)
46 | mime-types (~> 1.16)
47 | treetop (~> 1.4.8)
48 | mime-types (1.17.2)
49 | multi_json (1.0.4)
50 | polyglot (0.3.3)
51 | rack (1.4.1)
52 | rack-cache (1.1)
53 | rack (>= 0.4)
54 | rack-ssl (1.3.2)
55 | rack
56 | rack-test (0.6.1)
57 | rack (>= 1.0)
58 | rails (3.2.1)
59 | actionmailer (= 3.2.1)
60 | actionpack (= 3.2.1)
61 | activerecord (= 3.2.1)
62 | activeresource (= 3.2.1)
63 | activesupport (= 3.2.1)
64 | bundler (~> 1.0)
65 | railties (= 3.2.1)
66 | railties (3.2.1)
67 | actionpack (= 3.2.1)
68 | activesupport (= 3.2.1)
69 | rack-ssl (~> 1.3.2)
70 | rake (>= 0.8.7)
71 | rdoc (~> 3.4)
72 | thor (~> 0.14.6)
73 | rake (0.9.2.2)
74 | rdoc (3.12)
75 | json (~> 1.4)
76 | sprockets (2.1.2)
77 | hike (~> 1.2)
78 | rack (~> 1.0)
79 | tilt (~> 1.1, != 1.3.0)
80 | thor (0.14.6)
81 | tilt (1.3.3)
82 | treetop (1.4.10)
83 | polyglot
84 | polyglot (>= 0.3.1)
85 | tzinfo (0.3.31)
86 |
87 | PLATFORMS
88 | ruby
89 |
90 | DEPENDENCIES
91 | slim_migrations!
92 |
--------------------------------------------------------------------------------