habtm_generator 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +12 -0
- data/Appraisals +9 -0
- data/Gemfile +3 -0
- data/README.md +4 -2
- data/Rakefile +16 -0
- data/gemfiles/rails_4.0.gemfile +9 -0
- data/gemfiles/rails_4.1.gemfile +9 -0
- data/gemfiles/rails_4.2.gemfile +9 -0
- data/habtm_generator.gemspec +6 -2
- data/lib/generators/habtm/habtm_generator.rb +26 -9
- data/lib/habtm_generator.rb +0 -0
- data/lib/ruby-habtm-generator.rb +1 -0
- data/test.rb +12 -0
- metadata +74 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 420c26808e6ee7e74fbbe012b2417f175e9ebbec
|
4
|
+
data.tar.gz: d3b660d302464614a81e83e8ca01f224874f6c9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 040bc699b38ca84bf5185517c5e4bfc587672f8b1685ec95846564bcb9df3d611d9e056e2f429a70a15ee9bff4d11c204a7be990041543eeb05e5ed89f020bc7
|
7
|
+
data.tar.gz: 03b950881d178c8542db3f1bae4b585ac99a9f1d8af72f99e89e98179e60082341c4db052a17fd7ee70b8a45ee8c2ebf0d2945773f3700ca4cd03260e05c1914
|
data/.gitignore
CHANGED
data/.travis.yml
ADDED
data/Appraisals
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# HabtmGenerator
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/zealot128/ruby-habtm-generator.svg?branch=master)](https://travis-ci.org/zealot128/ruby-habtm-generator)
|
3
4
|
|
4
5
|
## Installation
|
5
6
|
|
@@ -29,5 +30,6 @@ This process is reversible (with ``rails destroy habtm model1 model2``).
|
|
29
30
|
|
30
31
|
## Potential Caveats
|
31
32
|
|
32
|
-
* Namespaced models will
|
33
|
-
* unique-constraint on index. I find this most useful for most situations. Just remove that from the generated migration before rake db:migrate
|
33
|
+
* Namespaced models should work (tested in form ``admin/post``). This will generate more verbose habtm lines in the model, with explicit table names and key names
|
34
|
+
* Default: adds unique-constraint on index. I find this most useful for most situations. Just remove that from the generated migration before rake db:migrate.
|
35
|
+
|
data/Rakefile
CHANGED
@@ -1,2 +1,18 @@
|
|
1
1
|
#!/usr/bin/env rake
|
2
2
|
require "bundler/gem_tasks"
|
3
|
+
require 'rails/dummy/tasks'
|
4
|
+
|
5
|
+
|
6
|
+
task :clear do
|
7
|
+
rm_rf 'spec/dummy'
|
8
|
+
end
|
9
|
+
task :test => [:clear, 'dummy:app'] do
|
10
|
+
system 'rails g model user'
|
11
|
+
system 'rails g model post'
|
12
|
+
system 'rails g model admin/process'
|
13
|
+
system 'rails g habtm user post'
|
14
|
+
system 'rails g habtm user admin/process'
|
15
|
+
system 'rake db:migrate'
|
16
|
+
system 'pwd'
|
17
|
+
system 'rails r ../../test.rb'
|
18
|
+
end
|
data/habtm_generator.gemspec
CHANGED
@@ -12,6 +12,10 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
13
|
gem.name = "habtm_generator"
|
14
14
|
gem.require_paths = ["lib"]
|
15
|
-
gem.version = "0.
|
16
|
-
gem.add_dependency "activerecord"
|
15
|
+
gem.version = "0.3"
|
16
|
+
gem.add_dependency "activerecord"
|
17
|
+
gem.add_development_dependency 'appraisal'
|
18
|
+
gem.add_development_dependency 'rspec'
|
19
|
+
gem.add_development_dependency 'pry'
|
20
|
+
gem.add_development_dependency 'rails-dummy'
|
17
21
|
end
|
@@ -10,18 +10,35 @@ class HabtmGenerator < ActiveRecord::Generators::Base
|
|
10
10
|
migration_template "habtm_migration.rb.erb",
|
11
11
|
"db/migrate/#{migration_name}.rb"
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
after: "class #{models[0].camelize} < ActiveRecord::Base\n"
|
16
|
-
insert_into_file "app/models/#{models[1]}.rb",
|
17
|
-
" has_and_belongs_to_many :#{models[0].pluralize}\n",
|
18
|
-
after: "class #{models[1].camelize} < ActiveRecord::Base\n"
|
13
|
+
add_migration_line model: models[0], other: models[1]
|
14
|
+
add_migration_line model: models[1], other: models[0]
|
19
15
|
end
|
20
16
|
|
21
17
|
private
|
22
18
|
|
19
|
+
def add_migration_line(hash)
|
20
|
+
other = hash[:other]
|
21
|
+
model = hash[:model]
|
22
|
+
extra = if other.include?('/') || model.include?('/')
|
23
|
+
", :join_table => '#{table_name}', :class_name => '#{other.camelcase}', :foreign_key => '#{no_ns(model.foreign_key)}', :association_foreign_key => '#{no_ns(other.foreign_key)}'"
|
24
|
+
else
|
25
|
+
""
|
26
|
+
end
|
27
|
+
insert_into_file "app/models/#{model}.rb",
|
28
|
+
" has_and_belongs_to_many :#{no_ns other.pluralize}#{extra}\n",
|
29
|
+
after: "class #{model.camelize} < ActiveRecord::Base\n"
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def no_ns(m)
|
34
|
+
m.gsub('/','_')
|
35
|
+
end
|
36
|
+
|
37
|
+
def join_table(model)
|
38
|
+
end
|
39
|
+
|
23
40
|
def table_name
|
24
|
-
sorted_models.map
|
41
|
+
sorted_models.map{|i| no_ns i.tableize}.join("_")
|
25
42
|
end
|
26
43
|
|
27
44
|
def models
|
@@ -33,11 +50,11 @@ class HabtmGenerator < ActiveRecord::Generators::Base
|
|
33
50
|
end
|
34
51
|
|
35
52
|
def references
|
36
|
-
sorted_models.map{|i| ":#{i}"}.join(", ")
|
53
|
+
sorted_models.map{|i| ":#{no_ns i.singularize}"}.join(", ")
|
37
54
|
end
|
38
55
|
|
39
56
|
def id_columns
|
40
|
-
sorted_models.map{|i| ":#{i}
|
57
|
+
sorted_models.map{|i| ":#{no_ns i.foreign_key}"}.join(", ")
|
41
58
|
end
|
42
59
|
|
43
60
|
def migration_name
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
# dummy
|
data/test.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
puts "Running test for #{Rails.version}"
|
2
|
+
user = User.create!
|
3
|
+
post = Post.create!
|
4
|
+
user.posts << post
|
5
|
+
post.reload.users.include?(user) || (raise StandardError.new('User not loaded'))
|
6
|
+
user.reload.posts.include?(post) || (raise StandardError.new('Post not loaded'))
|
7
|
+
|
8
|
+
process = Admin::Process.create!
|
9
|
+
|
10
|
+
user.admin_processes << process
|
11
|
+
|
12
|
+
process.reload.users.include?(user) || (raise StandardError.new('Admin/Process not loaded'))
|
metadata
CHANGED
@@ -1,29 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: habtm_generator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Wienert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: appraisal
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails-dummy
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
27
83
|
description: Generates has-and-belongs-to-many migrations. Use rails generate habtm
|
28
84
|
model1 model2
|
29
85
|
email:
|
@@ -32,15 +88,23 @@ executables: []
|
|
32
88
|
extensions: []
|
33
89
|
extra_rdoc_files: []
|
34
90
|
files:
|
35
|
-
- .gitignore
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Appraisals
|
36
94
|
- Gemfile
|
37
95
|
- LICENSE
|
38
96
|
- README.md
|
39
97
|
- Rakefile
|
98
|
+
- gemfiles/rails_4.0.gemfile
|
99
|
+
- gemfiles/rails_4.1.gemfile
|
100
|
+
- gemfiles/rails_4.2.gemfile
|
40
101
|
- habtm_generator.gemspec
|
41
102
|
- lib/generators/habtm/USAGE
|
42
103
|
- lib/generators/habtm/habtm_generator.rb
|
43
104
|
- lib/generators/habtm/templates/habtm_migration.rb.erb
|
105
|
+
- lib/habtm_generator.rb
|
106
|
+
- lib/ruby-habtm-generator.rb
|
107
|
+
- test.rb
|
44
108
|
homepage: https://github.com/zealot128/ruby-habtm-generator
|
45
109
|
licenses: []
|
46
110
|
metadata: {}
|
@@ -50,17 +114,17 @@ require_paths:
|
|
50
114
|
- lib
|
51
115
|
required_ruby_version: !ruby/object:Gem::Requirement
|
52
116
|
requirements:
|
53
|
-
- -
|
117
|
+
- - ">="
|
54
118
|
- !ruby/object:Gem::Version
|
55
119
|
version: '0'
|
56
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
121
|
requirements:
|
58
|
-
- -
|
122
|
+
- - ">="
|
59
123
|
- !ruby/object:Gem::Version
|
60
124
|
version: '0'
|
61
125
|
requirements: []
|
62
126
|
rubyforge_project:
|
63
|
-
rubygems_version: 2.2.
|
127
|
+
rubygems_version: 2.2.2
|
64
128
|
signing_key:
|
65
129
|
specification_version: 4
|
66
130
|
summary: Generates has-and-belongs-to-many migrations. Use rails generate habtm model1
|