active_record_schema 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -366,40 +366,12 @@ rails g migration:from post --add title description
366
366
  create db/migrate/20120801104036_add_title_and_description_to_posts.rb
367
367
  ```
368
368
 
369
- # rails g migration:from post --add name address
370
-
371
-
372
369
  ```
373
370
  Usage:
374
371
  rails generate migration:from [model model] [options]
375
372
 
376
373
  Options:
377
374
  [--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
-
387
- ### `rails g model`
388
-
389
- ```
390
- [--inheritable] # Add 'inheritable' to the generated model
391
- [--timestamps] # Add 'timestamps' to the generated model
392
- [--scope=SCOPE] # The subpath of app/models in which model file will be created
393
- [--parent=PARENT] # The parent class for the generated model
394
- -t, [--test-framework=NAME] # Test framework to be invoked
395
- # Default: test_unit
396
- ```
397
-
398
- ### `rails g migration`
399
-
400
- ```
401
- [--from=FROM] # calculates the changes to be applied on model table from the schema defined inside the model itself
402
- [--id=N] # The id to be used in this migration
403
375
  ```
404
376
 
405
377
  ## Why do not also generate irreversible changes (change/remove columns or indexes)?
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.5.1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_record_schema"
8
- s.version = "0.5.0"
8
+ s.version = "0.5.1"
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-08-01"
12
+ s.date = "2012-08-05"
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 = [
@@ -33,15 +33,9 @@ Gem::Specification.new do |s|
33
33
  "lib/active_record_schema/railtie.rb",
34
34
  "lib/active_record_schema/schema.rb",
35
35
  "lib/active_record_schema/schema_diff.rb",
36
- "lib/generators/active_record_schema.rb",
37
- "lib/generators/active_record_schema/migration/migration_generator.rb",
38
- "lib/generators/active_record_schema/migration/templates/migration.rb",
39
- "lib/generators/active_record_schema/migration/templates/migration_from_model.rb.erb",
40
- "lib/generators/active_record_schema/model/model_generator.rb",
41
- "lib/generators/active_record_schema/model/templates/model.rb",
42
- "lib/generators/active_record_schema/model/templates/module.rb",
43
36
  "lib/generators/migration/from_generator.rb",
44
- "lib/generators/migration/templates/migration_from_model.rb.erb"
37
+ "lib/generators/migration/templates/migration_from_model.rb.erb",
38
+ "lib/generators/model_generator.rb"
45
39
  ]
46
40
  s.homepage = "http://github.com/mcasimir/active_record_schema"
47
41
  s.licenses = ["MIT"]
@@ -0,0 +1,25 @@
1
+ class ModelGenerator < ::Rails::Generators::NamedBase
2
+
3
+ def create_model_file
4
+ create_file "app/models/#{file_name}.rb" do
5
+ <<-eos
6
+ class #{class_name} < ActiveRecord::Base
7
+
8
+ end
9
+
10
+ eos
11
+
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def class_name
18
+ name.camelize
19
+ end
20
+
21
+ def file_name
22
+ name.underscore
23
+ end
24
+
25
+ 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.5.0
4
+ version: 0.5.1
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-08-01 00:00:00.000000000 Z
12
+ date: 2012-08-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -69,15 +69,9 @@ files:
69
69
  - lib/active_record_schema/railtie.rb
70
70
  - lib/active_record_schema/schema.rb
71
71
  - lib/active_record_schema/schema_diff.rb
72
- - lib/generators/active_record_schema.rb
73
- - lib/generators/active_record_schema/migration/migration_generator.rb
74
- - lib/generators/active_record_schema/migration/templates/migration.rb
75
- - lib/generators/active_record_schema/migration/templates/migration_from_model.rb.erb
76
- - lib/generators/active_record_schema/model/model_generator.rb
77
- - lib/generators/active_record_schema/model/templates/model.rb
78
- - lib/generators/active_record_schema/model/templates/module.rb
79
72
  - lib/generators/migration/from_generator.rb
80
73
  - lib/generators/migration/templates/migration_from_model.rb.erb
74
+ - lib/generators/model_generator.rb
81
75
  homepage: http://github.com/mcasimir/active_record_schema
82
76
  licenses:
83
77
  - MIT
@@ -93,7 +87,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
87
  version: '0'
94
88
  segments:
95
89
  - 0
96
- hash: -903927516269149712
90
+ hash: 1202354396919684106
97
91
  required_rubygems_version: !ruby/object:Gem::Requirement
98
92
  none: false
99
93
  requirements:
@@ -1,48 +0,0 @@
1
- require 'generators/active_record_schema'
2
-
3
- module ActiveRecordSchema
4
- module Generators
5
- class MigrationGenerator < Base
6
- source_root File.expand_path('../templates', __FILE__)
7
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
- class_option :from, :type => :string, :desc => "calculates the changes to be applied on model table from the schema defined inside the model itself"
9
- class_option :id, :type => :numeric, :desc => "The id to be used in this migration"
10
-
11
- def create_migration_file
12
- set_local_assigns!
13
- if options[:from]
14
- # preload every model
15
- ActiveRecordSchema.autoload_paths.each do |p|
16
- load(p)
17
- end
18
- migration_template "migration_from_model.rb.erb", "db/migrate/#{file_name}.rb"
19
- else
20
- migration_template "migration.rb", "db/migrate/#{file_name}.rb"
21
- end
22
- end
23
-
24
- protected
25
-
26
-
27
- def model
28
- @model ||= if !!options[:from]
29
- options[:from].constantize
30
- else
31
- false
32
- end
33
- end
34
-
35
- attr_reader :migration_action
36
-
37
- def set_local_assigns!
38
- if file_name =~ /^(add|remove|drop)_.*_(?:to|from)_(.*)/
39
- @migration_action = $1 == 'add' ? 'add' : 'drop'
40
- @table_name = $2.pluralize
41
- end
42
- end
43
-
44
-
45
- end
46
- end
47
- end
48
-
@@ -1,34 +0,0 @@
1
- class <%= migration_class_name %> < ActiveRecord::Migration
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
- <%- if attribute.has_index? && migration_action == 'remove' -%>
28
- add_index :<%= table_name %>, :<%= attribute.index_name %><%= attribute.inject_index_options %>
29
- <%- end -%>
30
- <%- end -%>
31
- <%- end -%>
32
- end
33
- <%- end -%>
34
- end
@@ -1,26 +0,0 @@
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
@@ -1,49 +0,0 @@
1
- require 'generators/active_record_schema'
2
-
3
- module ActiveRecordSchema
4
- module Generators
5
- class ModelGenerator < Base
6
-
7
- argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]"
8
-
9
- check_class_collision
10
-
11
- class_option :inheritable, :type => :boolean, :desc => "Add 'inheritable' to the generated model"
12
- class_option :timestamps, :type => :boolean, :desc => "Add 'timestamps' to the generated model"
13
- class_option :scope, :type => :string, :desc => "The subpath of app/models in which model file will be created"
14
- class_option :parent, :type => :string, :desc => "The parent class for the generated model"
15
-
16
- def create_model_file
17
- template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb")
18
- end
19
-
20
- def create_module_file
21
- return if regular_class_path.empty?
22
- scope_path = ["app", "models", subdir].compact.join('/')
23
- template 'module.rb', File.join(scope_path, "#{class_path.join('/')}.rb") if behavior == :invoke
24
- end
25
-
26
- def attributes_with_index
27
- attributes.select { |a| a.has_index? || (a.reference? && options[:indexes]) }
28
- end
29
-
30
- def accessible_attributes
31
- attributes.reject(&:reference?)
32
- end
33
-
34
- hook_for :test_framework
35
-
36
- protected
37
-
38
- def subdir
39
- in_opt = "#{options[:in]}".strip
40
- in_opt.empty? || in_opt.match(/\//) ? nil : in_opt
41
- end
42
-
43
- def parent_class_name
44
- options[:parent] || "ActiveRecord::Base"
45
- end
46
-
47
- end
48
- end
49
- end
@@ -1,26 +0,0 @@
1
- <% module_namespacing do -%>
2
- class <%= class_name %> < <%= parent_class_name.classify %>
3
- <% if options[:inheritable]-%>
4
- inheritable
5
- <% end %>
6
- <% attributes.each do |attribute| -%>
7
- field :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
8
- <% end -%>
9
- <% if options[:timestamps] %>
10
- timestamps
11
- <% end -%>
12
- <% attributes.select {|attr| attr.reference? }.each do |attribute| -%>
13
- belongs_to :<%= attribute.name %>
14
- <% end -%>
15
- <% if !accessible_attributes.empty? -%>
16
- attr_accessible <%= accessible_attributes.map {|a| ":#{a.name}" }.sort.join(', ') %>
17
- <% else -%>
18
- # attr_accessible :title, :body
19
- <% end -%>
20
- end
21
- <% end -%>
22
-
23
-
24
-
25
-
26
-
@@ -1,7 +0,0 @@
1
- <% module_namespacing do -%>
2
- module <%= class_path.map(&:camelize).join('::') %>
3
- def self.table_name_prefix
4
- '<%= class_path.join('_') %>_'
5
- end
6
- end
7
- <% end -%>
@@ -1,22 +0,0 @@
1
- require 'rails/generators/named_base'
2
- require 'rails/generators/migration'
3
- require 'rails/generators/active_model'
4
-
5
- module ActiveRecordSchema
6
- module Generators
7
- class Base < ::Rails::Generators::NamedBase
8
-
9
- include Rails::Generators::Migration
10
-
11
- def self.base_root
12
- File.dirname(__FILE__)
13
- end
14
-
15
- def self.next_migration_number(dirname) #:nodoc:
16
- next_migration_number = current_migration_number(dirname) + 1
17
- ActiveRecord::Migration.next_migration_number(next_migration_number)
18
- end
19
-
20
- end
21
- end
22
- end