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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c46747cad6fd140413833b912ecf05a62239a36a
4
- data.tar.gz: 3b79a6cb3a77fa652d49de1879e26adb7d1ec7a8
3
+ metadata.gz: 7056a560bd55cb5d4bb1089d0fd86d8a5dd4821d
4
+ data.tar.gz: 2e35c6fd8984c13cd448cbc7743dfa1a96142924
5
5
  SHA512:
6
- metadata.gz: f6b498a063510ed25780015d9b346f0652e222d779ae96aa9062874972d54c81eb99058a4013d1ac80e91fe3d808f63c21f37a5ebcc1129b075b7a7fa1d38868
7
- data.tar.gz: c3cff2675dcb281ced97f4c1f4739a00557eb4eb1bc24d5587b0b9562a903eed0b673cf2559317c1d7fa9039010083c109b6d4d187441c2f8832cae33a203d58
6
+ metadata.gz: b20f02a8790ff7d13c7c585d012c4bb9f8f8e2d00e68810f4e41de5b30fc89acb504a72ed1d8460b4ac6291a8b411704d2053458615be0d55c74689b3c13a9ae
7
+ data.tar.gz: ad33961eda1ebdcadf63eae1a89631c892bb1ef8c28f9681653dbcca5309a005b04d4f52d2dbda79db264482cd64c4e69b4e8c6832d73cf96b6299d285df5d46
data/Appraisals CHANGED
@@ -16,3 +16,11 @@ end
16
16
  appraise 'rails-4.1' do
17
17
  gem 'rails', '~> 4.1.1'
18
18
  end
19
+
20
+ appraise 'rails-4.2' do
21
+ gem 'rails', '~> 4.2'
22
+ end
23
+
24
+ appraise 'rails-5.1' do
25
+ gem 'rails', '~> 5.1'
26
+ end
data/Gemfile CHANGED
@@ -10,11 +10,10 @@ group :development do
10
10
  end
11
11
 
12
12
  group :test do
13
- gem 'rspec', '>= 3'
14
- gem 'capybara', '~> 2.0'
15
- gem 'launchy', '~> 2.4.3'
16
- gem 'sqlite3', '~> 1.3.4'
17
- gem 'addressable', '~> 2.3.7'
13
+ gem 'rspec', '~> 3.7.0'
14
+ gem 'rspec-rails', '~> 3.6'
15
+ gem 'capybara', '~> 2.15.4'
16
+ gem 'activerecord-nulldb-adapter', '~> 0.3.7'
18
17
  end
19
18
 
20
19
  gemspec
@@ -1,8 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- micro_sessions (0.3.0)
5
- rails (>= 3.0.0, <= 4.1.1)
4
+ micro_sessions (0.4.0)
5
+ rails (>= 3.0, <= 5.1)
6
6
 
7
7
  GEM
8
8
  remote: http://rubygems.org/
@@ -27,41 +27,40 @@ GEM
27
27
  activemodel (= 4.1.1)
28
28
  activesupport (= 4.1.1)
29
29
  arel (~> 5.0.0)
30
+ activerecord-nulldb-adapter (0.3.7)
31
+ activerecord (>= 2.0.0)
30
32
  activesupport (4.1.1)
31
33
  i18n (~> 0.6, >= 0.6.9)
32
34
  json (~> 1.7, >= 1.7.7)
33
35
  minitest (~> 5.1)
34
36
  thread_safe (~> 0.1)
35
37
  tzinfo (~> 1.1)
36
- addressable (2.3.7)
38
+ addressable (2.3.8)
37
39
  appraisal (2.2.0)
38
40
  bundler
39
41
  rake
40
42
  thor (>= 0.14.0)
41
43
  arel (5.0.1.20140414130214)
42
44
  builder (3.2.2)
43
- capybara (2.15.2)
45
+ capybara (2.15.4)
44
46
  addressable
45
47
  mini_mime (>= 0.1.3)
46
48
  nokogiri (>= 1.3.3)
47
49
  rack (>= 1.0.0)
48
50
  rack-test (>= 0.5.4)
49
51
  xpath (~> 2.0)
50
- diff-lcs (1.2.5)
52
+ concurrent-ruby (1.0.5)
53
+ diff-lcs (1.3)
51
54
  erubis (2.7.0)
52
- hike (1.2.3)
53
55
  i18n (0.7.0)
54
56
  json (1.8.6)
55
- launchy (2.4.3)
56
- addressable (~> 2.3)
57
- mail (2.5.4)
57
+ mail (2.5.5)
58
58
  mime-types (~> 1.16)
59
59
  treetop (~> 1.4.8)
60
60
  mime-types (1.25.1)
61
61
  mini_mime (0.1.4)
62
62
  mini_portile2 (2.3.0)
63
63
  minitest (5.5.1)
64
- multi_json (1.10.1)
65
64
  nokogiri (1.8.1)
66
65
  mini_portile2 (~> 2.3.0)
67
66
  polyglot (0.3.5)
@@ -86,32 +85,36 @@ GEM
86
85
  rake (10.5.0)
87
86
  rdoc (4.2.2)
88
87
  json (~> 1.4)
89
- rspec (3.4.0)
90
- rspec-core (~> 3.4.0)
91
- rspec-expectations (~> 3.4.0)
92
- rspec-mocks (~> 3.4.0)
93
- rspec-core (3.4.4)
94
- rspec-support (~> 3.4.0)
95
- rspec-expectations (3.4.0)
88
+ rspec (3.7.0)
89
+ rspec-core (~> 3.7.0)
90
+ rspec-expectations (~> 3.7.0)
91
+ rspec-mocks (~> 3.7.0)
92
+ rspec-core (3.7.0)
93
+ rspec-support (~> 3.7.0)
94
+ rspec-expectations (3.7.0)
96
95
  diff-lcs (>= 1.2.0, < 2.0)
97
- rspec-support (~> 3.4.0)
98
- rspec-mocks (3.4.1)
96
+ rspec-support (~> 3.7.0)
97
+ rspec-mocks (3.7.0)
99
98
  diff-lcs (>= 1.2.0, < 2.0)
100
- rspec-support (~> 3.4.0)
101
- rspec-support (3.4.1)
102
- sprockets (2.12.3)
103
- hike (~> 1.2)
104
- multi_json (~> 1.0)
105
- rack (~> 1.0)
106
- tilt (~> 1.1, != 1.3.0)
107
- sprockets-rails (2.2.4)
99
+ rspec-support (~> 3.7.0)
100
+ rspec-rails (3.7.1)
101
+ actionpack (>= 3.0)
102
+ activesupport (>= 3.0)
103
+ railties (>= 3.0)
104
+ rspec-core (~> 3.7.0)
105
+ rspec-expectations (~> 3.7.0)
106
+ rspec-mocks (~> 3.7.0)
107
+ rspec-support (~> 3.7.0)
108
+ rspec-support (3.7.0)
109
+ sprockets (3.7.1)
110
+ concurrent-ruby (~> 1.0)
111
+ rack (> 1, < 3)
112
+ sprockets-rails (2.3.3)
108
113
  actionpack (>= 3.0)
109
114
  activesupport (>= 3.0)
110
115
  sprockets (>= 2.8, < 4.0)
111
- sqlite3 (1.3.13)
112
116
  thor (0.19.1)
113
117
  thread_safe (0.3.4)
114
- tilt (1.4.1)
115
118
  treetop (1.4.15)
116
119
  polyglot
117
120
  polyglot (>= 0.3.1)
@@ -125,15 +128,14 @@ PLATFORMS
125
128
  ruby
126
129
 
127
130
  DEPENDENCIES
128
- addressable (~> 2.3.7)
131
+ activerecord-nulldb-adapter (~> 0.3.7)
129
132
  appraisal (~> 2.2.0)
130
- capybara (~> 2.0)
131
- launchy (~> 2.4.3)
133
+ capybara (~> 2.15.4)
132
134
  micro_sessions!
133
135
  rake (< 11)
134
136
  rdoc (~> 4.2.2)
135
- rspec (>= 3)
136
- sqlite3 (~> 1.3.4)
137
+ rspec (~> 3.7.0)
138
+ rspec-rails (~> 3.6)
137
139
  wwtd (~> 1.3.0)
138
140
 
139
141
  BUNDLED WITH
@@ -0,0 +1,91 @@
1
+ # Micro Sessions
2
+
3
+ [![Build Status](https://secure.travis-ci.org/rubiety/micro_sessions.png?branch=master)](http://travis-ci.org/rubiety/micro_sessions)
4
+
5
+ **DEPRECATION WARNING: micro_session 0.4.0 includes breaking changes, for previous versions see [Readme](https://github.com/santiagodoldan/micro_sessions/wiki/micro_sessions---=-0.3.0-%5BREADME%5D).**
6
+
7
+ In terms of passing data amongst HTTP requests there are really two options:
8
+ * Param or form data passing
9
+ * Storing in cookies
10
+ * Storing in server-side session, keyed off an ID stored as a cookie.
11
+
12
+ 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:
13
+ * Storing in server-side session, keyed off an ID stored as a cookie *and* keyed off a request parameter.
14
+
15
+ 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.
16
+
17
+ ## Installation
18
+
19
+ Include the gem in your Gemfile:
20
+
21
+ ```ruby
22
+ gem "micro_sessions"
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Simply include MicroSessionable in your controller:
28
+
29
+ ```ruby
30
+ class ApplicationController
31
+ include MicroSessionable
32
+ end
33
+ ```
34
+
35
+ From there you will have access to a `micro_session` object in your controllers and views, just like the `session` object.
36
+
37
+ ```ruby
38
+ def new
39
+ micro_session[:key] = "Value"
40
+ end
41
+ ```
42
+
43
+ By default the name of the request parameter used to differentiate micro-sessions is `_msid`, though this can be configured:
44
+
45
+ ```ruby
46
+ MicroSession.configure do |config|
47
+ config.key = "_msid"
48
+ end
49
+ ```
50
+
51
+ By default the key generated will be a 10-digit random hash, though this can be configured as well:
52
+
53
+ ```ruby
54
+ MicroSession.configure do |config|
55
+ config.type = :integer
56
+ config.length = 10
57
+ end
58
+ ```
59
+
60
+ Valid values for `type` are:
61
+ * `:random_hash`
62
+ * `:integer` (simply increments from one)
63
+ * `:random_integer` (random integer with a maximum of `:length` digits)
64
+
65
+ 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:
66
+
67
+ ```ruby
68
+ micro_session[:key] = "Value"
69
+ micro_session[:key] # => "Value"
70
+ ```
71
+
72
+ When transitioning between pages for which you'd like to keep the micro-session alive, just use the custom `ms_link_to` helper method:
73
+
74
+ ```ruby
75
+ ms_link_to "Persist session", user_path(@user)
76
+ ```
77
+
78
+ You can also build the url manually getting the proper information from micro_session:
79
+
80
+ ```ruby
81
+ redirect_to user_path(@user, micro_session.url_options)
82
+ ```
83
+
84
+ If you wish to transmit the identifier via a form, be sure to include the hidden field:
85
+
86
+ ```ruby
87
+ <% form_for(@user) do |f| %>
88
+ <%= micro_session_hidden_field %>
89
+ ...
90
+ <% end %>
91
+ ```
data/Rakefile CHANGED
@@ -1,30 +1,7 @@
1
1
  require 'rubygems'
2
2
  require 'bundler/setup'
3
3
 
4
- require 'rake'
5
- require 'rspec/core/rake_task'
6
- require 'appraisal'
7
-
8
4
  Bundler::GemHelper.install_tasks
9
5
 
10
- desc 'Default: run unit tests.'
11
- task :default => [:clean, :all]
12
-
13
- desc "Run Specs against all Appraisals"
14
- task :all => :spec do
15
- Rake::Task["appraisal:install"].execute
16
- system("bundle exec rake -s appraisal test")
17
- end
18
-
19
- desc "Run Specs"
20
- RSpec::Core::RakeTask.new(:spec) do |t|
21
- end
22
-
23
- task :test => [:spec]
24
-
25
- desc "Clean up files."
26
- task :clean do |t|
27
- FileUtils.rm_rf "tmp"
28
- Dir.glob("micro_sessions-*.gem").each {|f| FileUtils.rm f }
29
- end
30
-
6
+ desc 'Default: run the rspec examples'
7
+ task :default => [:spec]
@@ -0,0 +1,20 @@
1
+ module MicroSession
2
+ class << self
3
+ alias configure tap
4
+
5
+ mattr_accessor :param, :key, :param_type, :length, :counter
6
+
7
+ self.param = '_msid'
8
+ self.key = '_micro_sessions'
9
+ self.param_type = :hash
10
+ self.length = 11
11
+ self.counter = 2
12
+
13
+ def options
14
+ { param: param, key: key, param_type: param_type, length: length, counter: counter }
15
+ end
16
+ end
17
+
18
+ class Engine < ::Rails::Engine
19
+ end
20
+ end
@@ -0,0 +1,60 @@
1
+ module MicroSession
2
+ class Store
3
+ delegate :empty?, :to_h, :to_hash, :inspect, to: :data
4
+
5
+ def initialize(controller)
6
+ @controller = controller
7
+ end
8
+
9
+ def data
10
+ @controller.session[MicroSession.key] ||= {}
11
+ @controller.session[MicroSession.key][id] ||= {}
12
+ @controller.session[MicroSession.key][id].symbolize_keys!
13
+ end
14
+
15
+ def [](key)
16
+ data[key.to_sym]
17
+ end
18
+
19
+ def []=(key, value)
20
+ data[key.to_sym] = value
21
+ end
22
+
23
+ def url_options
24
+ if empty?
25
+ {}
26
+ else
27
+ { MicroSession.param => id }
28
+ end
29
+ end
30
+
31
+ def id
32
+ @id ||= id_from_params || generate_id
33
+ end
34
+
35
+ private
36
+
37
+ def id_from_params
38
+ @controller.params[MicroSession.param] if @controller.params
39
+ end
40
+
41
+ def generate_id
42
+ case MicroSession.param_type
43
+ when :hash
44
+ secure_random.hex(MicroSession.length / 2)
45
+ when :integer
46
+ self.counter += 1
47
+ when :random_integer
48
+ secure_random.integer
49
+ end
50
+ end
51
+
52
+ def secure_random
53
+ if Object.const_defined?('SecureRandom')
54
+ SecureRandom
55
+ else
56
+ ActiveSupport::SecureRandom
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,4 @@
1
+ module MicroSession
2
+ VERSION = '0.4.0'
3
+ end
4
+
@@ -1,18 +1,4 @@
1
- require "rails"
2
- require "action_controller"
3
- require "action_view"
1
+ require 'micro_session/engine'
4
2
 
5
- module MicroSessions
6
- extend ActiveSupport::Concern
7
- extend ActiveSupport::Autoload
8
-
9
- autoload :Controller
10
- autoload :Helpers
11
- autoload :MicroSession
12
-
13
- included do
14
- include MicroSessions::Controller
15
- end
3
+ module MicroSession
16
4
  end
17
-
18
- require "micro_sessions/railtie"
@@ -0,0 +1,9 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe HumansController do
4
+ describe 'GET \'index\'' do
5
+ it 'does not respond to micro_session' do
6
+ expect(subject.respond_to?(:micro_session)).to be_falsey
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,31 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe MutantsController do
4
+ describe 'GET \'index\'' do
5
+ it 'returns a MicroSession::Store instance' do
6
+ get :index
7
+
8
+ expect(subject.micro_session).to be_kind_of(MicroSession::Store)
9
+ end
10
+
11
+ context 'when there is no data stored in the session' do
12
+ it 'returns an empty hash' do
13
+ get :index
14
+
15
+ expect(subject.micro_session.to_h).to be_empty
16
+ end
17
+ end
18
+
19
+ context 'when there is data stored in the session' do
20
+ before do
21
+ subject.micro_session['test'] = 'test'
22
+ end
23
+
24
+ it 'returns stored data' do
25
+ get :index
26
+
27
+ expect(subject.micro_session.to_h).to eq(test: 'test')
28
+ end
29
+ end
30
+ end
31
+ end