model_renamer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1b8b52e2cd1b2e710a07b3cc9ec750f592eef1a0
4
+ data.tar.gz: 11479430580608401a4d53d67f6ebc1d77df55b1
5
+ SHA512:
6
+ metadata.gz: b8006391f22d132d1b0c5244c25bd5d3f44b2c27b1cf49333df31280d2f5d687a8fb151ad7b7b5c715caeccc358927f7da1fdec275e48e4f6fdbe463541b72eb
7
+ data.tar.gz: f46aba920d4e217765d8a8b5a3c1969ecc4d3585959b47a3bac4221a8d7e1f7f8be049d2d9cadec8b20091d90e6db1ad854f3fe5fdca38d4ac4623b0ff54435d
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activerecord'
4
+ gem 'activesupport'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pawel Gut
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ModelRenamer
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/model_renamer`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'model_renamer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install model_renamer
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/model_renamer/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/bin/model_renamer ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'model_renamer'
4
+
5
+ ModelRenamerRunner.new(ARGV[0], ARGV[1]).replace_and_generate_migration
@@ -0,0 +1,21 @@
1
+ require "model_renamer/version"
2
+
3
+ #!/usr/bin/env ruby
4
+
5
+ require 'find'
6
+ require 'fileutils'
7
+ require 'active_support'
8
+ require 'active_record'
9
+ require 'model_renamer/migration_generator.rb'
10
+ require 'model_renamer/variations_generator.rb'
11
+ require 'model_renamer/model_renamer_runner.rb'
12
+
13
+
14
+
15
+
16
+
17
+
18
+
19
+
20
+ # ModelRenamer.new(ARGV[0], ARGV[1]).replace_and_generate_migration
21
+
@@ -0,0 +1,82 @@
1
+ #
2
+ # Generates a migration that renames columns and tables
3
+ #
4
+ class MigrationGenerator
5
+
6
+ def initialize(old_name_plural:, new_name_plural:, old_name_singular:, new_name_singular:)
7
+ @old_name_plural = old_name_plural
8
+ @new_name_plural = new_name_plural
9
+ @old_name_singular = old_name_singular
10
+ @new_name_singular = new_name_singular
11
+ end
12
+
13
+ def create_migration_file
14
+ path = Rails::Generators.invoke("active_record:migration", [migration_name])[1]
15
+ File.write(path, migration_file_content)
16
+ end
17
+
18
+ private
19
+
20
+ def migration_file_content
21
+ """class #{migration_name} < ActiveRecord::Migration
22
+ def change
23
+ #{rename_statements.join("\n ")}
24
+ end
25
+ end
26
+ """
27
+ end
28
+
29
+ def migration_name
30
+ "Rename#{@old_name_singular.split('_').map(&:capitalize).join}To#{@new_name_singular.split('_').map(&:capitalize).join}"
31
+ end
32
+
33
+ # {
34
+ # "table_1_name" => ["column_1", column_2, ...],
35
+ # "table_2_name" => ["column_1", column_2, ...],
36
+ # ...
37
+ # }
38
+ def tables_hash
39
+ @tables_hash ||= Hash[ ActiveRecord::Base.connection.tables.collect { |table| [table, ActiveRecord::Base.connection.columns(table).map(&:name)] } ]
40
+ end
41
+
42
+ # [
43
+ # 'rename_column :table_1, :old_name_1, :new_name_1',
44
+ # 'rename_column :table_1, :old_name_2, :new_name_2',
45
+ # 'rename_table :table_1, :new_table_1',
46
+ # ...
47
+ # ]
48
+ def rename_statements
49
+ @rename_statements ||= tables_hash.map do |table_name, columns|
50
+ columns.map { |column| rename_column(table_name, column) } << rename_table(table_name)
51
+ end.flatten.compact
52
+ end
53
+
54
+ def rename_column table_name, column_name
55
+ if column_name.include?(@old_name_plural)
56
+ """if table_exists?(:#{table_name}) && column_exists?(:#{table_name}, :#{column_name})
57
+ rename_column :#{table_name}, :#{column_name}, :#{column_name.gsub(@old_name_plural, @new_name_plural)}
58
+ end
59
+ """
60
+ elsif column_name.include?(@old_name_singular)
61
+ """if table_exists?(:#{table_name}) && column_exists?(:#{table_name}, :#{column_name})
62
+ rename_column :#{table_name}, :#{column_name}, :#{column_name.gsub(@old_name_singular, @new_name_singular)}
63
+ end
64
+ """
65
+ end
66
+ end
67
+
68
+ def rename_table table_name
69
+ if table_name.include?(@old_name_plural)
70
+ """if table_exists?(:#{table_name})
71
+ rename_table :#{table_name}, :#{table_name.gsub(@old_name_plural, @new_name_plural)}
72
+ end
73
+ """
74
+ elsif table_name.include?(@old_name_singular)
75
+ """if table_exists?(:#{table_name})
76
+ rename_table :#{table_name}, :#{table_name.gsub(@old_name_singular, @new_name_singular)}
77
+ end
78
+ """
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,71 @@
1
+ #
2
+ # Uses VariationsGenerator output to first change all the occurences of the model name in the files,
3
+ # then to create new directories and rename files. Then uses MigrationGenerator to create a migration
4
+ #
5
+ class ModelRenamerRunner
6
+
7
+ def initialize old_name, new_name
8
+ @variations_generator = VariationsGenerator.new(old_name, new_name)
9
+ end
10
+
11
+ def replace_and_generate_migration
12
+ replace_all_occurrences
13
+ MigrationGenerator.new(@variations_generator.underscore_variations).create_migration_file
14
+ end
15
+
16
+ private
17
+
18
+ def replace_all_occurrences
19
+ all_filepaths.each do |filepath|
20
+ replace_all_variations_in_file filepath
21
+ rename_file filepath
22
+ end
23
+ end
24
+
25
+ def variation_pairs
26
+ @variation_pairs ||= @variations_generator.pairs_to_convert
27
+ end
28
+
29
+ def all_filepaths
30
+ Find.find('.').to_a.reject do |path|
31
+ FileTest.directory?(path) || !acceptable_filetype?(path)
32
+ end
33
+ end
34
+
35
+ def acceptable_filetype? path
36
+ [
37
+ 'js', 'coffee', 'hamlc', 'skim', 'erb',
38
+ 'sass', 'scss', 'css', 'rb', 'slim',
39
+ 'haml', 'rabl', 'html', 'txt', 'feature',
40
+ 'rake', 'json', 'sh', 'yaml'
41
+ ].include? path.split('.').last
42
+ end
43
+
44
+ def replace_all_variations_in_file filepath
45
+ variation_pairs.each do |variation|
46
+ replace_in_file filepath, variation[0], variation[1]
47
+ end
48
+ end
49
+
50
+ def replace_in_file filepath, old_string, new_string
51
+ old_text = File.read(filepath)
52
+ if old_text.include? old_string
53
+ new_text = old_text.gsub(old_string, new_string)
54
+ File.open(filepath, "w") { |file| file.puts new_text }
55
+ end
56
+ end
57
+
58
+ def rename_file filepath
59
+ variation_pairs.each do |variation|
60
+ create_directory filepath, variation[0], variation[1]
61
+ File.rename(filepath, filepath.gsub(variation[0], variation[1])) if File.file?(filepath)
62
+ end
63
+ end
64
+
65
+ def create_directory filepath, old_name, new_name
66
+ if File.dirname(filepath).include? old_name
67
+ FileUtils.mkdir_p File.dirname(filepath).gsub(old_name, new_name)
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,66 @@
1
+ #
2
+ # Generates different versions of the model name and pairs them with the ones the should switch into,
3
+ # for example [ ['activity_log_iteration', 'deal_stage'], [activityLogIteration, dealStage], ... ]
4
+ #
5
+ class VariationsGenerator
6
+
7
+ def initialize old_name, new_name
8
+ @old_plural = split_into_words old_name.pluralize
9
+ @new_plural = split_into_words new_name.pluralize
10
+ @old_singular = split_into_words old_name
11
+ @new_singular = split_into_words new_name
12
+ end
13
+
14
+ def pairs_to_convert
15
+ # Important: plurals have to be applied first in case we are working with irregular pluralizations
16
+ (variations(@old_plural) + variations(@old_singular)).zip (variations(@new_plural) + variations(@new_singular))
17
+ end
18
+
19
+ def underscore_variations
20
+ {
21
+ old_name_plural: @old_plural.join('_'),
22
+ new_name_plural: @new_plural.join('_'),
23
+ old_name_singular: @old_singular.join('_'),
24
+ new_name_singular: @new_singular.join('_')
25
+ }
26
+ end
27
+
28
+ private
29
+
30
+ def split_into_words str
31
+ str.split(/(?=[A-Z])/).map(&:downcase)
32
+ end
33
+
34
+ def variations words
35
+
36
+ variations_array = []
37
+
38
+ # underscore separated (Ruby methods)
39
+ variations_array << words.join('_')
40
+
41
+ # dash separated (CSS)
42
+ variations_array << words.join('-')
43
+
44
+ # capitalized (ActiveRecord model)
45
+ variations_array << words.map(&:capitalize).join
46
+
47
+ # camelCase (JavaScript)
48
+ variations_array << words[1, words.count].map(&:capitalize).unshift(words[0]).join
49
+
50
+ # space separated (comments)
51
+ variations_array << words.join(' ')
52
+
53
+ # space separated, every word capitalized (comments)
54
+ variations_array << words.map(&:capitalize).join(' ')
55
+
56
+ # space separated, first word capitalized (comments)
57
+ variations_array << words[1, words.count].unshift(words[0].capitalize).join(' ')
58
+
59
+ # all uppercase, underscore separated (constant)
60
+ variations_array << words.map(&:upcase).join('_')
61
+
62
+ variations_array
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,3 @@
1
+ module ModelRenamer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'model_renamer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "model_renamer"
8
+ spec.version = ModelRenamer::VERSION
9
+ spec.authors = ["Paul Gut"]
10
+ spec.email = ["pg@vts.com"]
11
+
12
+ spec.summary = "A gem that renames your ActiveRecord models and generates corresponding migrations"
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
16
+ # delete this section to allow pushing this gem to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
21
+ end
22
+
23
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.9"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: model_renamer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Gut
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - pg@vts.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - bin/model_renamer
53
+ - lib/model_renamer.rb
54
+ - lib/model_renamer/migration_generator.rb
55
+ - lib/model_renamer/model_renamer_runner.rb
56
+ - lib/model_renamer/variations_generator.rb
57
+ - lib/model_renamer/version.rb
58
+ - model_renamer.gemspec
59
+ homepage:
60
+ licenses:
61
+ - MIT
62
+ metadata:
63
+ allowed_push_host: https://rubygems.org
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.2
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A gem that renames your ActiveRecord models and generates corresponding migrations
84
+ test_files: []