collection_controller 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 718921821e19134beb2b22e6a49b8f2b9fb1cf93
4
+ data.tar.gz: '08630b140a1824f20b5a486262e43be5f759b3e2'
5
+ SHA512:
6
+ metadata.gz: af16a2661b51664db7696ca1fc85209c090148d3d3fa546379cbb8b86019d1cd2ea13b7d9c129e60557213a2a734961defb280a4f52f6ed902e8e7dffa52cc00
7
+ data.tar.gz: 0604475b5a8cbe93134110830912988b9da61fe5a17dc8fc91c2ca47e7b6f59d471dc668be33d1d7d4d3d871309002c1208a59c32a349c9a3e8d2d7f04824d0e
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in collection_controller.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,134 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ collection_controller (0.1.0)
5
+ rails (> 4, < 6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (5.1.4)
11
+ actionpack (= 5.1.4)
12
+ nio4r (~> 2.0)
13
+ websocket-driver (~> 0.6.1)
14
+ actionmailer (5.1.4)
15
+ actionpack (= 5.1.4)
16
+ actionview (= 5.1.4)
17
+ activejob (= 5.1.4)
18
+ mail (~> 2.5, >= 2.5.4)
19
+ rails-dom-testing (~> 2.0)
20
+ actionpack (5.1.4)
21
+ actionview (= 5.1.4)
22
+ activesupport (= 5.1.4)
23
+ rack (~> 2.0)
24
+ rack-test (>= 0.6.3)
25
+ rails-dom-testing (~> 2.0)
26
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
+ actionview (5.1.4)
28
+ activesupport (= 5.1.4)
29
+ builder (~> 3.1)
30
+ erubi (~> 1.4)
31
+ rails-dom-testing (~> 2.0)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
+ activejob (5.1.4)
34
+ activesupport (= 5.1.4)
35
+ globalid (>= 0.3.6)
36
+ activemodel (5.1.4)
37
+ activesupport (= 5.1.4)
38
+ activerecord (5.1.4)
39
+ activemodel (= 5.1.4)
40
+ activesupport (= 5.1.4)
41
+ arel (~> 8.0)
42
+ activesupport (5.1.4)
43
+ concurrent-ruby (~> 1.0, >= 1.0.2)
44
+ i18n (~> 0.7)
45
+ minitest (~> 5.1)
46
+ tzinfo (~> 1.1)
47
+ arel (8.0.0)
48
+ builder (3.2.3)
49
+ concurrent-ruby (1.0.5)
50
+ crass (1.0.2)
51
+ diff-lcs (1.3)
52
+ erubi (1.7.0)
53
+ globalid (0.4.1)
54
+ activesupport (>= 4.2.0)
55
+ i18n (0.9.1)
56
+ concurrent-ruby (~> 1.0)
57
+ loofah (2.1.1)
58
+ crass (~> 1.0.2)
59
+ nokogiri (>= 1.5.9)
60
+ mail (2.7.0)
61
+ mini_mime (>= 0.1.1)
62
+ method_source (0.9.0)
63
+ mini_mime (0.1.4)
64
+ mini_portile2 (2.3.0)
65
+ minitest (5.10.3)
66
+ nio4r (2.1.0)
67
+ nokogiri (1.8.1)
68
+ mini_portile2 (~> 2.3.0)
69
+ rack (2.0.3)
70
+ rack-test (0.7.0)
71
+ rack (>= 1.0, < 3)
72
+ rails (5.1.4)
73
+ actioncable (= 5.1.4)
74
+ actionmailer (= 5.1.4)
75
+ actionpack (= 5.1.4)
76
+ actionview (= 5.1.4)
77
+ activejob (= 5.1.4)
78
+ activemodel (= 5.1.4)
79
+ activerecord (= 5.1.4)
80
+ activesupport (= 5.1.4)
81
+ bundler (>= 1.3.0)
82
+ railties (= 5.1.4)
83
+ sprockets-rails (>= 2.0.0)
84
+ rails-dom-testing (2.0.3)
85
+ activesupport (>= 4.2.0)
86
+ nokogiri (>= 1.6)
87
+ rails-html-sanitizer (1.0.3)
88
+ loofah (~> 2.0)
89
+ railties (5.1.4)
90
+ actionpack (= 5.1.4)
91
+ activesupport (= 5.1.4)
92
+ method_source
93
+ rake (>= 0.8.7)
94
+ thor (>= 0.18.1, < 2.0)
95
+ rake (10.5.0)
96
+ rspec (3.7.0)
97
+ rspec-core (~> 3.7.0)
98
+ rspec-expectations (~> 3.7.0)
99
+ rspec-mocks (~> 3.7.0)
100
+ rspec-core (3.7.0)
101
+ rspec-support (~> 3.7.0)
102
+ rspec-expectations (3.7.0)
103
+ diff-lcs (>= 1.2.0, < 2.0)
104
+ rspec-support (~> 3.7.0)
105
+ rspec-mocks (3.7.0)
106
+ diff-lcs (>= 1.2.0, < 2.0)
107
+ rspec-support (~> 3.7.0)
108
+ rspec-support (3.7.0)
109
+ sprockets (3.7.1)
110
+ concurrent-ruby (~> 1.0)
111
+ rack (> 1, < 3)
112
+ sprockets-rails (3.2.1)
113
+ actionpack (>= 4.0)
114
+ activesupport (>= 4.0)
115
+ sprockets (>= 3.0.0)
116
+ thor (0.20.0)
117
+ thread_safe (0.3.6)
118
+ tzinfo (1.2.4)
119
+ thread_safe (~> 0.1)
120
+ websocket-driver (0.6.5)
121
+ websocket-extensions (>= 0.1.0)
122
+ websocket-extensions (0.1.2)
123
+
124
+ PLATFORMS
125
+ ruby
126
+
127
+ DEPENDENCIES
128
+ bundler (~> 1.16)
129
+ collection_controller!
130
+ rake (~> 10.0)
131
+ rspec (~> 3.0)
132
+
133
+ BUNDLED WITH
134
+ 1.16.0
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ben Sharpe
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # CollectionController
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/collection_controller`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'collection_controller'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install collection_controller
22
+
23
+ ## Usage
24
+
25
+ Say you have a Rails app that needs to manage Widgets. You create a WidgetsController and add
26
+
27
+ `include CollectionController`
28
+
29
+ to it.
30
+
31
+ ```
32
+ class WidgetsController < ApplicationController
33
+ include CollectionController
34
+
35
+ def index
36
+ @widgets = collection
37
+ end
38
+
39
+ def new
40
+ member = build_member
41
+ end
42
+
43
+ def edit
44
+ member # refers to the widget in the URL
45
+ end
46
+
47
+ def show
48
+ member # refers to the widget in the URL
49
+ end
50
+
51
+ def create
52
+ member = build_member(valid_params) # creates a new member from the params
53
+ member.save!
54
+ end
55
+
56
+ def update
57
+ member.update!(valid_params)
58
+ end
59
+
60
+ def destroy
61
+ member.destroy!
62
+ end
63
+
64
+ protected
65
+
66
+ def valid_params
67
+ params.permit(:name, :value, :status)
68
+ end
69
+ end
70
+ ```
71
+
72
+ I find I use this frequently enough to warrant being a gem. I like that there's very little under the hood, but it saves a lot of boilerplate coding on standard controllers.
73
+
74
+ Override `collection` if your controller has to do something odd to find the collection of things.
75
+
76
+ If your members are nested, you can do something like:
77
+
78
+ ```
79
+ def collection
80
+ @collection ||= begin
81
+ if params[:parent_id]
82
+ Parent.find(params[:parent_id]).widgets
83
+ else
84
+ super
85
+ end
86
+ end
87
+ end
88
+ ```
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/collection_controller.
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "collection_controller"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "collection_controller/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "collection_controller"
8
+ spec.version = CollectionController::VERSION
9
+ spec.authors = ["Ben Sharpe"]
10
+ spec.email = ["bsharpe@gmail.com"]
11
+
12
+ spec.summary = %q{A great concern for basic Rail's collection/member style controllers.}
13
+ spec.description = %q{}
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+
28
+ spec.add_dependency "rails", "> 4", "< 6"
29
+ end
@@ -0,0 +1,64 @@
1
+ require "collection_controller/version"
2
+
3
+ module CollectionController
4
+ extend ActiveSupport::Concern
5
+
6
+ protected
7
+
8
+ def collection
9
+ @collection ||= controller_name.classify.constantize
10
+ end
11
+
12
+ def member
13
+ @member ||= begin
14
+ if member_is_one?
15
+ model_name = controller_name.singularize
16
+ collection.send(model_name) || collection.send("build_#{model_name}")
17
+ else
18
+ params[:id] ? find_member(params[:id]) : build_member
19
+ end
20
+ end
21
+ end
22
+
23
+ def find_member(id)
24
+ @member = collection.find(id)
25
+ end
26
+
27
+ def build_member(attrs = nil)
28
+ @member = begin
29
+ if member_is_one?
30
+ collection.send(controller_name.singularize) || collection.send("build_#{controller_name.singularize}")
31
+ else
32
+ collection.respond_to?(:new) ? collection.new(attrs) : collection.build(attrs)
33
+ end
34
+ end
35
+ end
36
+
37
+ def member_association
38
+ @_member_association ||= begin
39
+ if collection.class.respond_to?(:reflect_on_association)
40
+ collection.class.reflect_on_association(controller_name.singularize)&.macro
41
+ end
42
+ end
43
+ end
44
+
45
+ def member_is_one?
46
+ member_association == :has_one
47
+ end
48
+
49
+ def member=(new_value)
50
+ @member = new_value
51
+ end
52
+
53
+ def collection=(new_value)
54
+ @collection = new_value
55
+ end
56
+
57
+ def current_page
58
+ collection.page(page_number)
59
+ end
60
+
61
+ def page_number
62
+ params[:page] ? params[:page][:number] : 1
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module CollectionController
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: collection_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Sharpe
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-11-06 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '4'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '6'
65
+ type: :runtime
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">"
70
+ - !ruby/object:Gem::Version
71
+ version: '4'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '6'
75
+ description: ''
76
+ email:
77
+ - bsharpe@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - ".rspec"
84
+ - ".travis.yml"
85
+ - Gemfile
86
+ - Gemfile.lock
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - bin/console
91
+ - bin/setup
92
+ - collection_controller.gemspec
93
+ - lib/collection_controller.rb
94
+ - lib/collection_controller/version.rb
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.6.13
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: A great concern for basic Rail's collection/member style controllers.
119
+ test_files: []