action_controller-parents 0.2.1 → 0.2.2
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/benchmarks/parent_resource.rb +26 -0
- data/gemfiles/activesupport_3.gemfile.lock +1 -1
- data/gemfiles/activesupport_4.2.gemfile.lock +1 -1
- data/gemfiles/activesupport_4.gemfile.lock +1 -1
- data/lib/action_controller/parents.rb +4 -10
- data/lib/action_controller/parents/finder.rb +21 -11
- data/lib/action_controller/parents/version.rb +1 -1
- data/spec/unit/action_controller-parents/finder_spec.rb +7 -4
- data/spec/unit/action_controller-parents_spec.rb +13 -8
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9955a18a42cd44581214f13676cb0ca148311877
|
4
|
+
data.tar.gz: 18f2101a9eaef3bdda29dcde745ae5c766aaac67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e93d6c25fb728d824831b31d15eab756ea205d11599653734dcf5056561599c4eb9be0be387ac98185a5225cbd8ad7655c50bcc8e92e89080d710fa82b59fb05
|
7
|
+
data.tar.gz: b733a9c41bdc93db5ab24916061eaaf41d65f7652fd240dc26d8b79e813b8aacb123d04dbb28bae52e6d1e8d6ca5600b3731d3c5c91eb78a970048d28e0d0668
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler.require
|
3
|
+
|
4
|
+
require 'benchmark'
|
5
|
+
require 'action_controller/parents'
|
6
|
+
|
7
|
+
class Model
|
8
|
+
class << self
|
9
|
+
def find(id)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
class Group < Model; end
|
14
|
+
class Event < Model; end
|
15
|
+
|
16
|
+
finder = ActionController::Parents::Finder.new(Group, Event)
|
17
|
+
|
18
|
+
n = 100_000
|
19
|
+
params = { stuff: { x: 1, y: 2 }, data: 'bla', event_id: 1 }
|
20
|
+
Benchmark.bm(20) do |x|
|
21
|
+
x.report("parent_resource") {
|
22
|
+
n.times do
|
23
|
+
finder.parent_resource(params)
|
24
|
+
end
|
25
|
+
}
|
26
|
+
end
|
@@ -3,7 +3,7 @@ require 'active_support/core_ext/string/conversions'
|
|
3
3
|
module ActionController
|
4
4
|
# To be included in a controller.
|
5
5
|
#
|
6
|
-
# Creates a `parent_resource` method, which will call `
|
6
|
+
# Creates a `parent_resource` method, which will call `find` using an id
|
7
7
|
# found in the params hash.
|
8
8
|
#
|
9
9
|
# @example Organization and Group as parents
|
@@ -29,15 +29,9 @@ module ActionController
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def initialize(*resource_classes)
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
def included(base)
|
36
|
-
finder = @finder
|
37
|
-
base.class_eval do
|
38
|
-
define_method :parent_resource do
|
39
|
-
finder.parent_resource(params)
|
40
|
-
end
|
32
|
+
finder = Finder.new(resource_classes)
|
33
|
+
define_method :parent_resource do
|
34
|
+
finder.parent_resource(params)
|
41
35
|
end
|
42
36
|
end
|
43
37
|
end # Parents
|
@@ -14,25 +14,35 @@ module ActionController
|
|
14
14
|
# @param [Class<ActiveRecord::Base>] parent_resource_classes
|
15
15
|
def initialize(*parent_resource_classes)
|
16
16
|
@parent_resource_classes = parent_resource_classes.flatten
|
17
|
-
@parent_resource_classes
|
18
|
-
unless c.respond_to?(FIND_METHOD)
|
19
|
-
fail NoFindMethodError,
|
20
|
-
"Parent resource #{c.name} doesn't respond to #{FIND_METHOD.inspect}"
|
21
|
-
end
|
22
|
-
end
|
17
|
+
assert_responds_to_find_method(@parent_resource_classes)
|
23
18
|
setup_primary_keys
|
24
19
|
end
|
25
20
|
|
26
|
-
|
27
|
-
|
21
|
+
# Finds the parent resources from the ActionController params hash
|
22
|
+
# @params [Hash] params
|
23
|
+
# @return [ActiveRecord::Base, nil]
|
24
|
+
def parent_resource(params)
|
25
|
+
key = find_primary_key(params)
|
28
26
|
return nil unless key
|
29
|
-
class_by_key(key).public_send(FIND_METHOD,
|
27
|
+
class_by_key(key).public_send(FIND_METHOD, params.fetch(key))
|
30
28
|
end
|
31
29
|
|
32
30
|
private
|
33
31
|
|
34
|
-
def
|
35
|
-
|
32
|
+
def assert_responds_to_find_method(classes)
|
33
|
+
res = classes.find { |c| !c.respond_to?(FIND_METHOD) }
|
34
|
+
unless res.nil?
|
35
|
+
fail NoFindMethodError,
|
36
|
+
"Parent resource #{res} doesn't respond to #{FIND_METHOD.inspect}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Looks for the primary key of any of the parent resources in the params
|
41
|
+
# hash
|
42
|
+
# @params [Hash] params
|
43
|
+
# @return [String, nil]
|
44
|
+
def find_primary_key(params)
|
45
|
+
params.keys.detect { |key| valid_primary_key?(key) }
|
36
46
|
end
|
37
47
|
|
38
48
|
def valid_primary_key?(key)
|
@@ -1,13 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe ActionController::Parents::Finder do
|
4
|
-
let(:
|
4
|
+
let(:finder) { described_class.new(parent_resources) }
|
5
5
|
let(:parent_resources) { [] }
|
6
6
|
|
7
7
|
it "doesn't allow classes which don't respond to :find method" do
|
8
8
|
expect {
|
9
9
|
described_class.new(String)
|
10
|
-
}.to raise_error(
|
10
|
+
}.to raise_error(
|
11
|
+
ActionController::Parents::NoFindMethodError,
|
12
|
+
"Parent resource String doesn't respond to :find",
|
13
|
+
)
|
11
14
|
end
|
12
15
|
|
13
16
|
describe '#parent_resource' do
|
@@ -28,7 +31,7 @@ describe ActionController::Parents::Finder do
|
|
28
31
|
end
|
29
32
|
|
30
33
|
describe '#primary_keys' do
|
31
|
-
subject {
|
34
|
+
subject { finder.primary_keys }
|
32
35
|
let(:parent_resources) { [DummyModel] }
|
33
36
|
|
34
37
|
it 'sets the keys based on the parent resources' do
|
@@ -55,7 +58,7 @@ describe ActionController::Parents::Finder do
|
|
55
58
|
end
|
56
59
|
|
57
60
|
describe '#primary_keys' do
|
58
|
-
subject {
|
61
|
+
subject { finder.primary_keys }
|
59
62
|
let(:parent_resources) { [DummyModel, Nested::NestedDummyModel] }
|
60
63
|
|
61
64
|
it 'sets the keys based on the parent resources' do
|
@@ -2,24 +2,29 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe ActionController::Parents do
|
4
4
|
describe 'when included' do
|
5
|
-
let(:
|
5
|
+
let(:controller) { @controller_class.new }
|
6
6
|
|
7
7
|
before(:each) do
|
8
8
|
@controller_class = Class.new do
|
9
|
-
include ActionController::Parents.new
|
9
|
+
include ActionController::Parents.new(DummyModel)
|
10
|
+
attr_accessor :params
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
|
-
it "create the #parent_resource" do
|
14
|
-
expect(
|
14
|
+
it "create the #parent_resource method" do
|
15
|
+
expect(controller).to respond_to(:parent_resource)
|
15
16
|
end
|
16
17
|
|
17
18
|
describe '#parent_resource' do
|
18
19
|
it 'deletegates to an instance of Finder' do
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
controller.params = { data: 'bla' }
|
21
|
+
expect_any_instance_of(described_class::Finder).to receive(:parent_resource).with(controller.params)
|
22
|
+
controller.parent_resource
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'returns the result' do
|
26
|
+
controller.params = { dummy_model_id: 1 }
|
27
|
+
expect(controller.parent_resource).to be_a(DummyModel)
|
23
28
|
end
|
24
29
|
end
|
25
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_controller-parents
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maarten Claes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,7 @@ files:
|
|
97
97
|
- README.md
|
98
98
|
- Rakefile
|
99
99
|
- action_controller-parents.gemspec
|
100
|
+
- benchmarks/parent_resource.rb
|
100
101
|
- gemfiles/activesupport_3.gemfile
|
101
102
|
- gemfiles/activesupport_3.gemfile.lock
|
102
103
|
- gemfiles/activesupport_4.2.gemfile
|