Skip to content

Instantly share code, notes, and snippets.

@m4tm4t
Created September 7, 2012 13:35
Show Gist options
  • Save m4tm4t/3666304 to your computer and use it in GitHub Desktop.
Save m4tm4t/3666304 to your computer and use it in GitHub Desktop.
class Article
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Document::Taggable # https://github.com/chebyte/mongoid-simple-tags
## Fields ##
field :title, type: String
## Accessors ##
attr_accessor :body
## Associations ##
belongs_to :user, index: true, inverse_of: :actu
## Validations##
validates :title, presence: true
validates :user, presence: true
## Index ##
index({ title: 1 }, { unique: true, background: true })
## Scopes ##
## Tire ##
include Tire::Model::Search
include Tire::Model::Callbacks
index_name "#{Tire::Model::Search.index_prefix}actus" if Rails.env == "test"
tire do
mapping do
indexes :title, boost: 100
indexes :tag_list
end
end
def to_indexed_json
{
title: title,
tag_list: tag_list
}.to_json
end
end
require 'spec_helper'
describe Article do
describe ".tags", :focus do
let!(:index_name) {"#{Tire::Model::Search.index_prefix}articles"}
let!(:index) { Tire::Index.new(index_name) }
before(:each) do
index.delete
create(:actu, tags: ["Sony", "Gaikai"])
create(:actu, tag_list: "Nintendo, Gamescom")
index.refresh
end
it "Should return all tags" do
search = Tire.search([index_name]) do
facet 'global-tags' do
terms :tag_list
end
end
search.results.facets['global-tags']['terms'].should eq([{"term"=>"sony", "count"=>1}, {"term"=>"nintendo", "count"=>1}, {"term"=>"gamescom", "count"=>1}, {"term"=>"gaikai", "count"=>1}])
end
it "Should return related article" do
search = Tire.search([index_name], load: true) do
filter :terms, tag_list: ['nintendo']
end
search.results.first.tags.should be_an_instance_of(Array)
search.results.first.tags.should include('Nintendo')
search.results.first.tags.should_not include('Sony')
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment