action_controller_tweaks 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +856 -0
- data/.travis.yml +11 -5
- data/Appraisals +20 -20
- data/CHANGELOG.md +73 -14
- data/Gemfile +1 -1
- data/README.md +7 -8
- data/Rakefile +4 -1
- data/action_controller_tweaks.gemspec +11 -9
- data/gemfiles/rails_5_0.gemfile +10 -0
- data/lib/action_controller_tweaks.rb +4 -4
- data/lib/action_controller_tweaks/caching.rb +8 -4
- data/lib/action_controller_tweaks/session.rb +71 -42
- data/lib/action_controller_tweaks/version.rb +1 -1
- data/spec/action_controller_tweaks_spec.rb +232 -173
- data/spec/fixtures/application.rb +8 -5
- data/spec/fixtures/controllers.rb +7 -5
- data/spec/spec_helper.rb +14 -14
- metadata +34 -19
- data/gemfiles/rails_3_2.gemfile +0 -10
data/.travis.yml
CHANGED
@@ -1,14 +1,15 @@
|
|
1
|
+
# Send builds to container-based infrastructure
|
2
|
+
# http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
3
|
+
sudo: false
|
1
4
|
language: ruby
|
2
5
|
cache:
|
3
6
|
- bundler
|
4
7
|
rvm:
|
5
|
-
- 1.
|
6
|
-
- 2.
|
7
|
-
- 2.
|
8
|
-
- 2.2
|
8
|
+
- 2.1.10
|
9
|
+
- 2.2.6
|
10
|
+
- 2.3.3
|
9
11
|
- ruby-head
|
10
12
|
gemfile:
|
11
|
-
- gemfiles/rails_3_2.gemfile
|
12
13
|
- gemfiles/rails_4_0.gemfile
|
13
14
|
- gemfiles/rails_4_1.gemfile
|
14
15
|
- gemfiles/rails_4_2.gemfile
|
@@ -16,4 +17,9 @@ matrix:
|
|
16
17
|
fast_finish: true
|
17
18
|
allow_failures:
|
18
19
|
- rvm: ruby-head
|
20
|
+
include:
|
21
|
+
- rvm: 2.3.3
|
22
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
23
|
+
- rvm: ruby-head
|
24
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
19
25
|
|
data/Appraisals
CHANGED
@@ -1,32 +1,32 @@
|
|
1
1
|
|
2
|
-
appraise "rails_3_2" do
|
3
|
-
version = "~> 3.2.20"
|
4
|
-
gem 'activesupport', version
|
5
|
-
gem 'actionpack', version
|
6
|
-
gem 'activerecord', version
|
7
|
-
gem 'railties', version
|
8
|
-
end
|
9
|
-
|
10
2
|
appraise "rails_4_0" do
|
11
3
|
version = "~> 4.0.12"
|
12
|
-
gem
|
13
|
-
gem
|
14
|
-
gem
|
15
|
-
gem
|
4
|
+
gem "activesupport", version
|
5
|
+
gem "actionpack", version
|
6
|
+
gem "activerecord", version
|
7
|
+
gem "railties", version
|
16
8
|
end
|
17
9
|
|
18
10
|
appraise "rails_4_1" do
|
19
11
|
version = "~> 4.1.8"
|
20
|
-
gem
|
21
|
-
gem
|
22
|
-
gem
|
23
|
-
gem
|
12
|
+
gem "activesupport", version
|
13
|
+
gem "actionpack", version
|
14
|
+
gem "activerecord", version
|
15
|
+
gem "railties", version
|
24
16
|
end
|
25
17
|
|
26
18
|
appraise "rails_4_2" do
|
27
19
|
version = "~> 4.2.0"
|
28
|
-
gem
|
29
|
-
gem
|
30
|
-
gem
|
31
|
-
gem
|
20
|
+
gem "activesupport", version
|
21
|
+
gem "actionpack", version
|
22
|
+
gem "activerecord", version
|
23
|
+
gem "railties", version
|
24
|
+
end
|
25
|
+
|
26
|
+
appraise "rails_5_0" do
|
27
|
+
version = "~> 5.0.0"
|
28
|
+
gem "activesupport", version
|
29
|
+
gem "actionpack", version
|
30
|
+
gem "activerecord", version
|
31
|
+
gem "railties", version
|
32
32
|
end
|
data/CHANGELOG.md
CHANGED
@@ -1,19 +1,78 @@
|
|
1
|
-
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
2
4
|
|
3
5
|
|
4
|
-
|
5
|
-
- Fix session key still gets deleted when no option is set
|
6
|
-
- Fix options not passed to `#set_session` when `#set_session_with_expiry` is called
|
6
|
+
## [Unreleased]
|
7
7
|
|
8
|
-
|
9
|
-
- Add method `#set_session_with_expiry`
|
10
|
-
- Add option `expires_in` and `expires_at` to #set_session
|
11
|
-
- Fix invalid header when using `#set_no_cache` for HTTP 1.0
|
12
|
-
- Raise error when reserved session key(s) is set through the provided method
|
8
|
+
### Added
|
13
9
|
|
14
|
-
-
|
15
|
-
|
16
|
-
|
10
|
+
- Nothing
|
11
|
+
|
12
|
+
### Changed
|
13
|
+
|
14
|
+
- Nothing
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
|
18
|
+
- Nothing
|
19
|
+
|
20
|
+
|
21
|
+
## [0.3.2] - 2016-12-23
|
22
|
+
|
23
|
+
### Changed
|
24
|
+
|
25
|
+
- Call `before_action` on included class if exists
|
26
|
+
- Add support for AR 5.0.x
|
27
|
+
- Drop support for AR 3.x
|
28
|
+
- Drop support for Ruby < 2.1
|
29
|
+
|
30
|
+
### Fixed
|
31
|
+
|
32
|
+
- Raise error if both `before_action` and `before_filter` does not exists
|
33
|
+
|
34
|
+
|
35
|
+
## [0.3.1] - 2015-03-03
|
36
|
+
|
37
|
+
### Fixed
|
38
|
+
|
39
|
+
- Fix session key still gets deleted when no option is set
|
40
|
+
- Fix options not passed to `#set_session` when `#set_session_with_expiry` is called
|
41
|
+
|
42
|
+
|
43
|
+
## [0.3.0] - 2014-03-14
|
44
|
+
|
45
|
+
### Added
|
46
|
+
|
47
|
+
- Add method `#set_session_with_expiry`
|
48
|
+
- Add option `expires_in` and `expires_at` to #set_session
|
49
|
+
|
50
|
+
### Changed
|
51
|
+
|
52
|
+
- Raise error when reserved session key(s) is set through the provided method
|
53
|
+
|
54
|
+
### Fixed
|
55
|
+
|
56
|
+
- Fix invalid header when using `#set_no_cache` for HTTP 1.0
|
57
|
+
|
58
|
+
|
59
|
+
## [0.2.0] - 2014-01-17
|
60
|
+
|
61
|
+
### Added
|
62
|
+
|
63
|
+
- Add option `expire_at` to #set_session
|
64
|
+
|
65
|
+
|
66
|
+
## 0.1 - 2013-08-30
|
67
|
+
|
68
|
+
### Added
|
69
|
+
|
70
|
+
- Initial Release
|
71
|
+
|
72
|
+
|
73
|
+
[Unreleased]: https://github.com/AssetSync/asset_sync/compare/v0.3.2...HEAD
|
74
|
+
[0.3.1]: https://github.com/AssetSync/asset_sync/compare/v0.3.1...v0.3.2
|
75
|
+
[0.3.1]: https://github.com/AssetSync/asset_sync/compare/v0.3.0...v0.3.1
|
76
|
+
[0.3.0]: https://github.com/AssetSync/asset_sync/compare/v0.2.0...v0.3.0
|
77
|
+
[0.2.0]: https://github.com/AssetSync/asset_sync/compare/v0.1...v0.2.0
|
17
78
|
|
18
|
-
- **0.1**
|
19
|
-
- Initial Release
|
data/Gemfile
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
source
|
1
|
+
source "https://rubygems.org"
|
2
2
|
gemspec
|
data/README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
Action Controller Tweaks
|
2
|
-
===========
|
1
|
+
# Action Controller Tweaks
|
3
2
|
|
4
3
|
ActionController is great, but could be better. Here are some tweaks for it.
|
5
4
|
|
6
|
-
|
7
|
-
|
5
|
+
|
6
|
+
## Status
|
8
7
|
|
9
8
|
[![Build Status](http://img.shields.io/travis/PikachuEXE/action_controller_tweaks.svg?style=flat-square)](https://travis-ci.org/PikachuEXE/action_controller_tweaks)
|
10
9
|
[![Gem Version](http://img.shields.io/gem/v/action_controller_tweaks.svg?style=flat-square)](http://badge.fury.io/rb/action_controller_tweaks)
|
@@ -12,15 +11,15 @@ ActionController is great, but could be better. Here are some tweaks for it.
|
|
12
11
|
[![Coverage Status](http://img.shields.io/coveralls/PikachuEXE/action_controller_tweaks.svg?style=flat-square)](https://coveralls.io/r/PikachuEXE/action_controller_tweaks)
|
13
12
|
[![Code Climate](http://img.shields.io/codeclimate/github/PikachuEXE/action_controller_tweaks.svg?style=flat-square)](https://codeclimate.com/github/PikachuEXE/action_controller_tweaks)
|
14
13
|
|
15
|
-
|
16
|
-
|
14
|
+
|
15
|
+
## Installation
|
17
16
|
|
18
17
|
```ruby
|
19
18
|
gem 'action_controller_tweaks'
|
20
19
|
```
|
21
20
|
|
22
|
-
|
23
|
-
|
21
|
+
|
22
|
+
## Usage
|
24
23
|
|
25
24
|
Either include it in specific controller or just `ApplicationController`
|
26
25
|
```ruby
|
data/Rakefile
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
require "appraisal"
|
2
2
|
require "bundler"
|
3
3
|
require "rspec/core/rake_task"
|
4
|
+
require "rubocop/rake_task"
|
4
5
|
|
5
6
|
Bundler::GemHelper.install_tasks
|
6
7
|
|
7
8
|
RSpec::Core::RakeTask.new(:spec)
|
8
9
|
|
10
|
+
RuboCop::RakeTask.new(:rubocop)
|
11
|
+
|
9
12
|
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
|
10
13
|
task :default do
|
11
14
|
sh "appraisal install && rake appraisal spec"
|
12
15
|
end
|
13
16
|
else
|
14
|
-
task :
|
17
|
+
task default: [:spec]
|
15
18
|
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
$LOAD_PATH.push File.expand_path("../lib", __FILE__)
|
3
3
|
|
4
4
|
author_name = "PikachuEXE"
|
5
5
|
gem_name = "action_controller_tweaks"
|
@@ -21,20 +21,19 @@ Gem::Specification.new do |s|
|
|
21
21
|
|
22
22
|
s.files = `git ls-files`.split("\n")
|
23
23
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
24
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
25
25
|
s.require_paths = ["lib"]
|
26
26
|
|
27
|
-
s.add_dependency "activesupport", ">=
|
28
|
-
s.add_dependency "actionpack", ">=
|
29
|
-
|
30
|
-
|
27
|
+
s.add_dependency "activesupport", ">= 4.0.0", "< 5.1.0"
|
28
|
+
s.add_dependency "actionpack", ">= 4.0.0", "< 5.1.0"
|
31
29
|
|
32
30
|
s.add_development_dependency "bundler", ">= 1.0.0"
|
33
|
-
s.add_development_dependency "rake", "
|
34
|
-
s.add_development_dependency "appraisal", "~>
|
31
|
+
s.add_development_dependency "rake", "~> 10.0"
|
32
|
+
s.add_development_dependency "appraisal", "~> 2.0"
|
35
33
|
s.add_development_dependency "rspec-rails", "~> 3.0"
|
36
34
|
s.add_development_dependency "rspec-its", "~> 1.0"
|
37
|
-
|
35
|
+
# rspec-rails needs activerecord...
|
36
|
+
s.add_development_dependency "activerecord", ">= 3.2.0", "< 5.0.0"
|
38
37
|
s.add_development_dependency "sqlite3", ">= 1.3"
|
39
38
|
s.add_development_dependency "database_cleaner", ">= 1.0"
|
40
39
|
s.add_development_dependency "timecop", ">= 0.6"
|
@@ -42,6 +41,9 @@ Gem::Specification.new do |s|
|
|
42
41
|
s.add_development_dependency "gem-release", ">= 0.7"
|
43
42
|
# This is for rails < 4.1 on MRI 2.2 (and other later version I suppose)
|
44
43
|
s.add_development_dependency "test-unit", ">= 3.0.0"
|
44
|
+
s.add_development_dependency "rubocop", "~> 0.30"
|
45
|
+
|
46
|
+
s.required_ruby_version = ">= 2.1.0"
|
45
47
|
|
46
48
|
s.required_rubygems_version = ">= 1.4.0"
|
47
49
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/concern"
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
3
|
+
require "action_controller_tweaks/version"
|
4
|
+
require "action_controller_tweaks/caching"
|
5
|
+
require "action_controller_tweaks/session"
|
6
6
|
|
7
7
|
module ActionControllerTweaks
|
8
8
|
extend ActiveSupport::Concern
|
@@ -1,13 +1,17 @@
|
|
1
|
-
require
|
1
|
+
require "active_support/concern"
|
2
2
|
|
3
3
|
module ActionControllerTweaks
|
4
4
|
module Caching
|
5
5
|
extend ActiveSupport::Concern
|
6
6
|
|
7
7
|
HEADERS = {
|
8
|
-
|
9
|
-
"
|
10
|
-
|
8
|
+
# HTTP 1.1
|
9
|
+
"Cache-Control" => "no-cache, no-store, max-age=0, "\
|
10
|
+
"must-revalidate, pre-check=0, post-check=0",
|
11
|
+
# HTTP 1.0
|
12
|
+
"Pragma" => "no-cache",
|
13
|
+
# HTTP 1.0
|
14
|
+
"Expires" => "Fri, 01 Jan 1990 00:00:00 GMT",
|
11
15
|
}.freeze
|
12
16
|
|
13
17
|
included do
|
@@ -1,5 +1,5 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require "active_support/concern"
|
2
|
+
require "active_support/time"
|
3
3
|
|
4
4
|
module ActionControllerTweaks
|
5
5
|
module Session
|
@@ -10,6 +10,27 @@ module ActionControllerTweaks
|
|
10
10
|
ReservedSessionKeyConflict = Class.new(ArgumentError)
|
11
11
|
end
|
12
12
|
|
13
|
+
module OptionProcessor
|
14
|
+
def self.extract_expires_in(options)
|
15
|
+
expires_in = options.delete(:expires_in) || options.delete(:expire_in)
|
16
|
+
if expires_in && !expires_in.is_a?(Numeric)
|
17
|
+
fail InvalidOptionValue.new(:expires_in, expires_in, Numeric)
|
18
|
+
end
|
19
|
+
expires_in
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.extract_expires_at(options)
|
23
|
+
expires_at = options.delete(:expires_at) || options.delete(:expire_at)
|
24
|
+
if expires_at && expires_at.respond_to?(:to_time)
|
25
|
+
expires_at = expires_at.to_time
|
26
|
+
end
|
27
|
+
if expires_at && !expires_at.is_a?(Time)
|
28
|
+
fail InvalidOptionValue.new(:expires_at, expires_at, Time)
|
29
|
+
end
|
30
|
+
expires_at
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
13
34
|
RESERVED_SESSION_KEYS = %w( session_keys_to_expire )
|
14
35
|
VALID_OPTION_KEYS = [
|
15
36
|
:expires_in,
|
@@ -20,30 +41,46 @@ module ActionControllerTweaks
|
|
20
41
|
|
21
42
|
class InvalidOptionValue < ArgumentError
|
22
43
|
def self.new(option_key, options_value, expected_types)
|
23
|
-
super("option key `#{option_key}` should contain value with type(s): #{expected_types}, "
|
44
|
+
super("option key `#{option_key}` should contain value with type(s): #{expected_types}, "\
|
24
45
|
"but got <#{options_value.inspect}> (#{options_value.class})")
|
25
46
|
end
|
26
47
|
end
|
27
48
|
|
28
49
|
included do
|
29
|
-
|
50
|
+
if respond_to?(:before_action)
|
51
|
+
before_action :_delete_expired_session_keys
|
52
|
+
elsif respond_to?(:before_filter)
|
53
|
+
before_filter :_delete_expired_session_keys
|
54
|
+
else
|
55
|
+
fail(
|
56
|
+
NotImplementedError,
|
57
|
+
"There is no `.before_action` nor `.before_filter` in this class",
|
58
|
+
)
|
59
|
+
end
|
30
60
|
|
31
61
|
private
|
32
62
|
|
33
63
|
# Set session just like `session[key] = value` but accept some options about expiry
|
34
64
|
#
|
35
|
-
# @option expires_in [Integer]
|
36
|
-
#
|
37
|
-
# @option
|
38
|
-
#
|
65
|
+
# @option expires_in [Integer]
|
66
|
+
# How long from now should the session value be expired
|
67
|
+
# @option expire_in [Integer]
|
68
|
+
# same as `expires_in`
|
69
|
+
# @option expires_at [Integer]
|
70
|
+
# What time should the session value be expired
|
71
|
+
# (using a time in the past would expire at next request)
|
72
|
+
# @option expire_at [Integer]
|
73
|
+
# same as `expires_at`
|
39
74
|
def set_session(key, value, options = {})
|
40
75
|
if RESERVED_SESSION_KEYS.include?(key.to_s)
|
41
|
-
|
76
|
+
fail Errors::ReservedSessionKeyConflict.new,
|
77
|
+
"you are trying to set #{value} to #{key}, "\
|
78
|
+
"but reserved by ActionControllerTweaks::Session"
|
42
79
|
end
|
43
80
|
|
44
81
|
session[key] = value
|
45
82
|
|
46
|
-
session[:session_keys_to_expire] =
|
83
|
+
session[:session_keys_to_expire] = _new_session_keys_to_expire(key, options)
|
47
84
|
end
|
48
85
|
|
49
86
|
# set value in session just like `set_session`, but checked option keys
|
@@ -51,30 +88,34 @@ module ActionControllerTweaks
|
|
51
88
|
# @raise [ActionControllerTweaks::Session::Errors::InvalidOptionKeys]
|
52
89
|
def set_session_with_expiry(key, value, options = {})
|
53
90
|
option_keys = options.symbolize_keys.keys
|
54
|
-
required_option_key_present = option_keys.any? do |
|
55
|
-
VALID_OPTION_KEYS.include?(
|
91
|
+
required_option_key_present = option_keys.any? do |k|
|
92
|
+
VALID_OPTION_KEYS.include?(k)
|
56
93
|
end
|
57
94
|
invalid_option_key_absent = (option_keys - VALID_OPTION_KEYS.dup).empty?
|
58
|
-
|
95
|
+
unless required_option_key_present && invalid_option_key_absent
|
96
|
+
fail ActionControllerTweaks::Session::Errors::InvalidOptionKeys
|
97
|
+
end
|
59
98
|
|
60
99
|
set_session(key, value, options)
|
61
100
|
end
|
62
101
|
|
63
|
-
def
|
102
|
+
def _delete_expired_session_keys
|
64
103
|
# Remove keys that are expired
|
65
104
|
session_keys_to_expire.each do |key, expire_at_str|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
session.delete(key)
|
75
|
-
session_keys_to_expire.delete(key)
|
76
|
-
end
|
105
|
+
_delete_expired_session_key(key, expire_at_str)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
def _delete_expired_session_key(key, expire_at_str)
|
110
|
+
if Time.now > Time.parse(expire_at_str.to_s)
|
111
|
+
session.delete(key)
|
112
|
+
session_keys_to_expire.delete(key)
|
77
113
|
end
|
114
|
+
rescue
|
115
|
+
# Parse error
|
116
|
+
# Let"s expire it to be safe
|
117
|
+
session.delete(key)
|
118
|
+
session_keys_to_expire.delete(key)
|
78
119
|
end
|
79
120
|
|
80
121
|
def session_keys_to_expire
|
@@ -86,27 +127,15 @@ module ActionControllerTweaks
|
|
86
127
|
session[:session_keys_to_expire]
|
87
128
|
end
|
88
129
|
|
89
|
-
def
|
130
|
+
def _new_session_keys_to_expire(key, options = {})
|
90
131
|
options.symbolize_keys!
|
91
132
|
|
92
133
|
result = session_keys_to_expire
|
134
|
+
expires_in = OptionProcessor.extract_expires_in(options)
|
135
|
+
expires_at = OptionProcessor.extract_expires_at(options)
|
93
136
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
if expires_at && expires_at.respond_to?(:to_time)
|
98
|
-
expires_at = expires_at.to_time
|
99
|
-
end
|
100
|
-
|
101
|
-
raise InvalidOptionValue.new(:expires_in, expires_in, Numeric) if expires_in && !expires_in.is_a?(Numeric)
|
102
|
-
raise InvalidOptionValue.new(:expires_at, expires_at, Time) if expires_at && !expires_at.is_a?(Time)
|
103
|
-
|
104
|
-
if expires_in || expires_at
|
105
|
-
result[key] = if expires_in
|
106
|
-
expires_in.seconds.from_now
|
107
|
-
elsif expires_at
|
108
|
-
expires_at
|
109
|
-
end
|
137
|
+
if [expires_in, expires_at].any?
|
138
|
+
result[key] = expires_in ? expires_in.seconds.from_now : expires_at
|
110
139
|
end
|
111
140
|
|
112
141
|
result
|