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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b240c06d0c86b72373164647ed1f125292dc675b
4
- data.tar.gz: be957e436140cf06fef5e29db763307e5527abc6
3
+ metadata.gz: 420c26808e6ee7e74fbbe012b2417f175e9ebbec
4
+ data.tar.gz: d3b660d302464614a81e83e8ca01f224874f6c9a
5
5
  SHA512:
6
- metadata.gz: b9e5479263091f96842468b8e7907c641493336db1f9fee8a6df79833bb341dc6c539c2aa6e5009608a08eacfa3b25fff0f0a4b9b4bbcab68b9f39c956b91429
7
- data.tar.gz: 09d051d3361acacf75a6f9d4eafb6864ebada17abd0c98f4dec3da11a45bf49762f69cf3681481a9a004538f8ac53cf7844bab6e9f6e735e0198b8c116ca2630
6
+ metadata.gz: 040bc699b38ca84bf5185517c5e4bfc587672f8b1685ec95846564bcb9df3d611d9e056e2f429a70a15ee9bff4d11c204a7be990041543eeb05e5ed89f020bc7
7
+ data.tar.gz: 03b950881d178c8542db3f1bae4b585ac99a9f1d8af72f99e89e98179e60082341c4db052a17fd7ee70b8a45ee8c2ebf0d2945773f3700ca4cd03260e05c1914
data/.gitignore CHANGED
@@ -3,7 +3,6 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
- Gemfile.lock
7
6
  InstalledFiles
8
7
  _yardoc
9
8
  coverage
@@ -15,3 +14,5 @@ spec/reports
15
14
  test/tmp
16
15
  test/version_tmp
17
16
  tmp
17
+ spec/dummy
18
+ gemfiles/*.lock
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ gemfile:
3
+ - gemfiles/rails_4.0.gemfile
4
+ - gemfiles/rails_4.1.gemfile
5
+ - gemfiles/rails_4.2.gemfile
6
+
7
+ rvm:
8
+ - 2.1.1
9
+ before_install:
10
+ - bundle install --jobs=3 --retry=3
11
+ script:
12
+ - bundle exec rake test
data/Appraisals ADDED
@@ -0,0 +1,9 @@
1
+ appraise "rails-4.0" do
2
+ gem "rails", "~> 4.0.0"
3
+ end
4
+ appraise "rails-4.1" do
5
+ gem "rails", "~> 4.1.0"
6
+ end
7
+ appraise "rails-4.2" do
8
+ gem "rails", "~> 4.2.0.beta1"
9
+ end
data/Gemfile CHANGED
@@ -2,3 +2,6 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in habtm_generator.gemspec
4
4
  gemspec
5
+
6
+ gem 'sqlite3'
7
+ gem 'rake'
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 not work
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
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3"
6
+ gem "rake"
7
+ gem "rails", "~> 4.0.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3"
6
+ gem "rake"
7
+ gem "rails", "~> 4.1.0"
8
+
9
+ gemspec :path => "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "sqlite3"
6
+ gem "rake"
7
+ gem "rails", "~> 4.2.0.beta1"
8
+
9
+ gemspec :path => "../"
@@ -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.2"
16
- gem.add_dependency "activerecord", "~> 4.1.0.rc1"
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
- 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"
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(&:pluralize).join("_")
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}_id"}.join(", ")
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.2'
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-02-24 00:00:00.000000000 Z
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: 4.1.0.rc1
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: 4.1.0.rc1
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.0
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