Skip to content

Commit

Permalink
Merge pull request #717 from ShelterTechSF/712-subcategory-priority-rank
Browse files Browse the repository at this point in the history
712 add priority rank to category_relationship table. Return categori…
  • Loading branch information
schroerbrian committed Jun 14, 2023
2 parents 9a87952 + 19ed741 commit fdd1e7d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ def show
end

def subcategories
categories = Category.where("id in (select child_id from category_relationships where parent_id=?)", params[:id])
# Fetch category children records. Records are ordered by (optional) child_priority_rank
category_relationships = CategoryRelationship.where(parent_id: params[:id])
.order(:child_priority_rank)
.select(:child_id, :child_priority_rank)

child_ids = category_relationships.pluck(:child_id)

# Query categories using child_ids and order by child_priority_rank
categories = Category.joins("INNER JOIN (#{category_relationships.to_sql}) cr ON categories.id = cr.child_id")
.where(id: child_ids)
.order("cr.child_priority_rank")

render json: CategoryPresenter.present(categories)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddChildPriorityRankColumnToCategoryRelationships < ActiveRecord::Migration[6.1]
def change
add_column :category_relationships, :child_priority_rank, :integer
add_index(:category_relationships, [:child_id, :parent_id], :unique => true)
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_05_30_212331) do
ActiveRecord::Schema.define(version: 2023_06_09_201040) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -117,6 +117,8 @@
create_table "category_relationships", id: false, force: :cascade do |t|
t.integer "parent_id", null: false
t.integer "child_id", null: false
t.integer "child_priority_rank"
t.index ["child_id", "parent_id"], name: "index_category_relationships_on_child_id_and_parent_id", unique: true
end

create_table "change_requests", force: :cascade do |t|
Expand Down

0 comments on commit fdd1e7d

Please sign in to comment.