adequate_exposure 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +40 -1
- data/lib/adequate_exposure/behavior.rb +4 -1
- data/lib/adequate_exposure/exposure.rb +4 -2
- data/lib/adequate_exposure/version.rb +1 -1
- data/spec/controller_spec.rb +11 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5915dfaad2605bed7254feedffb99c2127dacfbc
|
4
|
+
data.tar.gz: 3f18159d981648e7bfb9f0856dcf08c43ebc419f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 423058f319f76c5d7b6c05b93401b32f3febf8d435d265156206f8fe904dd95a59c1fdd35eb598d9f3ad7fc85bb715bb3b5e8913ec5c694fc6e86341ad16bc12
|
7
|
+
data.tar.gz: 8cda1d9cb3b2c6607d3947bb46150e4c8d50182bac4f2a67ce2842387ec5a61df5b255213f03314218aa56e30afb47cde6e4a036974d7c441efd29f687679ebe
|
data/README.md
CHANGED
@@ -34,6 +34,46 @@ ID and try to perform `Thing.find(id)`. If the ID isn't found, it'll call
|
|
34
34
|
`Thing.new(things_params)`. The result will be memoized in an `@exposed_thing`
|
35
35
|
instance variable.
|
36
36
|
|
37
|
+
## Example Controller
|
38
|
+
|
39
|
+
Here's what a standard Rails 4 CRUD controller using Adequate Exposure might look like:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class ThingsController < ApplicationController
|
43
|
+
expose :things, ->{ Thing.all }
|
44
|
+
expose :thing
|
45
|
+
|
46
|
+
def create
|
47
|
+
if thing.save
|
48
|
+
redirect_to thing_path(thing)
|
49
|
+
else
|
50
|
+
render :new
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def update
|
55
|
+
if thing.update(thing_params)
|
56
|
+
redirect_to thing_path(thing)
|
57
|
+
else
|
58
|
+
render :edit
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def destroy
|
63
|
+
thing.destroy
|
64
|
+
redirect_to things_path
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def thing_params
|
70
|
+
params.require(:thing).permit(:foo, :bar)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
```
|
74
|
+
|
75
|
+
## Under the Hood
|
76
|
+
|
37
77
|
The default resolving workflow is pretty powerful and customizable. It could be
|
38
78
|
expressed with the following pseudocode:
|
39
79
|
|
@@ -273,7 +313,6 @@ expose :thing, with: [:cool_find, :cool_build]
|
|
273
313
|
expose :another_thing, with: :cool_build
|
274
314
|
```
|
275
315
|
|
276
|
-
|
277
316
|
## Contributing
|
278
317
|
|
279
318
|
1. Fork it (https://github.com/rwz/adequate_exposure/fork)
|
@@ -1,7 +1,6 @@
|
|
1
1
|
module AdequateExposure
|
2
2
|
module Behavior
|
3
3
|
def fetch
|
4
|
-
computed_scope = scope(model)
|
5
4
|
instance = id ? find(id, computed_scope) : build(build_params, computed_scope)
|
6
5
|
decorate(instance)
|
7
6
|
end
|
@@ -52,5 +51,9 @@ module AdequateExposure
|
|
52
51
|
def model_param_key
|
53
52
|
model.name.underscore
|
54
53
|
end
|
54
|
+
|
55
|
+
def computed_scope
|
56
|
+
scope(model)
|
57
|
+
end
|
55
58
|
end
|
56
59
|
end
|
@@ -74,7 +74,9 @@ module AdequateExposure
|
|
74
74
|
exposure_name = options.fetch(:name)
|
75
75
|
|
76
76
|
if from = options.delete(:from)
|
77
|
-
merge_lambda_option :
|
77
|
+
merge_lambda_option :build, ->{ send(from).send(exposure_name) }
|
78
|
+
merge_lambda_option :model, ->{ nil }
|
79
|
+
merge_lambda_option :id, ->{ nil }
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
@@ -159,7 +161,7 @@ module AdequateExposure
|
|
159
161
|
end
|
160
162
|
|
161
163
|
def assert_singleton_option(name)
|
162
|
-
if options.except(name, :name).any? && options.key?(name)
|
164
|
+
if options.except(name, :name, :decorate).any? && options.key?(name)
|
163
165
|
fail ArgumentError, "Using #{name.inspect} option with other options doesn't make sense"
|
164
166
|
end
|
165
167
|
end
|
data/spec/controller_spec.rb
CHANGED
@@ -348,10 +348,9 @@ describe AdequateExposure::Controller do
|
|
348
348
|
|
349
349
|
context "from option" do
|
350
350
|
it "allows scope to be called from method" do
|
351
|
-
post = double("Post")
|
352
351
|
comments = double("Comments")
|
352
|
+
post = double("Post", comments: comments)
|
353
353
|
allow(controller).to receive(:post).and_return(post)
|
354
|
-
expect(post).to receive(:comments).and_return(comments)
|
355
354
|
expose :comments, from: :post
|
356
355
|
|
357
356
|
expect(controller.comments).to eq(comments)
|
@@ -361,5 +360,15 @@ describe AdequateExposure::Controller do
|
|
361
360
|
action = ->{ expose :thing, from: :foo, parent: :bar }
|
362
361
|
expect(&action).to raise_error(ArgumentError, "Using :from option with other options doesn't make sense")
|
363
362
|
end
|
363
|
+
|
364
|
+
it "should still work with decorate option" do
|
365
|
+
decorated_thing = double("DecoratedThing")
|
366
|
+
thing = double("Thing")
|
367
|
+
foo = double("Foo", thing: thing)
|
368
|
+
expect(controller).to receive(:foo).and_return(foo)
|
369
|
+
expect(controller).to receive(:decorate).with(thing).and_return(decorated_thing)
|
370
|
+
expose :thing, from: :foo, decorate: ->(thing){ decorate(thing) }
|
371
|
+
expect(controller.thing).to eq(decorated_thing)
|
372
|
+
end
|
364
373
|
end
|
365
374
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adequate_exposure
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Pravosud
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|