habtm_generator 0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +2 -0
- data/habtm_generator.gemspec +17 -0
- data/lib/generators/habtm/USAGE +10 -0
- data/lib/generators/habtm/habtm_generator.rb +50 -0
- data/lib/generators/habtm/templates/habtm_migration.rb.erb +11 -0
- metadata +68 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Stefan Wienert
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# HabtmGenerator
|
2
|
+
|
3
|
+
|
4
|
+
## Installation
|
5
|
+
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
|
8
|
+
gem 'habtm_generator'
|
9
|
+
|
10
|
+
And then execute:
|
11
|
+
|
12
|
+
$ bundle
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install habtm_generator
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
|
21
|
+
```bash
|
22
|
+
rails generate habtm user post
|
23
|
+
```
|
24
|
+
|
25
|
+
This will generate a migration, for:
|
26
|
+
* creating table "posts\_users" with user\_id, post\_id, no primary key
|
27
|
+
* index on both columns
|
28
|
+
|
29
|
+
And will copy the "has\_and\_belongs\_to\_many :model" into both models (near the top of the models)
|
30
|
+
|
31
|
+
This process is reversible (with ``rails destroy habtm model1 model2``).
|
32
|
+
|
33
|
+
## Potential Caveats
|
34
|
+
|
35
|
+
* Namespaced models will not work
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.authors = ["Stefan Wienert"]
|
5
|
+
gem.email = ["stefan.wienert@pludoni.de"]
|
6
|
+
gem.description = %q{Generates has-and-belongs-to-many migrations. Use rails generate habtm model1 model2}
|
7
|
+
gem.summary = gem.description
|
8
|
+
gem.homepage = "https://github.com/zealot128/ruby-habtm-generator"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "habtm_generator"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = "0.1"
|
16
|
+
gem.add_dependency "activerecord", ">3.1"
|
17
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Description:
|
2
|
+
Generates has and belongs to many relationship for two given models
|
3
|
+
|
4
|
+
Example:
|
5
|
+
rails generate habtm user job
|
6
|
+
|
7
|
+
This will create:
|
8
|
+
* Migration for generating jobs_users table, with index
|
9
|
+
* Pasting habtm into each model
|
10
|
+
reversible! (rails destroy habtm user job)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
require 'rails/generators/active_record'
|
3
|
+
class HabtmGenerator < ActiveRecord::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
argument :other_model, required: true,
|
6
|
+
type: :string, desc: "List both part of the habtm migration to generate the table"
|
7
|
+
|
8
|
+
def create_migration
|
9
|
+
models.map!{|i|i.singularize}
|
10
|
+
migration_template "habtm_migration.rb.erb",
|
11
|
+
"db/migrate/#{migration_name}.rb"
|
12
|
+
|
13
|
+
insert_into_file "app/models/#{models[0]}.rb",
|
14
|
+
" has_and_belongs_to_many :#{models[1].pluralize}\n",
|
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"
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def table_name
|
24
|
+
sorted_models.map(&:pluralize).join("_")
|
25
|
+
end
|
26
|
+
|
27
|
+
def models
|
28
|
+
[name, other_model]
|
29
|
+
end
|
30
|
+
|
31
|
+
def sorted_models
|
32
|
+
models.map(&:singularize).map(&:underscore).sort
|
33
|
+
end
|
34
|
+
|
35
|
+
def references
|
36
|
+
sorted_models.map{|i| ":#{i}"}.join(", ")
|
37
|
+
end
|
38
|
+
|
39
|
+
def id_columns
|
40
|
+
sorted_models.map{|i| ":#{i}_id"}.join(", ")
|
41
|
+
end
|
42
|
+
|
43
|
+
def migration_name
|
44
|
+
"create_#{table_name}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def migration_class_name
|
48
|
+
migration_name.camelize
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :<%= table_name %>, :id => false do |t|
|
4
|
+
t.references <%= references %>
|
5
|
+
end
|
6
|
+
|
7
|
+
add_index :<%= table_name %>, [<%=id_columns %>],
|
8
|
+
name: "<%=table_name%>_index",
|
9
|
+
unique: true
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: habtm_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stefan Wienert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-10 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &29538100 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>'
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *29538100
|
25
|
+
description: Generates has-and-belongs-to-many migrations. Use rails generate habtm
|
26
|
+
model1 model2
|
27
|
+
email:
|
28
|
+
- stefan.wienert@pludoni.de
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- habtm_generator.gemspec
|
39
|
+
- lib/generators/habtm/USAGE
|
40
|
+
- lib/generators/habtm/habtm_generator.rb
|
41
|
+
- lib/generators/habtm/templates/habtm_migration.rb.erb
|
42
|
+
homepage: https://github.com/zealot128/ruby-habtm-generator
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.8.10
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Generates has-and-belongs-to-many migrations. Use rails generate habtm model1
|
66
|
+
model2
|
67
|
+
test_files: []
|
68
|
+
has_rdoc:
|