Skip to content

Commit

Permalink
Add NullSerializer to defer serialization to the ActiveRecord databas…
Browse files Browse the repository at this point in the history
…e adapter
  • Loading branch information
bdewater committed Jul 6, 2016
1 parent 7eebb44 commit 4f30c73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ The default assumes a `sessions` tables with columns:

* `id` (numeric primary key),
* `session_id` (string, usually varchar; maximum length is 255), and
* `data` (text or longtext; careful if your session data exceeds 65KB).
* `data` (text, longtext, json or jsonb); careful if your session data exceeds
65KB).

The `session_id` column should always be indexed for speedy lookups.
Session data is marshaled to the `data` column in Base64 format.
Expand All @@ -53,11 +54,14 @@ having a separate `id` column if you don't want it. However, you must
set `session.model.id = session.session_id` by hand! A before filter
on ApplicationController is a good place.

The serializer may be one of `marshal`, `json`, or `hybrid`. `marshal` is
the default and uses the built-in Marshal methods coupled with Base64
encoding. `json` does what it says on the tin, using the `parse()` and
`generate()` methods of the JSON module. `hybrid` will read either type
but write as JSON.
The serializer may be class responding to `#load(value)` and `#dump(value)`, or
a symbol of `marshal`, `json`, `hybrid` or `null`. `marshal` is the default and
uses the built-in Marshal methods coupled with Base64 encoding. `json` does
what it says on the tin, using the `parse()` and `generate()` methods of the
JSON module. `hybrid` will read either type but write as JSON. `null` will
not perform serialization, leaving that up to the ActiveRecord database
adapter. This allows you to take advantage of the native JSON capabilities of
your database.

Since the default class is a simple Active Record, you get timestamps
for free if you add `created_at` and `updated_at` datetime columns to
Expand Down
13 changes: 13 additions & 0 deletions lib/active_record/session_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def serializer_class
JsonSerializer
when :hybrid then
HybridSerializer
when :null then
NullSerializer
else
self.serializer
end
Expand Down Expand Up @@ -91,6 +93,17 @@ def self.needs_migration?(value)
value.start_with?(MARSHAL_SIGNATURE)
end
end

# Defer serialization to the ActiveRecord database adapter
class NullSerializer
def self.load(value)
value
end

def self.dump(value)
value
end
end
end
end
end
Expand Down

0 comments on commit 4f30c73

Please sign in to comment.