controller_resource_class 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 70c0bbcf5399a3e208ab7f08d68c53bba49f120f
4
+ data.tar.gz: 4e25a83905181ceb4169412902a57018a68fad9e
5
+ SHA512:
6
+ metadata.gz: 0dfbf0a44508c0b4dc9787960f534a4303cd64a36c154f3143936e778e76bfdbf8c9dca2b18886e7d1d7ce9b47276db1043ddf18e45ac25769bcf453839b8f5a
7
+ data.tar.gz: 59f01ff4f86cc980c98b3e82a969d4538f7f9a7f090308010052ac1dc7cc81aa7d8272df3815f8fbfd3c776ea9fa7332dcaaccc74af48c4714d7917aecca8890
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in controller_resource_class.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 barelyknown
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.
@@ -0,0 +1,35 @@
1
+ # ControllerResourceClass
2
+
3
+ Adds a `resource_class` method to Rails Controllers.
4
+
5
+ class VehiclesController < ApplicationController
6
+ # resource_class automatically set to Vehicle
7
+ def new
8
+ @vehicle = resource_class.new
9
+ end
10
+ end
11
+
12
+ You can also specify the `resource_class` to be used.
13
+
14
+ class HyperloopController < ApplicationController
15
+ set_resource_class Vehicle
16
+ end
17
+
18
+ Don't want a `resource_class` for a controller?
19
+
20
+ class JetpackController < ApplicationController
21
+ set_resource_class false
22
+ end
23
+
24
+ ##Installation
25
+
26
+ gem "controller_resource_class"
27
+
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'controller_resource_class/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "controller_resource_class"
8
+ spec.version = ControllerResourceClass::VERSION
9
+ spec.authors = ["barelyknown"]
10
+ spec.email = ["sean@buytruckload.com"]
11
+ spec.description = %q{Add a resource_class method to Rails controllers.}
12
+ spec.summary = %q{Add a resource_class method to Rails controllers.}
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 "guard-rspec"
25
+ spec.add_dependency "activesupport"
26
+ end
@@ -0,0 +1,13 @@
1
+ require "active_support/core_ext/module/delegation"
2
+ require "active_support/concern"
3
+ require "active_support/inflector"
4
+
5
+ require "controller_resource_class/version"
6
+
7
+ require "controller_resource_class/controller"
8
+ require "controller_resource_class/able"
9
+ require "controller_resource_class/railtie" if defined?(Rails)
10
+
11
+ module ControllerResourceClass
12
+ # Your code goes here...
13
+ end
@@ -0,0 +1,35 @@
1
+ module ControllerResourceClass
2
+ module Able
3
+ extend ActiveSupport::Concern
4
+
5
+ def resource_class
6
+ self.class.resource_class
7
+ end
8
+
9
+ module ClassMethods
10
+ def set_resource_class(value)
11
+ self.resource_class_name = value
12
+ end
13
+
14
+ def resource_class
15
+ return nil if resource_class_name == false
16
+ self.resource_class_name = Controller.new(self).resource_class_name unless resource_class_name
17
+ resource_class_name.constantize
18
+ end
19
+
20
+ private
21
+ def resource_class_name=(value)
22
+ @resource_class_name = case value
23
+ when false then false
24
+ when String, Class then String(value)
25
+ else
26
+ end
27
+ end
28
+
29
+ def resource_class_name
30
+ @resource_class_name
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ module ControllerResourceClass
2
+ class Controller
3
+
4
+ attr_reader :application_controller, :resource_class_name
5
+
6
+ def initialize(klass)
7
+ @application_controller = klass
8
+ @resource_class_name = infer_resource_class_name
9
+ end
10
+
11
+ def infer_resource_class_name
12
+ application_controller.to_s.gsub(/Controller\z/,"").singularize
13
+ end
14
+
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module ControllerResourceClass
2
+ class Railtie < Rails::Railtie
3
+ initializer "controller_resource_class.action_controller" do
4
+ ActiveSupport.on_load(:action_controller) do
5
+ include Able
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module ControllerResourceClass
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ module ControllerResourceClass
4
+ class ApplicationController
5
+ include Able
6
+ end
7
+
8
+ class NonResourcedController < ApplicationController
9
+ set_resource_class false
10
+ end
11
+
12
+ class Model; end
13
+ class ModelsController < ApplicationController; end
14
+ class OtherModelsController < ApplicationController
15
+ set_resource_class "ControllerResourceClass::Model"
16
+ end
17
+
18
+ describe NonResourcedController do
19
+ it "should return a nil resource class if false" do
20
+ expect(subject.resource_class).to be_nil
21
+ end
22
+ end
23
+
24
+ describe ModelsController do
25
+ it "should return the Model class" do
26
+ expect(subject.resource_class).to eq ControllerResourceClass::Model
27
+ end
28
+ end
29
+
30
+ describe OtherModelsController do
31
+ it "should return the Model class" do
32
+ expect(subject.resource_class).to eq ControllerResourceClass::Model
33
+ end
34
+ end
35
+
36
+
37
+ end
@@ -0,0 +1,25 @@
1
+ require "spec_helper"
2
+
3
+ module ControllerResourceClass
4
+
5
+ class ModelsController; end
6
+ class Model; end
7
+
8
+ describe Controller do
9
+
10
+ let!(:application_controller) { ModelsController }
11
+
12
+ subject do
13
+ described_class.new(application_controller)
14
+ end
15
+
16
+ it "should set the controller" do
17
+ expect(subject.application_controller).to eq application_controller
18
+ end
19
+
20
+ it "should infer the resource class name" do
21
+ expect(subject.resource_class_name).to eq "ControllerResourceClass::Model"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require "controller_resource_class"
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: controller_resource_class
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - barelyknown
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-27 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: guard-rspec
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: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Add a resource_class method to Rails controllers.
84
+ email:
85
+ - sean@buytruckload.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - .rspec
92
+ - Gemfile
93
+ - Guardfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - controller_resource_class.gemspec
98
+ - lib/controller_resource_class.rb
99
+ - lib/controller_resource_class/able.rb
100
+ - lib/controller_resource_class/controller.rb
101
+ - lib/controller_resource_class/railtie.rb
102
+ - lib/controller_resource_class/version.rb
103
+ - spec/lib/controller_resource_class/able_spec.rb
104
+ - spec/lib/controller_resource_class/controller_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.0.2
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Add a resource_class method to Rails controllers.
130
+ test_files:
131
+ - spec/lib/controller_resource_class/able_spec.rb
132
+ - spec/lib/controller_resource_class/controller_spec.rb
133
+ - spec/spec_helper.rb