attr_accessible2strong_params 0.0.3

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: 927125c37ab8f459e901579e83fac0d2cf175171
4
+ data.tar.gz: c6d7fe03226735b514a6f8c403d3fb6dc0dc1829
5
+ SHA512:
6
+ metadata.gz: 44d2384e455b0b150d158b06c533732fcfee3896e50b29afa235e0667f8194a871008e2bedd4f3951ecb65c1366531d7168b73cefa3efbb4e40e8e6872956028
7
+ data.tar.gz: 317fd73fc50f2de09376df0397e184397c1bd4073091d9f9757ec6cce3b6a9f66349703acb1131dec0649220bcf7bdd1f68928bacae1240d62c96190f3963baf
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attr_accessible2strong_params.gemspec
4
+ gemspec
5
+
6
+ gem "coveralls", :require => false, :group => :development
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Eric Guo
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # Attr Accessible TO Strong Params
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/attr_accessible2strong_params.svg)](http://badge.fury.io/rb/attr_accessible2strong_params)
4
+ [![Dependency Status](https://gemnasium.com/Eric-Guo/attr_accessible2strong_params.svg)](https://gemnasium.com/Eric-Guo/attr_accessible2strong_params)
5
+ [![Travis CI tests](https://travis-ci.org/Eric-Guo/attr_accessible2strong_params.png)](https://travis-ci.org/Eric-Guo/attr_accessible2strong_params)
6
+ [![Coverage Status](https://coveralls.io/repos/Eric-Guo/attr_accessible2strong_params/badge.png?branch=master)](https://coveralls.io/r/Eric-Guo/attr_accessible2strong_params?branch=master)
7
+ [![Code Climate](https://codeclimate.com/github/Eric-Guo/attr_accessible2strong_params/badges/gpa.svg)](https://codeclimate.com/github/Eric-Guo/attr_accessible2strong_params)
8
+
9
+ Automatically convert Rails 3 attr_accessible to Rails 4 Strong Parameter
10
+
11
+ ## Installation
12
+
13
+ $ gem install attr_accessible2strong_params
14
+
15
+ ## Usage
16
+
17
+ $ aa2sp [filename OR dirname OR .]
18
+
19
+ Run `aa2sp .` in your rails root folder and program will do auto convert. Ensure the Git repository is clean. (You don't want to mix up your changes and auto convert changes, right?)
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << 'test'
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,43 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attr_accessible2strong_params/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attr_accessible2strong_params"
8
+ spec.version = AttrAccessible2StrongParams::VERSION
9
+ spec.required_ruby_version = '>= 1.9.3'
10
+ spec.authors = ['Eric Guo']
11
+ spec.email = %q{eric.guo@sandisk.com}
12
+ spec.description = %q{Automatically convert Rails 3 attr_accessible to Rails 4 Strong Parameter}
13
+ spec.summary = spec.description
14
+ spec.homepage = %q{https://github.com/Eric-Guo/attr_accessible2strong_params}
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files`.split($/) - ['.gitignore','.travis.yml','attr_accessible2strong_params.sublime-project']
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.extra_rdoc_files = ['LICENSE.txt', 'README.md']
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'minitest', '~> 5.4'
26
+
27
+
28
+ spec.required_rubygems_version = Gem::Requirement.new(">= 0") if spec.respond_to? :required_rubygems_version=
29
+ if spec.respond_to? :specification_version then
30
+ spec.specification_version = 3
31
+
32
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
33
+ spec.add_runtime_dependency(%q<astrolabe>, ["~> 1.3"])
34
+ spec.add_runtime_dependency(%q<activesupport>, ['>= 3.2', '< 5.0'])
35
+ else
36
+ spec.add_dependency(%q<astrolabe>, ["~> 1.3"])
37
+ spec.add_dependency(%q<activesupport>, ['>= 3.2', '< 5.0'])
38
+ end
39
+ else
40
+ spec.add_dependency(%q<astrolabe>, ["~> 1.3"])
41
+ spec.add_dependency(%q<activesupport>, ['>= 3.2', '< 5.0'])
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'attr_accessible2strong_params'
4
+ AttrAccessible2StrongParams.convert(ARGV[0])
@@ -0,0 +1,35 @@
1
+ require 'find'
2
+ require 'active_support/core_ext/object'
3
+ require 'active_support/inflector'
4
+
5
+ class AttrAccessible2StrongParams
6
+ def self.convert(file_or_dir, no_rename = false)
7
+ @files = []
8
+ if file_or_dir.nil? or not File.exist?(file_or_dir)
9
+ puts 'aa2sp [filename OR dirname]'
10
+ return
11
+ end
12
+ if File.directory?(file_or_dir)
13
+ Find.find("#{file_or_dir}/app/models/") do |path|
14
+ next if path.end_with? '.rb.rb'
15
+ @files << path if path.end_with? '.rb'
16
+ end
17
+ else
18
+ @files << file_or_dir if File.exist? file_or_dir
19
+ end
20
+ @files.each do |filename|
21
+ c = AttrAccessible2StrongParams::Converter.new
22
+ next if c.read_attr_accessible(filename).blank?
23
+ print(filename, ' found:', c.model_fields)
24
+ c.remove_attr_accessible_from_model(filename, no_rename)
25
+ c.write_controller_with_strong_params(nil, no_rename)
26
+ puts
27
+ end
28
+ end
29
+ end
30
+
31
+ require 'parser/current'
32
+ require 'attr_accessible2strong_params/remove_attr_accessible_rewriter'
33
+ require 'attr_accessible2strong_params/modify_controller_rewriter'
34
+ require 'attr_accessible2strong_params/converter'
35
+ require 'attr_accessible2strong_params/version'
@@ -0,0 +1,52 @@
1
+ require 'astrolabe/builder'
2
+
3
+ class AttrAccessible2StrongParams::Converter
4
+ attr_reader :model_fields
5
+
6
+ def read_attr_accessible(filename)
7
+ @model_file_name = filename
8
+ root_node, comments, buffer = parse_file_with_comments(filename)
9
+ aa_nodes = root_node.each_node(:send).select {|n| n.children[1] == :attr_accessible}
10
+ aa_fields = []
11
+ aa_nodes.each do |m|
12
+ @model_class_name = m.parent.parent.children[0].children[1].to_s
13
+ aa_fields <<= m.each_node(:sym).collect {|n| n.children[0]}
14
+ end
15
+ @model_fields = aa_fields.flatten
16
+ end
17
+
18
+ def remove_attr_accessible_from_model(filename, no_rename = false)
19
+ root_node, comments, buffer = parse_file_with_comments(filename)
20
+ no_aa_src_buffer = Parser::Source::Buffer.new('(string)')
21
+ rewritten_src = RemoveAttrAccessibleRewriter.new.rewrite(buffer,root_node)
22
+ write_file_with_comments(filename, rewritten_src, no_rename)
23
+ end
24
+
25
+ def write_controller_with_strong_params(filename = nil, no_rename = false)
26
+ filename = "#{File.dirname @model_file_name}/../controllers/#{@model_class_name.pluralize.underscore}_controller.rb" if filename.nil?
27
+ return unless File.exist? filename
28
+ root_node, comments, buffer = parse_file_with_comments(filename)
29
+ source = ModifyControllerRewriter.new(@model_class_name, @model_fields).rewrite(buffer,root_node)
30
+ write_file_with_comments(filename, source, no_rename)
31
+ end
32
+
33
+ private
34
+ def parse_file_with_comments(filename)
35
+ parser = Parser::CurrentRuby.new(Astrolabe::Builder.new)
36
+ buffer = Parser::Source::Buffer.new(filename)
37
+ buffer.source = File.read(filename)
38
+ root_node, comments = parser.parse_with_comments(buffer)
39
+ return root_node, comments, buffer
40
+ end
41
+
42
+ def write_file_with_comments(filename, source_code, no_rename)
43
+ temp_path = "#{filename}.rb"
44
+ File.open(temp_path, 'w') do |temp_file|
45
+ temp_file.write(source_code)
46
+ temp_file.puts unless source_code.end_with?(?\n)
47
+ end
48
+
49
+ return 0 if no_rename
50
+ File.rename(temp_path, filename)
51
+ end
52
+ end
@@ -0,0 +1,31 @@
1
+ class ModifyControllerRewriter < Parser::Rewriter
2
+ def initialize(model_class_name, model_fields)
3
+ @model_class_name = model_class_name
4
+ @model_fields = model_fields
5
+ super()
6
+ end
7
+
8
+ def on_class(node)
9
+ if node.children.count == 3 and node.children[1].children[1] == :ApplicationController \
10
+ and node.children[2].begin_type? == true then
11
+ insert_after node.children[2].loc.expression.end, <<-END
12
+
13
+ private
14
+ # Only allow a trusted parameter "white list" through.
15
+ def #{@model_class_name.underscore}_params
16
+ params.require(:#{@model_class_name.underscore}).permit(#{@model_fields.collect {|s| ":#{s}"}.join ", "})
17
+ end
18
+ END
19
+ end
20
+ super
21
+ end
22
+
23
+ def on_send(node)
24
+ if node.children.count == 3 and node.children[1] == :[] \
25
+ and node.children[0].children[1] == :params and node.children[2].sym_type? \
26
+ and node.children[2].children[0].to_s == @model_class_name.underscore then
27
+ replace node.loc.expression, "#{@model_class_name.underscore}_params"
28
+ end
29
+ super
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ class RemoveAttrAccessibleRewriter < Parser::Rewriter
2
+ def on_send(node)
3
+ if node.children[1] == :attr_accessible
4
+ remove(node.loc.expression)
5
+ end
6
+ super
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ class AttrAccessible2StrongParams
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,118 @@
1
+ require("csv")
2
+ class MaterialTransferIssuesController < ApplicationController
3
+ load_and_authorize_resource
4
+ # GET /material_transfer_issues
5
+ # GET /material_transfer_issues.json
6
+ def index
7
+ @q = MaterialTransferIssue.search(params[:q])
8
+ @material_transfer_issues = @q.result.order("created_at DESC").paginate(page: params[:page], per_page: 10)
9
+ respond_to do |format|
10
+ format.html
11
+ # index.html.erb
12
+ format.json do
13
+ render(json: @material_transfer_issues)
14
+ end
15
+ format.csv do
16
+ render_csv_header(:material_transfer_issues.to_s)
17
+ csv_res = CSV.generate do |csv|
18
+ csv << MaterialTransferIssue.new.attributes.keys
19
+ MaterialTransferIssue.where("created_at > ?", 3.months.ago).find_each do |o|
20
+ csv << o.attributes.values
21
+ end
22
+ end
23
+ send_data("\uFEFF" << csv_res)
24
+ end
25
+ end
26
+ end
27
+ # GET /material_transfer_issues/1
28
+ # GET /material_transfer_issues/1.json
29
+ def show
30
+ @material_transfer_issue = MaterialTransferIssue.find(params[:id])
31
+ respond_to do |format|
32
+ format.html
33
+ # show.html.erb
34
+ format.json do
35
+ render(json: @material_transfer_issue)
36
+ end
37
+ end
38
+ end
39
+ # GET /material_transfer_issues/new
40
+ # GET /material_transfer_issues/new.json
41
+ def new
42
+ if params[:last]
43
+ @material_transfer_issue = MaterialTransferIssue.all.last.dup
44
+ else
45
+ @material_transfer_issue = MaterialTransferIssue.new
46
+ end
47
+ # after_controller_new
48
+ respond_to do |format|
49
+ format.html
50
+ # new.html.erb
51
+ format.json do
52
+ render(json: @material_transfer_issue)
53
+ end
54
+ end
55
+ end
56
+ # GET /material_transfer_issues/1/edit
57
+ def edit
58
+ @material_transfer_issue = MaterialTransferIssue.find(params[:id])
59
+ end
60
+ # POST /material_transfer_issues
61
+ # POST /material_transfer_issues.json
62
+ def create
63
+ @material_transfer_issue = MaterialTransferIssue.new(merge_create_fields(params[:material_transfer_issue]))
64
+ respond_to do |format|
65
+ if @material_transfer_issue.save
66
+ format.html do
67
+ redirect_to(material_transfer_issues_path, notice: "Material transfer issue was successfully created.")
68
+ end
69
+ format.json do
70
+ render(json: @material_transfer_issue, status: :created, location: @material_transfer_issue)
71
+ end
72
+ else
73
+ format.html do
74
+ render(action: "new")
75
+ end
76
+ format.json do
77
+ render(json: @material_transfer_issue.errors, status: :unprocessable_entity)
78
+ end
79
+ end
80
+ end
81
+ end
82
+ # PUT /material_transfer_issues/1
83
+ # PUT /material_transfer_issues/1.json
84
+ def update
85
+ @material_transfer_issue = MaterialTransferIssue.find(params[:id])
86
+ respond_to do |format|
87
+ if @material_transfer_issue.update_attributes(merge_update_fields(params[:material_transfer_issue]))
88
+ format.html do
89
+ redirect_to(@material_transfer_issue, notice: "Material transfer issue was successfully updated.")
90
+ end
91
+ format.json do
92
+ head(:no_content)
93
+ end
94
+ else
95
+ format.html do
96
+ render(action: "edit")
97
+ end
98
+ format.json do
99
+ render(json: @material_transfer_issue.errors, status: :unprocessable_entity)
100
+ end
101
+ end
102
+ end
103
+ end
104
+ # DELETE /material_transfer_issues/1
105
+ # DELETE /material_transfer_issues/1.json
106
+ def destroy
107
+ @material_transfer_issue = MaterialTransferIssue.find(params[:id])
108
+ @material_transfer_issue.destroy
109
+ respond_to do |format|
110
+ format.html do
111
+ redirect_to(material_transfer_issues_url)
112
+ end
113
+ format.json do
114
+ head(:no_content)
115
+ end
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,5 @@
1
+ class MaterialTransferIssue < ActiveRecord::Base
2
+ attr_accessible(:create_badge, :create_shift_code, :in_time, :out_time)
3
+ audited
4
+ attr_accessible(:profile_check, :update_badge, :update_shift_code)
5
+ end
@@ -0,0 +1,32 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ require 'minitest/autorun'
5
+ require 'attr_accessible2strong_params'
6
+
7
+ class AttrAccessible2StrongParamsTest < MiniTest::Test
8
+ def test_convert_by_folder
9
+ AttrAccessible2StrongParams.convert('test/railsapp', true)
10
+ end
11
+
12
+ def test_convert_by_model_files
13
+ AttrAccessible2StrongParams.convert('test/railsapp/app/models/material_transfer_issue.rb', true)
14
+ end
15
+
16
+ def test_read_model_attr_accessible_sym_list
17
+ c = AttrAccessible2StrongParams::Converter.new
18
+ assert_equal [:create_badge, :create_shift_code, :in_time, :out_time, :profile_check, :update_badge, :update_shift_code] \
19
+ , c.read_attr_accessible('test/railsapp/app/models/material_transfer_issue.rb')
20
+ end
21
+
22
+ def test_remove_attr_accessible_from_model
23
+ c = AttrAccessible2StrongParams::Converter.new
24
+ assert_equal 0, c.remove_attr_accessible_from_model('test/railsapp/app/models/material_transfer_issue.rb', true)
25
+ end
26
+
27
+ def test_write_controller_src
28
+ c = AttrAccessible2StrongParams::Converter.new
29
+ c.read_attr_accessible('test/railsapp/app/models/material_transfer_issue.rb')
30
+ assert_equal 0, c.write_controller_with_strong_params(nil, true)
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attr_accessible2strong_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Eric Guo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-05 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: astrolabe
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ - - "<"
77
+ - !ruby/object:Gem::Version
78
+ version: '5.0'
79
+ type: :runtime
80
+ prerelease: false
81
+ version_requirements: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '3.2'
86
+ - - "<"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ description: Automatically convert Rails 3 attr_accessible to Rails 4 Strong Parameter
90
+ email: eric.guo@sandisk.com
91
+ executables:
92
+ - aa2sp
93
+ extensions: []
94
+ extra_rdoc_files:
95
+ - LICENSE.txt
96
+ - README.md
97
+ files:
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - attr_accessible2strong_params.gemspec
103
+ - bin/aa2sp
104
+ - lib/attr_accessible2strong_params.rb
105
+ - lib/attr_accessible2strong_params/converter.rb
106
+ - lib/attr_accessible2strong_params/modify_controller_rewriter.rb
107
+ - lib/attr_accessible2strong_params/remove_attr_accessible_rewriter.rb
108
+ - lib/attr_accessible2strong_params/version.rb
109
+ - test/railsapp/app/controllers/material_transfer_issues_controller.rb
110
+ - test/railsapp/app/models/material_transfer_issue.rb
111
+ - test/test_attr_accessible2strong_params.rb
112
+ homepage: https://github.com/Eric-Guo/attr_accessible2strong_params
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: 1.9.3
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.2
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: Automatically convert Rails 3 attr_accessible to Rails 4 Strong Parameter
136
+ test_files:
137
+ - test/railsapp/app/controllers/material_transfer_issues_controller.rb
138
+ - test/railsapp/app/models/material_transfer_issue.rb
139
+ - test/test_attr_accessible2strong_params.rb