action_controller-parents 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.rspec +1 -0
- data/.yardopts +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +11 -0
- data/action_controller-parents.gemspec +27 -0
- data/lib/action_controller/parents/version.rb +5 -0
- data/lib/action_controller/parents.rb +82 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/action_controller-parents_spec.rb +79 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8971a4188ec77e694b85ba6bcc8b716f0e2a3ae2
|
4
|
+
data.tar.gz: bbbd2f28eaa82115a4809f9af0bc2f0702d72363
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 832cb24c7816516c1e9b1a30ab8d2244d1b062bf278e848eb619edfc1ce8a3564ed4da8e50dbf5b2cc97a6416e03fc7dd6a2f195d4fc54ed1b6823f425476041
|
7
|
+
data.tar.gz: 080a58489983e0b8fe582f58c952a436616a32e460555a729293f7a23a96fe8ce3846c50b3793f5c7dcbcec5f1de1b97a71da292e51d140cc9f60fd349c15181
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Maarten Claes
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ActionController::Parents
|
2
|
+
|
3
|
+
Easily access parent resources.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'action_controller-parents'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install action_controller-parents
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Example:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class MembersController < ActionController::Base
|
25
|
+
include Parents.new(Organization, Group)
|
26
|
+
|
27
|
+
# GET /organizations/:organization_id/members
|
28
|
+
# GET /groups/:group_id/members
|
29
|
+
def index
|
30
|
+
@members = parent_resource.members
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
desc 'Run mutation tests'
|
9
|
+
task :mutant do
|
10
|
+
system 'bundle exec mutant --include lib --require "action_controller/parent" --use rspec ::ActionController::Parent*'
|
11
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'action_controller/parents/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "action_controller-parents"
|
8
|
+
spec.version = ActionController::Parents::VERSION
|
9
|
+
spec.authors = ["Maarten Claes"]
|
10
|
+
spec.email = ["maartencls@gmail.com"]
|
11
|
+
spec.description = %q{Easily access parent resources}
|
12
|
+
spec.summary = %q{Easily access parent resources}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "mutant"
|
25
|
+
|
26
|
+
spec.add_dependency "activesupport", ">= 3.0.0"
|
27
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require "action_controller/parents/version"
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
# To be included in a controller.
|
5
|
+
#
|
6
|
+
# Creates a `parent_resource` method, which will call `find_by_id!` using an id
|
7
|
+
# found in the params hash.
|
8
|
+
#
|
9
|
+
# @example Organization and Group as parents
|
10
|
+
#
|
11
|
+
# class MembersController < ActionController::Base
|
12
|
+
# include Parents.new(Organization, Group)
|
13
|
+
#
|
14
|
+
# def index
|
15
|
+
# @members = parent_resource.members
|
16
|
+
# end
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# @!method parent_resource()
|
20
|
+
# Fetches the parent resource.
|
21
|
+
#
|
22
|
+
# @return [ActiveRecord::Base, nil] The result or `nil` if no matching key
|
23
|
+
# has been found.
|
24
|
+
#
|
25
|
+
# @raise [ActiveRecord::RecordNotFound] When a parent resource key is
|
26
|
+
# present, but no parent resource is found for it.
|
27
|
+
#
|
28
|
+
class Parents < Module
|
29
|
+
private
|
30
|
+
|
31
|
+
def initialize(*resource_classes)
|
32
|
+
@finder = Finder.new(resource_classes)
|
33
|
+
end
|
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
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Used to find the parent resource
|
45
|
+
#
|
46
|
+
# @api private
|
47
|
+
#
|
48
|
+
class Finder
|
49
|
+
attr_reader :parent_resource_classes, :primary_keys
|
50
|
+
|
51
|
+
# @param [Class<ActiveRecord::Base>] parent_resource_classes
|
52
|
+
def initialize(*parent_resource_classes)
|
53
|
+
@parent_resource_classes = parent_resource_classes.flatten
|
54
|
+
setup_primary_keys
|
55
|
+
end
|
56
|
+
|
57
|
+
def parent_resource(hsh)
|
58
|
+
key = matched_key(hsh)
|
59
|
+
return nil unless key
|
60
|
+
to_resource_class(key).find_by_id!(hsh[key])
|
61
|
+
end
|
62
|
+
|
63
|
+
def to_resource_class(key)
|
64
|
+
key.to_s.sub(/_id$/, '').classify.constantize
|
65
|
+
end
|
66
|
+
|
67
|
+
def matched_key(hsh)
|
68
|
+
hsh.keys.detect { |key| valid_primary_key?(key) }
|
69
|
+
end
|
70
|
+
|
71
|
+
def valid_primary_key?(key)
|
72
|
+
primary_keys.include?(key.to_s)
|
73
|
+
end
|
74
|
+
|
75
|
+
def setup_primary_keys
|
76
|
+
@primary_keys = parent_resource_classes.map { |klass|
|
77
|
+
klass.to_s.underscore.concat('_id')
|
78
|
+
}
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'spec_helper'
|
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
|
+
describe ActionController::Parents do
|
17
|
+
describe 'when included' do
|
18
|
+
let(:dummy) { DummyController.new }
|
19
|
+
|
20
|
+
before(:each) do
|
21
|
+
class DummyController; include(ActionController::Parents.new); end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "create the #parent_resource" do
|
25
|
+
expect(dummy).to respond_to(:parent_resource)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#parent_resource' do
|
29
|
+
it 'deletegates to an instance of Finder' do
|
30
|
+
args = []
|
31
|
+
allow(dummy).to receive(:params) { args }
|
32
|
+
expect_any_instance_of(described_class::Finder).to receive(:parent_resource).with(args)
|
33
|
+
dummy.parent_resource
|
34
|
+
end
|
35
|
+
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
|
+
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_controller-parents
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Maarten Claes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mutant
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.0.0
|
83
|
+
description: Easily access parent resources
|
84
|
+
email:
|
85
|
+
- maartencls@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- .rspec
|
92
|
+
- .yardopts
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- action_controller-parents.gemspec
|
98
|
+
- lib/action_controller/parents.rb
|
99
|
+
- lib/action_controller/parents/version.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- spec/unit/action_controller-parents_spec.rb
|
102
|
+
homepage: ''
|
103
|
+
licenses:
|
104
|
+
- MIT
|
105
|
+
metadata: {}
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - '>='
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.2.1
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Easily access parent resources
|
126
|
+
test_files:
|
127
|
+
- spec/spec_helper.rb
|
128
|
+
- spec/unit/action_controller-parents_spec.rb
|
129
|
+
has_rdoc:
|