be_strong 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6722db649de25140197ef5ac456c18a559412c3f
4
+ data.tar.gz: cdfee4026165da233d3771c20ed821af6bf26042
5
+ SHA512:
6
+ metadata.gz: a1ff1e1d18db545f882bc323505d9445c5cf9007dfc95a73a5eca619e74768d4ce67fdc84ebdb45826473aac3cf16fe910541369a96c212943a636297e3569fd
7
+ data.tar.gz: a16dd4b4a176f82ba3074d0c5abc4ad6ffe6d08865010182196e0f69696cdb85bad95d055757848aa02d2f9cfc567275d989671feb3e74dbd51dabb276f9b46c
@@ -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
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -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
@@ -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.
@@ -0,0 +1,82 @@
1
+ # BeStrong [![Build Status](https://travis-ci.org/monochromegane/be_strong.svg?branch=master)](https://travis-ci.org/monochromegane/be_strong)
2
+
3
+ BeStrong is a tool for converting your `attr_accessible` and `attr_protected` to **strong parameter** method.
4
+
5
+ ## Examples
6
+
7
+ If your code isn't strong like the following:
8
+
9
+ ```rb
10
+ # Model
11
+ class Author < ActiveRecord::Base
12
+ attr_accessible :name, :age
13
+ end
14
+
15
+ # Controller
16
+ class AuthorsController < ApplicationController
17
+ def create
18
+ Author.new(params[:author])
19
+ ...
20
+ end
21
+ end
22
+ ```
23
+
24
+ After run `be_strong` command:
25
+
26
+ ```rb
27
+ # Strong Model
28
+ class Author < ActiveRecord::Base
29
+ end
30
+
31
+ # Strong Controller
32
+ class AuthorsController < ApplicationController
33
+ def create
34
+ Author.new(author_params)
35
+ ...
36
+ end
37
+
38
+ private
39
+
40
+ def author_params
41
+ params.require(:author).permit(:name, :age)
42
+ end
43
+ end
44
+ ```
45
+
46
+ ## Feature
47
+
48
+ - Generate strong parameter method from model.
49
+ - Apply strong parameter method to controller.
50
+ - Remove attr_accessible method from model.
51
+ - Remove attr_protected method from model.
52
+
53
+ ## Installation
54
+
55
+ Add this line to your application's Gemfile:
56
+
57
+ ```ruby
58
+ gem 'be_strong'
59
+ ```
60
+
61
+ ## Usage
62
+
63
+ ```sh
64
+ $ cd your-project
65
+ $ bundle exec rake be_strong:convert
66
+ ```
67
+
68
+ ## Development
69
+
70
+ 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.
71
+
72
+ 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).
73
+
74
+ ## Contributing
75
+
76
+ Bug reports and pull requests are welcome on GitHub at https://github.com/monochromegane/be_strong. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
77
+
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
82
+
@@ -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
@@ -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 = "be_strong"
8
+ spec.version = BeStrong::VERSION
9
+ spec.authors = ["monochromegane"]
10
+ spec.email = ["dev.kuro.obi@gmail.com"]
11
+
12
+ spec.summary = %q{Strong parameter converter.}
13
+ spec.description = %q{Strong parameter converter.}
14
+ spec.homepage = "https://github.com/monochromegane/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
@@ -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
@@ -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,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
@@ -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
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: be_strong
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - monochromegane
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-18 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.kuro.obi@gmail.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/monochromegane/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.4.5.1
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Strong parameter converter.
162
+ test_files: []