Module: Tire::Model::Naming::ClassMethods

Extended by:
ClassMethods
Included in:
ClassMethods, Search::ClassMethodsProxy
Defined in:
lib/tire/model/naming.rb

Constant Summary collapse

@@__index_prefix__ =

Set or get index prefix for all models or for a specific model.

To set the prefix for all models (preferably in an initializer inside Rails):

Tire::Model::Search.index_prefix Rails.env

To set the prefix for specific model:

class Article
  # ...
  index_prefix 'my_prefix'
end

TODO: Maybe this would be more sane with ActiveSupport extensions such as class_attribute?

nil

Instance Method Summary collapse

Instance Method Details

#document_type(name = nil) ⇒ Object

Get or set the document type for this model, based on arguments.

By default, uses ActiveSupport inflection, so a class named Article will be stored as the article type.

To get the document type:

Article.document_type

To set the document type:

Article.document_type 'my-custom-type'


76
77
78
79
# File 'lib/tire/model/naming.rb', line 76

def document_type name=nil
  @document_type = name if name
  @document_type || klass.model_name.to_s.underscore
end

#index_name(name = nil, &block) ⇒ Object

Get or set the index name for this model, based on arguments.

By default, uses ActiveSupport inflection, so a class named Article will be stored in the articles index.

To get the index name:

Article.index_name

To set the index name:

Article.index_name 'my-custom-name'

You can also use a block for defining the index name, which is evaluated in the class context:

Article.index_name { "articles-#{Time.now.year}" }

Article.index_name { "articles-#{Rails.env}" }


30
31
32
33
34
35
# File 'lib/tire/model/naming.rb', line 30

def index_name name=nil, &block
  @index_name = name if name
  @index_name = block if block_given?
  # TODO: Try to get index_name from ancestor classes
  @index_name || [index_prefix, klass.model_name.plural].compact.join('_')
end

#index_prefix(*args) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/tire/model/naming.rb', line 53

def index_prefix(*args)
  # Uses class or instance variable depending on the context
  if args.size > 0
    value = args.pop
    self.is_a?(Module) ? ( @@__index_prefix__ = value ) : ( @__index_prefix__ = value )
  end
  self.is_a?(Module) ? ( @@__index_prefix__ || nil ) : ( @__index_prefix__ || @@__index_prefix__ || nil )
end