Skip to content

Commit

Permalink
Fix node-select display order
Browse files Browse the repository at this point in the history
List nodes sorted by `lft` so they appear in nested sorted order.
  • Loading branch information
tvdeyen committed May 16, 2024
1 parent d6d1971 commit 0c79541
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/api/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
@nodes = Node.all
@nodes = @nodes.includes(:parent)
@nodes = @nodes.where(language_id: params[:language_id]) if params[:language_id]
@nodes = @nodes.ransack(params[:filter]).result
@nodes = @nodes.ransack(params[:filter]).result.order(:lft)

if params[:page]
@nodes = @nodes.page(params[:page]).per(params[:per_page])
Expand Down
18 changes: 11 additions & 7 deletions spec/requests/alchemy/api/nodes_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ module Alchemy
context "with nodes present" do
let!(:node) { create(:alchemy_node, name: "lol") }
let!(:node2) { create(:alchemy_node, name: "yup") }
let!(:node3) { create(:alchemy_node, name: "foo", parent: node) }

let(:result) { JSON.parse(response.body) }

it "returns JSON" do
Expand All @@ -26,19 +28,21 @@ module Alchemy
expect(result).to have_key("data")
end

it "returns all nodes" do
it "returns all nodes ordered by nesting" do
get alchemy.api_nodes_path(params: {format: :json})

expect(result["data"].size).to eq(2)
expect(result["data"].size).to eq(3)
expect(result["data"][0]).to match(hash_including("id" => node.id))
expect(result["data"][1]).to match(hash_including("id" => node3.id))
expect(result["data"][2]).to match(hash_including("id" => node2.id))
end

it "includes meta data" do
get alchemy.api_nodes_path(params: {format: :json})

expect(result["data"].size).to eq(2)
expect(result["data"].size).to eq(3)
expect(result["meta"]["page"]).to eq(1)
expect(result["meta"]["per_page"]).to eq(2)
expect(result["meta"]["total_count"]).to eq(2)
expect(result["meta"]["per_page"]).to eq(3)
expect(result["meta"]["total_count"]).to eq(3)
end

context "with page param given" do
Expand All @@ -52,7 +56,7 @@ module Alchemy
expect(result["data"].size).to eq(1)
expect(result["meta"]["page"]).to eq(2)
expect(result["meta"]["per_page"]).to eq(1)
expect(result["meta"]["total_count"]).to eq(2)
expect(result["meta"]["total_count"]).to eq(3)
end
end

Expand Down

0 comments on commit 0c79541

Please sign in to comment.