lazy_crud 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lazy_crud/version.rb +1 -1
- data/lib/lazy_crud.rb +12 -3
- data/spec/integration/discounts_controller_spec.rb +1 -0
- data/spec/integration/users_controller_spec.rb +89 -0
- data/spec/support/factories.rb +1 -1
- data/spec/support/rails_app/app/models/user.rb +1 -4
- data/spec/support/rails_app/db/development.sqlite3 +0 -0
- data/spec/unit/lazy_crud_spec.rb +22 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 32871f8c7fcf29804cba0f25c3ab1fa585cf0516
|
4
|
+
data.tar.gz: af379a9727d4b46fe0451da95a133512f04ff925
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d23a9e140f80b25061906919c41bb190927b29d7658e3b619545baf2fade1958d0b36c6ec3f297d184591d7aff33bc369992b7959dd5cbf33ef19e0e760a2a37
|
7
|
+
data.tar.gz: 556c8e9fe7e60af0ade37b871e2bca0b783e14252821a6bb8a0c21dcb498fdc4bd223b026485428b5c9e280e250872d1f914c277bad95a1c5dce4ef042713705
|
data/lib/lazy_crud/version.rb
CHANGED
data/lib/lazy_crud.rb
CHANGED
@@ -54,7 +54,7 @@ module LazyCrud
|
|
54
54
|
end
|
55
55
|
|
56
56
|
def create
|
57
|
-
@resource = resource_proxy.
|
57
|
+
@resource = resource_proxy.send(build_method, resource_params)
|
58
58
|
|
59
59
|
# ensure we can still use model name-based instance variables
|
60
60
|
set_resource_instance
|
@@ -105,8 +105,10 @@ module LazyCrud
|
|
105
105
|
@resource = resource_proxy.find(params[:id])
|
106
106
|
end
|
107
107
|
|
108
|
+
# use .try() on the params hash, in case the user forgot to provide
|
109
|
+
# the attributes
|
108
110
|
def resource_params
|
109
|
-
params[resource_singular_name].permit
|
111
|
+
params[resource_singular_name].try(:permit, self.class.param_whitelist)
|
110
112
|
end
|
111
113
|
|
112
114
|
# determines if we want to use the parent class if available or
|
@@ -125,6 +127,13 @@ module LazyCrud
|
|
125
127
|
proxy
|
126
128
|
end
|
127
129
|
|
130
|
+
# if the resource_proxy has a parent, we can use the
|
131
|
+
# build method. Otherwise, resource_proxy, is just the
|
132
|
+
# resource's class - in which case we'll use new
|
133
|
+
def build_method
|
134
|
+
resource_proxy.respond_to?(:build) ? :build : :new
|
135
|
+
end
|
136
|
+
|
128
137
|
# allows all of our views to still use things like
|
129
138
|
# @level, @event, @whatever
|
130
139
|
# rather than just @resource
|
@@ -138,7 +147,7 @@ module LazyCrud
|
|
138
147
|
end
|
139
148
|
|
140
149
|
def parent_instance
|
141
|
-
|
150
|
+
if (not @parent) and self.class.parent_class.present?
|
142
151
|
# e.g.: Event => 'event'
|
143
152
|
parent_instance_name = self.class.parent_class.name.underscore
|
144
153
|
@parent = instance_variable_get("@#{parent_instance_name}")
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
# standard, non-nested resource
|
4
|
+
describe UsersController, type: :controller do
|
5
|
+
|
6
|
+
describe '#index' do
|
7
|
+
it 'sets the collection' do
|
8
|
+
create(:user)
|
9
|
+
get :index
|
10
|
+
|
11
|
+
expect(assigns(:users)).to be_present
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#show' do
|
16
|
+
|
17
|
+
it 'gets the resource' do
|
18
|
+
user = create(:user)
|
19
|
+
get :show, id: user.id
|
20
|
+
|
21
|
+
expect(assigns(:user)).to eq user
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#new' do
|
26
|
+
it 'creates an instance of the object' do
|
27
|
+
get :new
|
28
|
+
expect(assigns(:user)).to be_new_record
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#edit' do
|
33
|
+
|
34
|
+
|
35
|
+
it 'sets the instance of the object' do
|
36
|
+
user = create(:user)
|
37
|
+
get :edit, id: user.id
|
38
|
+
expect(assigns(:user)).to eq user
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#create' do
|
43
|
+
it 'creates and persists a new object' do
|
44
|
+
expect{
|
45
|
+
post :create, user: build(:user).attributes
|
46
|
+
}.to change(User, :count).by 1
|
47
|
+
|
48
|
+
expect(assigns(:user)).to be_present
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'does not create' do
|
52
|
+
expect{
|
53
|
+
post :create
|
54
|
+
}.to change(User, :count).by 0
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#update' do
|
59
|
+
it 'updates and persists changes' do
|
60
|
+
user = create(:user)
|
61
|
+
put :update, id: user.id,
|
62
|
+
user: { name: "updated" }
|
63
|
+
|
64
|
+
updated_user = assigns(:user)
|
65
|
+
expect(updated_user.name).to eq "updated"
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'does not update' do
|
69
|
+
user = create(:user)
|
70
|
+
put :update, id: user.id, user: { name: "" }
|
71
|
+
|
72
|
+
updated_user = assigns(:user)
|
73
|
+
expect(updated_user.errors.full_messages.count).to eq 1
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
describe '#destroy' do
|
79
|
+
it 'destroys the object' do
|
80
|
+
user = create(:user)
|
81
|
+
|
82
|
+
expect{
|
83
|
+
delete :destroy, id: user.id
|
84
|
+
}.to change(User.all, :count).by -1
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
end
|
data/spec/support/factories.rb
CHANGED
Binary file
|
data/spec/unit/lazy_crud_spec.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe LazyCrud do
|
4
|
+
|
5
|
+
class Example < ActionController::Base
|
6
|
+
include LazyCrud
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
describe '#build_method' do
|
11
|
+
|
12
|
+
it 'uses build when the resource_proxy is a class' do
|
13
|
+
example = Example.new
|
14
|
+
allow(example).to receive(:resource_proxy){ Example }
|
15
|
+
|
16
|
+
expect(example.send(:build_method)).to eq :new
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_crud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L. Preston Sego III
|
@@ -197,6 +197,7 @@ files:
|
|
197
197
|
- lib/lazy_crud.rb
|
198
198
|
- lib/lazy_crud/version.rb
|
199
199
|
- spec/integration/discounts_controller_spec.rb
|
200
|
+
- spec/integration/users_controller_spec.rb
|
200
201
|
- spec/rails_helper.rb
|
201
202
|
- spec/spec_helper.rb
|
202
203
|
- spec/support/factories.rb
|
@@ -296,9 +297,10 @@ rubyforge_project:
|
|
296
297
|
rubygems_version: 2.4.6
|
297
298
|
signing_key:
|
298
299
|
specification_version: 4
|
299
|
-
summary: LazyCrud-0.9.
|
300
|
+
summary: LazyCrud-0.9.3
|
300
301
|
test_files:
|
301
302
|
- spec/integration/discounts_controller_spec.rb
|
303
|
+
- spec/integration/users_controller_spec.rb
|
302
304
|
- spec/rails_helper.rb
|
303
305
|
- spec/spec_helper.rb
|
304
306
|
- spec/support/factories.rb
|