Skip to content

Commit

Permalink
Add ability to override the session globally via Mongoid.override_ses…
Browse files Browse the repository at this point in the history
…sion.
  • Loading branch information
durran committed Jun 15, 2012
1 parent fc7d3e7 commit 4b75c61
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Expand Up @@ -7,10 +7,12 @@ For instructions on upgrading to newer versions, visit

### New Features

* \#2080/\#2087 The database that Mongoid persists to can now be overridden
on a global level for cases where `Model#with` is not a viable option.
* \#2080/\#2087 The database or session that Mongoid persists to can now be
overridden on a global level for cases where `Model#with` is not a viable
option.

Mongoid.override_database(:secondary)
Mongoid.override_session(:secondary)

Band.create(name: "Placebo") #=> Persists to secondary.
band.albums.create #=> Persists to secondary.
Expand All @@ -20,6 +22,7 @@ For instructions on upgrading to newer versions, visit
to nil if you no longer want the override to happen.

Mongoid.override_database(nil)
Mongoid.override_session(nil)

* \#1989 Criteria `count`, `size` and `length` now behave as Active Record
with regards to database access.
Expand Down
14 changes: 14 additions & 0 deletions lib/mongoid/config.rb
Expand Up @@ -85,6 +85,20 @@ def override_database(name)
Threaded.database_override = name
end

# Override the session to use globally.
#
# @example Override the session globally.
# config.override_session(:optional)
#
# @param [ String, Symbol ] name The name of the session.
#
# @return [ String, Symbol ] The global override.
#
# @since 3.0.0
def override_session(name)
Threaded.session_override = name ? name.to_s : nil
end

# Purge all data in all collections, including indexes.
#
# @example Purge all data.
Expand Down
16 changes: 15 additions & 1 deletion lib/mongoid/sessions.rb
Expand Up @@ -227,6 +227,20 @@ def persistence_options
Threaded.persistence_options(self)
end

# Get the overridden session name. This either can be overridden by
# using +Model.with+ or by overriding at the global level via
# +Mongoid.override_session(:name)+.
#
# @example Get the overridden session name.
# Model.session_override
#
# @return [ String, Symbol ] The overridden session name.
#
# @since 3.0.0
def session_override
persistence_options.try { |opts| opts[:session] } || Threaded.session_override
end

# Give this model specific custom default storage options.
#
# @example Store this model by default in "artists"
Expand Down Expand Up @@ -356,7 +370,7 @@ def __session_name__
#
# @since 3.0.0
def __session__
if persistence_options && name = persistence_options[:session]
if name = session_override
Sessions.with_name(name)
elsif storage_options && name = storage_options[:session]
Sessions.with_name(name)
Expand Down
26 changes: 26 additions & 0 deletions lib/mongoid/threaded.rb
Expand Up @@ -322,6 +322,32 @@ def selection=(value)
Thread.current["[mongoid]:selection"] = value
end

# Get the global session override.
#
# @example Get the global session override.
# Threaded.session_override
#
# @return [ String, Symbol ] The override.
#
# @since 3.0.0
def session_override
Thread.current["[mongoid]:session-override"]
end

# Set the global session override.
#
# @example Set the global session override.
# Threaded.session_override = :testing
#
# @param [ String, Symbol ] The global override name.
#
# @return [ String, Symbol ] The override.
#
# @since 3.0.0
def session_override=(name)
Thread.current["[mongoid]:session-override"] = name
end

# Get the mongoid scope stack for chained criteria.
#
# @example Get the scope stack.
Expand Down
27 changes: 27 additions & 0 deletions spec/mongoid/sessions_spec.rb
Expand Up @@ -753,4 +753,31 @@
end
end
end

context "when overriding the default session", config: :mongohq do

context "when the override is global" do

before do
Mongoid.override_session(:mongohq_single)
end

after do
Band.with(database: "mongoid").delete_all
Mongoid.override_session(nil)
end

let!(:band) do
Band.with(database: "mongoid").create(name: "Tool")
end

let(:persisted) do
Band.with(session: :mongohq_single, database: "mongoid").where(name: "Tool").first
end

it "persists to the overridden session" do
persisted.should eq(band)
end
end
end
end

0 comments on commit 4b75c61

Please sign in to comment.