decent_exposure 2.2.1 → 2.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b2623237fcd46b98cc5ccff366d581a22ca4d90
4
- data.tar.gz: 4c2dafb2461a01d1cdce0ebe3f97ee62788cf202
3
+ metadata.gz: d0b3584786f08fa92a4a19b425f21c2916edf332
4
+ data.tar.gz: 99b35c17b658c58bba300b4079e1a038eafabe3d
5
5
  SHA512:
6
- metadata.gz: 8f9d1123d826932b420c528986513af7d1dfa01f21d64569dd0fdcd64d0cef5d7208b30e9039e67843ebc313a8f82ee99a11ee249778827e657bb59bb482d7c2
7
- data.tar.gz: b35b0c987f0c647dbd10c3a01e9161859281165be3e3857e81647328fbe48c09c01ed898c8569a2a210054e584f6624d8b7c09c99a8c180cd3ecbd7a1d81ec88
6
+ metadata.gz: 940f770c0a27c186bd9ec2d5e7a9540e8dbc007074520d2d107d96bc7690a3ad66bcaf4d5b174541283669b999727f87ba9e8661ee07c7d6fd65eb4bdd70be3a
7
+ data.tar.gz: c196f21d5c6b6edfd9be8c246f31bfaba9d484f9826792f330e0743d9534a316a7cfce96885857702dc8c82a96ad41d8de02727df735303c9e8f7397bde7370c
data/README.md CHANGED
@@ -298,6 +298,20 @@ expose(:environment) { Rails.env }
298
298
  This block is evaluated and the memoized result is returned whenever you call
299
299
  `environment`.
300
300
 
301
+ #### Using the Default decent_exposure Goodness
302
+
303
+ If you don't want to go too far off the beaten path, the value of the default
304
+ exposure can be easily obtained inside of your custom block. The block will
305
+ receive a proxy object that you can use to lazily evaluate
306
+ the default decent_exposure logic. For example:
307
+
308
+ ```ruby
309
+ expose(:articles) {|default| default.limit(10) }
310
+ ```
311
+
312
+ This allows you to customize your exposures, without having to redo all of
313
+ the built-in logic decent_exposure gives you out of the box.
314
+
301
315
  ### Custom strategies
302
316
 
303
317
  For the times when custom behavior is needed for resource finding,
@@ -1,4 +1,5 @@
1
1
  require 'decent_exposure/expose'
2
+ require 'decent_exposure/error'
2
3
 
3
4
  ActiveSupport.on_load(:action_controller) do
4
5
  extend DecentExposure::Expose
@@ -4,6 +4,11 @@ require 'active_support/core_ext/module/delegation'
4
4
  module DecentExposure
5
5
  class ActiveRecordStrategy < Strategy
6
6
  delegate :plural?, :parameter, :to => :inflector
7
+ delegate :get?, :delete?, :to => :request
8
+
9
+ def singular?
10
+ !plural?
11
+ end
7
12
 
8
13
  def collection
9
14
  inflector.plural.to_sym
@@ -38,7 +43,7 @@ module DecentExposure
38
43
  end
39
44
 
40
45
  def collection_resource
41
- return scope if scope.respond_to?(:each)
46
+ return scope if scope.respond_to?(:proxy_association) || scope.respond_to?(:each)
42
47
  scope.send(scope_method)
43
48
  end
44
49
 
@@ -1,31 +1,8 @@
1
1
  require 'decent_exposure/active_record_strategy'
2
+ require 'decent_exposure/strategies/assign_from_params'
2
3
 
3
4
  module DecentExposure
4
5
  class ActiveRecordWithEagerAttributesStrategy < ActiveRecordStrategy
5
- delegate :get?, :to => :request
6
- delegate :delete?, :to => :request
7
-
8
- def singular?
9
- !plural?
10
- end
11
-
12
- def attributes
13
- params[options[:param_key] || inflector.param_key] || {}
14
- end
15
-
16
- def assign_attributes?
17
- return false unless attributes && singular?
18
- (!get? && !delete?) || new_record?
19
- end
20
-
21
- def new_record?
22
- !id
23
- end
24
-
25
- def resource
26
- super.tap do |r|
27
- r.attributes = attributes if assign_attributes?
28
- end
29
- end
6
+ include Strategies::AssignFromParams
30
7
  end
31
8
  end
@@ -0,0 +1,4 @@
1
+ module DecentExposure
2
+ # Public: A generic Error class for decent_exposure
3
+ class Error < ::StandardError; end
4
+ end
@@ -12,6 +12,8 @@ module DecentExposure
12
12
  @_resources ||= {}
13
13
  end
14
14
  hide_action :_resources
15
+
16
+ protected_instance_variables.push("@_resources")
15
17
  end
16
18
  end
17
19
 
@@ -41,17 +43,23 @@ module DecentExposure
41
43
 
42
44
  _exposures[name] = exposure = Strategizer.new(name, options, &block).strategy
43
45
 
46
+ define_exposure_methods(name, exposure)
47
+ end
48
+
49
+ private
50
+
51
+ def define_exposure_methods(name, exposure)
44
52
  define_method(name) do
45
53
  return _resources[name] if _resources.has_key?(name)
46
54
  _resources[name] = exposure.call(self)
47
55
  end
56
+ helper_method name
57
+ hide_action name
48
58
 
49
59
  define_method("#{name}=") do |value|
50
60
  _resources[name] = value
51
61
  end
52
-
53
- helper_method name
54
- hide_action name
62
+ hide_action "#{name}="
55
63
  end
56
64
  end
57
65
  end
@@ -0,0 +1,20 @@
1
+ module DecentExposure
2
+ module Strategies
3
+ module AssignFromMethod
4
+ def attributes
5
+ return @attributes if defined?(@attributes)
6
+ @attributes = controller.send(options[:attributes]) if options[:attributes]
7
+ end
8
+
9
+ def assign_attributes?
10
+ singular? && !get? && !delete? && attributes.present?
11
+ end
12
+
13
+ def resource
14
+ super.tap do |r|
15
+ r.attributes = attributes if assign_attributes?
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module DecentExposure
2
+ module Strategies
3
+ module AssignFromParams
4
+ def attributes
5
+ params[options[:param_key] || inflector.param_key] || {}
6
+ end
7
+
8
+ def assign_attributes?
9
+ return false unless attributes && singular?
10
+ (!get? && !delete?) || new_record?
11
+ end
12
+
13
+ def new_record?
14
+ !id
15
+ end
16
+
17
+ def resource
18
+ r = super
19
+ r.attributes = attributes if assign_attributes?
20
+ r
21
+ end
22
+ end
23
+ end
24
+ end
@@ -14,25 +14,17 @@ module DecentExposure
14
14
  end
15
15
 
16
16
  def strategy
17
- [block_strategy, exposure_strategy].detect(&applicable)
18
- end
19
-
20
- def model
21
- options[:model] || name
17
+ block_strategy || exposure_strategy
22
18
  end
23
19
 
24
20
  private
25
21
 
26
- def applicable
27
- lambda { |s| s }
28
- end
29
-
30
22
  def exposure_strategy
31
23
  Exposure.new(name, exposure_strategy_class, options)
32
24
  end
33
25
 
34
26
  def block_strategy
35
- BlockStrategy.new(block) if block
27
+ BlockStrategy.new(block, exposure_strategy) if block
36
28
  end
37
29
 
38
30
  def exposure_strategy_class
@@ -40,9 +32,23 @@ module DecentExposure
40
32
  end
41
33
  end
42
34
 
43
- BlockStrategy = Struct.new(:block) do
35
+ BlockStrategy = Struct.new(:block, :exposure_strategy) do
44
36
  def call(controller)
45
- controller.instance_eval(&block)
37
+ default = ExposureProxy.new(exposure_strategy, controller)
38
+ controller.instance_exec(default, &block)
39
+ end
40
+ end
41
+
42
+ class ExposureProxy
43
+ instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }
44
+
45
+ def initialize(exposure, controller)
46
+ @exposure, @controller = exposure, controller
47
+ end
48
+
49
+ def method_missing(*args)
50
+ @target ||= @exposure.call(@controller)
51
+ @target.send(*args)
46
52
  end
47
53
  end
48
54
  end
@@ -1,27 +1,8 @@
1
1
  require 'decent_exposure/active_record_strategy'
2
+ require 'decent_exposure/strategies/assign_from_method'
2
3
 
3
4
  module DecentExposure
4
5
  class StrongParametersStrategy < ActiveRecordStrategy
5
- delegate :get?, :to => :request
6
- delegate :delete?, :to => :request
7
-
8
- def singular?
9
- !plural?
10
- end
11
-
12
- def attributes
13
- return @attributes if defined?(@attributes)
14
- @attributes = controller.send(options[:attributes]) if options[:attributes]
15
- end
16
-
17
- def assign_attributes?
18
- singular? && !get? && !delete? && attributes.present?
19
- end
20
-
21
- def resource
22
- super.tap do |r|
23
- r.attributes = attributes if assign_attributes?
24
- end
25
- end
6
+ include Strategies::AssignFromMethod
26
7
  end
27
8
  end
@@ -1,3 +1,3 @@
1
1
  module DecentExposure #:nodoc
2
- VERSION = "2.2.1"
2
+ VERSION = "2.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decent_exposure
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stephen Caudill
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-08-04 00:00:00.000000000 Z
13
+ date: 2013-09-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -83,9 +83,12 @@ files:
83
83
  - lib/decent_exposure/active_record_with_eager_attributes_strategy.rb
84
84
  - lib/decent_exposure/configuration.rb
85
85
  - lib/decent_exposure/constant_resolver.rb
86
+ - lib/decent_exposure/error.rb
86
87
  - lib/decent_exposure/expose.rb
87
88
  - lib/decent_exposure/exposure.rb
88
89
  - lib/decent_exposure/inflector.rb
90
+ - lib/decent_exposure/strategies/assign_from_method.rb
91
+ - lib/decent_exposure/strategies/assign_from_params.rb
89
92
  - lib/decent_exposure/strategizer.rb
90
93
  - lib/decent_exposure/strategy.rb
91
94
  - lib/decent_exposure/strong_parameters_strategy.rb