before_or_404 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.
@@ -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 documentation
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rails'
6
+ gem 'rspec'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Eugene Korpan
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,84 @@
1
+ == BeforeOr404 {<img src="https://travis-ci.org/eugenekorpan/before_or_404.png" alt="Build Status" />}[https://travis-ci.org/eugenekorpan/before_or_404] {<img src="https://codeclimate.com/github/eugenekorpan/before_or_404.png" />}[https://codeclimate.com/github/eugenekorpan/before_or_404]
2
+
3
+ Very simple gem that adds additional filter <code>before_or_404</code> to ActionController::Base.
4
+ It behaves just as a regular <code>before_filter</code> but in a case when error raises in the filter it returns 404 page with 404 status.
5
+
6
+ == Installation
7
+
8
+ 1 Add this line to your application's Gemfile:
9
+
10
+ gem 'before_or_404'
11
+
12
+ 2 And then execute:
13
+
14
+ $ bundle
15
+
16
+ == When to use it
17
+
18
+ Let's say you have a restful controller and appropriate url to edit a resource
19
+
20
+ http://bestsite.com/books/3/edit
21
+
22
+ And you have a <code>before_filter</code> to avoid duplication
23
+
24
+ class BooksController < ApplicationController
25
+ before_filter :find_book, :only => [:show, :edit, :update, :destroy]
26
+
27
+ ...
28
+
29
+ private
30
+
31
+ def find_book
32
+ @book = Book.find(params[:id])
33
+ end
34
+ end
35
+
36
+ But what if I manually change the id in the url to one which is invalid
37
+
38
+ http://bestsite.com/books/just a super book/edit
39
+
40
+ Then this url considered to be invalid and hence user should see 404 page ("Page not found")
41
+ In this case you could do the next
42
+
43
+ class BooksController < ApplicationController
44
+ before_filter :find_book, :only => [:show, :edit, :update, :destroy]
45
+
46
+ ...
47
+
48
+ private
49
+
50
+ def page_not_found
51
+ render 'public/404', status: 404
52
+ end
53
+
54
+ def find_book
55
+ @book = Book.find_by_id(params[:id])
56
+ @book || page_not_found
57
+ end
58
+ end
59
+
60
+ But this meens you should do this in every <code>before_filter</code> where you find a resource by id which comes as a part of url.
61
+ As alternative you could use <code>before_or_404</code> instead of <code>before_filter</code>.
62
+
63
+ == Usage Example
64
+
65
+ class UsersController < ApplicationController
66
+ before_or_404 :find_user, only: [:show, :edit, :destroy]
67
+
68
+ ...
69
+
70
+ private
71
+
72
+ def find_user
73
+ @user = User.find(params[:id])
74
+ end
75
+
76
+ end
77
+
78
+ == Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (<code>git checkout -b my-new-feature</code>)
82
+ 3. Commit your changes (<code>git commit -am 'Added some feature'</code>)
83
+ 4. Push to the branch (<code>git push origin my-new-feature</code>)
84
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'before_or_404/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "before_or_404"
8
+ gem.version = BeforeOr404::VERSION
9
+ gem.authors = ["Eugene Korpan"]
10
+ gem.email = ["korpan.eugene@gmail.com"]
11
+ gem.description = %q{adds before_filter which renders 404 page if exception was raised}
12
+ gem.summary = %q{adds before_filter which renders 404 page if exception was raised}
13
+ gem.homepage = "https://github.com/eugenekorpan/before_or_404"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ require 'before_or_404/version'
2
+ require 'before_or_404/filter_wrapper'
3
+ require 'before_or_404/action_controller_extensions'
@@ -0,0 +1,27 @@
1
+ module ActionControllerExtensions
2
+ def before_or_404(*names, &blk)
3
+ _insert_callbacks(names, blk) do |name, options|
4
+ set_callback :process_action, :before, wrapper_for(name).method, options
5
+ end
6
+ end
7
+
8
+ def prepend_before_or_404(*names, &blk)
9
+ _insert_callbacks(names, blk) do |name, options|
10
+ set_callback(:process_action, :before, wrapper_for(name).method, options.merge(:prepend => true))
11
+ end
12
+ end
13
+
14
+ def skip_before_or_404(*names, &blk)
15
+ _insert_callbacks(names, blk) do |name, options|
16
+ skip_callback(:process_action, :before, wrapper_for(name).method, options)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def wrapper_for(name)
23
+ BeforeOr404::FilterWrapper.new(name, self)
24
+ end
25
+ end
26
+
27
+ ActionController::Base.send :extend, ActionControllerExtensions
@@ -0,0 +1,29 @@
1
+ module BeforeOr404
2
+ class FilterWrapper
3
+ def initialize(original_method, klass)
4
+ @original_method = original_method
5
+ @klass = klass
6
+ @wrapped = false
7
+ wrap!
8
+ end
9
+
10
+ def method
11
+ @name ||= :"before_or_404_#{Time.now.to_i}_#{@original_method}"
12
+ end
13
+
14
+ def wrap!
15
+ return @wrapped if @wrapped
16
+
17
+ @klass.class_eval %Q{
18
+ def #{method}
19
+ begin
20
+ #{@original_method}
21
+ rescue
22
+ render 'public/404', status: 404
23
+ end
24
+ end
25
+ }
26
+ @wrapped = true
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module BeforeOr404
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActionController::Base do
4
+ subject { ActionController::Base }
5
+
6
+ it { should respond_to(:before_or_404)}
7
+ it { should respond_to(:prepend_before_or_404)}
8
+ it { should respond_to(:skip_before_or_404)}
9
+
10
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe BeforeOr404::FilterWrapper do
4
+ let(:acb) { ActionController::Base }
5
+
6
+ subject { BeforeOr404::FilterWrapper.new(:find_user, acb) }
7
+
8
+ context '#method' do
9
+ it 'should generate name' do
10
+ expect(subject.method).to be
11
+ expect(subject.method).to_not be_blank
12
+ end
13
+
14
+ it 'should generate only one name for single instance' do
15
+ expect(subject.method).to eql(subject.method)
16
+ end
17
+
18
+ it 'should generate uniq names for different instances' do
19
+ find = BeforeOr404::FilterWrapper.new(:find, acb)
20
+ fetch = BeforeOr404::FilterWrapper.new(:fetch, acb)
21
+ expect(find.method).to_not eql(fetch.method)
22
+ end
23
+ end
24
+
25
+ context '#wrap!' do
26
+ it 'should add wrapped method' do
27
+ subject.wrap!
28
+ expect(acb.instance_methods).to include(subject.method)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'action_controller'
3
+ require 'before_or_404'
4
+ require 'before_or_404/action_controller_extensions'
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: before_or_404
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Eugene Korpan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: adds before_filter which renders 404 page if exception was raised
15
+ email:
16
+ - korpan.eugene@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - .rspec
23
+ - .travis.yml
24
+ - Gemfile
25
+ - LICENSE.txt
26
+ - README.rdoc
27
+ - Rakefile
28
+ - before_or_404.gemspec
29
+ - lib/before_or_404.rb
30
+ - lib/before_or_404/action_controller_extensions.rb
31
+ - lib/before_or_404/filter_wrapper.rb
32
+ - lib/before_or_404/version.rb
33
+ - spec/lib/action_controller_extensions_spec.rb
34
+ - spec/lib/filter_wrapper_spec.rb
35
+ - spec/spec_helper.rb
36
+ homepage: https://github.com/eugenekorpan/before_or_404
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 1.8.21
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: adds before_filter which renders 404 page if exception was raised
60
+ test_files:
61
+ - spec/lib/action_controller_extensions_spec.rb
62
+ - spec/lib/filter_wrapper_spec.rb
63
+ - spec/spec_helper.rb