test/test_helper.rb in attr_searchable-0.0.2 vs test/test_helper.rb in attr_searchable-0.0.3
- old
+ new
@@ -18,17 +18,26 @@
DATABASE = ENV["DATABASE"] || "sqlite"
ActiveRecord::Base.establish_connection YAML.load_file(File.expand_path("../database.yml", __FILE__))[DATABASE]
-class Comment < ActiveRecord::Base; end
+class User < ActiveRecord::Base; end
+class Comment < ActiveRecord::Base
+ include AttrSearchable
+
+ belongs_to :user
+
+ attr_searchable :user => "user.username"
+ attr_searchable :title, :message
+end
+
class Product < ActiveRecord::Base
include AttrSearchable
attr_searchable :title, :description, :brand, :stock, :price, :created_at, :available
- attr_searchable :comment => ["comments.title", "comments.message"]
+ attr_searchable :comment => ["comments.title", "comments.message"], :user => "users.username"
attr_searchable :primary => [:title, :description]
if DATABASE != "sqlite"
attr_searchable_options :title, :type => :fulltext
@@ -39,22 +48,27 @@
if DATABASE == "postgres"
attr_searchable_options :title, :dictionary => "english"
end
has_many :comments
+ has_many :users, :through => :comments
end
FactoryGirl.define do
factory :product do
end
factory :comment do
end
+
+ factory :user do
+ end
end
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS products"
ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS comments"
+ActiveRecord::Base.connection.execute "DROP TABLE IF EXISTS users"
ActiveRecord::Base.connection.create_table :products do |t|
t.string :title
t.text :description
t.integer :stock
@@ -64,11 +78,16 @@
t.string :brand
end
ActiveRecord::Base.connection.create_table :comments do |t|
t.references :product
+ t.references :user
t.string :title
t.text :message
+end
+
+ActiveRecord::Base.connection.create_table :users do |t|
+ t.string :username
end
if DATABASE == "mysql"
ActiveRecord::Base.connection.execute "ALTER TABLE products ENGINE=MyISAM"
ActiveRecord::Base.connection.execute "ALTER TABLE products ADD FULLTEXT INDEX(title), ADD FULLTEXT INDEX(description), ADD FULLTEXT INDEX(title, description)"