<commit_before>require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))

require 'active_record'
require 'sqlite3' if !defined?(JRUBY_VERSION)
require 'logger'

AlgoliaSearch.configuration = { :application_id => ENV['ALGOLIA_APPLICATION_ID'], :api_key => ENV['ALGOLIA_API_KEY'] }

FileUtils.rm( 'data.sqlite3' ) rescue nil
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.establish_connection(
    'adapter' => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
    'database' => 'data.sqlite3',
    'pool' => 5,
    'timeout' => 5000
)

ActiveRecord::Schema.define do
  create_table :products do |t|
    t.string :name
    t.string :href
    t.string :tags
    t.string :type
    t.text :description
    t.datetime :release_date
  end
  create_table :colors do |t|
    t.string :name
    t.string :short_name
    t.integer :hex
  end
  create_table :namespaced_models do |t|
    t.string :name
  end
  create_table :uniq_users, :id => false do |t|
    t.string :name
  end
  create_table :nested_items do |t|
    t.integer :parent_id
    t.boolean :hidden
  end
  create_table :cities do |t|
    t.string :name
    t.string :country
    t.float :lat
    t.float :lng
  end
  create_table :mongo_objects do |t|
    t.string :name
  end
  create_table :books do |t|
    t.string :name
    t.string :author
    t.boolean :premium
    t.boolean :released
  end
end

class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :auto_index => false,
    :if => :published?, :unless => lambda { |o| o.href.blank? },
    :index_name => safe_index_name("my_products_index") do

    attribute :href, :name, :tags
    tags do
      [name, name] # multiple tags
    end
  end

  def tags=(names)
    @tags = names.join(",")
  end

  def published?
    release_date.blank? || release_date <= Time.now
  end
end

class Camera < Product
end

class Color < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("Color"), :per_environment => true do
    attributesToIndex [:name]
    attributesForFaceting [:short_name]
    customRanking ["asc(hex)"]
    tags do
      name # single tag
    end
  end
end

module Namespaced
  def self.table_name_prefix
    'namespaced_'
  end
end
class Namespaced::Model < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true do
    attribute :customAttr do
      40 + another_private_value
    end
    attribute :myid do
      id
    end
    tags ['static_tag1', 'static_tag2']
  end

  private
  def another_private_value
    2
  end
end

class UniqUser < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("UniqUser"), :per_environment => true, :id => :name do
  end
end

class NestedItem < ActiveRecord::Base
  has_many :children, :class_name => "NestedItem", :foreign_key => "parent_id"

  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("NestedItem"), :per_environment => true, :unless => :hidden do
    add_attribute :nb_children
  end

  def nb_children
    children.count
  end
end

class City < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("City"), :per_environment => true do
    geoloc :lat, :lng

    add_slave safe_index_name('City_slave1'), :per_environment => true do
      attributesToIndex [:country]
    end
  end
end

class MongoObject < ActiveRecord::Base
  include AlgoliaSearch
<commit_msg>Cosmetics, improved UT<commit_after>require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))

require 'active_record'
require 'sqlite3' if !defined?(JRUBY_VERSION)
require 'logger'

AlgoliaSearch.configuration = { :application_id => ENV['ALGOLIA_APPLICATION_ID'], :api_key => ENV['ALGOLIA_API_KEY'] }

FileUtils.rm( 'data.sqlite3' ) rescue nil
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.establish_connection(
    'adapter' => defined?(JRUBY_VERSION) ? 'jdbcsqlite3' : 'sqlite3',
    'database' => 'data.sqlite3',
    'pool' => 5,
    'timeout' => 5000
)

ActiveRecord::Schema.define do
  create_table :products do |t|
    t.string :name
    t.string :href
    t.string :tags
    t.string :type
    t.text :description
    t.datetime :release_date
  end
  create_table :colors do |t|
    t.string :name
    t.string :short_name
    t.integer :hex
  end
  create_table :namespaced_models do |t|
    t.string :name
  end
  create_table :uniq_users, :id => false do |t|
    t.string :name
  end
  create_table :nested_items do |t|
    t.integer :parent_id
    t.boolean :hidden
  end
  create_table :cities do |t|
    t.string :name
    t.string :country
    t.float :lat
    t.float :lng
  end
  create_table :mongo_objects do |t|
    t.string :name
  end
  create_table :books do |t|
    t.string :name
    t.string :author
    t.boolean :premium
    t.boolean :released
  end
end

class Product < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :auto_index => false,
    :if => :published?, :unless => lambda { |o| o.href.blank? },
    :index_name => safe_index_name("my_products_index") do

    attribute :href, :name, :tags
    tags do
      [name, name] # multiple tags
    end
  end

  def tags=(names)
    @tags = names.join(",")
  end

  def published?
    release_date.blank? || release_date <= Time.now
  end
end

class Camera < Product
end

class Color < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("Color"), :per_environment => true do
    attributesToIndex [:name]
    attributesForFaceting [:short_name]
    customRanking ["asc(hex)"]
    tags do
      name # single tag
    end
  end
end

module Namespaced
  def self.table_name_prefix
    'namespaced_'
  end
end
class Namespaced::Model < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true do
    attribute :customAttr do
      40 + another_private_value
    end
    attribute :myid do
      id
    end
    tags ['static_tag1', 'static_tag2']
  end

  private
  def another_private_value
    2
  end
end

class UniqUser < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("UniqUser"), :per_environment => true, :id => :name do
  end
end

class NestedItem < ActiveRecord::Base
  has_many :children, :class_name => "NestedItem", :foreign_key => "parent_id"

  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("NestedItem"), :per_environment => true, :unless => :hidden do
    attribute :nb_children
  end

  def nb_children
    children.count
  end
end

class City < ActiveRecord::Base
  include AlgoliaSearch

  algoliasearch :synchronous => true, :index_name => safe_index_name("City"), :per_environment => true do
    geoloc :lat, :lng

    add_slave safe_index_name('City_slave1'), :per_environment => true do
      attributesToIndex [:country]
    end
  end
end

class MongoObject < ActiveRecord::Base
  include AlgoliaSearch

{'fraction_non_alphanumeric': 0.10669077757685352, 'fraction_numerical': 0.004451244957574071, 'mean_word_length': 3.7521480502313285, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3067319', 'n_tokens_mistral': 2482, 'n_tokens_neox': 2420, 'n_words': 622}