Commit ed00548cd8ab9427fa95829c01aafdbb6116c525
1 parent
e7f608cd
Exists in
master
and in
21 other branches
added the possibility to use a proc or labda as an index_name
Showing
3 changed files
with
9 additions
and
7 deletions
Show diff stats
lib/searchkick/model.rb
@@ -3,17 +3,19 @@ module Searchkick | @@ -3,17 +3,19 @@ module Searchkick | ||
3 | 3 | ||
4 | def searchkick(options = {}) | 4 | def searchkick(options = {}) |
5 | class_eval do | 5 | class_eval do |
6 | - cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass, :searchkick_index | 6 | + cattr_reader :searchkick_options, :searchkick_env, :searchkick_klass |
7 | 7 | ||
8 | class_variable_set :@@searchkick_options, options.dup | 8 | class_variable_set :@@searchkick_options, options.dup |
9 | class_variable_set :@@searchkick_env, ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" | 9 | class_variable_set :@@searchkick_env, ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development" |
10 | class_variable_set :@@searchkick_klass, self | 10 | class_variable_set :@@searchkick_klass, self |
11 | class_variable_set :@@searchkick_callbacks, options[:callbacks] != false | 11 | class_variable_set :@@searchkick_callbacks, options[:callbacks] != false |
12 | + class_variable_set :@@searchkick_index, options[:index_name] || [options[:index_prefix], model_name.plural, searchkick_env].compact.join("_") | ||
12 | 13 | ||
13 | - # set index name | ||
14 | - # TODO support proc | ||
15 | - index_name = options[:index_name] || [options[:index_prefix], model_name.plural, searchkick_env].compact.join("_") | ||
16 | - class_variable_set :@@searchkick_index, Searchkick::Index.new(index_name) | 14 | + def self.searchkick_index |
15 | + index = class_variable_get :@@searchkick_index | ||
16 | + index = index.call if index.respond_to? :call | ||
17 | + Searchkick::Index.new(index) | ||
18 | + end | ||
17 | 19 | ||
18 | extend Searchkick::Search | 20 | extend Searchkick::Search |
19 | extend Searchkick::Reindex | 21 | extend Searchkick::Reindex |
test/inheritance_test.rb
@@ -10,7 +10,7 @@ class TestInheritance < Minitest::Unit::TestCase | @@ -10,7 +10,7 @@ class TestInheritance < Minitest::Unit::TestCase | ||
10 | end | 10 | end |
11 | 11 | ||
12 | def test_child_index_name | 12 | def test_child_index_name |
13 | - assert_equal "animals_test", Dog.searchkick_index.name | 13 | + assert_equal "animals-#{Date.today.year}", Dog.searchkick_index.name |
14 | end | 14 | end |
15 | 15 | ||
16 | def test_child_search | 16 | def test_child_search |
test/test_helper.rb
@@ -143,7 +143,7 @@ class Store | @@ -143,7 +143,7 @@ class Store | ||
143 | end | 143 | end |
144 | 144 | ||
145 | class Animal | 145 | class Animal |
146 | - searchkick autocomplete: [:name], suggest: [:name] | 146 | + searchkick autocomplete: [:name], suggest: [:name], index_name: -> { "#{self.name.tableize}-#{Date.today.year}" } |
147 | end | 147 | end |
148 | 148 | ||
149 | Product.searchkick_index.delete if Product.searchkick_index.exists? | 149 | Product.searchkick_index.delete if Product.searchkick_index.exists? |