├── spec
├── dummy
│ ├── app
│ │ ├── models
│ │ │ ├── .gitkeep
│ │ │ └── user.rb
│ │ ├── controllers
│ │ │ ├── users_controller.rb
│ │ │ └── application_controller.rb
│ │ └── views
│ │ │ └── layouts
│ │ │ └── application.html.erb
│ ├── config
│ │ ├── routes.rb
│ │ ├── database.yml
│ │ ├── environment.rb
│ │ ├── boot.rb
│ │ ├── locales
│ │ │ └── en.yml
│ │ ├── initializers
│ │ │ ├── session_store.rb
│ │ │ ├── secret_token.rb
│ │ │ └── wrap_parameters.rb
│ │ ├── environments
│ │ │ └── test.rb
│ │ └── application.rb
│ ├── db
│ │ ├── test.sqlite3
│ │ ├── migrate
│ │ │ └── 20171026174906_create_users.rb
│ │ └── schema.rb
│ ├── Gemfile
│ ├── config.ru
│ ├── Rakefile
│ ├── script
│ │ └── rails
│ ├── .gitignore
│ └── Gemfile.lock
├── spec_helper.rb
└── rails_upgrader_spec.rb
├── .rspec
├── lib
├── rails_upgrader
│ ├── version.rb
│ ├── strong_params.rb
│ └── cli.rb
└── rails_upgrader.rb
├── bin
├── setup
├── console
└── rails_upgrader
├── Gemfile
├── .gitignore
├── .travis.yml
├── Rakefile
├── pull_request_template.md
├── .github
└── ISSUE_TEMPLATE
│ ├── feature_request.md
│ └── bug_report.md
├── CHANGELOG.md
├── LICENSE.txt
├── rails_upgrader.gemspec
├── README.md
└── CODE_OF_CONDUCT.md
/spec/dummy/app/models/.gitkeep:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.rspec:
--------------------------------------------------------------------------------
1 | --format documentation
2 | --color
3 |
--------------------------------------------------------------------------------
/lib/rails_upgrader/version.rb:
--------------------------------------------------------------------------------
1 | module RailsUpgrader
2 | VERSION = "0.2.1"
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/routes.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.routes.draw do
2 | resources :users
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/users_controller.rb:
--------------------------------------------------------------------------------
1 | class UsersController < ApplicationController
2 | end
3 |
--------------------------------------------------------------------------------
/spec/dummy/db/test.sqlite3:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/fastruby/rails_upgrader/HEAD/spec/dummy/db/test.sqlite3
--------------------------------------------------------------------------------
/spec/dummy/app/models/user.rb:
--------------------------------------------------------------------------------
1 | class User < ActiveRecord::Base
2 | attr_accessible :first_name, :last_name, :project_id
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/config/database.yml:
--------------------------------------------------------------------------------
1 | test:
2 | adapter: sqlite3
3 | database: db/test.sqlite3
4 | pool: 5
5 | timeout: 5000
6 |
--------------------------------------------------------------------------------
/spec/dummy/app/controllers/application_controller.rb:
--------------------------------------------------------------------------------
1 | class ApplicationController < ActionController::Base
2 | protect_from_forgery
3 | end
4 |
--------------------------------------------------------------------------------
/spec/dummy/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gem 'rails', '3.2.22.5'
4 | gem 'sqlite3'
5 | gem 'pry'
6 | gem 'rails_upgrader', path: '../../'
7 |
--------------------------------------------------------------------------------
/bin/setup:
--------------------------------------------------------------------------------
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
9 |
--------------------------------------------------------------------------------
/lib/rails_upgrader.rb:
--------------------------------------------------------------------------------
1 | require "rails_upgrader/version"
2 | require "rails_upgrader/cli"
3 | require "rails_upgrader/strong_params"
4 |
5 | module RailsUpgrader
6 | end
7 |
--------------------------------------------------------------------------------
/spec/dummy/config.ru:
--------------------------------------------------------------------------------
1 | # This file is used by Rack-based servers to start the application.
2 |
3 | require ::File.expand_path('../config/environment', __FILE__)
4 | run Dummy::Application
5 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
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
7 |
--------------------------------------------------------------------------------
/spec/dummy/config/environment.rb:
--------------------------------------------------------------------------------
1 | # Load the rails application
2 | require File.expand_path('../application', __FILE__)
3 |
4 | # Initialize the rails application
5 | Dummy::Application.initialize!
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
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 |
15 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: ruby
3 | rvm:
4 | - 2.4.10
5 | before_install:
6 | - gem install bundler:1.17.3
7 | install:
8 | - bundle install
9 | - bundle install --gemfile=$PWD/spec/dummy/Gemfile
10 |
--------------------------------------------------------------------------------
/spec/dummy/config/boot.rb:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 |
3 | # Set up gems listed in the Gemfile.
4 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
5 |
6 | require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
7 |
--------------------------------------------------------------------------------
/spec/dummy/config/locales/en.yml:
--------------------------------------------------------------------------------
1 | # Sample localization file for English. Add more files in this directory for other locales.
2 | # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3 |
4 | en:
5 | hello: "Hello world"
6 |
--------------------------------------------------------------------------------
/spec/dummy/Rakefile:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env rake
2 | # Add your own tasks in files placed in lib/tasks ending in .rake,
3 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4 |
5 | require File.expand_path('../config/application', __FILE__)
6 |
7 | Dummy::Application.load_tasks
8 |
--------------------------------------------------------------------------------
/spec/dummy/db/migrate/20171026174906_create_users.rb:
--------------------------------------------------------------------------------
1 | class CreateUsers < ActiveRecord::Migration
2 | def change
3 | create_table :users do |t|
4 | t.string :first_name
5 | t.string :last_name
6 | t.integer :project_id
7 |
8 | t.timestamps
9 | end
10 | end
11 | end
12 |
--------------------------------------------------------------------------------
/spec/dummy/script/rails:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env ruby
2 | # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3 |
4 | APP_PATH = File.expand_path('../../config/application', __FILE__)
5 | require File.expand_path('../../config/boot', __FILE__)
6 | require 'rails/commands'
7 |
--------------------------------------------------------------------------------
/spec/dummy/app/views/layouts/application.html.erb:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Dummy
5 | <%= stylesheet_link_tag "application", :media => "all" %>
6 | <%= javascript_include_tag "application" %>
7 | <%= csrf_meta_tags %>
8 |
9 |
10 |
11 | <%= yield %>
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
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
16 |
--------------------------------------------------------------------------------
/bin/console:
--------------------------------------------------------------------------------
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__)
15 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/session_store.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | Dummy::Application.config.session_store :cookie_store, key: '_dummy_session'
4 |
5 | # Use the database for sessions instead of the cookie-based default,
6 | # which shouldn't be used to store highly confidential information
7 | # (create the session table with "rails generate session_migration")
8 | # Dummy::Application.config.session_store :active_record_store
9 |
--------------------------------------------------------------------------------
/spec/dummy/.gitignore:
--------------------------------------------------------------------------------
1 | # See http://help.github.com/ignore-files/ for more about ignoring files.
2 | #
3 | # If you find yourself ignoring temporary files generated by your text editor
4 | # or operating system, you probably want to add a global ignore instead:
5 | # git config --global core.excludesfile ~/.gitignore_global
6 |
7 | # Ignore bundler config
8 | /.bundle
9 |
10 | # Ignore the default SQLite database.
11 | # /db/*.sqlite3
12 |
13 | # Ignore all logfiles and tempfiles.
14 | /log/*.log
15 | /log
16 | /tmp
17 |
--------------------------------------------------------------------------------
/bin/rails_upgrader:
--------------------------------------------------------------------------------
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 | write_to_file = ARGV[1] == '--file'
11 | RailsUpgrader::CLI.new.send(:upgrade_strong_params, write_to_file)
12 | else
13 | puts <<-EOF
14 | Usage: rails_upgrader COMMAND
15 |
16 | Commands:
17 | go: attempt to upgrade your models and controllers in place.
18 | dry-run: write strong parameter migrations to `all_strong_params.rb`.
19 | EOF
20 | end
21 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/secret_token.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 |
3 | # Your secret key for verifying the integrity of signed cookies.
4 | # If you change this key, all old signed cookies will become invalid!
5 | # Make sure the secret is at least 30 characters and all random,
6 | # no regular words or you'll be exposed to dictionary attacks.
7 | Dummy::Application.config.secret_token = '19ac48d1d1aa98597823de8eed7de9263fc4aa424c455eb9840e528a0d4250f0b4a36ee44506f13737a743b5d5ca9c9e275f9b7cf54d771e60d5cdd795f1a042'
8 |
--------------------------------------------------------------------------------
/spec/dummy/config/initializers/wrap_parameters.rb:
--------------------------------------------------------------------------------
1 | # Be sure to restart your server when you modify this file.
2 | #
3 | # This file contains settings for ActionController::ParamsWrapper which
4 | # is enabled by default.
5 |
6 | # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7 | ActiveSupport.on_load(:action_controller) do
8 | wrap_parameters format: [:json]
9 | end
10 |
11 | # Disable root element in JSON by default.
12 | ActiveSupport.on_load(:active_record) do
13 | self.include_root_in_json = false
14 | end
15 |
--------------------------------------------------------------------------------
/pull_request_template.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | - [ ] Add an entry to `CHANGELOG.md` that links to this PR under the "main (unreleased)" heading.
4 |
5 | ## Description
6 |
7 |
8 | ## Motivation and Context
9 |
10 |
11 |
12 | ## How Has This Been Tested?
13 |
14 |
15 |
16 | ## Screenshots:
17 |
18 |
19 | **I will abide by the [code of conduct](CODE_OF_CONDUCT.md)**
20 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Request a new feature
4 | title: "[REQUEST]"
5 | labels: 'enhancement'
6 | assignees: ''
7 |
8 | ---
9 |
10 |
11 |
12 |
13 |
14 | ## Description
15 |
16 |
17 | ## Possible Implementation
18 |
19 |
20 | ## Resources:
21 |
22 |
23 |
24 | **I will abide by the [code of conduct](CODE_OF_CONDUCT.md)**
25 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # main ([unreleased](https://github.com/fastruby/rails_upgrader/compare/v0.2.1...main))
2 |
3 | * [CHORE: Added updated templates for bug fixes, feature requests and pull requests](https://github.com/fastruby/next_rails/pull/64) as per [this RFC](https://github.com/fastruby/RFCs/blob/main/2021-10-13-github-templates.md)
4 | * [FEATURE: Add new param to show the migration at the terminal](https://github.com/fastruby/rails_upgrader/pull/8)
5 | * [BUGFIX: fixed broken build](https://github.com/fastruby/rails_upgrader/pull/14)
6 | * [CHORE: Add Travis CI Build Badge to README](https://github.com/fastruby/rails_upgrader/pull/18)
7 | * [CHORE: Add Code of Conduct](https://github.com/fastruby/rails_upgrader/pull/23)
8 | * [CHORE: Add PR Template](https://github.com/fastruby/rails_upgrader/pull/24)
9 | * [CHORE: Add Issue Template](https://github.com/fastruby/rails_upgrader/pull/25)
10 |
11 | # v0.2.1 / 2017-11-9 ([commits](https://github.com/fastruby/rails_upgrader/compare/v0.2.0...v0.2.1))
12 |
13 | # v0.2.0 / 2017-11-9 ([commits](https://github.com/fastruby/rails_upgrader/compare/v0.1.0...v0.2.0))
14 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | require "pry"
2 |
3 | RSpec.configure do |config|
4 | config.expect_with :rspec do |expectations|
5 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true
6 | end
7 |
8 | config.mock_with :rspec do |mocks|
9 | mocks.verify_partial_doubles = true
10 | end
11 |
12 | config.shared_context_metadata_behavior = :apply_to_host_groups
13 | end
14 |
15 | def reset_dummy_files
16 | reset_controller_content
17 | reset_model_content
18 | end
19 |
20 | def reset_controller_content
21 | original_content = <<-END
22 | class UsersController < ApplicationController
23 | end
24 | END
25 |
26 | path = File.join(File.dirname(__FILE__), "dummy", "app", "controllers", "users_controller.rb")
27 | File.write(path, original_content)
28 | end
29 |
30 | def reset_model_content
31 | original_content = <<-END
32 | class User < ActiveRecord::Base
33 | attr_accessible :first_name, :last_name, :project_id
34 | end
35 | END
36 |
37 | path = File.join(File.dirname(__FILE__), "dummy", "app", "models", "user.rb")
38 | File.write(path, original_content)
39 | end
40 |
--------------------------------------------------------------------------------
/spec/dummy/db/schema.rb:
--------------------------------------------------------------------------------
1 | # encoding: UTF-8
2 | # This file is auto-generated from the current state of the database. Instead
3 | # of editing this file, please use the migrations feature of Active Record to
4 | # incrementally modify your database, and then regenerate this schema definition.
5 | #
6 | # Note that this schema.rb definition is the authoritative source for your
7 | # database schema. If you need to create the application database on another
8 | # system, you should be using db:schema:load, not running all the migrations
9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10 | # you'll amass, the slower it'll run and the greater likelihood for issues).
11 | #
12 | # It's strongly recommended to check this file into your version control system.
13 |
14 | ActiveRecord::Schema.define(:version => 20171026174906) do
15 |
16 | create_table "users", :force => true do |t|
17 | t.string "first_name"
18 | t.string "last_name"
19 | t.integer "project_id"
20 | t.datetime "created_at", :null => false
21 | t.datetime "updated_at", :null => false
22 | end
23 |
24 | end
25 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
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.
22 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: "[BUG]"
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 |
11 |
12 |
13 |
14 | ## Expected Behavior
15 |
16 |
17 |
18 | ## Actual Behavior
19 |
20 |
21 |
22 | ## Possible Fix
23 |
24 |
25 | ## To Reproduce
26 |
27 |
28 |
29 | 1.
30 | 2.
31 | 3.
32 | 4.
33 |
34 | ## Additional Information
35 |
36 |
37 |
38 | **I will abide by the [code of conduct](CODE_OF_CONDUCT.md)**
39 |
--------------------------------------------------------------------------------
/rails_upgrader.gemspec:
--------------------------------------------------------------------------------
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.0"
28 | spec.add_development_dependency "rspec", "~> 3.0"
29 | end
30 |
--------------------------------------------------------------------------------
/spec/dummy/config/environments/test.rb:
--------------------------------------------------------------------------------
1 | Dummy::Application.configure do
2 | # Settings specified here will take precedence over those in config/application.rb
3 |
4 | # The test environment is used exclusively to run your application's
5 | # test suite. You never need to work with it otherwise. Remember that
6 | # your test database is "scratch space" for the test suite and is wiped
7 | # and recreated between test runs. Don't rely on the data there!
8 | config.cache_classes = true
9 |
10 | # Configure static asset server for tests with Cache-Control for performance
11 | config.serve_static_assets = true
12 | config.static_cache_control = "public, max-age=3600"
13 |
14 | # Log error messages when you accidentally call methods on nil
15 | config.whiny_nils = true
16 |
17 | # Show full error reports and disable caching
18 | config.consider_all_requests_local = true
19 | config.action_controller.perform_caching = false
20 |
21 | # Raise exceptions instead of rendering exception templates
22 | config.action_dispatch.show_exceptions = false
23 |
24 | # Disable request forgery protection in test environment
25 | config.action_controller.allow_forgery_protection = false
26 |
27 | # Tell Action Mailer not to deliver emails to the real world.
28 | # The :test delivery method accumulates sent emails in the
29 | # ActionMailer::Base.deliveries array.
30 | config.action_mailer.delivery_method = :test
31 |
32 | # Raise exception on mass assignment protection for Active Record models
33 | config.active_record.mass_assignment_sanitizer = :strict
34 |
35 | # Print deprecation notices to the stderr
36 | config.active_support.deprecation = :stderr
37 | end
38 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Rails Upgrader
2 |
3 | [](https://travis-ci.org/fastruby/rails_upgrader)
4 |
5 | This gem helps you automate the migration to strong parameters.
6 |
7 | ## Installation
8 |
9 | Add this line to your application's Gemfile:
10 |
11 | ```ruby
12 | group :development do
13 | gem 'rails_upgrader'
14 | end
15 | ```
16 |
17 | And then execute:
18 |
19 | $ bundle
20 |
21 | Or install it yourself as:
22 |
23 | $ gem install rails_upgrader
24 |
25 | ## Usage
26 |
27 | `rails_upgrader COMMAND`
28 |
29 | Commands:
30 |
31 | - `go`: attempt to upgrade your models and controllers in place.
32 |
33 | - `dry-run`: write strong parameter migrations in the terminal.
34 |
35 | - `dry-run --file`: write strong parameter migrations to `all_strong_params.rb` file.
36 |
37 | ## Development
38 |
39 | 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.
40 |
41 | 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).
42 |
43 | ## Contributing
44 |
45 | Bug reports and pull requests are welcome on GitHub at [https://github.com/fastruby/rails_upgrader](https://github.com/fastruby/rails_upgrader). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
46 |
47 | When Submitting a Pull Request:
48 |
49 | * If your PR closes any open GitHub issues, please include `Closes #XXXX` in your comment
50 |
51 | * Please include a summary of the change and which issue is fixed or which feature is introduced.
52 |
53 | * If changes to the behavior are made, clearly describe what changes.
54 |
55 | * If changes to the UI are made, please include screenshots of the before and after.
56 |
57 | ## License
58 |
59 | The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
60 |
--------------------------------------------------------------------------------
/lib/rails_upgrader/strong_params.rb:
--------------------------------------------------------------------------------
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 | def generate_method
36 | result = " def #{param_key}_params\n"
37 | result += " params.require(:#{param_key})\n"
38 |
39 | param_list = entity.attributes.reject do |attribute|
40 | attribute.to_s =~ /^id$|^type$|^created_at$|^updated_at$|_token$|_count$/
41 | end.map { |attribute| ":#{attribute}" }.join(", ")
42 | result += " .permit(#{param_list})\n"
43 |
44 | if entity.model.nested_attributes_options.present?
45 | result += " # TODO: check nested attributes for: #{entity.model.nested_attributes_options.keys.join(', ')}\n"
46 | end
47 | result += " end\n\n"
48 | result
49 | end
50 |
51 | private
52 |
53 | def appended_strong_params
54 | result = controller_content
55 | last_end = controller_content.rindex("end")
56 | result[last_end..last_end+3] = "\n#{generate_method}end\n"
57 | result
58 | end
59 |
60 | def removed_attr_accessible
61 | result = model_content
62 | result[ATTR_ACCESSIBLES] = "\n"
63 | result
64 | end
65 |
66 | def controller_content
67 | File.read(controller_path)
68 | end
69 |
70 | def model_content
71 | File.read(model_path)
72 | end
73 | end
74 | end
75 |
--------------------------------------------------------------------------------
/lib/rails_upgrader/cli.rb:
--------------------------------------------------------------------------------
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(write_to_file)
28 | result = domain.entities.map do |entity|
29 | RailsUpgrader::StrongParams.new(entity).generate_method if entity.model
30 | end.join
31 |
32 | if write_to_file
33 | filename = "all_strong_params.rb"
34 | File.open(filename, "w") { |f| f.write(result) }
35 | puts "See the strong params result at generated file: #{filename}"
36 | else
37 | puts "\n\n==== ALL STRONG PARAMS: ====="
38 | puts result
39 | puts "============================="
40 | end
41 | end
42 |
43 | def upgrade_strong_params!
44 | domain.entities.each do |entity|
45 | next unless entity.model
46 | entity_to_upgrade = RailsUpgrader::StrongParams.new(entity)
47 |
48 | unless File.file?(entity_to_upgrade.controller_path)
49 | puts "Skipping #{entity.name}"
50 | next
51 | end
52 |
53 | next if entity_to_upgrade.already_upgraded?
54 |
55 | begin
56 | entity_to_upgrade.update_controller_content!
57 | entity_to_upgrade.update_model_content!
58 | rescue => e
59 | puts e.message
60 | puts e.backtrace
61 | next
62 | end
63 | end
64 | end
65 |
66 | def preload_environment
67 | begin
68 | require "#{Dir.pwd}/config/environment"
69 | rescue LoadError => e
70 | puts "Rails application not found! If you're on "\
71 | "a Rails application, please open a Github issue: "\
72 | "https://github.com/ombulabs/rails_upgrader/issues"
73 | abort
74 | end
75 |
76 | puts "Preloading environment..."
77 | Rails.application.eager_load!
78 |
79 | if Rails.application.respond_to?(:config) && Rails.application.config
80 | rails_config = Rails.application.config
81 | if rails_config.respond_to?(:eager_load_namespaces)
82 | rails_config.eager_load_namespaces.each(&:eager_load!)
83 | end
84 | end
85 | end
86 | end
87 | end
88 |
--------------------------------------------------------------------------------
/spec/rails_upgrader_spec.rb:
--------------------------------------------------------------------------------
1 | require "spec_helper"
2 |
3 | RSpec.describe RailsUpgrader do
4 | let(:dummy_path) do
5 | File.join(File.dirname(__FILE__), "dummy")
6 | end
7 |
8 | let(:gemfile) { File.expand_path("../dummy/Gemfile", __FILE__) }
9 | let(:env_variables) { "BUNDLE_GEMFILE=#{gemfile} RAILS_ENV=test" }
10 |
11 | let(:strong_parameters) do
12 | <<-END
13 | def user_params
14 | params.require(:user)
15 | .permit(:first_name, :last_name, :project_id)
16 | end
17 | END
18 | end
19 |
20 | describe "#go" do
21 | let(:model) do
22 | File.join(dummy_path, "app", "models", "user.rb")
23 | end
24 | let(:controller) do
25 | File.join(dummy_path, "app", "controllers", "users_controller.rb")
26 | end
27 |
28 | before { reset_dummy_files }
29 | after { reset_dummy_files }
30 |
31 | it "migrates controller from using protected attributes to strong params" do
32 | system("cd #{dummy_path} && #{env_variables} bundle exec rails_upgrader go")
33 |
34 | accessible_attributes = "attr_accessible :first_name, :last_name, :project_id"
35 |
36 | expect(File.read(controller)).to include strong_parameters
37 | expect(File.read(controller)).to include "class UsersController < ApplicationController"
38 | expect(File.read(model)).not_to include accessible_attributes
39 | expect(File.read(model)).to include "class User < ActiveRecord::Base"
40 | end
41 |
42 | xit "migrates with nested attributes" do
43 | # future test case
44 | end
45 |
46 | xit "writes to file instead of upgrading files in place" do
47 | # future test case, move to RailsUpgrader::CLI
48 | end
49 |
50 | xit "skips the controller if already upgraded" do
51 | # future test case, move to RailsUpgrader::CLI
52 | end
53 | end
54 |
55 | describe "#dry-run" do
56 | let(:strong_params_file) { File.join(dummy_path, "all_strong_params.rb") }
57 |
58 | let(:upgrade_command) do
59 | "cd #{dummy_path} && #{env_variables} bundle exec rails_upgrader dry-run"
60 | end
61 |
62 | before do
63 | File.delete(strong_params_file) if File.exist?(strong_params_file)
64 | end
65 |
66 | context "without extra params" do
67 | it "do not save the params to a file" do
68 | system(upgrade_command)
69 | expect { expect(strong_params_file).to_not be_an_existing_file }
70 | end
71 |
72 | it "output the result at console" do
73 | expect { system(upgrade_command) }.to output(/#{Regexp.quote(strong_parameters)}/).to_stdout_from_any_process
74 | end
75 | end
76 |
77 | context "with the param --file" do
78 | before do
79 | system("#{upgrade_command} --file")
80 | end
81 |
82 | it "write strong parameters migrations to a file" do
83 | expect { expect(strong_params_file).to be_an_existing_file }
84 | expect(File.read(strong_params_file)).to include strong_parameters
85 | end
86 | end
87 | end
88 | end
89 |
--------------------------------------------------------------------------------
/spec/dummy/config/application.rb:
--------------------------------------------------------------------------------
1 | require File.expand_path('../boot', __FILE__)
2 |
3 | require 'rails/all'
4 |
5 | if defined?(Bundler)
6 | # If you precompile assets before deploying to production, use this line
7 | Bundler.require(*Rails.groups(:assets => %w(development test)))
8 | # If you want your assets lazily compiled in production, use this line
9 | # Bundler.require(:default, :assets, Rails.env)
10 | end
11 |
12 | module Dummy
13 | class Application < Rails::Application
14 | # Settings in config/environments/* take precedence over those specified here.
15 | # Application configuration should go into files in config/initializers
16 | # -- all .rb files in that directory are automatically loaded.
17 |
18 | # Custom directories with classes and modules you want to be autoloadable.
19 | # config.autoload_paths += %W(#{config.root}/extras)
20 |
21 | # Only load the plugins named here, in the order given (default is alphabetical).
22 | # :all can be used as a placeholder for all plugins not explicitly named.
23 | # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24 |
25 | # Activate observers that should always be running.
26 | # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27 |
28 | # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29 | # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30 | # config.time_zone = 'Central Time (US & Canada)'
31 |
32 | # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33 | # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34 | # config.i18n.default_locale = :de
35 |
36 | # Configure the default encoding used in templates for Ruby 1.9.
37 | config.encoding = "utf-8"
38 |
39 | # Configure sensitive parameters which will be filtered from the log file.
40 | config.filter_parameters += [:password]
41 |
42 | # Enable escaping HTML in JSON.
43 | config.active_support.escape_html_entities_in_json = true
44 |
45 | # Use SQL instead of Active Record's schema dumper when creating the database.
46 | # This is necessary if your schema can't be completely dumped by the schema dumper,
47 | # like if you have constraints or database-specific column types
48 | # config.active_record.schema_format = :sql
49 |
50 | # Enforce whitelist mode for mass assignment.
51 | # This will create an empty whitelist of attributes available for mass-assignment for all models
52 | # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
53 | # parameters by using an attr_accessible or attr_protected declaration.
54 | config.active_record.whitelist_attributes = true
55 |
56 | # Enable the asset pipeline
57 | config.assets.enabled = true
58 |
59 | # Version of your assets, change this if you want to expire all your assets
60 | config.assets.version = '1.0'
61 | end
62 | end
63 |
--------------------------------------------------------------------------------
/spec/dummy/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: ../..
3 | specs:
4 | rails_upgrader (0.2.1)
5 | rails (~> 3.2)
6 | rails-erd (~> 1.5)
7 |
8 | GEM
9 | remote: https://rubygems.org/
10 | specs:
11 | actionmailer (3.2.22.5)
12 | actionpack (= 3.2.22.5)
13 | mail (~> 2.5.4)
14 | actionpack (3.2.22.5)
15 | activemodel (= 3.2.22.5)
16 | activesupport (= 3.2.22.5)
17 | builder (~> 3.0.0)
18 | erubis (~> 2.7.0)
19 | journey (~> 1.0.4)
20 | rack (~> 1.4.5)
21 | rack-cache (~> 1.2)
22 | rack-test (~> 0.6.1)
23 | sprockets (~> 2.2.1)
24 | activemodel (3.2.22.5)
25 | activesupport (= 3.2.22.5)
26 | builder (~> 3.0.0)
27 | activerecord (3.2.22.5)
28 | activemodel (= 3.2.22.5)
29 | activesupport (= 3.2.22.5)
30 | arel (~> 3.0.2)
31 | tzinfo (~> 0.3.29)
32 | activeresource (3.2.22.5)
33 | activemodel (= 3.2.22.5)
34 | activesupport (= 3.2.22.5)
35 | activesupport (3.2.22.5)
36 | i18n (~> 0.6, >= 0.6.4)
37 | multi_json (~> 1.0)
38 | arel (3.0.3)
39 | builder (3.0.4)
40 | choice (0.2.0)
41 | coderay (1.1.2)
42 | concurrent-ruby (1.0.5)
43 | erubis (2.7.0)
44 | hike (1.2.3)
45 | i18n (0.9.0)
46 | concurrent-ruby (~> 1.0)
47 | journey (1.0.4)
48 | json (1.8.6)
49 | mail (2.5.5)
50 | mime-types (~> 1.16)
51 | treetop (~> 1.4.8)
52 | method_source (0.9.0)
53 | mime-types (1.25.1)
54 | multi_json (1.12.2)
55 | polyglot (0.3.5)
56 | pry (0.11.2)
57 | coderay (~> 1.1.0)
58 | method_source (~> 0.9.0)
59 | rack (1.4.7)
60 | rack-cache (1.7.1)
61 | rack (>= 0.4)
62 | rack-ssl (1.3.4)
63 | rack
64 | rack-test (0.6.3)
65 | rack (>= 1.0)
66 | rails (3.2.22.5)
67 | actionmailer (= 3.2.22.5)
68 | actionpack (= 3.2.22.5)
69 | activerecord (= 3.2.22.5)
70 | activeresource (= 3.2.22.5)
71 | activesupport (= 3.2.22.5)
72 | bundler (~> 1.0)
73 | railties (= 3.2.22.5)
74 | rails-erd (1.5.2)
75 | activerecord (>= 3.2)
76 | activesupport (>= 3.2)
77 | choice (~> 0.2.0)
78 | ruby-graphviz (~> 1.2)
79 | railties (3.2.22.5)
80 | actionpack (= 3.2.22.5)
81 | activesupport (= 3.2.22.5)
82 | rack-ssl (~> 1.3.2)
83 | rake (>= 0.8.7)
84 | rdoc (~> 3.4)
85 | thor (>= 0.14.6, < 2.0)
86 | rake (12.2.1)
87 | rdoc (3.12.2)
88 | json (~> 1.4)
89 | rexml (3.2.5)
90 | ruby-graphviz (1.2.5)
91 | rexml
92 | sprockets (2.2.3)
93 | hike (~> 1.2)
94 | multi_json (~> 1.0)
95 | rack (~> 1.0)
96 | tilt (~> 1.1, != 1.3.0)
97 | sqlite3 (1.3.13)
98 | thor (0.20.0)
99 | tilt (1.4.1)
100 | treetop (1.4.15)
101 | polyglot
102 | polyglot (>= 0.3.1)
103 | tzinfo (0.3.61)
104 |
105 | PLATFORMS
106 | ruby
107 |
108 | DEPENDENCIES
109 | pry
110 | rails (= 3.2.22.5)
111 | rails_upgrader!
112 | sqlite3
113 |
114 | BUNDLED WITH
115 | 1.17.3
116 |
--------------------------------------------------------------------------------
/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to make participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, sex characteristics, gender identity and expression,
9 | level of experience, education, socio-economic status, nationality, personal
10 | appearance, race, religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies within all project spaces, and it also applies when
49 | an individual is representing the project or its community in public spaces.
50 | Examples of representing a project or community include using an official
51 | project e-mail address, posting via an official social media account, or acting
52 | as an appointed representative at an online or offline event. Representation of
53 | a project may be further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the project team at oss@ombulabs.com. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 | For answers to common questions about this code of conduct, see
76 | https://www.contributor-covenant.org/faq
77 |
78 |
--------------------------------------------------------------------------------