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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 263f00fa376f7527f7915d2239c759f134ef5749
4
- data.tar.gz: f7f14d873285dfc5092e48a959e2e199186b504a
3
+ metadata.gz: 86cf9cff8facbc13e13dca8fe4399f734a9c0601
4
+ data.tar.gz: 0b2ac36a7bb59335b7026519e9e7428c45edc696
5
5
  SHA512:
6
- metadata.gz: 9d294b839f70a0f8a8243dbba1e6fa8735322016d9292db89992933129065dcffcee790becee77218eaaa47bfc85b14191f8df1b04616403f7649f47c38de54d
7
- data.tar.gz: 1a3654f5b903b1398bfedf7078381505b69c83cbac8600bd0f3c57251240c343a2ea061b7b71339af39dd1c44826327d0a3b879075d958de5a72f40149eeb0da
6
+ metadata.gz: 3be12c7d6461d5586e5fd83c7001685a15af73dac62e23034226e64c458d4588062cf00f948421bda51c5bae79c8a241afd5894978843bf4973708350229e56d
7
+ data.tar.gz: 2f121ff75f6c9c206720ffe28c5d7d6b4e11f91449a9799e32461a63c8e9a5495d24dacdbf166dc38a1be96cdbfd029cdc7ac5396b54475b1c6501c13acf0cae
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.0.15
4
+
5
+ * Support attr_protected for strong_parameters snippet.
6
+
3
7
  ## 0.0.14
4
8
 
5
9
  * Complement code comments.
@@ -13,4 +13,5 @@ module Synvert
13
13
  autoload :Rewriter, 'synvert/rewriter'
14
14
  autoload :RewriterNotFound, 'synvert/exceptions'
15
15
  autoload :GemfileLockNotFound, 'synvert/exceptions'
16
+ autoload :MethodNotSupported, 'synvert/exceptions'
16
17
  end
@@ -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) }.join(', ')
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(#{parameters[object_name]})\n"
77
+ new_code << " params.require(:#{object_name}).permit(#{permit_params})\n"
56
78
  new_code << "end"
57
79
  append new_code
58
80
  end
@@ -1,5 +1,5 @@
1
1
  # coding: utf-8
2
2
 
3
3
  module Synvert
4
- VERSION = "0.0.14"
4
+ VERSION = "0.0.15"
5
5
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synvert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Huang