Skip to content

Commit b217b91

Browse files
committed
Support ordering facets
1 parent e4da53c commit b217b91

5 files changed

Lines changed: 39 additions & 4 deletions

File tree

app/models/facet.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ class Facet < ApplicationRecord
1111

1212
validates :name, presence: true
1313
validates :name, uniqueness: true
14+
15+
# ------------------------------------------------------------
16+
# Scopes
17+
18+
default_scope { order(:ord) }
1419
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class SupportOrderedFacets < ActiveRecord::Migration[7.0]
2+
ORDERED_FACETS = %w[
3+
Size
4+
Decade
5+
Genre
6+
Medium
7+
Appearance
8+
].freeze
9+
10+
def change
11+
add_column(:facets, :ord, :integer, default: 0, null: false)
12+
set_order!
13+
add_index(:facets, :ord, unique: true)
14+
end
15+
16+
private
17+
18+
def set_order!
19+
return if Rails.env.test?
20+
21+
ORDERED_FACETS.each_with_index do |name, index|
22+
facet = Facet.find_by!(name: name)
23+
facet.update(ord: index)
24+
end
25+
end
26+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[7.0].define(version: 2022_09_21_205344) do
13+
ActiveRecord::Schema[7.0].define(version: 2022_09_21_220637) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "plpgsql"
1616

@@ -19,7 +19,9 @@
1919
t.datetime "created_at", null: false
2020
t.datetime "updated_at", null: false
2121
t.boolean "ordered", default: false, null: false
22+
t.integer "ord", default: 0, null: false
2223
t.index ["name"], name: "index_facets_on_name", unique: true
24+
t.index ["ord"], name: "index_facets_on_ord", unique: true
2325
end
2426

2527
create_table "items", force: :cascade do |t|

spec/factories/facets.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
FactoryBot.define do
22
factory :facet do
33
name { 'Concept' }
4+
ord { -1 }
45

56
to_create do |instance|
6-
instance.id = Facet.find_or_create_by(name: instance.name).id
7+
instance.id = Facet.find_or_create_by(name: instance.name, ord: instance.ord).id
78
instance.reload
89
end
910

10-
%w[Appearance Size Decade Genre Medium].each do |facet_name|
11+
%w[Size Decade Genre Medium Appearance].each_with_index do |facet_name, index|
1112
factory_name = "facet_#{facet_name.downcase}".to_sym
1213
factory(factory_name) do
1314
name { facet_name }
15+
ord { index }
1416
end
1517
end
1618
end

spec/models/term_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
it 'cannot have a parent in a different facet' do
4040
term = create(:term)
41-
other_facet = create(:facet, name: "Not #{term.facet.name}")
41+
other_facet = create(:facet, name: "Not #{term.facet.name}", ord: -2)
4242
other_term = create(:term, value: "Not #{term.value}", facet: other_facet)
4343
term.parent = other_term
4444
expect(term).not_to be_valid

0 commit comments

Comments
 (0)