resourcerer 1.0.0 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +191 -180
- data/lib/resourcerer.rb +12 -3
- data/lib/resourcerer/configuration.rb +166 -0
- data/lib/resourcerer/controller.rb +49 -0
- data/lib/resourcerer/resource.rb +195 -10
- data/lib/resourcerer/version.rb +5 -0
- data/spec/features/guitars_controller_spec.rb +51 -0
- data/spec/resourcerer/controller_spec.rb +394 -0
- data/spec/resourcerer/param_key_spec.rb +48 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/rails_app.rb +60 -0
- metadata +99 -29
- data/lib/resourcerer/configuration/strong_parameters.rb +0 -12
- data/lib/resourcerer/inflector.rb +0 -33
- data/lib/resourcerer/resource_configuration.rb +0 -49
- data/lib/resourcerer/resourceable.rb +0 -44
- data/lib/resourcerer/strategies/assign_attributes.rb +0 -34
- data/lib/resourcerer/strategies/assign_from_method.rb +0 -23
- data/lib/resourcerer/strategies/assign_from_params.rb +0 -13
- data/lib/resourcerer/strategies/default_strategy.rb +0 -40
- data/lib/resourcerer/strategies/eager_attributes_strategy.rb +0 -10
- data/lib/resourcerer/strategies/optional_strategy.rb +0 -24
- data/lib/resourcerer/strategies/strong_parameters_strategy.rb +0 -31
- data/lib/resourcerer/strategy.rb +0 -61
@@ -1,34 +0,0 @@
|
|
1
|
-
module Resourcerer
|
2
|
-
module Strategies
|
3
|
-
module AssignAttributes
|
4
|
-
|
5
|
-
def resource
|
6
|
-
super.tap do |r|
|
7
|
-
assign_attributes(r) if r && assign_attributes?
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
def attributes
|
12
|
-
@attributes ||= controller_eval(config.attributes)
|
13
|
-
end
|
14
|
-
|
15
|
-
def assign_attributes?
|
16
|
-
!get? && !delete? && attributes.present?
|
17
|
-
end
|
18
|
-
|
19
|
-
def assign_attributes(resource)
|
20
|
-
resource.attributes = attributes
|
21
|
-
end
|
22
|
-
|
23
|
-
def resource_params
|
24
|
-
params[param_key]
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def param_key
|
30
|
-
config.param_key || inflector.param_key
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'resourcerer/strategies/assign_attributes'
|
2
|
-
|
3
|
-
module Resourcerer
|
4
|
-
module Strategies
|
5
|
-
module AssignFromMethod
|
6
|
-
include AssignAttributes
|
7
|
-
|
8
|
-
def attributes
|
9
|
-
super || attributes_from_method
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def attributes_from_method
|
15
|
-
controller.send(attributes_method) if resource_params
|
16
|
-
end
|
17
|
-
|
18
|
-
def attributes_method
|
19
|
-
config.attributes_method || "#{name}_params"
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'resourcerer/strategy'
|
2
|
-
require 'active_support/core_ext/module/delegation'
|
3
|
-
|
4
|
-
module Resourcerer
|
5
|
-
module Strategies
|
6
|
-
class DefaultStrategy < Strategy
|
7
|
-
delegate :get?, :delete?, to: :request
|
8
|
-
|
9
|
-
def resource
|
10
|
-
if id
|
11
|
-
find_resource(id)
|
12
|
-
else
|
13
|
-
build_resource
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def id
|
18
|
-
@id ||= params[finder_param] || params[finder_attribute]
|
19
|
-
end
|
20
|
-
|
21
|
-
def find_resource(id)
|
22
|
-
controller_eval(config.finder, id) || collection.find_by(finder_attribute => id)
|
23
|
-
end
|
24
|
-
|
25
|
-
def build_resource
|
26
|
-
controller_eval(config.builder) || collection.new
|
27
|
-
end
|
28
|
-
|
29
|
-
protected
|
30
|
-
|
31
|
-
def controller_eval(proc, *args)
|
32
|
-
controller.instance_exec(*args, &proc) if proc
|
33
|
-
end
|
34
|
-
|
35
|
-
def collection
|
36
|
-
controller_eval(config.collection) || model
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'resourcerer/strategies/default_strategy'
|
2
|
-
|
3
|
-
module Resourcerer
|
4
|
-
module Strategies
|
5
|
-
class OptionalStrategy < DefaultStrategy
|
6
|
-
|
7
|
-
def resource
|
8
|
-
find_resource(id) if id
|
9
|
-
end
|
10
|
-
|
11
|
-
def find_resource(id)
|
12
|
-
if config.optional
|
13
|
-
model.where(finder_attribute => id).first
|
14
|
-
else
|
15
|
-
model.find_by(finder_attribute => id)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def id
|
20
|
-
@id ||= params[finder_param]
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'resourcerer/strategies/default_strategy'
|
2
|
-
require 'resourcerer/strategies/assign_from_method'
|
3
|
-
require 'resourcerer/configuration/strong_parameters'
|
4
|
-
|
5
|
-
module Resourcerer
|
6
|
-
module Strategies
|
7
|
-
class StrongParametersStrategy < DefaultStrategy
|
8
|
-
include Strategies::AssignFromMethod
|
9
|
-
|
10
|
-
delegate :permitted_attributes, to: :config
|
11
|
-
|
12
|
-
def attributes
|
13
|
-
strong_attributes || super
|
14
|
-
end
|
15
|
-
|
16
|
-
protected
|
17
|
-
|
18
|
-
def build_configuration(options)
|
19
|
-
Configuration::StrongParameters.new(options)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def strong_attributes
|
25
|
-
if permitted_attributes && params.has_key?(param_key)
|
26
|
-
params.require(param_key).permit(*permitted_attributes)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/lib/resourcerer/strategy.rb
DELETED
@@ -1,61 +0,0 @@
|
|
1
|
-
require 'resourcerer/inflector'
|
2
|
-
require 'resourcerer/resource_configuration'
|
3
|
-
|
4
|
-
module Resourcerer
|
5
|
-
class Strategy
|
6
|
-
attr_reader :controller, :name, :options, :config_proc
|
7
|
-
attr_writer :model, :inflector
|
8
|
-
|
9
|
-
def initialize(controller, name, options={}, config_proc=nil)
|
10
|
-
@controller, @name, @options, @config_proc = controller, name.to_s, options, config_proc
|
11
|
-
end
|
12
|
-
|
13
|
-
def resource
|
14
|
-
raise 'Implement in subclass'
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
# Subclasses may provide an extended version of ResourceConfiguration to extend the DSL
|
20
|
-
def build_configuration(options)
|
21
|
-
ResourceConfiguration.new(options)
|
22
|
-
end
|
23
|
-
|
24
|
-
def config
|
25
|
-
@config ||= build_configuration(options).tap do |rc|
|
26
|
-
rc.instance_eval(&config_proc) if config_proc # This evaluates the configuration in the DSL block
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def inflector
|
31
|
-
@inflector ||= Resourcerer::Inflector.new(name, model, finder_attribute)
|
32
|
-
end
|
33
|
-
|
34
|
-
def model
|
35
|
-
@model ||= case config.model
|
36
|
-
when Class, Module
|
37
|
-
config.model
|
38
|
-
else
|
39
|
-
Resourcerer::Inflector.class_for(config.model || name)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def finder_attribute
|
44
|
-
config.find_by || :id
|
45
|
-
end
|
46
|
-
|
47
|
-
def finder_param
|
48
|
-
config.finder_param ||
|
49
|
-
(config.param_key && "#{config.param_key}_#{finder_attribute}") ||
|
50
|
-
inflector.finder_param
|
51
|
-
end
|
52
|
-
|
53
|
-
def params
|
54
|
-
controller.params
|
55
|
-
end
|
56
|
-
|
57
|
-
def request
|
58
|
-
controller.request
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|