Skip to content

Commit

Permalink
Add expire_after_seconds to index options.
Browse files Browse the repository at this point in the history
[ close #2443 ]
  • Loading branch information
durran committed Oct 31, 2012
1 parent b19fefc commit ac3c4bb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,14 @@ For instructions on upgrading to newer versions, visit

### New Features

* \#2443 `expire_after_seconds` is now a valid index option.

class Event
include Mongoid::Document
field :status, type: Integer
index({ status: 1 }, { expire_after_seconds: 3600 })
end

* Added `Document.first_or_create!` and `Criteria#first_or_create!`. This
raises a validations error if creation fails validation.

Expand Down
3 changes: 3 additions & 0 deletions lib/mongoid/indexes.rb
Expand Up @@ -99,6 +99,9 @@ def normalize_index_options(options)
opts = options || {}
opts[:dropDups] = opts.delete(:drop_dups) if opts.has_key?(:drop_dups)
opts[:bucketSize] = opts.delete(:bucket_size) if opts.has_key?(:bucket_size)
if opts.has_key?(:expire_after_seconds)
opts[:expireAfterSeconds] = opts.delete(:expire_after_seconds)
end
opts
end

Expand Down
14 changes: 13 additions & 1 deletion lib/mongoid/indexes/validators/options.rb
Expand Up @@ -7,7 +7,19 @@ module Validators
module Options
extend self

VALID_OPTIONS = [ :background, :drop_dups, :name, :sparse, :unique, :max, :min, :bits, :bucket_size ]
VALID_OPTIONS = [
:background,
:drop_dups,
:name,
:sparse,
:unique,
:max,
:min,
:bits,
:bucket_size,
:expire_after_seconds
]

VALID_TYPES = [ 1, -1, "2d", "geoHaystack" ]

# Validate the index specification.
Expand Down
15 changes: 15 additions & 0 deletions spec/mongoid/indexes_spec.rb
Expand Up @@ -227,6 +227,21 @@ def self.hereditary?
end
end

context "when providing an expire_after_seconds option" do

before do
klass.index({ name: 1 }, { expire_after_seconds: 3600 })
end

let(:options) do
klass.index_options[name: 1]
end

it "sets the index with sparse options" do
options.should eq(expireAfterSeconds: 3600)
end
end

context "when providing an invalid option" do

it "raises an error" do
Expand Down

0 comments on commit ac3c4bb

Please sign in to comment.