active_record_schema 0.3.5 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,9 +1,29 @@
1
1
  # ActiveRecordSchema
2
2
 
3
- **ActiveRecordSchema** is an `ActiveRecord` extension that allows you to write database schema for a model within the model itself and to generate migrations directly from models.
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
5
  Unlike other libraries (eg. mini_record) ActiveRecordSchema is not an alternative to Rails migrations, but rather a tool to simplify their use.
6
6
 
7
+ _ex._
8
+
9
+ ```
10
+ rails g model Post title:string body:text --timestamps
11
+ ```
12
+
13
+ ``` rb
14
+ class Post < ActiveRecord::Base
15
+ field :title, :string
16
+ field :body, :text
17
+
18
+ attr_accessible :date, :name
19
+ timestamps
20
+ end
21
+ ```
22
+
23
+ ```
24
+ rails g migration create_posts --from Post
25
+ ```
26
+
7
27
  ## Features
8
28
 
9
29
  * Defining columns and indexes directly in model
@@ -196,7 +216,7 @@ class InitContents < ActiveRecord::Migration
196
216
  def change
197
217
  add_column :contents, :type, :string
198
218
  add_column :contents, :title, :string
199
- add_column :contents, :author_id, :integer
219
+ add_column :contents, :author_id, :string
200
220
  add_column :contents, :body, :text
201
221
  add_column :contents, :url, :string
202
222
 
@@ -312,6 +332,26 @@ end
312
332
  ```
313
333
 
314
334
 
335
+ ## Generators
336
+
337
+ ### `rails g model`
338
+
339
+ ```
340
+ [--inheritable] # Add 'inheritable' to the generated model
341
+ [--timestamps] # Add 'timestamps' to the generated model
342
+ [--scope=SCOPE] # The subpath of app/models in which model file will be created
343
+ [--parent=PARENT] # The parent class for the generated model
344
+ -t, [--test-framework=NAME] # Test framework to be invoked
345
+ # Default: test_unit
346
+ ```
347
+
348
+ ### `rails g migration`
349
+
350
+ ```
351
+ [--from=FROM] # calculates the changes to be applied on model table from the schema defined inside the model itself
352
+ [--id=N] # The id to be used in this migration
353
+ ```
354
+
315
355
  ## Why do not also generate irreversible changes (change/remove columns or indexes)?
316
356
 
317
357
  ActiveRecordSchema does not take into account the removal of columns and indexes or changes in the types of columns. The reason for this is that these changes are not reversible, so it's a better idea to introduce them by hand rather than let them be generated automatically. Anyway the need to resort to harsh measures such as irreversible changes is limited to non-routine situations.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.4.0
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{active_record_schema}
8
- s.version = "0.3.5"
7
+ s.name = "active_record_schema"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{mcasimir}]
12
- s.date = %q{2012-05-11}
13
- s.description = %q{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
- s.email = %q{maurizio.cas@gmail.com}
11
+ s.authors = ["mcasimir"]
12
+ s.date = "2012-05-18"
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
+ s.email = "maurizio.cas@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.md"
@@ -37,13 +37,15 @@ Gem::Specification.new do |s|
37
37
  "lib/generators/active_record_schema/migration/migration_generator.rb",
38
38
  "lib/generators/active_record_schema/migration/templates/migration.rb",
39
39
  "lib/generators/active_record_schema/migration/templates/migration_from_model.rb.erb",
40
- "lib/generators/active_record_schema/model/model_generator.rb"
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"
41
43
  ]
42
- s.homepage = %q{http://github.com/mcasimir/active_record_schema}
43
- s.licenses = [%q{MIT}]
44
- s.require_paths = [%q{lib}]
45
- s.rubygems_version = %q{1.8.5}
46
- s.summary = %q{ActiveRecord extension allowing to write schema in models and to generate migrations from models}
44
+ s.homepage = "http://github.com/mcasimir/active_record_schema"
45
+ s.licenses = ["MIT"]
46
+ s.require_paths = ["lib"]
47
+ s.rubygems_version = "1.8.24"
48
+ s.summary = "ActiveRecord extension allowing to write schema in models and to generate migrations from models"
47
49
 
48
50
  if s.respond_to? :specification_version then
49
51
  s.specification_version = 3
@@ -33,7 +33,7 @@ module ActiveRecordSchema
33
33
  skip_index = options.delete(:index) == false
34
34
 
35
35
  foreign_key = options[:foreign_key] || "#{name}_id"
36
- field :"#{foreign_key}", :integer
36
+ field :"#{foreign_key}"
37
37
 
38
38
  if options[:polymorphic]
39
39
  foreign_type = options[:foreign_type] || "#{name}_type"
@@ -15,6 +15,10 @@ module ActiveRecordSchema
15
15
  _table_exists?
16
16
  end
17
17
 
18
+ def prefixed_table_name
19
+ _table
20
+ end
21
+
18
22
  def _diff_fields_add
19
23
  model.schema.fields.values.delete_if {|field| _column_names.include?(field.name.to_s) }
20
24
  end
@@ -32,6 +36,7 @@ module ActiveRecordSchema
32
36
  end
33
37
 
34
38
  def _table
39
+ # prefix = model.parents.find {|p| p.respond_to?(:table_name_prefix)}.try(:table_name_prefix)
35
40
  model.table_name
36
41
  end
37
42
 
@@ -15,11 +15,7 @@ module ActiveRecordSchema
15
15
  ActiveRecordSchema.autoload_paths.each do |p|
16
16
  load(p)
17
17
  end
18
- if model.schema.nothing_to_do?
19
- say "Nothing to do for '#{options[:from]}'"
20
- else
21
- migration_template "migration_from_model.rb.erb", "db/migrate/#{file_name}.rb"
22
- end
18
+ migration_template "migration_from_model.rb.erb", "db/migrate/#{file_name}.rb"
23
19
  else
24
20
  migration_template "migration.rb", "db/migrate/#{file_name}.rb"
25
21
  end
@@ -1,15 +1,15 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration
2
2
  def change
3
3
  <%- if !model.schema.table_exists? -%>
4
- create_table :<%= model.table_name %>
4
+ create_table :<%= model.schema.prefixed_table_name %>
5
5
 
6
6
  <%- end -%>
7
7
  <%- model.schema.diff(:fields, :add).each do |field| -%>
8
- add_column :<%= model.table_name %>, <%= field.name.inspect %>, <%= field.type.inspect %><%= ", #{field.options.inspect}" if !field.options.blank? %>
8
+ add_column :<%= model.schema.prefixed_table_name %>, <%= field.name.inspect %>, <%= field.type.inspect %><%= ", #{field.options.inspect}" if !field.options.blank? %>
9
9
  <%- end -%>
10
10
 
11
11
  <%- model.schema.diff(:indexes, :add).each do |index| -%>
12
- add_index :<%= model.table_name %>, <%= index.name.inspect %><%= ", #{index.options.inspect}" if !index.options.blank? %>
12
+ add_index :<%= model.schema.prefixed_table_name %>, <%= index.name.inspect %><%= ", #{index.options.inspect}" if !index.options.blank? %>
13
13
  <%- end -%>
14
14
 
15
15
  <%- model.schema.diff(:joins, :add).each do |join| -%>
@@ -3,30 +3,47 @@ require 'generators/active_record_schema'
3
3
  module ActiveRecordSchema
4
4
  module Generators
5
5
  class ModelGenerator < Base
6
- class_option :in, :type => :string
7
- class_option :inherit, :type => :string, :default => 'ActiveRecord::Base'
8
- class_option :from
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"
9
15
 
10
16
  def create_model_file
11
- create_file ["app", "models", subdir, "#{file_name}.rb"].compact.join('/'), <<-FILE
12
- class #{class_name} < #{ inherit }
13
- end
14
- 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]) }
15
28
  end
16
-
17
- private
29
+
30
+ def accessible_attributes
31
+ attributes.reject(&:reference?)
32
+ end
33
+
34
+ hook_for :test_framework
35
+
36
+ protected
18
37
 
19
38
  def subdir
20
39
  in_opt = "#{options[:in]}".strip
21
40
  in_opt.empty? || in_opt.match(/\//) ? nil : in_opt
22
41
  end
23
-
24
- def inherit
25
- options[:inherit]
42
+
43
+ def parent_class_name
44
+ options[:parent] || "ActiveRecord::Base"
26
45
  end
27
-
28
-
46
+
29
47
  end
30
48
  end
31
- end
32
-
49
+ end
@@ -0,0 +1,26 @@
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
+
@@ -0,0 +1,7 @@
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 -%>
metadata CHANGED
@@ -1,49 +1,58 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: active_record_schema
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
4
5
  prerelease:
5
- version: 0.3.5
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - mcasimir
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2012-05-11 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2012-05-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rails
17
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
- requirements:
18
+ requirements:
20
19
  - - ~>
21
- - !ruby/object:Gem::Version
22
- version: "3.0"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
27
31
  name: jeweler
28
- requirement: &id002 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
29
33
  none: false
30
- requirements:
34
+ requirements:
31
35
  - - ~>
32
- - !ruby/object:Gem::Version
36
+ - !ruby/object:Gem::Version
33
37
  version: 1.8.3
34
38
  type: :development
35
39
  prerelease: false
36
- version_requirements: *id002
37
- 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.
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.3
46
+ description: ActiveRecordSchema is an ActiveRecord extension that allows you to write
47
+ the database schema for a model within the model itself and to generate migrations
48
+ directly from models.
38
49
  email: maurizio.cas@gmail.com
39
50
  executables: []
40
-
41
51
  extensions: []
42
-
43
- extra_rdoc_files:
52
+ extra_rdoc_files:
44
53
  - LICENSE.txt
45
54
  - README.md
46
- files:
55
+ files:
47
56
  - Gemfile
48
57
  - Gemfile.lock
49
58
  - LICENSE.txt
@@ -65,35 +74,35 @@ files:
65
74
  - lib/generators/active_record_schema/migration/templates/migration.rb
66
75
  - lib/generators/active_record_schema/migration/templates/migration_from_model.rb.erb
67
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
68
79
  homepage: http://github.com/mcasimir/active_record_schema
69
- licenses:
80
+ licenses:
70
81
  - MIT
71
82
  post_install_message:
72
83
  rdoc_options: []
73
-
74
- require_paths:
84
+ require_paths:
75
85
  - lib
76
- required_ruby_version: !ruby/object:Gem::Requirement
86
+ required_ruby_version: !ruby/object:Gem::Requirement
77
87
  none: false
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- hash: 401825820335667031
82
- segments:
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
83
93
  - 0
84
- version: "0"
85
- required_rubygems_version: !ruby/object:Gem::Requirement
94
+ hash: -2966481555422020030
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
96
  none: false
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: "0"
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
91
101
  requirements: []
92
-
93
102
  rubyforge_project:
94
- rubygems_version: 1.8.5
103
+ rubygems_version: 1.8.24
95
104
  signing_key:
96
105
  specification_version: 3
97
- summary: ActiveRecord extension allowing to write schema in models and to generate migrations from models
106
+ summary: ActiveRecord extension allowing to write schema in models and to generate
107
+ migrations from models
98
108
  test_files: []
99
-