karmi / tire-contrib

Additions and extensions for the Tire gem
MIT License
65 stars 31 forks source link

Add support for dynamic templates in the mapping DSL #32

Closed tzar closed 11 years ago

tzar commented 11 years ago

I added some basic syntax around defining dynamic templates on the root object. As I was finishing up implementing this I realised you could actually just do this by adding an extra argument to the mapping call so maybe un-necessary, if so feel free to ignore.

Had a go at doing a basic test for it too, I'm not too familiar with this testing environment so let me know if there's anything I can change or make cleaner there.

Cheers

vhyza commented 11 years ago

Hello,

I think this is not necessary. As you wrote; this should do the same:

require 'tire'
require 'mocha/setup'

include Mocha::API

class Article
  include Tire::Model::Search

  def self.model_name
    model_name = mock
    model_name.stubs(:to_s).returns('article')
    model_name.stubs(:plural).returns('articles')
    model_name
  end

  mapping dynamic_templates: [
      {
        string_template: {
          path_match:         'meta.*',
          match_mapping_type: 'string',
          mapping:            {
            type:  'string',
            index: 'not_analyzed'
          }
        }
      }
    ] do

    indexes :title,   analyzer: 'snowball'
    indexes :content, analyzer: 'snowball'
    indexes :meta,    analyzer: 'snowball'

  end
end

p Article.mapping_to_hash
{:article=>{:dynamic_templates=>[{:string_template=>{:path_match=>"meta.*", :match_mapping_type=>"string", :mapping=>{:type=>"string", :index=>"not_analyzed"}}}], :properties=>{:title=>{:analyzer=>"snowball", :type=>"string"}, :content=>{:analyzer=>"snowball", :type=>"string"}, :meta=>{:analyzer=>"snowball", :type=>"string"}}}}
tzar commented 11 years ago

Yep fair enough. Looks like I used ruby 1.9 hash syntax in my test as well which would need to be fixed anyway