decent_exposure 2.3.3 → 3.0.0.beta1
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/.gitignore +22 -0
- data/.travis.yml +3 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +200 -363
- data/Rakefile +6 -0
- data/decent_exposure.gemspec +30 -0
- data/decent_exposure.png +0 -0
- data/hashrocket_logo.png +0 -0
- data/lib/decent_exposure.rb +13 -4
- data/lib/decent_exposure/attribute.rb +55 -0
- data/lib/decent_exposure/behavior.rb +100 -0
- data/lib/decent_exposure/context.rb +61 -0
- data/lib/decent_exposure/controller.rb +53 -0
- data/lib/decent_exposure/exposure.rb +199 -9
- data/lib/decent_exposure/flow.rb +89 -0
- data/lib/decent_exposure/version.rb +2 -2
- data/spec/controller_spec.rb +374 -0
- data/spec/integration_spec.rb +26 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/support/rails_app.rb +37 -0
- metadata +63 -41
- data/lib/decent_exposure/active_record_strategy.rb +0 -84
- data/lib/decent_exposure/active_record_with_eager_attributes_strategy.rb +0 -8
- data/lib/decent_exposure/configuration.rb +0 -19
- data/lib/decent_exposure/constant_resolver.rb +0 -34
- data/lib/decent_exposure/error.rb +0 -4
- data/lib/decent_exposure/expose.rb +0 -62
- data/lib/decent_exposure/inflector.rb +0 -39
- data/lib/decent_exposure/strategies/assign_from_method.rb +0 -20
- data/lib/decent_exposure/strategies/assign_from_params.rb +0 -24
- data/lib/decent_exposure/strategizer.rb +0 -54
- data/lib/decent_exposure/strategy.rb +0 -47
- data/lib/decent_exposure/strong_parameters_strategy.rb +0 -8
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'active_support/inflector'
|
2
|
-
require 'active_support/core_ext/string'
|
3
|
-
require 'active_model/naming'
|
4
|
-
|
5
|
-
module DecentExposure
|
6
|
-
class Inflector
|
7
|
-
attr_reader :original, :model
|
8
|
-
|
9
|
-
def initialize(name, model)
|
10
|
-
@original = name.to_s
|
11
|
-
@model = model
|
12
|
-
end
|
13
|
-
|
14
|
-
alias name original
|
15
|
-
|
16
|
-
def model_name
|
17
|
-
@model_name ||= if model.respond_to?(:model_name)
|
18
|
-
model.model_name
|
19
|
-
else
|
20
|
-
ActiveModel::Name.new(model)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
delegate :singular, :plural, :param_key, :to => :model_name
|
25
|
-
alias collection plural
|
26
|
-
|
27
|
-
def parameter
|
28
|
-
"#{model_name.singular}_id"
|
29
|
-
end
|
30
|
-
|
31
|
-
def plural?
|
32
|
-
original.pluralize == original && !uncountable?
|
33
|
-
end
|
34
|
-
|
35
|
-
def uncountable?
|
36
|
-
original.pluralize == original.singularize
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,20 +0,0 @@
|
|
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? && (post? || put? || patch?) && 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
|
@@ -1,24 +0,0 @@
|
|
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
|
-
post? || put? || patch? || 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
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'decent_exposure/exposure'
|
2
|
-
require 'decent_exposure/active_record_with_eager_attributes_strategy'
|
3
|
-
require 'decent_exposure/strong_parameters_strategy'
|
4
|
-
|
5
|
-
module DecentExposure
|
6
|
-
class Strategizer
|
7
|
-
attr_accessor :name, :block, :options, :custom_strategy_class
|
8
|
-
|
9
|
-
def initialize(name, options={})
|
10
|
-
self.name = name
|
11
|
-
self.custom_strategy_class = options.delete(:strategy)
|
12
|
-
self.options = options.merge(:name => name)
|
13
|
-
self.block = Proc.new if block_given?
|
14
|
-
end
|
15
|
-
|
16
|
-
def strategy
|
17
|
-
block_strategy || exposure_strategy
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def exposure_strategy
|
23
|
-
Exposure.new(name, exposure_strategy_class, options)
|
24
|
-
end
|
25
|
-
|
26
|
-
def block_strategy
|
27
|
-
BlockStrategy.new(block, exposure_strategy) if block
|
28
|
-
end
|
29
|
-
|
30
|
-
def exposure_strategy_class
|
31
|
-
custom_strategy_class || ActiveRecordWithEagerAttributesStrategy
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
BlockStrategy = Struct.new(:block, :exposure_strategy) do
|
36
|
-
def call(controller)
|
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)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'decent_exposure/inflector'
|
2
|
-
require 'decent_exposure/constant_resolver'
|
3
|
-
|
4
|
-
module DecentExposure
|
5
|
-
class Strategy
|
6
|
-
attr_reader :controller, :name, :options
|
7
|
-
attr_writer :model, :inflector
|
8
|
-
|
9
|
-
def initialize(controller, name, options={})
|
10
|
-
@controller, @name, @options = controller, name.to_s, options
|
11
|
-
end
|
12
|
-
|
13
|
-
def resource
|
14
|
-
raise 'Implement in subclass'
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
def inflector
|
20
|
-
@inflector ||= DecentExposure::Inflector.new(name, model)
|
21
|
-
end
|
22
|
-
|
23
|
-
def model
|
24
|
-
@model ||= case options[:model]
|
25
|
-
when Class, Module
|
26
|
-
options[:model]
|
27
|
-
else
|
28
|
-
name_or_model = options[:model] || name
|
29
|
-
DecentExposure::ConstantResolver.new(name_or_model.to_s, controller.class).constant
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def params
|
34
|
-
controller.send(params_method)
|
35
|
-
end
|
36
|
-
|
37
|
-
def request
|
38
|
-
controller.request
|
39
|
-
end
|
40
|
-
|
41
|
-
private
|
42
|
-
|
43
|
-
def params_method
|
44
|
-
options[:params] || :params
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|