rails_upgrader 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6d34a70205347c3b66eece9da6995bf7ba8acec7
4
+ data.tar.gz: 8a7757edb3b8a7a16eb057a2d3c0ed2f4a1422e1
5
+ SHA512:
6
+ metadata.gz: b9a9f19b94233dff0f32195b32267af749421d2fad317fbaafc51c2cada2a5bac969870f0c9013cf4d5974910326a5447958d7603351a9455d9201ccb791ad9f
7
+ data.tar.gz: 8f773fe56912cdf4445d1870ef5f4cb1e2c04d48492ae5a276f973424c3e63d266a51010c9bc7de2237ecfcd677ad44a44f318831faa63538ce233d2afe45289
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
14
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.3
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rails_upgrader.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ombu Labs
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,45 @@
1
+ # Rails Upgrader
2
+
3
+ This gem helps you automate the migration to strong parameters.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ group :development do
11
+ gem 'rails_upgrader'
12
+ end
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install rails_upgrader
22
+
23
+ ## Usage
24
+
25
+ `rails_upgrader COMMAND`
26
+
27
+ Commands:
28
+
29
+ - `go`: attempt to upgrade your models and controllers in place.
30
+
31
+ - `dry-run`: write strong parameter migrations to `all_strong_params.rb`.
32
+
33
+ ## Development
34
+
35
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
36
+
37
+ 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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/fastruby/rails_upgrader.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
7
+
8
+ task :build do
9
+ sh "gem build rails_upgrader.gemspec"
10
+ end
11
+
12
+ task release: :build do
13
+ sh "git push origin master"
14
+ sh "gem push rails_upgrader-#{RailsUpgrader::VERSION}.gem"
15
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_upgrader"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rails_upgrader"
5
+
6
+ case ARGV[0]
7
+ when "go"
8
+ RailsUpgrader::CLI.call
9
+ when "dry-run"
10
+ RailsUpgrader::CLI.new.send(:upgrade_strong_params)
11
+ else
12
+ puts <<-EOF
13
+ Usage: rails_upgrader COMMAND
14
+
15
+ Commands:
16
+ go: attempt to upgrade your models and controllers in place.
17
+ dry-run: write strong parameter migrations to `all_strong_params.rb`.
18
+ EOF
19
+ end
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ require "rails_upgrader/version"
2
+ require "rails_upgrader/cli"
3
+ require "rails_upgrader/strong_params"
4
+
5
+ module RailsUpgrader
6
+ end
@@ -0,0 +1,79 @@
1
+ require "rails"
2
+ require "rails_erd"
3
+ require "rails_erd/domain"
4
+
5
+ module RailsUpgrader
6
+ class CLI
7
+ attr_reader :domain
8
+
9
+ def self.call
10
+ new.upgrade
11
+ end
12
+
13
+ def initialize
14
+ preload_environment
15
+ puts "Preloading relationships..."
16
+ @domain = RailsERD::Domain.generate
17
+ end
18
+
19
+ def upgrade
20
+ puts "Upgrading Rails..."
21
+ upgrade_strong_params!
22
+ puts "Rails is upgraded!"
23
+ end
24
+
25
+ private
26
+
27
+ def upgrade_strong_params
28
+ result = domain.entities.map do |entity|
29
+ RailsUpgrader::StrongParams.new(entity).generate_method if entity.model
30
+ end.join
31
+
32
+ File.open("all_strong_params.rb", "w") { |f| f.write(result) }
33
+ end
34
+
35
+ def upgrade_strong_params!
36
+ domain.entities.each do |entity|
37
+ next unless entity.model
38
+ entity_to_upgrade = RailsUpgrader::StrongParams.new(entity)
39
+
40
+ unless File.file?(entity_to_upgrade.controller_path)
41
+ puts "Skipping #{entity.name}"
42
+ next
43
+ end
44
+
45
+ next if entity_to_upgrade.already_upgraded?
46
+
47
+ begin
48
+ entity_to_upgrade.update_controller_content!
49
+ entity_to_upgrade.update_model_content!
50
+ rescue => e
51
+ puts e.message
52
+ puts e.backtrace
53
+ next
54
+ end
55
+ end
56
+ end
57
+
58
+ def preload_environment
59
+ begin
60
+ require "#{Dir.pwd}/config/environment"
61
+ rescue LoadError => e
62
+ puts "Rails application not found! If you're on "\
63
+ "a Rails application, please open a Github issue: "\
64
+ "https://github.com/ombulabs/rails_upgrader/issues"
65
+ abort
66
+ end
67
+
68
+ puts "Preloading environment..."
69
+ Rails.application.eager_load!
70
+
71
+ if Rails.application.respond_to?(:config) && Rails.application.config
72
+ rails_config = Rails.application.config
73
+ if rails_config.respond_to?(:eager_load_namespaces)
74
+ rails_config.eager_load_namespaces.each(&:eager_load!)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,74 @@
1
+ require "active_model/naming"
2
+
3
+ module RailsUpgrader
4
+ class StrongParams
5
+ attr_reader :entity, :param_key, :controller_path, :model_path
6
+ ATTR_ACCESSIBLES = /\s+attr_accessible\s+([:]\w+[,]?\s+)+/.freeze
7
+
8
+ def initialize(entity)
9
+ @entity = entity
10
+ @param_key = ActiveModel::Naming.param_key(entity.model)
11
+ @controller_path = "app/controllers/#{param_key}s_controller.rb"
12
+ @model_path = "app/models/#{param_key}.rb"
13
+ end
14
+
15
+ def already_upgraded?
16
+ controller_content.include?("def #{param_key}_params")
17
+ end
18
+
19
+ def update_controller_content!
20
+ updated_content = appended_strong_params
21
+
22
+ File.open(controller_path, 'wb') do |file|
23
+ file.write(updated_content)
24
+ end
25
+ end
26
+
27
+ def update_model_content!
28
+ updated_content = removed_attr_accessible
29
+
30
+ File.open(model_path, 'wb') do |file|
31
+ file.write(updated_content)
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def appended_strong_params
38
+ result = controller_content
39
+ last_end = controller_content.rindex("end")
40
+ result[last_end..last_end+3] = "\n#{generate_method}end\n"
41
+ result
42
+ end
43
+
44
+ def removed_attr_accessible
45
+ result = model_content
46
+ result[ATTR_ACCESSIBLES] = "\n"
47
+ result
48
+ end
49
+
50
+ def controller_content
51
+ File.read(controller_path)
52
+ end
53
+
54
+ def model_content
55
+ File.read(model_path)
56
+ end
57
+
58
+ def generate_method
59
+ result = " def #{param_key}_params\n"
60
+ result += " params.require(:#{param_key})\n"
61
+
62
+ param_list = entity.attributes.reject do |attribute|
63
+ attribute.to_s =~ /^id$|^type$|^created_at$|^updated_at$|_token$|_count$/
64
+ end.map { |attribute| ":#{attribute}" }.join(", ")
65
+ result += " .permit(#{param_list})\n"
66
+
67
+ if entity.model.nested_attributes_options.present?
68
+ result += " # TODO: check nested attributes for: #{entity.model.nested_attributes_options.keys.join(', ')}\n"
69
+ end
70
+ result += " end\n\n"
71
+ result
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,3 @@
1
+ module RailsUpgrader
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "rails_upgrader/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rails_upgrader"
8
+ spec.version = RailsUpgrader::VERSION
9
+ spec.authors = ["Mauro Otonelli"]
10
+ spec.email = ["mauro@ombulabs.com"]
11
+
12
+ spec.summary = %q{Upgrade your Rails 3 app to Rails 4}
13
+ spec.description = %q{Helps with the process of upgrading your Rails 3 app to Rails 4}
14
+ spec.homepage = "https://www.fastruby.io"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.require_paths = ["lib"]
21
+ spec.executables = ["rails_upgrader"]
22
+
23
+ spec.add_dependency "rails-erd", "~> 1.5"
24
+ spec.add_dependency "rails", "~> 3.2"
25
+ spec.add_development_dependency "bundler", "~> 1.15"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "pry-byebug", "~> 3.4.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails_upgrader
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mauro Otonelli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails-erd
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.15'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.15'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.4.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.4.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Helps with the process of upgrading your Rails 3 app to Rails 4
98
+ email:
99
+ - mauro@ombulabs.com
100
+ executables:
101
+ - rails_upgrader
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/rails_upgrader
114
+ - bin/setup
115
+ - lib/rails_upgrader.rb
116
+ - lib/rails_upgrader/cli.rb
117
+ - lib/rails_upgrader/strong_params.rb
118
+ - lib/rails_upgrader/version.rb
119
+ - rails_upgrader.gemspec
120
+ homepage: https://www.fastruby.io
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.6.14
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Upgrade your Rails 3 app to Rails 4
144
+ test_files: []