resourcerer 0.2.2 → 2.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +7 -0
- data/README.md +207 -186
- data/lib/resourcerer.rb +12 -3
- data/lib/resourcerer/configuration.rb +166 -0
- data/lib/resourcerer/controller.rb +50 -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 +400 -0
- data/spec/resourcerer/param_key_spec.rb +48 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/rails_app.rb +60 -0
- metadata +103 -32
- data/lib/resourcerer/inflector.rb +0 -33
- data/lib/resourcerer/resource_configuration.rb +0 -45
- data/lib/resourcerer/resourceable.rb +0 -39
- 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 -34
- data/lib/resourcerer/strategies/eager_attributes_strategy.rb +0 -10
- data/lib/resourcerer/strategies/strong_parameters_strategy.rb +0 -10
- data/lib/resourcerer/strategy.rb +0 -55
@@ -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,34 +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 finder_param
|
22
|
-
config.finder_param || inflector.finder_param
|
23
|
-
end
|
24
|
-
|
25
|
-
def find_resource(id)
|
26
|
-
config.finder.try(:call, id) || model.find_by(finder_attribute => id)
|
27
|
-
end
|
28
|
-
|
29
|
-
def build_resource
|
30
|
-
config.builder.try(:call) || model.new
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
data/lib/resourcerer/strategy.rb
DELETED
@@ -1,55 +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 params
|
48
|
-
controller.params
|
49
|
-
end
|
50
|
-
|
51
|
-
def request
|
52
|
-
controller.request
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|