Skip to content

Commit 9420774

Browse files
committed
Make model fixtures generic
Make all models in specs generic to allow them to be easily ported to Mongoid and Sequel instead of being stuck on ActiveRecord only.
1 parent ab19423 commit 9420774

File tree

16 files changed

+232
-217
lines changed

16 files changed

+232
-217
lines changed

spec/support/4_active_record_helper.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,6 @@ def self.schema
2424
end
2525

2626
def self.initialize_model(specification)
27-
schema.create_table specification.name.underscore.pluralize do |t|
28-
specification.fields.each do |field|
29-
t.send(field.type, field.name)
30-
end
31-
end
32-
3327
klass = Class.new(::ActiveRecord::Base) do
3428
define_singleton_method(:model_name) do
3529
name = "Models::ActiveRecord::#{specification.name}"
@@ -40,6 +34,12 @@ def self.initialize_model(specification)
4034
const_set(specification.name, klass)
4135

4236
klass.class_eval(&specification.body)
37+
38+
schema.create_table klass.table_name do |t|
39+
specification.fields.each do |field|
40+
t.send(field.type, field.name)
41+
end
42+
end
4343
end
4444
end
4545
end

spec/support/models/animals.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
require 'support/active_record_schema'
22

3-
ar_schema.create_table :cats do |t|
4-
t.string :name
5-
end
6-
7-
ar_schema.create_table :dogs do |t|
8-
t.string :name
9-
end
10-
11-
class Cat < ActiveRecord::Base
3+
cats_specification = Models::ModelSpecification.new(
4+
'Cat',
5+
fields: [%i[name string]]
6+
) do
127
include Meilisearch::Rails
138

149
meilisearch index_uid: safe_index_uid('animals'), synchronous: true, primary_key: :ms_id
@@ -20,7 +15,10 @@ def ms_id
2015
end
2116
end
2217

23-
class Dog < ActiveRecord::Base
18+
dogs_specification = Models::ModelSpecification.new(
19+
'Dog',
20+
fields: [%i[name string]]
21+
) do
2422
include Meilisearch::Rails
2523

2624
meilisearch index_uid: safe_index_uid('animals'), synchronous: true, primary_key: :ms_id
@@ -32,11 +30,5 @@ def ms_id
3230
end
3331
end
3432

35-
module TestUtil
36-
def self.reset_animals!
37-
Cat.clear_index!(true)
38-
Cat.delete_all
39-
Dog.clear_index!(true)
40-
Dog.delete_all
41-
end
42-
end
33+
Models::ActiveRecord.initialize_model(cats_specification)
34+
Models::ActiveRecord.initialize_model(dogs_specification)

spec/support/models/book.rb

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :books do |t|
4-
t.string :name
5-
t.string :author
6-
t.boolean :premium
7-
t.boolean :released
8-
t.string :genre
9-
end
10-
11-
class Book < ActiveRecord::Base
1+
book_specification = Models::ModelSpecification.new(
2+
'Book',
3+
fields: [
4+
%i[name string],
5+
%i[author string],
6+
%i[premium boolean],
7+
%i[released boolean],
8+
%i[genre string]
9+
]
10+
) do
1211
include Meilisearch::Rails
1312

1413
meilisearch synchronous: true, index_uid: safe_index_uid('SecuredBook'), sanitize: true do
@@ -33,10 +32,37 @@ def public?
3332
end
3433
end
3534

36-
module TestUtil
37-
def self.reset_books!
38-
Book.clear_index!(true)
39-
Book.index(safe_index_uid('BookAuthor')).delete_all_documents
40-
Book.index(safe_index_uid('Book')).delete_all_documents
41-
end
42-
end
35+
Models::ActiveRecord.initialize_model(book_specification)
36+
37+
# class Book < ActiveRecord::Base
38+
# include Meilisearch::Rails
39+
#
40+
# meilisearch synchronous: true, index_uid: safe_index_uid('SecuredBook'), sanitize: true do
41+
# searchable_attributes [:name]
42+
# typo_tolerance min_word_size_for_typos: { one_typo: 5, twoTypos: 8 }
43+
# filterable_attributes %i[genre author]
44+
# faceting max_values_per_facet: 3
45+
#
46+
# add_index safe_index_uid('BookAuthor') do
47+
# searchable_attributes [:author]
48+
# end
49+
#
50+
# add_index safe_index_uid('Book'), if: :public? do
51+
# searchable_attributes [:name]
52+
# end
53+
# end
54+
#
55+
# private
56+
#
57+
# def public?
58+
# released && !premium
59+
# end
60+
# end
61+
62+
# module TestUtil
63+
# def self.reset_books!
64+
# Book.clear_index!(true)
65+
# Book.index(safe_index_uid('BookAuthor')).delete_all_documents
66+
# Book.index(safe_index_uid('Book')).delete_all_documents
67+
# end
68+
# end

spec/support/models/color.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :colors do |t|
4-
t.string :name
5-
t.string :short_name
6-
t.integer :hex
7-
end
8-
9-
class Color < ActiveRecord::Base
1+
colors_specification = Models::ModelSpecification.new(
2+
'Color',
3+
fields: [
4+
%i[name string],
5+
%i[short_name string],
6+
%i[hex integer]
7+
]
8+
) do
109
include Meilisearch::Rails
1110
attr_accessor :not_indexed
1211

@@ -37,9 +36,4 @@ def will_save_change_to_short_name?
3736
end
3837
end
3938

40-
module TestUtil
41-
def self.reset_colors!
42-
Color.clear_index!(true)
43-
Color.delete_all
44-
end
45-
end
39+
Models::ActiveRecord.initialize_model(colors_specification)
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :disabled_booleans do |t|
4-
t.string :name
5-
end
6-
7-
ar_schema.create_table :disabled_procs do |t|
8-
t.string :name
9-
end
10-
11-
ar_schema.create_table :disabled_symbols do |t|
12-
t.string :name
13-
end
14-
15-
class DisabledBoolean < ActiveRecord::Base
1+
disabled_booleans_specification = Models::ModelSpecification.new(
2+
'DisabledBoolean',
3+
fields: [%i[name string]]
4+
) do
165
include Meilisearch::Rails
176

187
meilisearch synchronous: true, disable_indexing: true, index_uid: safe_index_uid('DisabledBoolean')
198
end
209

21-
class DisabledProc < ActiveRecord::Base
10+
Models::ActiveRecord.initialize_model(disabled_booleans_specification)
11+
12+
disabled_procs_specification = Models::ModelSpecification.new(
13+
'DisabledProc',
14+
fields: [%i[name string]]
15+
) do
2216
include Meilisearch::Rails
2317

2418
meilisearch synchronous: true, disable_indexing: proc { true }, index_uid: safe_index_uid('DisabledProc')
2519
end
2620

27-
class DisabledSymbol < ActiveRecord::Base
21+
Models::ActiveRecord.initialize_model(disabled_procs_specification)
22+
23+
disabled_symbols_specification = Models::ModelSpecification.new(
24+
'DisabledSymbol',
25+
fields: [%i[name string]]
26+
) do
2827
include Meilisearch::Rails
2928

3029
meilisearch synchronous: true, disable_indexing: :truth, index_uid: safe_index_uid('DisabledSymbol')
@@ -33,3 +32,5 @@ def self.truth
3332
true
3433
end
3534
end
35+
36+
Models::ActiveRecord.initialize_model(disabled_symbols_specification)

spec/support/models/fruit.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :fruits do |t|
4-
t.string :name
5-
end
6-
7-
class Fruit < ActiveRecord::Base
1+
fruits_specification = Models::ModelSpecification.new(
2+
'Fruit',
3+
fields: [%i[name string]]
4+
) do
85
include Meilisearch::Rails
96

107
# only raise exceptions in development env
118
meilisearch raise_on_failure: true, index_uid: safe_index_uid('Fruit') do
129
attribute :name
1310
end
1411
end
12+
13+
Models::ActiveRecord.initialize_model(fruits_specification)

spec/support/models/movie.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :movies do |t|
4-
t.string :title
5-
end
6-
7-
class Movie < ActiveRecord::Base
1+
movies_specification = Models::ModelSpecification.new(
2+
'Movie',
3+
fields: [%i[title string]]
4+
) do
85
include Meilisearch::Rails
96

107
meilisearch index_uid: safe_index_uid('Movie') do
@@ -13,9 +10,4 @@ class Movie < ActiveRecord::Base
1310
end
1411
end
1512

16-
module TestUtil
17-
def self.reset_movies!
18-
Movie.clear_index!(true)
19-
Movie.delete_all
20-
end
21-
end
13+
Models::ActiveRecord.initialize_model(movies_specification)

spec/support/models/people.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :people do |t|
4-
t.string :first_name
5-
t.string :last_name
6-
t.integer :card_number
7-
end
8-
9-
class People < ActiveRecord::Base
1+
people_specification = Models::ModelSpecification.new(
2+
'People',
3+
fields: [
4+
%i[first_name string],
5+
%i[last_name string],
6+
%i[card_number integer]
7+
]
8+
) do
109
include Meilisearch::Rails
1110

1211
meilisearch synchronous: true, index_uid: safe_index_uid('MyCustomPeople'), primary_key: :card_number,
@@ -23,9 +22,4 @@ def will_save_change_to_full_name?
2322
end
2423
end
2524

26-
module TestUtil
27-
def self.reset_people!
28-
People.clear_index!(true)
29-
People.delete_all
30-
end
31-
end
25+
Models::ActiveRecord.initialize_model(people_specification)

spec/support/models/post.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :posts do |t|
4-
t.string :title
5-
end
6-
7-
ar_schema.create_table :comments do |t|
8-
t.integer :post_id
9-
t.string :body
10-
end
11-
12-
class Post < ActiveRecord::Base
1+
posts_specification = Models::ModelSpecification.new(
2+
'Post',
3+
fields: [%i[title string]]
4+
) do
135
has_many :comments
146

157
include Meilisearch::Rails
@@ -23,10 +15,20 @@ class Post < ActiveRecord::Base
2315
scope :meilisearch_import, -> { includes(:comments) }
2416
end
2517

26-
class Comment < ActiveRecord::Base
18+
Models::ActiveRecord.initialize_model(posts_specification)
19+
20+
comments_specification = Models::ModelSpecification.new(
21+
'Comment',
22+
fields: [
23+
%i[post_id integer],
24+
%i[body string]
25+
]
26+
) do
2727
belongs_to :post
2828

2929
include Meilisearch::Rails
3030

3131
meilisearch
3232
end
33+
34+
Models::ActiveRecord.initialize_model(comments_specification)

spec/support/models/product.rb

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
require 'support/active_record_schema'
2-
3-
ar_schema.create_table :products do |t|
4-
t.string :name
5-
t.string :href
6-
t.string :tags
7-
t.string :type
8-
t.text :description
9-
t.datetime :release_date
10-
end
11-
12-
class Product < ActiveRecord::Base
1+
products_specification = Models::ModelSpecification.new(
2+
'Product',
3+
fields: [
4+
%i[name string],
5+
%i[href string],
6+
%i[tags string],
7+
%i[type string],
8+
%i[description text],
9+
%i[release_date datetime]
10+
]
11+
) do
1312
include Meilisearch::Rails
1413

1514
meilisearch auto_index: false,
@@ -30,5 +29,11 @@ def published?
3029
end
3130
end
3231

33-
class Camera < Product
32+
Models::ActiveRecord.initialize_model(products_specification)
33+
34+
module Models
35+
module ActiveRecord
36+
class Camera < Product
37+
end
38+
end
3439
end

0 commit comments

Comments
 (0)