action_controller-parents 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -1
- data/gemfiles/activesupport_3.gemfile.lock +1 -1
- data/gemfiles/activesupport_4.gemfile.lock +1 -1
- data/lib/action_controller/parents.rb +3 -39
- data/lib/action_controller/parents/finder.rb +41 -0
- data/lib/action_controller/parents/methods.rb +33 -0
- data/lib/action_controller/parents/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/support/dummy_model.rb +5 -0
- data/spec/unit/action_controller-parents/finder_spec.rb +41 -0
- data/spec/unit/action_controller-parents/methods_spec.rb +33 -0
- data/spec/unit/action_controller-parents_spec.rb +4 -57
- metadata +10 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a9f2bdc1c4e85e3c0d39e223566ee29b1ec14cd
|
4
|
+
data.tar.gz: 8788d5de3a3a647774347603be67f560e46f9a5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0d81e8a1697bb974675e72a4a5e0aa2e75ab209a25ceed5931c76cd68866fa052fd297065d78fbdb527438f086a1898c51616f0366cb9c7f68139f857f7f6ba8
|
7
|
+
data.tar.gz: fe34b89177ceb2248ec54f25c2c6341ca097e80ded2216575198c6df09b58204d78ab541d23468f65d4e8d4db27a26912bc02c29bf2d2ff5637f2af98ed9b170
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# ActionController::Parents
|
2
2
|
|
3
|
+
[![Gem
|
4
|
+
Version](https://badge.fury.io/rb/action_controller-parents.png)](http://badge.fury.io/rb/action_controller-parents)
|
3
5
|
[![Build
|
4
6
|
Status](https://travis-ci.org/mcls/action_controller-parents.png?branch=master)](https://travis-ci.org/mcls/action_controller-parents)
|
5
7
|
|
@@ -21,7 +23,26 @@ Or install it yourself as:
|
|
21
23
|
|
22
24
|
## Usage
|
23
25
|
|
24
|
-
|
26
|
+
Extend your `ApplicationController` with the
|
27
|
+
`ActionController::Parents::Methods` module and use `parent_resources`:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ApplicationController < ActionController::Base
|
31
|
+
extend ActionController::Parents::Methods
|
32
|
+
end
|
33
|
+
|
34
|
+
class MembersController < ApplicationController
|
35
|
+
parent_resources Group, Organization
|
36
|
+
|
37
|
+
# GET /organizations/:organization_id/members
|
38
|
+
# GET /groups/:group_id/members
|
39
|
+
def index
|
40
|
+
@members = parent_resource.members
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
Or use `ActionController::Parents` directly:
|
25
46
|
|
26
47
|
```ruby
|
27
48
|
class MembersController < ActionController::Base
|
@@ -35,6 +56,7 @@ Example:
|
|
35
56
|
end
|
36
57
|
```
|
37
58
|
|
59
|
+
|
38
60
|
## Contributing
|
39
61
|
|
40
62
|
1. Fork it
|
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'active_support/core_ext/string/conversions'
|
2
|
+
require 'action_controller/parents/finder'
|
3
|
+
require 'action_controller/parents/methods'
|
2
4
|
require 'action_controller/parents/version'
|
3
5
|
|
4
6
|
module ActionController
|
@@ -41,43 +43,5 @@ module ActionController
|
|
41
43
|
end
|
42
44
|
end
|
43
45
|
end
|
44
|
-
|
45
|
-
# Used to find the parent resource
|
46
|
-
#
|
47
|
-
# @api private
|
48
|
-
#
|
49
|
-
class Finder
|
50
|
-
attr_reader :parent_resource_classes, :primary_keys
|
51
|
-
|
52
|
-
# @param [Class<ActiveRecord::Base>] parent_resource_classes
|
53
|
-
def initialize(*parent_resource_classes)
|
54
|
-
@parent_resource_classes = parent_resource_classes.flatten
|
55
|
-
setup_primary_keys
|
56
|
-
end
|
57
|
-
|
58
|
-
def parent_resource(hsh)
|
59
|
-
key = matched_key(hsh)
|
60
|
-
return nil unless key
|
61
|
-
to_resource_class(key).find_by_id!(hsh[key])
|
62
|
-
end
|
63
|
-
|
64
|
-
def to_resource_class(key)
|
65
|
-
key.to_s.sub(/_id$/, '').classify.constantize
|
66
|
-
end
|
67
|
-
|
68
|
-
def matched_key(hsh)
|
69
|
-
hsh.keys.detect { |key| valid_primary_key?(key) }
|
70
|
-
end
|
71
|
-
|
72
|
-
def valid_primary_key?(key)
|
73
|
-
primary_keys.include?(key.to_s)
|
74
|
-
end
|
75
|
-
|
76
|
-
def setup_primary_keys
|
77
|
-
@primary_keys = parent_resource_classes.map { |klass|
|
78
|
-
klass.to_s.underscore.concat('_id')
|
79
|
-
}
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
46
|
+
end # Parents
|
83
47
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActionController
|
2
|
+
class Parents
|
3
|
+
# Used to find the parent resource
|
4
|
+
#
|
5
|
+
# @api private
|
6
|
+
#
|
7
|
+
class Finder
|
8
|
+
attr_reader :parent_resource_classes, :primary_keys
|
9
|
+
|
10
|
+
# @param [Class<ActiveRecord::Base>] parent_resource_classes
|
11
|
+
def initialize(*parent_resource_classes)
|
12
|
+
@parent_resource_classes = parent_resource_classes.flatten
|
13
|
+
setup_primary_keys
|
14
|
+
end
|
15
|
+
|
16
|
+
def parent_resource(hsh)
|
17
|
+
key = matched_key(hsh)
|
18
|
+
return nil unless key
|
19
|
+
to_resource_class(key).find_by_id!(hsh[key])
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_resource_class(key)
|
23
|
+
key.to_s.sub(/_id$/, '').classify.constantize
|
24
|
+
end
|
25
|
+
|
26
|
+
def matched_key(hsh)
|
27
|
+
hsh.keys.detect { |key| valid_primary_key?(key) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_primary_key?(key)
|
31
|
+
primary_keys.include?(key.to_s)
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup_primary_keys
|
35
|
+
@primary_keys = parent_resource_classes.map { |klass|
|
36
|
+
klass.to_s.underscore.concat('_id')
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module ActionController
|
2
|
+
class Parents
|
3
|
+
# Defines a {#parent_resources} method which can then be used instead of
|
4
|
+
# doing `include ActionController::Parents.new(...)`.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
#
|
8
|
+
# class ApplicationController < ActionController::Base
|
9
|
+
# extend ActionController::Parents::Methods
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# class MembersController < ApplicationController
|
13
|
+
# parent_resources Group, Organization
|
14
|
+
#
|
15
|
+
# def index
|
16
|
+
# @members = parent_resource.members
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
module Methods
|
20
|
+
|
21
|
+
# Includes new instance of {ActionController::Parents}, defining
|
22
|
+
# `#parent_resource` as a result.
|
23
|
+
#
|
24
|
+
# @param [Class<ActiveRecord::Base>] classes
|
25
|
+
#
|
26
|
+
# @return [undefined]
|
27
|
+
#
|
28
|
+
def parent_resources(*classes)
|
29
|
+
include Parents.new(*classes)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActionController::Parents::Finder do
|
4
|
+
let(:nested_resource) { described_class.new(parent_resources) }
|
5
|
+
let(:parent_resources) { [] }
|
6
|
+
|
7
|
+
describe '#parent_resource' do
|
8
|
+
subject { described_class.new(DummyModel).parent_resource(params) }
|
9
|
+
let(:params) { { :dummy_model_id => 1 } }
|
10
|
+
|
11
|
+
it 'calls find_by_id! on the found class' do
|
12
|
+
expect(DummyModel).to receive(:find_by_id!).with(1)
|
13
|
+
subject
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when class not found' do
|
17
|
+
let(:params) { { :random_model_id => 1 } }
|
18
|
+
it 'returns nil' do
|
19
|
+
expect(subject).to be_nil
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#to_resource_class' do
|
25
|
+
subject { described_class.new(DummyModel).to_resource_class(param_key) }
|
26
|
+
let(:param_key) { :dummy_model_id }
|
27
|
+
|
28
|
+
it 'transforms the key to a class' do
|
29
|
+
expect(subject).to eq DummyModel
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#primary_keys' do
|
34
|
+
subject { nested_resource.primary_keys }
|
35
|
+
let(:parent_resources) { [String, Integer] }
|
36
|
+
|
37
|
+
it 'sets the keys based on the parent resources' do
|
38
|
+
expect(subject).to eq %w(string_id integer_id)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
describe ActionController::Parents::Methods do
|
2
|
+
context 'when extended' do
|
3
|
+
subject do
|
4
|
+
mod = described_class
|
5
|
+
@controller ||= Class.new { extend mod }
|
6
|
+
end
|
7
|
+
|
8
|
+
it { should respond_to(:parent_resources) }
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when parent_resources is called' do
|
12
|
+
subject do
|
13
|
+
mod = described_class
|
14
|
+
@controller ||= Class.new do
|
15
|
+
extend mod
|
16
|
+
parent_resources DummyModel
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'includes new ActionController::Parents' do
|
21
|
+
expect(subject.ancestors.map(&:class)).to include(ActionController::Parents)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'the Parents module is instantiated with the same arguments of parent_resources' do
|
25
|
+
expect(ActionController::Parents).to receive(:new).with(DummyModel).and_call_original
|
26
|
+
mod = described_class
|
27
|
+
Class.new do
|
28
|
+
extend mod
|
29
|
+
parent_resources DummyModel
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -1,24 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class DummyController
|
4
|
-
attr_accessor :params
|
5
|
-
def params
|
6
|
-
@params || {}
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class DummyModel
|
11
|
-
def self.find_by_id!(id)
|
12
|
-
DummyModel.new
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
3
|
describe ActionController::Parents do
|
17
4
|
describe 'when included' do
|
18
|
-
let(:dummy) {
|
5
|
+
let(:dummy) { @controller_class.new }
|
19
6
|
|
20
7
|
before(:each) do
|
21
|
-
|
8
|
+
@controller_class = Class.new do
|
9
|
+
include ActionController::Parents.new
|
10
|
+
end
|
22
11
|
end
|
23
12
|
|
24
13
|
it "create the #parent_resource" do
|
@@ -33,47 +22,5 @@ describe ActionController::Parents do
|
|
33
22
|
dummy.parent_resource
|
34
23
|
end
|
35
24
|
end
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
describe ActionController::Parents::Finder do
|
41
|
-
let(:nested_resource) { described_class.new(parent_resources) }
|
42
|
-
let(:parent_resources) { [] }
|
43
|
-
|
44
|
-
describe '#parent_resource' do
|
45
|
-
subject { described_class.new(DummyModel).parent_resource(params) }
|
46
|
-
let(:params) { { :dummy_model_id => 1 } }
|
47
|
-
|
48
|
-
it 'calls find_by_id! on the found class' do
|
49
|
-
expect(DummyModel).to receive(:find_by_id!).with(1)
|
50
|
-
subject
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'when class not found' do
|
54
|
-
let(:params) { { :random_model_id => 1 } }
|
55
|
-
it 'returns nil' do
|
56
|
-
expect(subject).to be_nil
|
57
|
-
end
|
58
|
-
end
|
59
25
|
end
|
60
|
-
|
61
|
-
describe '#to_resource_class' do
|
62
|
-
subject { described_class.new(DummyModel).to_resource_class(param_key) }
|
63
|
-
let(:param_key) { :dummy_model_id }
|
64
|
-
|
65
|
-
it 'transforms the key to a class' do
|
66
|
-
expect(subject).to eq DummyModel
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
describe '#primary_keys' do
|
71
|
-
subject { nested_resource.primary_keys }
|
72
|
-
let(:parent_resources) { [String, Integer] }
|
73
|
-
|
74
|
-
it 'sets the keys based on the parent resources' do
|
75
|
-
expect(subject).to eq %w(string_id integer_id)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
26
|
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.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maarten Claes
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -116,8 +116,13 @@ files:
|
|
116
116
|
- gemfiles/activesupport_4.gemfile
|
117
117
|
- gemfiles/activesupport_4.gemfile.lock
|
118
118
|
- lib/action_controller/parents.rb
|
119
|
+
- lib/action_controller/parents/finder.rb
|
120
|
+
- lib/action_controller/parents/methods.rb
|
119
121
|
- lib/action_controller/parents/version.rb
|
120
122
|
- spec/spec_helper.rb
|
123
|
+
- spec/support/dummy_model.rb
|
124
|
+
- spec/unit/action_controller-parents/finder_spec.rb
|
125
|
+
- spec/unit/action_controller-parents/methods_spec.rb
|
121
126
|
- spec/unit/action_controller-parents_spec.rb
|
122
127
|
homepage: ''
|
123
128
|
licenses:
|
@@ -145,4 +150,7 @@ specification_version: 4
|
|
145
150
|
summary: Easily access parent resources
|
146
151
|
test_files:
|
147
152
|
- spec/spec_helper.rb
|
153
|
+
- spec/support/dummy_model.rb
|
154
|
+
- spec/unit/action_controller-parents/finder_spec.rb
|
155
|
+
- spec/unit/action_controller-parents/methods_spec.rb
|
148
156
|
- spec/unit/action_controller-parents_spec.rb
|