From ac3c4bbe43c8756daff844c808c99b84f10834bc Mon Sep 17 00:00:00 2001 From: Durran Jordan Date: Wed, 31 Oct 2012 11:50:44 +0100 Subject: [PATCH] Add expire_after_seconds to index options. [ close #2443 ] --- CHANGELOG.md | 8 ++++++++ lib/mongoid/indexes.rb | 3 +++ lib/mongoid/indexes/validators/options.rb | 14 +++++++++++++- spec/mongoid/indexes_spec.rb | 15 +++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2e3a5ed6f3..885c372e53 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/lib/mongoid/indexes.rb b/lib/mongoid/indexes.rb index d36f07e466..b46b0257fd 100644 --- a/lib/mongoid/indexes.rb +++ b/lib/mongoid/indexes.rb @@ -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 diff --git a/lib/mongoid/indexes/validators/options.rb b/lib/mongoid/indexes/validators/options.rb index e3095f7d48..529dce28dd 100644 --- a/lib/mongoid/indexes/validators/options.rb +++ b/lib/mongoid/indexes/validators/options.rb @@ -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. diff --git a/spec/mongoid/indexes_spec.rb b/spec/mongoid/indexes_spec.rb index 3bc7e05ad3..e8428ca337 100644 --- a/spec/mongoid/indexes_spec.rb +++ b/spec/mongoid/indexes_spec.rb @@ -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