ae-be_strong 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: c8215353560337f496f3af674c6776a4eb1fb91d
4
+ data.tar.gz: ecb9edac2ce95ded0056d327ed519a5f45fe30bb
5
+ SHA512:
6
+ metadata.gz: c833a4ff3843d5fa7bdd2536698adeae0da6b74d252d1d9e72374773d522757f11e21cd8de444d5c4c90c963382a6c64b40773bfe45f365a03421754cc056d4e
7
+ data.tar.gz: 66c74c29941037bd85b90d894aceffc3b71858af6ff2ace49e2a58f09d094279449eecab9dbcb01837d5f37251bd2d921e889c1f41c14d2a25de648d7d4061b2
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ /spec/dummy_app
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.3.3
5
+ - ruby-head
6
+
7
+ before_install:
8
+ - gem install bundler -v 1.15.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in be_strong.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 miyakey
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,84 @@
1
+ # BeStrong [![Build Status](https://travis-ci.org/appfolio/be_strong.svg?branch=appfolio)](https://travis-ci.org/appfolio/be_strong)
2
+
3
+ BeStrong is a tool for converting your `attr_accessible` and `attr_protected` to **strong parameter** method.
4
+ This is useful if you are upgrading a large Rails app.
5
+
6
+ +This is a bug fix fork of the original [be_strong](https://github.com/monochromegane/be_strong) gem by [monochromegane](https://github.com/monochromegane).
7
+
8
+ ## Examples
9
+
10
+ If your code isn't strong like the following:
11
+
12
+ ```rb
13
+ # Model
14
+ class Author < ActiveRecord::Base
15
+ attr_accessible :name, :age
16
+ end
17
+
18
+ # Controller
19
+ class AuthorsController < ApplicationController
20
+ def create
21
+ Author.new(params[:author])
22
+ ...
23
+ end
24
+ end
25
+ ```
26
+
27
+ After run `be_strong` command:
28
+
29
+ ```rb
30
+ # Strong Model
31
+ class Author < ActiveRecord::Base
32
+ end
33
+
34
+ # Strong Controller
35
+ class AuthorsController < ApplicationController
36
+ def create
37
+ Author.new(author_params)
38
+ ...
39
+ end
40
+
41
+ private
42
+
43
+ def author_params
44
+ params.require(:author).permit(:name, :age)
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Feature
50
+
51
+ - Generate strong parameter method from model.
52
+ - Apply strong parameter method to controller.
53
+ - Remove `attr_accessible` method from model.
54
+ - Remove `attr_protected` method from model.
55
+
56
+ ## Installation
57
+
58
+ Add this line to your application's Gemfile:
59
+
60
+ ```ruby
61
+ gem 'ae-be_strong'
62
+ ```
63
+
64
+ ## Usage
65
+
66
+ ```sh
67
+ $ cd your-project
68
+ $ bundle exec rake be_strong:convert
69
+ ```
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/appfolio/be_strong).
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
84
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/be_strong.gemspec ADDED
@@ -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 'be_strong/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ae-be_strong"
8
+ spec.version = BeStrong::VERSION
9
+ spec.authors = ["AppFolio"]
10
+ spec.email = ["dev@appfolio.com"]
11
+
12
+ spec.summary = %q{Strong parameter converter.}
13
+ spec.description = %q{Strong parameter converter.}
14
+ spec.homepage = "https://github.com/appfolio/be_strong"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rails"
27
+ spec.add_development_dependency "sqlite3"
28
+ spec.add_development_dependency "protected_attributes"
29
+ end
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "be_strong"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,23 @@
1
+ module BeStrong
2
+ class AttrAccessible
3
+ class << self
4
+ def remove_all(dir)
5
+ files = []
6
+ Dir.glob(File.join(dir, '**/*.rb')).each do |file|
7
+ removed = remove(file)
8
+ files << file if removed
9
+ end
10
+ files
11
+ end
12
+
13
+ def remove(file)
14
+ buf = File.open(file, 'r'){|f| f.read}
15
+ code = Code.new(buf).remove_attr_accessible_and_protected!
16
+ return false unless code.changed?
17
+
18
+ File.open(file, 'w'){|f| f.puts(code.to_str)}
19
+ true
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,79 @@
1
+ module BeStrong
2
+ class Code
3
+ include Comparable
4
+
5
+ REG_MASS_ASSIGNMENT_METHOD = /((new|build|build_.*|update|update!|assign_attributes|update_attributes|update_attributes!)([\( )])params\[:(\w*)\])/
6
+
7
+ def initialize(code)
8
+ @code = code
9
+ @original = code.dup
10
+ end
11
+
12
+ def apply_strong_parameter!
13
+ # replace params[:model] to model_params
14
+ models = []
15
+ code.gsub!(REG_MASS_ASSIGNMENT_METHOD) do
16
+ if StrongParameterMethods.method_for($4)
17
+ models << $4
18
+ "#{$2}#{$3}#{$4}_params"
19
+ else
20
+ $1
21
+ end
22
+ end
23
+
24
+ return self if models.size.zero?
25
+
26
+ # add private section
27
+ add_private!
28
+
29
+ # add model_params method as private method
30
+ models.each do |model|
31
+ method = StrongParameterMethods.method_for(model)
32
+ next unless method
33
+
34
+ next if code.include?("def #{model}_params")
35
+
36
+ code.sub!(/^ private$/) do
37
+ " private\n\n#{method.gsub(/^/, ' ').chomp}"
38
+ end
39
+ end
40
+
41
+ self
42
+ end
43
+
44
+ def add_private!
45
+ unless code.include?('private')
46
+ code.sub!(/^end/) do
47
+ "\n private\nend"
48
+ end
49
+ end
50
+ self
51
+ end
52
+
53
+ def remove_attr_accessible_and_protected!
54
+ %w(accessible protected).each do |name|
55
+ code.gsub!(/( *attr_#{name}\(.*?\)$)/m, '')
56
+ code.gsub!(/( *attr_#{name}.+?[^,\\]$)/m, '')
57
+ end
58
+ self
59
+ end
60
+
61
+ def changed?
62
+ code != @original
63
+ end
64
+
65
+ def to_str
66
+ code
67
+ end
68
+
69
+ def <=>(other)
70
+ code <=> other
71
+ end
72
+
73
+ private
74
+
75
+ def code
76
+ @code
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,9 @@
1
+ module BeStrong
2
+ class Converter
3
+ def self.convert(controller_path: 'app/controllers', model_path: 'app/models')
4
+ applied = StrongParameter.apply_all(controller_path)
5
+ removed = AttrAccessible.remove_all(model_path)
6
+ {applied: applied, removed: removed}
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module BeStrong
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load 'be_strong/rails/tasks.rake'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ namespace :be_strong do
2
+ desc 'Apply strong parameter method and remove attr_accessible, attr_protected.'
3
+ task convert: :environment do
4
+ result = BeStrong::Converter.convert
5
+ result[:applied].each{|file| puts "Apply strong parameter: #{file}"}
6
+ result[:removed].each{|file| puts "Remove attr_[accessible|protected]: #{file}"}
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ module BeStrong
2
+ class StrongParameter
3
+ class << self
4
+ def apply_all(dir)
5
+ files = []
6
+ Dir.glob(File.join(dir, '**/*.rb')).each do |file|
7
+ applied = apply(file)
8
+ files << file if applied
9
+ end
10
+ files
11
+ end
12
+
13
+ def apply(file)
14
+ buf = File.open(file, 'r'){|f| f.read}
15
+ code = Code.new(buf).apply_strong_parameter!
16
+ return false unless code.changed?
17
+
18
+ File.open(file, 'w'){|f| f.puts(code.to_str)}
19
+ true
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ module BeStrong
2
+ class StrongParameterMethods
3
+ def self.method_for(name)
4
+ model = Model.new(name)
5
+ permits = model.accessible_attributes.map{|attr| ":#{attr}"}.join(', ')
6
+ permit_method = permits.present? ? ".permit(#{permits})" : ''
7
+
8
+ <<-"EOS".strip_heredoc
9
+ def #{name}_params
10
+ params.require(:#{name})#{permit_method}
11
+ end
12
+ EOS
13
+ rescue NameError
14
+ nil
15
+ end
16
+
17
+ class Model
18
+ def initialize(name)
19
+ @klass = name.to_s.classify.constantize
20
+ end
21
+
22
+ def accessible_attributes
23
+ accssible = @klass.accessible_attributes
24
+ return accssible if accssible.size.nonzero?
25
+
26
+ @klass.attribute_names - @klass.protected_attributes.to_a
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module BeStrong
2
+ VERSION = "0.1.0"
3
+ end
data/lib/be_strong.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "be_strong/version"
2
+ require "be_strong/strong_parameter.rb"
3
+ require "be_strong/attr_accessible.rb"
4
+ require "be_strong/code.rb"
5
+ require "be_strong/strong_parameter_methods.rb"
6
+ require "be_strong/converter.rb"
7
+ require "be_strong/rails/railtie.rb" if defined?(Rails)
8
+
9
+ module BeStrong
10
+ # Your code goes here...
11
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ae-be_strong
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AppFolio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-07-07 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.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
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
+ - !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
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: protected_attributes
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Strong parameter converter.
112
+ email:
113
+ - dev@appfolio.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - be_strong.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/be_strong.rb
130
+ - lib/be_strong/attr_accessible.rb
131
+ - lib/be_strong/code.rb
132
+ - lib/be_strong/converter.rb
133
+ - lib/be_strong/rails/railtie.rb
134
+ - lib/be_strong/rails/tasks.rake
135
+ - lib/be_strong/strong_parameter.rb
136
+ - lib/be_strong/strong_parameter_methods.rb
137
+ - lib/be_strong/version.rb
138
+ homepage: https://github.com/appfolio/be_strong
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.5.2
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Strong parameter converter.
162
+ test_files: []