micro_sessions 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Appraisals +8 -0
- data/Gemfile +4 -5
- data/Gemfile.lock +36 -34
- data/README.md +91 -0
- data/Rakefile +2 -25
- data/lib/micro_session/engine.rb +20 -0
- data/lib/micro_session/store.rb +60 -0
- data/lib/micro_session/version.rb +4 -0
- data/lib/micro_sessions.rb +2 -16
- data/spec/controllers/humans_controller_spec.rb +9 -0
- data/spec/controllers/mutants_controller_spec.rb +31 -0
- data/spec/dummy/app/controllers/application_controller.rb +2 -0
- data/spec/dummy/app/controllers/humans_controller.rb +9 -0
- data/spec/dummy/app/controllers/mutants_controller.rb +12 -0
- data/spec/dummy/app/views/humans/index.html.erb +1 -0
- data/spec/dummy/app/views/mutants/index.html.erb +9 -0
- data/spec/dummy/app/views/mutants/show.html.erb +3 -0
- data/spec/dummy/config/database.yml +2 -0
- data/spec/dummy/config/routes.rb +4 -0
- data/spec/dummy/init.rb +32 -0
- data/spec/dummy/log/test.log +1327 -0
- data/spec/features/persisting_session_using_links_spec.rb +23 -0
- data/spec/rails_helper.rb +17 -0
- data/spec/spec_helper.rb +16 -7
- metadata +31 -19
- data/README.rdoc +0 -80
- data/lib/micro_sessions/controller.rb +0 -30
- data/lib/micro_sessions/helpers.rb +0 -37
- data/lib/micro_sessions/micro_session.rb +0 -48
- data/lib/micro_sessions/railtie.rb +0 -31
- data/lib/micro_sessions/version.rb +0 -3
- data/spec/controller_spec.rb +0 -80
- data/spec/helpers_spec.rb +0 -25
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'persisting storage using links' do
|
4
|
+
context 'when clicking a MicroSession link' do
|
5
|
+
it 'persists the session between different pages' do
|
6
|
+
visit '/mutants'
|
7
|
+
|
8
|
+
click_link 'micro_session_link'
|
9
|
+
|
10
|
+
expect(page).to have_content 'Wolverine'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when clicking a Rails standard link' do
|
15
|
+
it 'does not persist the session between different pages' do
|
16
|
+
visit '/mutants'
|
17
|
+
|
18
|
+
click_link 'rails_standard_link'
|
19
|
+
|
20
|
+
expect(page).to_not have_content 'Wolverine'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
ENV['RAILS_ENV'] ||= 'test'
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require File.expand_path('../dummy/init', __FILE__)
|
5
|
+
|
6
|
+
# Prevent database truncation if the environment is production
|
7
|
+
abort('The Rails environment is running in production mode!') if Rails.env.production?
|
8
|
+
|
9
|
+
require 'rspec/rails'
|
10
|
+
require 'capybara/rspec'
|
11
|
+
|
12
|
+
RSpec.configure do |config|
|
13
|
+
config.include Capybara::DSL
|
14
|
+
|
15
|
+
config.infer_spec_type_from_file_location!
|
16
|
+
config.filter_rails_from_backtrace!
|
17
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,18 @@
|
|
1
|
-
require "rubygems"
|
2
|
-
require "rspec"
|
3
|
-
require "active_support"
|
4
|
-
require "action_pack"
|
5
|
-
require "micro_sessions"
|
6
|
-
|
7
1
|
RSpec.configure do |config|
|
8
|
-
config.
|
2
|
+
config.expect_with :rspec do |expectations|
|
3
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
4
|
+
end
|
5
|
+
|
6
|
+
config.mock_with :rspec do |mocks|
|
7
|
+
mocks.verify_partial_doubles = true
|
8
|
+
end
|
9
|
+
|
10
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
11
|
+
config.expose_dsl_globally = true
|
12
|
+
config.filter_run_when_matching :focus
|
13
|
+
config.disable_monkey_patching!
|
14
|
+
config.profile_examples = 10
|
15
|
+
config.order = :random
|
16
|
+
|
17
|
+
Kernel.srand config.seed
|
9
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: micro_sessions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ben Hughes
|
8
|
+
- Santiago Doldan
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
12
|
+
date: 2017-10-31 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: rails
|
@@ -16,23 +17,23 @@ dependencies:
|
|
16
17
|
requirements:
|
17
18
|
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: 3.0
|
20
|
+
version: '3.0'
|
20
21
|
- - "<="
|
21
22
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
23
|
+
version: '5.1'
|
23
24
|
type: :runtime
|
24
25
|
prerelease: false
|
25
26
|
version_requirements: !ruby/object:Gem::Requirement
|
26
27
|
requirements:
|
27
28
|
- - ">="
|
28
29
|
- !ruby/object:Gem::Version
|
29
|
-
version: 3.0
|
30
|
+
version: '3.0'
|
30
31
|
- - "<="
|
31
32
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
33
|
+
version: '5.1'
|
33
34
|
description: Provides 'micro-sessions' within the global sessions object keyed off
|
34
35
|
an identifier.
|
35
|
-
email:
|
36
|
+
email: santiagodolte@gmail.com
|
36
37
|
executables: []
|
37
38
|
extensions: []
|
38
39
|
extra_rdoc_files: []
|
@@ -41,19 +42,30 @@ files:
|
|
41
42
|
- Gemfile
|
42
43
|
- Gemfile.lock
|
43
44
|
- LICENSE
|
44
|
-
- README.
|
45
|
+
- README.md
|
45
46
|
- Rakefile
|
47
|
+
- lib/micro_session/engine.rb
|
48
|
+
- lib/micro_session/store.rb
|
49
|
+
- lib/micro_session/version.rb
|
46
50
|
- lib/micro_sessions.rb
|
47
|
-
-
|
48
|
-
-
|
49
|
-
-
|
50
|
-
-
|
51
|
-
-
|
52
|
-
- spec/
|
53
|
-
- spec/
|
51
|
+
- spec/controllers/humans_controller_spec.rb
|
52
|
+
- spec/controllers/mutants_controller_spec.rb
|
53
|
+
- spec/dummy/app/controllers/application_controller.rb
|
54
|
+
- spec/dummy/app/controllers/humans_controller.rb
|
55
|
+
- spec/dummy/app/controllers/mutants_controller.rb
|
56
|
+
- spec/dummy/app/views/humans/index.html.erb
|
57
|
+
- spec/dummy/app/views/mutants/index.html.erb
|
58
|
+
- spec/dummy/app/views/mutants/show.html.erb
|
59
|
+
- spec/dummy/config/database.yml
|
60
|
+
- spec/dummy/config/routes.rb
|
61
|
+
- spec/dummy/init.rb
|
62
|
+
- spec/dummy/log/test.log
|
63
|
+
- spec/features/persisting_session_using_links_spec.rb
|
64
|
+
- spec/rails_helper.rb
|
54
65
|
- spec/spec_helper.rb
|
55
|
-
homepage: http://github.com/
|
56
|
-
licenses:
|
66
|
+
homepage: http://github.com/santiagodoldan/micro_sessions
|
67
|
+
licenses:
|
68
|
+
- MIT
|
57
69
|
metadata: {}
|
58
70
|
post_install_message:
|
59
71
|
rdoc_options: []
|
@@ -63,7 +75,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
75
|
requirements:
|
64
76
|
- - ">="
|
65
77
|
- !ruby/object:Gem::Version
|
66
|
-
version:
|
78
|
+
version: '2.0'
|
67
79
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
80
|
requirements:
|
69
81
|
- - ">="
|
@@ -71,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
83
|
version: 1.3.5
|
72
84
|
requirements: []
|
73
85
|
rubyforge_project:
|
74
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.4.3
|
75
87
|
signing_key:
|
76
88
|
specification_version: 4
|
77
89
|
summary: Keyed sessions within the global sessions object.
|
data/README.rdoc
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
{<img src="https://secure.travis-ci.org/rubiety/micro_sessions.png?branch=master" alt="Build Status" />}[http://travis-ci.org/rubiety/micro_sessions]
|
2
|
-
|
3
|
-
== Micro Sessions
|
4
|
-
|
5
|
-
In terms of passing data amongst HTTP requests there are really two options:
|
6
|
-
* Param or form data passing
|
7
|
-
* Storing in cookies
|
8
|
-
* Storing in server-side session, keyed off an ID stored as a cookie.
|
9
|
-
|
10
|
-
For a certain application where you're using number 3, this comes at a cost - the session is "global". In other words, in a single browser window with multiple tabs, the server side cannot distinguish between individual "sessions" *within* that browser session. This gem introduces a fourth, more granular method:
|
11
|
-
* Storing in server-side session, keyed off an ID stored as a cookie *and* keyed off a request parameter.
|
12
|
-
|
13
|
-
This could be useful in instances such as a very complex wizard where transmitting the entire history of data RESTfully via param passing may not be feasible, yet you'd still like to support multiple tabs within the same browser window separately.
|
14
|
-
|
15
|
-
== Installation
|
16
|
-
|
17
|
-
This gem requires ActionPack and ActiveSupport 3.0 or greater.
|
18
|
-
|
19
|
-
Include the gem in your Gemfile:
|
20
|
-
|
21
|
-
gem "micro_sessions"
|
22
|
-
|
23
|
-
NOTE: This gem currently only works with rails 3.0 and rails 3.1, but *not* rails 3.2, due to an issue with overriding url_for. If you want to help with this, see issue #1.
|
24
|
-
|
25
|
-
== Usage
|
26
|
-
|
27
|
-
Simply include the middleware in your controller:
|
28
|
-
|
29
|
-
class ApplicationController
|
30
|
-
micro_sessions
|
31
|
-
end
|
32
|
-
|
33
|
-
From there you will have access to a +micro_session+ object in your controllers and views, just like the +session+ object.
|
34
|
-
|
35
|
-
def new
|
36
|
-
micro_session[:key] = "Value"
|
37
|
-
end
|
38
|
-
|
39
|
-
By default the name of the request parameter used to differentiate micro-sessions is "_msid", though this can be configured:
|
40
|
-
|
41
|
-
class ApplicationController
|
42
|
-
micro_sessions :key => "_msid"
|
43
|
-
end
|
44
|
-
|
45
|
-
By default the key generated will be a 10-digit random hash, though this can be configured as well:
|
46
|
-
|
47
|
-
class ApplicationController
|
48
|
-
micro_sessions :type => :integer, :length => 10
|
49
|
-
end
|
50
|
-
|
51
|
-
Valid values for +:type+: are:
|
52
|
-
* +:random_hash+
|
53
|
-
* +:integer+ (simply increments from one)
|
54
|
-
* +:random_integer+ (random integer with a maximum of +:length+ digits)
|
55
|
-
|
56
|
-
Session data is accessible to the controller and view through the +micro_session+ object, which works identically to the +session+ object but it specific to the micro-session:
|
57
|
-
|
58
|
-
micro_session[:key] = "Value"
|
59
|
-
micro_session[:key] # => "Value"
|
60
|
-
|
61
|
-
When transitioning between pages for which you'd like to keep the micro-session alive, the request parameter must be passed along with any link, form, or redirect. The preferred way is to make use of the new +:micro_session+ option on available on the +url_for+ helper, which will accept either a specific value or just "true" to use the "current" micro-session to this request:
|
62
|
-
|
63
|
-
link_to user_path(@user, :micro_session => true)
|
64
|
-
|
65
|
-
Note that depending on the context in which you're producing this URL, it may not work correctly. Unfortunately the low-level url_for call in the Rails routing engine does not have access to the controller object (and hence the micro_session object), so I can't make this work outside of simple ActionView helpers. You can always explicitly include the URL options by calling:
|
66
|
-
|
67
|
-
micro_session.url_options # => {"_msid" => "0123456789"}
|
68
|
-
|
69
|
-
Can be used something like this:
|
70
|
-
|
71
|
-
|
72
|
-
redirect_to user_path(@user, micro_session.url_options)
|
73
|
-
|
74
|
-
If you wish to transmit the identifier via a form, be sure to include the hidden field:
|
75
|
-
|
76
|
-
<% form_for(@user) do |f| %>
|
77
|
-
<%= micro_session_hidden_field %>
|
78
|
-
...
|
79
|
-
<% end %>
|
80
|
-
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module MicroSessions
|
2
|
-
module Controller
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
module ClassMethods
|
6
|
-
def micro_sessions(options = {})
|
7
|
-
options.stringify_keys!
|
8
|
-
options.assert_valid_keys(:param, :param_type, :key, :length, :counter)
|
9
|
-
|
10
|
-
options[:param] ||= "_msid"
|
11
|
-
options[:key] ||= "_micro_sessions"
|
12
|
-
options[:param_type] ||= :hash
|
13
|
-
options[:length] ||= 10
|
14
|
-
options[:counter] ||= 1
|
15
|
-
|
16
|
-
class_attribute :micro_session_options
|
17
|
-
self.micro_session_options = options
|
18
|
-
|
19
|
-
class_eval do
|
20
|
-
def micro_session
|
21
|
-
@micro_session ||= MicroSessions::MicroSession.new(self)
|
22
|
-
end
|
23
|
-
|
24
|
-
helper_method :micro_session
|
25
|
-
helper_method :micro_session_options
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module MicroSessions
|
2
|
-
module Helpers
|
3
|
-
module UrlHelper
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
def link_to(name = nil, options = nil, html_options = nil, &block)
|
7
|
-
options = add_micro_session(options)
|
8
|
-
|
9
|
-
super
|
10
|
-
end
|
11
|
-
|
12
|
-
def url_for(options = nil)
|
13
|
-
options = add_micro_session(options)
|
14
|
-
|
15
|
-
super
|
16
|
-
end
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def add_micro_session(options)
|
21
|
-
if options.is_a?(Hash) && ms_value = options.delete(:micro_sessions) && !micro_session.empty?
|
22
|
-
options[micro_session.options[:param]] = (ms_value == true ? micro_session.id : ms_value)
|
23
|
-
end
|
24
|
-
|
25
|
-
options
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
module FormTagHelper
|
30
|
-
extend ActiveSupport::Concern
|
31
|
-
|
32
|
-
def micro_session_hidden_field
|
33
|
-
hidden_field_tag controller.class.micro_session_options[:param], micro_session.id
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module MicroSessions
|
2
|
-
class MicroSession
|
3
|
-
def initialize(controller)
|
4
|
-
@controller = controller
|
5
|
-
end
|
6
|
-
|
7
|
-
def options
|
8
|
-
@controller.class.micro_session_options
|
9
|
-
end
|
10
|
-
|
11
|
-
def data
|
12
|
-
@controller.session[options[:key]] ||= {}
|
13
|
-
@controller.session[options[:key]][id] ||= {}
|
14
|
-
@controller.session[options[:key]][id].symbolize_keys!
|
15
|
-
end
|
16
|
-
|
17
|
-
def url_options
|
18
|
-
if empty?
|
19
|
-
{}
|
20
|
-
else
|
21
|
-
{options[:param] => id}
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
delegate :empty?, :[], :[]=, :to_h, :to_hash, :inspect, :to => :data
|
26
|
-
|
27
|
-
def id
|
28
|
-
@id ||= id_from_params || generate_id
|
29
|
-
end
|
30
|
-
|
31
|
-
protected
|
32
|
-
|
33
|
-
def id_from_params
|
34
|
-
@controller.params[options[:param]] if @controller.params
|
35
|
-
end
|
36
|
-
|
37
|
-
def generate_id
|
38
|
-
case options[:param_type]
|
39
|
-
when :hash
|
40
|
-
(Object::const_defined?("SecureRandom") ? SecureRandom : ActiveSupport::SecureRandom).hex(options[:length] / 2)
|
41
|
-
when :integer
|
42
|
-
self.counter += 1
|
43
|
-
when :random_integer
|
44
|
-
(Object::const_defined?("SecureRandom") ? SecureRandom : ActiveSupport::SecureRandom).integer
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
module MicroSessions
|
2
|
-
class Railtie < Rails::Railtie
|
3
|
-
initializer "micro_sessions.configure_view_controller" do
|
4
|
-
ActiveSupport.on_load(:action_controller) do
|
5
|
-
include MicroSessions::Controller
|
6
|
-
end
|
7
|
-
|
8
|
-
ActiveSupport.on_load(:action_view) do
|
9
|
-
include MicroSessions::Helpers::FormTagHelper
|
10
|
-
include MicroSessions::Helpers::UrlHelper
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.insert_controller
|
15
|
-
ActionController::Base.send(:include, MicroSessions::Controller)
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.insert_view
|
19
|
-
ActionView::Helpers::FormTagHelper.send(:include, MicroSessions::Helpers::FormTagHelper)
|
20
|
-
ActionView::Helpers::UrlHelper.send(:include, MicroSessions::Helpers::UrlHelper)
|
21
|
-
end
|
22
|
-
|
23
|
-
MicroSessions::Railtie.insert_controller
|
24
|
-
MicroSessions::Railtie.insert_view
|
25
|
-
|
26
|
-
config.after_initialize do
|
27
|
-
MicroSessions::Railtie.insert_controller
|
28
|
-
MicroSessions::Railtie.insert_view
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/spec/controller_spec.rb
DELETED
@@ -1,80 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe MicroSessions::Controller do
|
4
|
-
context "with a controller" do
|
5
|
-
before do
|
6
|
-
@controller_class = class SampleController < ActionController::Base
|
7
|
-
micro_sessions
|
8
|
-
self
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should default param to _msid" do
|
13
|
-
expect(@controller_class.micro_session_options[:param]).to eq("_msid")
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should default key to _micro_sessions" do
|
17
|
-
expect(@controller_class.micro_session_options[:key]).to eq("_micro_sessions")
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should default length to 10" do
|
21
|
-
expect(@controller_class.micro_session_options[:length]).to eq(10)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should default counter to 1" do
|
25
|
-
expect(@controller_class.micro_session_options[:counter]).to eq(1)
|
26
|
-
end
|
27
|
-
|
28
|
-
context "an instance of the controller" do
|
29
|
-
before do
|
30
|
-
@controller = @controller_class.new
|
31
|
-
|
32
|
-
allow(@controller).to receive(:params).and_return({})
|
33
|
-
end
|
34
|
-
|
35
|
-
it "should expose #micro_session as a method" do
|
36
|
-
expect(@controller.micro_session).to be_kind_of(MicroSessions::MicroSession)
|
37
|
-
end
|
38
|
-
|
39
|
-
it "should generate a 10-character hash id" do
|
40
|
-
expect(@controller.micro_session.id).to be_a(String)
|
41
|
-
expect(@controller.micro_session.id.size).to eq(10)
|
42
|
-
end
|
43
|
-
|
44
|
-
context "with micro session data and another controller" do
|
45
|
-
before do
|
46
|
-
@micro_session_data = { integer: 1, string: "String" }
|
47
|
-
|
48
|
-
@another_controller = @controller_class.new
|
49
|
-
|
50
|
-
allow(@another_controller).to receive(:params).and_return({})
|
51
|
-
allow(@another_controller).to receive(:session).and_return({
|
52
|
-
"_micro_sessions" => {@controller.micro_session.id => @micro_session_data}
|
53
|
-
})
|
54
|
-
end
|
55
|
-
|
56
|
-
context "without passing id" do
|
57
|
-
subject do
|
58
|
-
@another_controller.micro_session
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should not have data" do
|
62
|
-
expect(subject.data).to be_empty
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when passing id" do
|
67
|
-
subject do
|
68
|
-
allow(@another_controller).to receive(:params).and_return({"_msid" => @controller.micro_session.id})
|
69
|
-
|
70
|
-
@another_controller.micro_session
|
71
|
-
end
|
72
|
-
|
73
|
-
it "should have data" do
|
74
|
-
expect(subject.data).to eq(@micro_session_data)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|