zertico 2.0.0.alpha.3 → 2.0.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zertico/controller.rb +13 -6
- data/lib/zertico/exceptions/missing_application_responder.rb +9 -0
- data/lib/zertico/exceptions.rb +1 -0
- data/lib/zertico/permitted_params.rb +8 -0
- data/lib/zertico/service/instance_methods.rb +3 -3
- data/lib/zertico/version.rb +1 -1
- data/spec/fake_app/app/permitted_params/users_permitted_params.rb +2 -2
- data/spec/fake_app/app/responders/application_responder.rb +2 -0
- data/spec/fake_app/app/responders/users_responder.rb +3 -1
- data/spec/zertico/controller_integration_spec.rb +5 -5
- data/spec/zertico/controller_spec.rb +22 -0
- data/spec/zertico/permitted_params_spec.rb +24 -5
- data/spec/zertico/service/instance_methods_spec.rb +2 -2
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d8785ae84211d21c303d594ef6afbe8838c56b8
|
4
|
+
data.tar.gz: c0bfdc7220e402fb48f16d0e4574a96bf43aa12c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a52137055dd954360280d921f46e4c671e75636221fc3acb7143c08993a0eeaca0fe882b6653c3f7a857a5d230385a00f1cb3bb71e8487fb2d839896a2ea9e31
|
7
|
+
data.tar.gz: 36b346023b38222766121096cb3994084f213faf1c9780c16558fe1b0e731cb1e76fc478d95c671ada8575ad7d55548b0f1706c4c2671745b7438b012a745bb5
|
data/lib/zertico/controller.rb
CHANGED
@@ -2,9 +2,12 @@ require 'action_controller'
|
|
2
2
|
|
3
3
|
module Zertico
|
4
4
|
class Controller < ActionController::Base
|
5
|
-
attr_reader :service
|
5
|
+
attr_reader :service, :permitted_params
|
6
|
+
|
7
|
+
before_filter :initialize_permitted_params, :only => [:create, :update]
|
6
8
|
|
7
9
|
def initialize
|
10
|
+
raise Zertico::Exceptions::MissingApplicationResponder unless defined?(ApplicationResponder)
|
8
11
|
initialize_service
|
9
12
|
set_responder
|
10
13
|
super
|
@@ -31,14 +34,12 @@ module Zertico
|
|
31
34
|
end
|
32
35
|
|
33
36
|
def create
|
34
|
-
|
35
|
-
instance_variable_set("@#{service.interface_name}", service.create(permitted_params))
|
37
|
+
instance_variable_set("@#{service.interface_name}", service.create(permitted_params.create))
|
36
38
|
respond_with(instance_variable_get("@#{service.interface_name}"), responder.create_options(self))
|
37
39
|
end
|
38
40
|
|
39
41
|
def update
|
40
|
-
|
41
|
-
instance_variable_set("@#{service.interface_name}", service.update(permitted_params))
|
42
|
+
instance_variable_set("@#{service.interface_name}", service.update(params, permitted_params.update))
|
42
43
|
respond_with(instance_variable_get("@#{service.interface_name}"), responder.update_options(self))
|
43
44
|
end
|
44
45
|
|
@@ -55,10 +56,16 @@ module Zertico
|
|
55
56
|
@service = Zertico::Service.new(self.class.name)
|
56
57
|
end
|
57
58
|
|
59
|
+
def initialize_permitted_params
|
60
|
+
@permitted_params = "::#{self.class.name.chomp('Controller').concat('PermittedParams')}".constantize.new(params)
|
61
|
+
rescue NameError
|
62
|
+
@permitted_params = Zertico::PermittedParams.new(params)
|
63
|
+
end
|
64
|
+
|
58
65
|
def set_responder
|
59
66
|
self.class.responder = "::#{self.class.name.gsub('Controller', 'Responder')}".constantize
|
60
67
|
rescue NameError
|
61
|
-
self.class.responder =
|
68
|
+
self.class.responder = ::ApplicationResponder
|
62
69
|
end
|
63
70
|
end
|
64
71
|
end
|
data/lib/zertico/exceptions.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
module Zertico
|
2
2
|
module Exceptions
|
3
|
+
autoload :MissingApplicationResponder, 'zertico/exceptions/missing_application_responder'
|
3
4
|
autoload :MissingStrongParameters, 'zertico/exceptions/missing_strong_parameters'
|
4
5
|
autoload :RollbackException, 'zertico/exceptions/rollback_exception'
|
5
6
|
end
|
@@ -11,6 +11,14 @@ end
|
|
11
11
|
module Zertico
|
12
12
|
class PermittedParams < Delegator
|
13
13
|
alias_method :params, :interface
|
14
|
+
|
15
|
+
def create
|
16
|
+
params[self.class.interface_class.name.underscore.to_sym]
|
17
|
+
end
|
18
|
+
|
19
|
+
def update
|
20
|
+
params[self.class.interface_class.name.underscore.to_sym]
|
21
|
+
end
|
14
22
|
|
15
23
|
def self.interface_class
|
16
24
|
name.chomp('PermittedParams').singularize.constantize
|
@@ -17,9 +17,9 @@ module Zertico
|
|
17
17
|
instance_variable_set("@#{interface_name}", resource_source.create(params))
|
18
18
|
end
|
19
19
|
|
20
|
-
def update(params)
|
21
|
-
|
22
|
-
instance_variable_get("@#{interface_name}").update_attributes(
|
20
|
+
def update(params, instance_params)
|
21
|
+
instance_variable_set("@#{interface_name}", resource_source.find(params[interface_id.to_sym]))
|
22
|
+
instance_variable_get("@#{interface_name}").update_attributes(instance_params)
|
23
23
|
instance_variable_get("@#{interface_name}")
|
24
24
|
end
|
25
25
|
|
data/lib/zertico/version.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
class UsersPermittedParams < Zertico::PermittedParams
|
2
2
|
def create
|
3
|
-
params.require(:user).permit(:
|
3
|
+
params.require(:user).permit(:first_name)
|
4
4
|
end
|
5
5
|
|
6
6
|
def update
|
7
|
-
params.require(:user).permit(:
|
7
|
+
params.require(:user).permit(:last_name)
|
8
8
|
end
|
9
9
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
require "#{File.join(File.dirname(__FILE__), '/application_responder')}"
|
2
|
+
|
3
|
+
class UsersResponder < ApplicationResponder
|
2
4
|
include Pjax
|
3
5
|
|
4
6
|
self.update_options = lambda { |controller| { :location => user_path(controller.user) } }
|
@@ -54,8 +54,8 @@ describe UsersController, :type => :controller do
|
|
54
54
|
end
|
55
55
|
|
56
56
|
it 'should pass the right parameters to the model' do
|
57
|
-
expect(User).to receive(:create).with(:
|
58
|
-
post :create, :user => { :
|
57
|
+
expect(User).to receive(:create).with(:first_name => 'name').and_return(user)
|
58
|
+
post :create, :user => { :first_name => 'name' }
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'should create a new user' do
|
@@ -67,12 +67,12 @@ describe UsersController, :type => :controller do
|
|
67
67
|
before :each do
|
68
68
|
allow(User).to receive(:find).and_return(user)
|
69
69
|
allow(user).to receive(:id).and_return(3)
|
70
|
-
put :update, :id => 1, :user => { :
|
70
|
+
put :update, :id => 1, :user => { :last_name => 'teste' }
|
71
71
|
end
|
72
72
|
|
73
73
|
it 'should pass the right parameters to the model' do
|
74
|
-
expect(user).to receive(:update_attributes).with('
|
75
|
-
put :update, :id => 1, :user => { :
|
74
|
+
expect(user).to receive(:update_attributes).with('last_name' => 'teste').and_return(true)
|
75
|
+
put :update, :id => 1, :user => { :last_name => 'teste' }
|
76
76
|
end
|
77
77
|
|
78
78
|
it 'should update an user' do
|
@@ -37,4 +37,26 @@ describe Zertico::Controller do
|
|
37
37
|
expect(users_controller.responder).to be == UsersResponder
|
38
38
|
end
|
39
39
|
end
|
40
|
+
|
41
|
+
context 'without a custom permitted_params' do
|
42
|
+
before :each do
|
43
|
+
allow(zertico_controller).to receive(:params).and_return({})
|
44
|
+
zertico_controller.send(:initialize_permitted_params)
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should initialize Zertico::PermittedParams' do
|
48
|
+
expect(zertico_controller.permitted_params).to be_an_instance_of(Zertico::PermittedParams)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with a custom permitted_params' do
|
53
|
+
before :each do
|
54
|
+
allow(users_controller).to receive(:params).and_return({})
|
55
|
+
users_controller.send(:initialize_permitted_params)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should initialize it!' do
|
59
|
+
expect(users_controller.permitted_params).to be_an_instance_of(UsersPermittedParams)
|
60
|
+
end
|
61
|
+
end
|
40
62
|
end
|
@@ -2,18 +2,37 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Zertico::PermittedParams do
|
4
4
|
let(:user) { User.new }
|
5
|
-
let(:params) { ActionController::Parameters.new(:
|
5
|
+
let(:params) { ActionController::Parameters.new(:id => 1,
|
6
|
+
:user => { :first_name => "first_name",
|
7
|
+
:last_name => "last_name" }) }
|
6
8
|
let(:user_permitted_params) { UsersPermittedParams.new(params) }
|
9
|
+
let(:zertico_permitted_params) { Zertico::PermittedParams.new(:zertico => { :id => 1, :name => "name" }) }
|
7
10
|
|
8
11
|
describe '#create' do
|
9
|
-
|
10
|
-
|
12
|
+
context 'on a custom PermittedParams' do
|
13
|
+
it "should ignore the id" do
|
14
|
+
expect(user_permitted_params.create).to eq({ 'first_name' => 'first_name' })
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'on Zertico::PermittedParams' do
|
19
|
+
it "should accept all attributes of the hash" do
|
20
|
+
expect(zertico_permitted_params.create).to eq({ :id => 1, :name => 'name' })
|
21
|
+
end
|
11
22
|
end
|
12
23
|
end
|
13
24
|
|
14
25
|
describe '#update' do
|
15
|
-
|
16
|
-
|
26
|
+
context 'on a custom PermittedParams' do
|
27
|
+
it "should accept id and a hash of attributes" do
|
28
|
+
expect(user_permitted_params.update).to eq({ "last_name" => "last_name" })
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'on Zertico::PermittedParams' do
|
33
|
+
it "should accept all attributes of the hash" do
|
34
|
+
expect(zertico_permitted_params.update).to eq({ :id => 1, :name => 'name' })
|
35
|
+
end
|
17
36
|
end
|
18
37
|
end
|
19
38
|
|
@@ -70,11 +70,11 @@ describe Zertico::Service do
|
|
70
70
|
|
71
71
|
describe '#update' do
|
72
72
|
it 'should search for an user' do
|
73
|
-
expect(users_service.update({ :id => 1, :user => {} })).to eq(user)
|
73
|
+
expect(users_service.update({ :user => { :id => 1, :user => {} }}, { :id => 1, :user => {} })).to eq(user)
|
74
74
|
end
|
75
75
|
|
76
76
|
before :each do
|
77
|
-
users_service.update({ :id => 1, :user => {} })
|
77
|
+
users_service.update({ :user => { :id => 1, :user => {} }}, { :id => 1, :user => {} })
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'should update the user' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zertico
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.beta.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paulo Henrique Lopes Ribeiro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/zertico/controller.rb
|
121
121
|
- lib/zertico/delegator.rb
|
122
122
|
- lib/zertico/exceptions.rb
|
123
|
+
- lib/zertico/exceptions/missing_application_responder.rb
|
123
124
|
- lib/zertico/exceptions/missing_strong_parameters.rb
|
124
125
|
- lib/zertico/exceptions/rollback_exception.rb
|
125
126
|
- lib/zertico/interactor.rb
|
@@ -154,6 +155,7 @@ files:
|
|
154
155
|
- spec/fake_app/app/organizers/buy_product_organizer.rb
|
155
156
|
- spec/fake_app/app/organizers/register_organizer.rb
|
156
157
|
- spec/fake_app/app/permitted_params/users_permitted_params.rb
|
158
|
+
- spec/fake_app/app/responders/application_responder.rb
|
157
159
|
- spec/fake_app/app/responders/users_responder.rb
|
158
160
|
- spec/fake_app/app/services/admin/user_service.rb
|
159
161
|
- spec/fake_app/app/services/person/gifts_service.rb
|
@@ -230,6 +232,7 @@ test_files:
|
|
230
232
|
- spec/fake_app/app/organizers/buy_product_organizer.rb
|
231
233
|
- spec/fake_app/app/organizers/register_organizer.rb
|
232
234
|
- spec/fake_app/app/permitted_params/users_permitted_params.rb
|
235
|
+
- spec/fake_app/app/responders/application_responder.rb
|
233
236
|
- spec/fake_app/app/responders/users_responder.rb
|
234
237
|
- spec/fake_app/app/services/admin/user_service.rb
|
235
238
|
- spec/fake_app/app/services/person/gifts_service.rb
|