invisible_controller 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff6914125b2b8a9245abe41fdb4b5edd789505ab
4
+ data.tar.gz: 743c4b1f2fb63f0bddb8223f47088b4a3fc07798
5
+ SHA512:
6
+ metadata.gz: a4cfe35839de4c2e74afacd118b0671acf02b96dfacf1e98d69fddcfa0c9437d43cd9380188421b63834dff17363f09774385925584691d2c5a3e1f524e0fbc6
7
+ data.tar.gz: e9aaa335940115e418ac7438b2dad9511f5d7895ece9ac202ac195b15d3f52e07381f48cf6b20c2ea4ee1a0ba3136d2340cf0e5a7cbbd5456bd28b96f28289ba
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.log
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ before_install: gem install bundler -v 1.10.4
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in invisible_controller.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Chris Moody
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,45 @@
1
+ # InvisibleController
2
+
3
+ This gem is to speed up development in restful applications. If you are using standard rails best practices and naming conventions, controllers are no longer neccessary.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'invisible_controller'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install invisible_controller
20
+
21
+ ## Usage
22
+
23
+ To use this gem just start deleting your controllers, or don't make them to begin with. If you find that you need some control over your controllers, or if you have nested routes, define your controllers as normal but have them inherit from InvisibleController::Base.
24
+
25
+ ```ruby
26
+ class SomeController < InvisibleController::Base
27
+ belongs_to: some_model, shallow: true, as :some_other_name
28
+ end
29
+ ```
30
+
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/transcon/invisible_controller.
41
+
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,86 @@
1
+ module InvisibleController
2
+ class Base < ::ApplicationController
3
+ authorize_resource if respond_to?('authorize_resource')
4
+ RESTFUL_ACTIONS = [:index,:create,:new,:edit,:show,:update,:destroy]
5
+ before_filter :load_resource, only: RESTFUL_ACTIONS
6
+
7
+ def self.belongs_to(parent_klass,args={})
8
+ @as ||= HashWithIndifferentAccess.new
9
+ @as[parent_klass] = args[:as]
10
+ end
11
+
12
+ def self.has_scope(name,options={})
13
+ raise NameError, "#{name} is an invalid scope name" if [:controller,:action,:format].include?(name.to_sym)
14
+ @scopes ||= []
15
+ @scopes << name.to_s
16
+ end
17
+ RESTFUL_ACTIONS.each do |method|
18
+ define_method("#{method}?") do
19
+ params[:action].to_sym == method
20
+ end
21
+ define_method(method) do
22
+ respond
23
+ end
24
+ end
25
+ def update
26
+ resource.update(processed_params) ? respond : respond_with_errors
27
+ end
28
+ def destroy
29
+ resource.destroy
30
+ render response: 204, nothing: true
31
+ end
32
+
33
+ private
34
+ def resource_with_errors() resource.as_json.merge(errors: resource.errors.full_messages) end
35
+ def respond_with_errors
36
+ respond_to do |format|
37
+ format.html {render nothing: true, status: 422}
38
+ format.json {render json: resource_with_errors, status: 422}
39
+ format.xml {render xml: resource_with_errors, status: 422}
40
+ end
41
+ end
42
+ def respond
43
+ respond_to do |format|
44
+ format.html {render layout: !request.headers['angular'], template: template}
45
+ format.json {
46
+ render json: {}, status: 204 if params[:suppress]
47
+ render json: resource unless params[:suppress]
48
+ }
49
+ format.xml {render xml: resource}
50
+ end
51
+ end
52
+ def load_resource()
53
+ instance_variable_set("@#{resource_name}", resource)
54
+ instance_variable_set("@#{parent_name}", parent_resource) if nested?
55
+ end
56
+ def controller_name() params[:controller].to_s end
57
+ def resource_name() index? ? controller_name : controller_name.singularize end
58
+ def nested_name() (self.class.instance_variable_get('@as') || {})[parent_name] end
59
+ def resource() @resource ||= define_resource end
60
+ def template() "#{controller_name}/#{params[:action]}" end
61
+
62
+ #belongs_to Functionality
63
+ def nested?() parent_name.split('.')[0].singularize != resource_name.singularize end
64
+ def parent_name() request.path.split('/')[1].singularize end
65
+
66
+ def parent_resource() @parent_resource ||= parent_name.classify.constantize.find(parent_id) end
67
+ def parent_id() params["#{parent_name}_id"] end
68
+ def active_class() nested? ? parent_resource.send(nested_name || resource_name.pluralize) : klass end
69
+ def scoped() ((params.keys || []) & (self.class.instance_variable_get("@scopes") || []))[0] end
70
+ def current_scope() scoped || :all end
71
+ def scope_args() params[current_scope] end
72
+ def index_with_args?() index? && scope_args.present? end
73
+
74
+ def define_resource()
75
+ return nil unless !!(klass < ActiveRecord::Base)
76
+ return active_class.send(current_scope,scope_args) if index_with_args?
77
+ return active_class.send(current_scope) if index?
78
+ return active_class.new(processed_params) if new?
79
+ return active_class.create(processed_params) if create?
80
+ active_class.find(params[:id])
81
+ end
82
+ def klass() resource_name.classify.constantize rescue Class end
83
+ def permitted_params() params.permit( resource_name.to_sym => klass.whitelisted ) end
84
+ def processed_params() permitted_params[resource_name] end
85
+ end
86
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "invisible_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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'invisible_controller/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "invisible_controller"
8
+ spec.version = InvisibleController::VERSION
9
+ spec.authors = ["Chris Moody"]
10
+ spec.email = ["cmoody@transcon.com"]
11
+
12
+ spec.summary = %q{RESTful api controllers.}
13
+ spec.description = %q{When controllers are truely restful, the become more
14
+ and more empty. We got to the point where we had many
15
+ controllers that were just two lines class .. end.
16
+ At this point it seemed pointless to have controllers.}
17
+ spec.homepage = "https://github.com/transcon/invisible_controller"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "rails"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "flying_table"
30
+ spec.add_development_dependency 'minitest'
31
+ spec.add_development_dependency 'awesome_print'
32
+ spec.add_development_dependency 'minitest-reporters', '>= 1.0.1'
33
+ spec.add_development_dependency 'sqlite3'
34
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveRecordExtension
2
+ extend ActiveSupport::Concern
3
+ module ClassMethods
4
+ def whitelisted()
5
+ return self::WHITELISTED if self.constants.include?(:WHITELISTED)
6
+ return column_names - self::BLACKLISTED if self.constants.include?(:BLACKLISTED)
7
+ column_names
8
+ end
9
+ end
10
+ end
11
+ ActiveRecord::Base.send(:include, ActiveRecordExtension)
@@ -0,0 +1,50 @@
1
+ module ActiveSupport
2
+ module Dependencies
3
+ #TODO: I really don't like intercepting this way, really disappointed with
4
+ # the lack of available hooks here
5
+ # This code should be checked against RAILS code probably after every release
6
+
7
+ # RAILS code copied (RAILS Comments removed)
8
+ def load_missing_constant(from_mod, const_name)
9
+ log_call from_mod, const_name
10
+
11
+ unless qualified_const_defined?(from_mod.name) && Inflector.constantize(from_mod.name).equal?(from_mod)
12
+ raise ArgumentError, "A copy of #{from_mod} has been removed from the module tree but is still active!"
13
+ end
14
+
15
+ qualified_name = qualified_name_for from_mod, const_name
16
+ path_suffix = qualified_name.underscore
17
+
18
+ file_path = search_for_file(path_suffix)
19
+ if file_path
20
+ expanded = File.expand_path(file_path)
21
+ expanded.sub!(/\.rb\z/, '')
22
+
23
+ if loading.include?(expanded)
24
+ raise "Circular dependency detected while autoloading constant #{qualified_name}"
25
+ else
26
+ require_or_load(expanded, qualified_name)
27
+ raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
28
+ return from_mod.const_get(const_name)
29
+ end
30
+ elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
31
+ return mod
32
+ elsif (parent = from_mod.parent) && parent != from_mod &&
33
+ ! from_mod.parents.any? { |p| p.const_defined?(const_name, false) }
34
+ begin
35
+ return parent.const_missing(const_name)
36
+ rescue NameError => e
37
+ raise unless e.missing_name? qualified_name_for(parent, const_name)
38
+ end
39
+ # NO CONTROLLER INTERCEPTOR
40
+ elsif !!(qualified_name =~ /Controller$/)
41
+ return Object.const_set(qualified_name, InvisibleController::Base)
42
+ end
43
+ # Continue Rails code
44
+ name_error = NameError.new("uninitialized constant #{qualified_name}", const_name)
45
+ name_error.set_backtrace(caller.reject {|l| l.starts_with? __FILE__ })
46
+ raise name_error
47
+ end
48
+ end
49
+ end
50
+ # ActiveSupport::Dependencies.send(:include, ActiveSupportExtension)
@@ -0,0 +1,3 @@
1
+ module InvisibleController
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,11 @@
1
+ require "active_record"
2
+ require "invisible_controller/version"
3
+ require "invisible_controller/active_support_override"
4
+ require "invisible_controller/active_record_extension"
5
+ require "rails/engine"
6
+
7
+ module InvisibleController
8
+ class Railtie < ::Rails::Engine
9
+ config.inheritted_resources = InvisibleController
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invisible_controller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Moody
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: flying_table
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: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: awesome_print
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 1.0.1
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 1.0.1
111
+ - !ruby/object:Gem::Dependency
112
+ name: sqlite3
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: |-
126
+ When controllers are truely restful, the become more
127
+ and more empty. We got to the point where we had many
128
+ controllers that were just two lines class .. end.
129
+ At this point it seemed pointless to have controllers.
130
+ email:
131
+ - cmoody@transcon.com
132
+ executables: []
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - ".gitignore"
137
+ - ".travis.yml"
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - app/controllers/invisible_controller/base.rb
143
+ - bin/console
144
+ - bin/setup
145
+ - invisible_controller.gemspec
146
+ - lib/invisible_controller.rb
147
+ - lib/invisible_controller/active_record_extension.rb
148
+ - lib/invisible_controller/active_support_override.rb
149
+ - lib/invisible_controller/version.rb
150
+ homepage: https://github.com/transcon/invisible_controller
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.2.1
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: RESTful api controllers.
174
+ test_files: []