Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[#446] Make the `:as` mapping option strict about the passed object

Based on discussion with @mauricio and @ches in #446.

Still not sure... why you shouldn't be able to do:

    class MyModel
        include Some::ORM
        include Tire::Model::Search

        mapping do
          indexes :field, :type => 'string', :analyzer => 'keyword', :as => SomeOtherObject.whatever
        end
      end
    end

or:

    class MyModel
        include Some::ORM
        include Tire::Model::Search

        mapping do
          indexes :field, :type => 'string', :analyzer => 'keyword', :as => [1, 2, 3]
        end
      end
    end

/cc @vhyza
  • Loading branch information...
commit 0ccee42ccfc1bba9c4264a8e9c56c3b4965cfb49 1 parent 0f38828
@karmi authored
Showing with 19 additions and 0 deletions.
  1. +7 −0 lib/tire/model/indexing.rb
  2. +12 −0 test/unit/model_search_test.rb
View
7 lib/tire/model/indexing.rb
@@ -5,6 +5,8 @@ module Model
#
module Indexing
+ VALID_AS_OPTIONS = [Symbol, String, Proc]
+
module ClassMethods
# Define [_settings_](http://www.elasticsearch.org/guide/reference/api/admin-indices-create-index.html)
@@ -85,6 +87,11 @@ def mapping(*args)
#
def indexes(name, options = {}, &block)
mapping[name] = options
+ if as_option = options[:as]
+ raise ArgumentError,
+ "Invalid `:as` option: #{as_option.inspect}. Please use on of: #{VALID_AS_OPTIONS}" \
+ unless VALID_AS_OPTIONS.include?(as_option.class)
+ end
if block_given?
mapping[name][:type] ||= 'object'
View
12 test/unit/model_search_test.rb
@@ -656,6 +656,18 @@ def method_missing(name, *args, &block)
assert_equal 3, document['three']
end
+ should "raise an exception when invalid :as option is set" do
+ assert_raise ArgumentError do
+ class ::ModelWithInvalidAsOption
+ include Tire::Model::Search
+
+ mapping do
+ indexes :three, :type => 'string', :analyzer => 'keyword', :as => Time.now
+ end
+ end
+ end
+ end
+
end
context "with percolation" do
Please sign in to comment.
Something went wrong with that request. Please try again.