Skip to content

Commit

Permalink
validate self class on store_in
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurnn committed Oct 3, 2013
1 parent b3231c8 commit 06a5333
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/mongoid/sessions/validators/storage.rb
Expand Up @@ -19,12 +19,25 @@ module Storage
#
# @since 3.0.0
def validate(klass, options)
if !options.is_a?(::Hash) || !valid_keys?(options)
if !valid_keys?(options) || !valid_parent?(klass)
raise Errors::InvalidStorageOptions.new(klass, options)
end
end

private
# Determine if the current klass is valid to change store_in
# options
#
# @api private
#
# @param [ Class ] klass
#
# @return [ true, false ] If the class is valid
#
# @since 4.0.0
def valid_parent?(klass)
!klass.superclass.include?(Mongoid::Document)
end

# Determine if all keys in the options hash are valid.
#
Expand All @@ -39,6 +52,7 @@ def validate(klass, options)
#
# @since 3.0.0
def valid_keys?(options)
return false unless options.is_a?(::Hash)
options.keys.all? do |key|
VALID_OPTIONS.include?(key)
end
Expand Down
13 changes: 13 additions & 0 deletions spec/mongoid/sessions_spec.rb
Expand Up @@ -505,6 +505,19 @@
end
end

context "when provided a class that extend another document" do

let(:klass) do
Class.new(Band)
end

it "raises an error" do
expect {
klass.store_in(database: :artists)
}.to raise_error(Mongoid::Errors::InvalidStorageOptions)
end
end

context "when provided a hash" do

context "when the hash is not valid" do
Expand Down

0 comments on commit 06a5333

Please sign in to comment.