active_record_schema 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  **ActiveRecordSchema** is an `ActiveRecord` extension that allows you to define fields for a model within the model itself and to generate migrations directly from models.
4
4
 
5
- Unlike other libraries (eg. mini_record) ActiveRecordSchema is not an alternative to Rails migrations, but rather a tool to simplify their use.
5
+ Unlike other libraries ActiveRecordSchema is not an alternative to Rails migrations, but rather a tool to simplify their use.
6
6
 
7
7
  _ex._
8
8
 
@@ -21,7 +21,8 @@ end
21
21
  ```
22
22
 
23
23
  ```
24
- rails g migration create_posts --from Post
24
+ rails g migration:from post
25
+ create db/migrate/20120801104035_create_posts.rb
25
26
  ```
26
27
 
27
28
  ## Features
@@ -81,7 +82,11 @@ end
81
82
  Now run `rails g migration` with `--from` option
82
83
 
83
84
  rails g migration init_posts_schema --from Post
84
-
85
+
86
+ or just
87
+
88
+ rails g migration:from post
89
+
85
90
  and the following migration will be generated
86
91
 
87
92
  ``` rb
@@ -115,6 +120,10 @@ Now run
115
120
 
116
121
  rails g migration add_pubdate_to_posts --from Post
117
122
 
123
+ or just
124
+
125
+ rails g migration:from post --add pubdate
126
+
118
127
  that will generate:
119
128
 
120
129
  ``` rb
@@ -334,6 +343,47 @@ end
334
343
 
335
344
  ## Generators
336
345
 
346
+ ### `rails g migration:from`
347
+
348
+ Generates one or more migration from models
349
+
350
+ *eg.*
351
+
352
+ ```
353
+ rails g migration:from link setting user menu photo photogallery
354
+ create db/migrate/20120801104031_create_links.rb
355
+ create db/migrate/20120801104032_create_settings.rb
356
+ create db/migrate/20120801104033_create_users.rb
357
+ create db/migrate/20120801104034_create_menus.rb
358
+ create db/migrate/20120801104035_create_photos.rb
359
+ create db/migrate/20120801104036_create_photogalleries.rb
360
+ ```
361
+
362
+ You can also generate a migration to add columns setting its name in a handy way *eg.*
363
+
364
+ ``` sh
365
+ rails g migration:from post --add title description
366
+ create db/migrate/20120801104036_add_title_and_description_to_posts.rb
367
+ ```
368
+
369
+ # rails g migration:from post --add name address
370
+
371
+
372
+ ```
373
+ Usage:
374
+ rails generate migration:from [model model] [options]
375
+
376
+ Options:
377
+ [--add=attrname attrname] # Indicates when to generate add
378
+
379
+ Runtime options:
380
+ -f, [--force] # Overwrite files that already exist
381
+ -p, [--pretend] # Run but do not make any changes
382
+ -q, [--quiet] # Suppress status output
383
+ -s, [--skip] # Skip files that already exist
384
+
385
+ ```
386
+
337
387
  ### `rails g model`
338
388
 
339
389
  ```
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.5.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_record_schema"
8
- s.version = "0.4.0"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mcasimir"]
12
- s.date = "2012-05-18"
12
+ s.date = "2012-08-01"
13
13
  s.description = "ActiveRecordSchema is an ActiveRecord extension that allows you to write the database schema for a model within the model itself and to generate migrations directly from models."
14
14
  s.email = "maurizio.cas@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -39,7 +39,9 @@ Gem::Specification.new do |s|
39
39
  "lib/generators/active_record_schema/migration/templates/migration_from_model.rb.erb",
40
40
  "lib/generators/active_record_schema/model/model_generator.rb",
41
41
  "lib/generators/active_record_schema/model/templates/model.rb",
42
- "lib/generators/active_record_schema/model/templates/module.rb"
42
+ "lib/generators/active_record_schema/model/templates/module.rb",
43
+ "lib/generators/migration/from_generator.rb",
44
+ "lib/generators/migration/templates/migration_from_model.rb.erb"
43
45
  ]
44
46
  s.homepage = "http://github.com/mcasimir/active_record_schema"
45
47
  s.licenses = ["MIT"]
@@ -0,0 +1,59 @@
1
+ require "rails/generators/migration"
2
+ require "active_record/migration"
3
+
4
+ # rails g migration:from post link blog photo
5
+ # rails g migration:from post --add name address
6
+
7
+ module Migration
8
+ class FromGenerator < Rails::Generators::Base
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ include Rails::Generators::Migration
12
+ class << self
13
+ def next_migration_number(dirname) #:nodoc:
14
+ next_migration_number = current_migration_number(dirname) + 1
15
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
16
+ end
17
+ end
18
+
19
+
20
+ argument :model_names, :type => :array, :default => [], :banner => "model model"
21
+ class_option :add, :type => :array, :default => [], :banner => "attrname attrname"
22
+
23
+ def preload_models
24
+ ActiveRecordSchema.autoload_paths.each do |p|
25
+ load(p)
26
+ end
27
+
28
+ end
29
+
30
+ def create_migrations
31
+ models.each do |current_model|
32
+ @current_model = current_model
33
+ migration_file_name = "#{migration_prefix}_#{current_model.name.underscore.gsub('::', '_').pluralize}"
34
+ migration_template "migration_from_model.rb.erb", "db/migrate/#{migration_file_name}.rb"
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def model
41
+ @current_model
42
+ end
43
+
44
+ def models
45
+ @models ||= model_names.map {|name|
46
+ name.camelize.constantize
47
+ }
48
+ end
49
+
50
+ def migration_prefix
51
+ if options[:add].any?
52
+ "add_#{options[:add].join('_and_')}_to"
53
+ else
54
+ "create"
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def change
3
+ <%- if !model.schema.table_exists? -%>
4
+ create_table :<%= model.schema.prefixed_table_name %>
5
+
6
+ <%- end -%>
7
+ <%- model.schema.diff(:fields, :add).each do |field| -%>
8
+ add_column :<%= model.schema.prefixed_table_name %>, <%= field.name.inspect %>, <%= field.type.inspect %><%= ", #{field.options.inspect}" if !field.options.blank? %>
9
+ <%- end -%>
10
+
11
+ <%- model.schema.diff(:indexes, :add).each do |index| -%>
12
+ add_index :<%= model.schema.prefixed_table_name %>, <%= index.name.inspect %><%= ", #{index.options.inspect}" if !index.options.blank? %>
13
+ <%- end -%>
14
+
15
+ <%- model.schema.diff(:joins, :add).each do |join| -%>
16
+ create_table :<%= join.table %>, :id => false do |t|
17
+ t.integer <%= join.key1.inspect %>
18
+ t.integer <%= join.key2.inspect %>
19
+ end
20
+ <%- if join.index -%>
21
+ add_index :<%= join.table %>, <%= join.key1.inspect %>
22
+ add_index :<%= join.table %>, <%= join.key2.inspect %>
23
+ <%- end -%>
24
+ <%- end -%>
25
+ end
26
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-18 00:00:00.000000000 Z
12
+ date: 2012-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -76,6 +76,8 @@ files:
76
76
  - lib/generators/active_record_schema/model/model_generator.rb
77
77
  - lib/generators/active_record_schema/model/templates/model.rb
78
78
  - lib/generators/active_record_schema/model/templates/module.rb
79
+ - lib/generators/migration/from_generator.rb
80
+ - lib/generators/migration/templates/migration_from_model.rb.erb
79
81
  homepage: http://github.com/mcasimir/active_record_schema
80
82
  licenses:
81
83
  - MIT
@@ -91,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
91
93
  version: '0'
92
94
  segments:
93
95
  - 0
94
- hash: -2966481555422020030
96
+ hash: -903927516269149712
95
97
  required_rubygems_version: !ruby/object:Gem::Requirement
96
98
  none: false
97
99
  requirements: