migratory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 ToastyApps
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.
data/README.textile ADDED
@@ -0,0 +1,29 @@
1
+ h1. Migratory
2
+
3
+ h2. Usage
4
+
5
+ Migratory overrides the default rails model migration and gives you the ability to do the following
6
+
7
+ h3. Specify Default Values
8
+
9
+ script/generate migration AddAdminToUsers _**admin:boolean:false**_
10
+
11
+ script/generate model User username:string password_encrypted:string email:string _**is_admin:boolean:false**_ _**rating:integer:0**_ _**role:string:"member"**_
12
+
13
+ script/generate resource User username:string password_encrypted:string email:string _**is_admin:boolean:false**_ _**rating:integer:0**_ _**role:string:"member"**_
14
+
15
+ h3. Specify Indexes
16
+
17
+ script/generate migration AddAdminToUsers _***admin:boolean:false**_
18
+
19
+ script/generate model User _***username:string**_ password_encrypted:string _***email:string**_ is_admin:boolean:false
20
+
21
+ script/generate resource User _***username:string**_ password_encrypted:string _***email:string**_ is_admin:boolean:false
22
+
23
+ h3. Specify Limit, Precision, and Scale
24
+
25
+ script/generate migration AddRoleToUsers _***role:string<notextile>[10]</notextile>:"member"**_
26
+
27
+ script/generate model Product _**name:string<notextile>[30]</notextile>**_ _**cost:decimal<notextile>[10,2]</notextile>:0.00**_
28
+
29
+ script/generate resource Product _**name:string<notextile>[30]</notextile>**_ _**cost:decimal<notextile>[10,2]</notextile>:0.00**_
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ gem_spec = Gem::Specification.new do |gem_spec|
5
+ gem_spec.name = 'migratory'
6
+ gem_spec.version = '0.1.0'
7
+ gem_spec.summary = 'Rails migration extender for default values and adding indexes'
8
+ gem_spec.description = 'Rails migration extender for default values and adding indexes'
9
+ gem_spec.email = 'matt@toastyapps.com'
10
+ gem_spec.homepage = 'http://github.com/toastyapps/migratory'
11
+ gem_spec.authors = ["Matthew Mongeau"]
12
+ gem_spec.files = FileList["[A-Z]*", "{generators,lib}/**/*"]
13
+ end
14
+
15
+ desc "Generate a gemspec file"
16
+ task :gemspec do
17
+ File.open("#{gem_spec.name}.gemspec", "w") do |f|
18
+ f.write gem_spec.to_yaml
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ Description:
2
+ Stubs out a new database migration. Pass the migration name, either
3
+ CamelCased or under_scored, and an optional list of attribute pairs as arguments.
4
+
5
+ A migration class is generated in db/migrate prefixed by a timestamp of the current date and time.
6
+
7
+ You can name your migration in either of these formats to generate add/remove
8
+ column lines from supplied attributes: AddColumnsToTable or RemoveColumnsFromTable
9
+
10
+ Example:
11
+ `./script/generate migration AddSslFlag`
12
+
13
+ If the current date is May 14, 2008 and the current time 09:09:12, this creates the AddSslFlag migration
14
+ db/migrate/20080514090912_add_ssl_flag.rb
15
+
16
+ `./script/generate migration AddTitleBodyToPost title:string body:text published:boolean`
17
+
18
+ This will create the AddTitleBodyToPost in db/migrate/20080514090912_add_title_body_to_post.rb with
19
+ this in the Up migration:
20
+
21
+ add_column :posts, :title, :string
22
+ add_column :posts, :body, :text
23
+ add_column :posts, :published, :boolean
24
+
25
+ And this in the Down migration:
26
+
27
+ remove_column :posts, :published
28
+ remove_column :posts, :body
29
+ remove_column :posts, :title
@@ -0,0 +1,2 @@
1
+ require 'rails_generator/generators/components/migration/migration_generator'
2
+ require 'migratory/generated_attribute'
@@ -0,0 +1,15 @@
1
+ class <%= class_name.underscore.camelize %> < ActiveRecord::Migration
2
+ def self.up<% attributes.each do |attribute| %>
3
+ <%= migration_action %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'add' %>, :<%= attribute.type %><%= attribute.options %><% end -%>
4
+ <% for attribute in attributes -%>
5
+ <% if attribute.is_indexed %>
6
+ add_index :<%= table_name %>, :<%= attribute.name %><% end -%><% end -%>
7
+ <%- end %>
8
+ end
9
+
10
+ def self.down<% attributes.reverse.each do |attribute| %>
11
+ <%= migration_action == 'add' ? 'remove' : 'add' %>_column :<%= table_name %>, :<%= attribute.name %><% if migration_action == 'remove' %>, :<%= attribute.type %><% end -%>
12
+ <%- end %>
13
+ end
14
+ end
15
+
@@ -0,0 +1,27 @@
1
+ Description:
2
+ Stubs out a new model. Pass the model name, either CamelCased or
3
+ under_scored, and an optional list of attribute pairs as arguments.
4
+
5
+ Attribute pairs are column_name:sql_type arguments specifying the
6
+ model's attributes. Timestamps are added by default, so you don't have to
7
+ specify them by hand as 'created_at:datetime updated_at:datetime'.
8
+
9
+ You don't have to think up every attribute up front, but it helps to
10
+ sketch out a few so you can start working with the model immediately.
11
+
12
+ This generates a model class in app/models, a unit test in test/unit,
13
+ a test fixture in test/fixtures/singular_name.yml, and a migration in
14
+ db/migrate.
15
+
16
+ Examples:
17
+ `./script/generate model account`
18
+
19
+ creates an Account model, test, fixture, and migration:
20
+ Model: app/models/account.rb
21
+ Test: test/unit/account_test.rb
22
+ Fixtures: test/fixtures/accounts.yml
23
+ Migration: db/migrate/XXX_add_accounts.rb
24
+
25
+ `./script/generate model post title:string body:text published:boolean`
26
+
27
+ creates a Post model with a string title, text body, and published flag.
@@ -0,0 +1,2 @@
1
+ require 'rails_generator/generators/components/model/model_generator'
2
+ require 'migratory/generated_attribute'
@@ -0,0 +1,19 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+
3
+ <% unless attributes.empty? -%>
4
+ one:
5
+ <% for attribute in attributes -%>
6
+ <%= attribute.name %>: <%= attribute.default %>
7
+ <% end -%>
8
+
9
+ two:
10
+ <% for attribute in attributes -%>
11
+ <%= attribute.name %>: <%= attribute.default %>
12
+ <% end -%>
13
+ <% else -%>
14
+ # one:
15
+ # column: value
16
+ #
17
+ # two:
18
+ # column: value
19
+ <% end -%>
@@ -0,0 +1,21 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ <% for attribute in attributes -%>
5
+ t.<%= attribute.type %> :<%= attribute.name %><%= attribute.options if attribute.respond_to?(:options) %>
6
+ <% end -%>
7
+ <% unless options[:skip_timestamps] %>
8
+ t.timestamps
9
+ <% end -%>
10
+ end
11
+ <% if attributes[0] && attributes[0].respond_to?(:is_indexed) %>
12
+ <% for attribute in attributes -%><% if attribute.is_indexed -%>
13
+ add_index :<%= table_name %>, :<%= attribute.name %>
14
+ <% end -%><% end -%>
15
+ <% end %>
16
+ end
17
+
18
+ def self.down
19
+ drop_table :<%= table_name %>
20
+ end
21
+ end
@@ -0,0 +1,5 @@
1
+ class <%= class_name %> < ActiveRecord::Base
2
+ <% attributes.select(&:reference?).each do |attribute| -%>
3
+ belongs_to :<%= attribute.name %>
4
+ <% end -%>
5
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class <%= class_name %>Test < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,44 @@
1
+ module Rails
2
+ module Generator
3
+ class GeneratedAttribute
4
+ attr_accessor :default, :is_indexed, :limit, :precision, :scale
5
+
6
+ def initialize_with_migratory(name, type, default = nil)
7
+ @default = default
8
+ @is_indexed = (name =~ /^\*/) && true
9
+ if type =~ /\[.*?\]/
10
+ details = type.match(/\[(.*?)\]/)[1]
11
+ if details =~ /,/
12
+ @precision, @scale = details.split(/,/).map(&:to_i)
13
+ else
14
+ case type.gsub(/\[.*?\]/, '').to_sym
15
+ when :string, :text, :binary, :integer then @limit = details.to_i
16
+ when :decimal, :float then @precision = details.to_i
17
+ end
18
+ end
19
+ end
20
+ initialize_without_migratory(name.gsub(/^\*/, ''), type.gsub(/\[.*?\]/, ''))
21
+ end
22
+
23
+ def options
24
+ options = ''
25
+ options << ", :limit => #{@limit}" if @limit
26
+ options << ", :precision => #{@precision}" if @precision
27
+ options << ", :scale => #{@scale}" if @scale
28
+ if @default
29
+ if @default == ''
30
+ options << ", :default => nil"
31
+ else
32
+ options << case @type
33
+ when :string, :text, :date, :datetime, :time, :timestamp then ', :default => "'+@default+'"'
34
+ else ", :default => #{@default}"
35
+ end
36
+ end
37
+ end
38
+ options
39
+ end
40
+
41
+ alias_method_chain :initialize, :migratory
42
+ end
43
+ end
44
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: migratory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Mongeau
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-30 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Rails migration extender for default values and adding indexes
17
+ email: matt@toastyapps.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - MIT-LICENSE
26
+ - Rakefile
27
+ - README.textile
28
+ - generators/migration/migration_generator.rb
29
+ - generators/migration/templates/migration.rb
30
+ - generators/migration/USAGE
31
+ - generators/model/model_generator.rb
32
+ - generators/model/templates/fixtures.yml
33
+ - generators/model/templates/migration.rb
34
+ - generators/model/templates/model.rb
35
+ - generators/model/templates/unit_test.rb
36
+ - generators/model/USAGE
37
+ - lib/migratory/generated_attribute.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/toastyapps/migratory
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options: []
44
+
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.4
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Rails migration extender for default values and adding indexes
66
+ test_files: []
67
+