synvert 0.0.14 → 0.0.15
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/synvert.rb +1 -0
- data/lib/synvert/snippets/rails/strong_parameters.rb +25 -3
- data/lib/synvert/version.rb +1 -1
- data/spec/synvert/snippets/rails/strong_parameters_spec.rb +55 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86cf9cff8facbc13e13dca8fe4399f734a9c0601
|
4
|
+
data.tar.gz: 0b2ac36a7bb59335b7026519e9e7428c45edc696
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3be12c7d6461d5586e5fd83c7001685a15af73dac62e23034226e64c458d4588062cf00f948421bda51c5bae79c8a241afd5894978843bf4973708350229e56d
|
7
|
+
data.tar.gz: 2f121ff75f6c9c206720ffe28c5d7d6b4e11f91449a9799e32461a63c8e9a5495d24dacdbf166dc38a1be96cdbfd029cdc7ac5396b54475b1c6501c13acf0cae
|
data/CHANGELOG.md
CHANGED
data/lib/synvert.rb
CHANGED
@@ -7,7 +7,7 @@ It uses string_parameters to replace attr_accessible.
|
|
7
7
|
config.active_record.whitelist_attributes = ...
|
8
8
|
config.active_record.mass_assignment_sanitizer = ...
|
9
9
|
|
10
|
-
2. it removes attr_accessible code in models.
|
10
|
+
2. it removes attr_accessible and attr_protected code in models.
|
11
11
|
|
12
12
|
3. it adds xxx_params in controllers
|
13
13
|
|
@@ -32,13 +32,34 @@ It uses string_parameters to replace attr_accessible.
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
+
attributes = {}
|
36
|
+
within_file 'db/schema.rb' do
|
37
|
+
within_node type: 'block', caller: {type: 'send', message: 'create_table'} do
|
38
|
+
object_name = eval(node.caller.arguments.first.source(self)).singularize
|
39
|
+
attributes[object_name] = []
|
40
|
+
with_node type: 'send', receiver: 't' do
|
41
|
+
attribute_name = eval(node.arguments.first.source(self)).to_sym
|
42
|
+
attributes[object_name] << attribute_name
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
35
47
|
parameters = {}
|
36
48
|
within_files 'app/models/**/*.rb' do
|
37
49
|
# assign and remove attr_accessible ...
|
38
50
|
within_node type: 'class' do
|
39
51
|
object_name = node.name.source(self).underscore
|
40
52
|
with_node type: 'send', message: 'attr_accessible' do
|
41
|
-
parameters[object_name] = node.arguments.map { |key| key.source(self) }
|
53
|
+
parameters[object_name] = node.arguments.map { |key| eval(key.source(self)) }
|
54
|
+
remove
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# assign and remove attr_protected ...
|
59
|
+
within_node type: 'class' do
|
60
|
+
object_name = node.name.source(self).underscore
|
61
|
+
with_node type: 'send', message: 'attr_protected' do
|
62
|
+
parameters[object_name] = attributes[object_name] - node.arguments.map { |key| eval(key.source(self)) }
|
42
63
|
remove
|
43
64
|
end
|
44
65
|
end
|
@@ -50,9 +71,10 @@ It uses string_parameters to replace attr_accessible.
|
|
50
71
|
if_exist_node type: 'send', receiver: 'params', message: '[]', arguments: [object_name.to_sym] do
|
51
72
|
if parameters[object_name]
|
52
73
|
# append def xxx_params; ...; end
|
74
|
+
permit_params = ":" + parameters[object_name].join(", :")
|
53
75
|
unless_exist_node type: 'def', name: "#{object_name}_params" do
|
54
76
|
new_code = "def #{object_name}_params\n"
|
55
|
-
new_code << " params.require(:#{object_name}).permit(#{
|
77
|
+
new_code << " params.require(:#{object_name}).permit(#{permit_params})\n"
|
56
78
|
new_code << "end"
|
57
79
|
append new_code
|
58
80
|
end
|
data/lib/synvert/version.rb
CHANGED
@@ -30,6 +30,27 @@ end
|
|
30
30
|
'''}
|
31
31
|
let(:post_model_rewritten_content) {'''
|
32
32
|
class Post < ActiveRecord::Base
|
33
|
+
end
|
34
|
+
'''}
|
35
|
+
let(:user_model_content) {'''
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
attr_protected :role, :admin
|
38
|
+
end
|
39
|
+
'''}
|
40
|
+
let(:user_model_rewritten_content) {'''
|
41
|
+
class User < ActiveRecord::Base
|
42
|
+
end
|
43
|
+
'''}
|
44
|
+
let(:schema_content) {'''
|
45
|
+
ActiveRecord::Schema.define(version: 20140211112752) do
|
46
|
+
create_table "users", force: true do |t|
|
47
|
+
t.string "login"
|
48
|
+
t.string "email"
|
49
|
+
t.datetime "created_at"
|
50
|
+
t.datetime "updated_at"
|
51
|
+
t.integer "role", default: 0, null: false
|
52
|
+
t.boolean "admin", default: false, null: false
|
53
|
+
end
|
33
54
|
end
|
34
55
|
'''}
|
35
56
|
let(:posts_controller_content) {'''
|
@@ -60,18 +81,52 @@ class PostsController < ApplicationController
|
|
60
81
|
end
|
61
82
|
end
|
62
83
|
'''}
|
84
|
+
let(:users_controller_content) {'''
|
85
|
+
class UsersController < ApplicationController
|
86
|
+
def update
|
87
|
+
@user = User.find(params[:id])
|
88
|
+
if @user.update_attributes params[:user]
|
89
|
+
redirect_to user_path(@user)
|
90
|
+
else
|
91
|
+
render :action => :edit
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
'''}
|
96
|
+
let(:users_controller_rewritten_content) {'''
|
97
|
+
class UsersController < ApplicationController
|
98
|
+
def update
|
99
|
+
@user = User.find(params[:id])
|
100
|
+
if @user.update_attributes user_params
|
101
|
+
redirect_to user_path(@user)
|
102
|
+
else
|
103
|
+
render :action => :edit
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def user_params
|
108
|
+
params.require(:user).permit(:login, :email, :created_at, :updated_at)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
'''}
|
63
112
|
|
64
113
|
it 'process' do
|
65
114
|
FileUtils.mkdir_p 'config'
|
115
|
+
FileUtils.mkdir_p 'db'
|
66
116
|
FileUtils.mkdir_p 'app/models'
|
67
117
|
FileUtils.mkdir_p 'app/controllers'
|
68
118
|
File.write 'config/application.rb', application_content
|
119
|
+
File.write 'db/schema.rb', schema_content
|
69
120
|
File.write 'app/models/post.rb', post_model_content
|
121
|
+
File.write 'app/models/user.rb', user_model_content
|
70
122
|
File.write 'app/controllers/posts_controller.rb', posts_controller_content
|
123
|
+
File.write 'app/controllers/users_controller.rb', users_controller_content
|
71
124
|
@rewriter.process
|
72
125
|
expect(File.read 'config/application.rb').to eq application_rewritten_content
|
73
126
|
expect(File.read 'app/models/post.rb').to eq post_model_rewritten_content
|
127
|
+
expect(File.read 'app/models/user.rb').to eq user_model_rewritten_content
|
74
128
|
expect(File.read 'app/controllers/posts_controller.rb').to eq posts_controller_rewritten_content
|
129
|
+
expect(File.read 'app/controllers/users_controller.rb').to eq users_controller_rewritten_content
|
75
130
|
end
|
76
131
|
end
|
77
132
|
end
|