Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/perron/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,20 @@ def pluck(*attributes)
end
end
end

def in_order_of(attribute, values, filter: true)
return Relation.new([]) if values.empty?

indexed = values.each_with_index.to_h

resources = if filter
select { indexed.key?(it.public_send(attribute)) }
.sort_by { indexed[it.public_send(attribute)] }
else
sort_by { indexed[it.public_send(attribute)] || Float::INFINITY }
end

Relation.new(resources)
end
end
end
2 changes: 2 additions & 0 deletions lib/perron/resource/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def count = all.size

def order(attribute, direction = :asc) = all.order(attribute, direction)

def in_order_of(attribute, values, filter: true) = all.in_order_of(attribute, values, filter:)

def first(n = nil)
n ? all.first(n) : all[0]
end
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/content/posts/2023-05-15-sample-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ image: https://example.com/images/sample.jpg
author_id: rails-designer
updated_at: 2023-05-15
processor_class: custom-from-metadata
category: news
---

Labore qui mollit commodo id nulla qui exercitation nulla reprehenderit nulla excepteur aute eu sint. Minim sit aute et ad ut esse aute sunt magna voluptate duis tempor. Commodo commodo qui amet ullamco Lorem ex cillum ea occaecat deserunt elit amet consequat nulla Lorem. Reprehenderit id aute dolor minim aliquip officia et reprehenderit nisi. Aute labore ex veniam enim enim duis sint elit proident aute sunt consectetur ut aliqua. Laboris elit exercitation veniam non commodo magna sunt in sunt id.
Expand Down
1 change: 1 addition & 0 deletions test/dummy/app/content/posts/2023-06-15-another-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: Another Sample Post
description: Describing another sample post
image: https://example.com/images/sample.jpg
author: Kendall
category: tutorial
---

Content goes here…
2 changes: 1 addition & 1 deletion test/dummy/app/models/content/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Content::Post < Perron::Resource
belongs_to :author
belongs_to :editor, class_name: "Content::Data::Editors"

delegate :title, to: :metadata
delegate :title, :category, to: :metadata

scope :ordered, -> { order(:slug) }
scope :limited, -> { limit(2) }
Expand Down
23 changes: 23 additions & 0 deletions test/perron/relation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,27 @@ class Perron::RelationTest < ActiveSupport::TestCase
assert_equal 2, slugs.size
assert_equal "another-post", slugs.first
end

test "#in_order_of filters and sorts by values (filter: true by default)" do
result = @posts.in_order_of(:category, %w[tutorial news])

assert_instance_of Perron::Relation, result
assert_equal 2, result.size
assert_equal "tutorial", result.first.category
assert_equal "news", result.second.category
end

test "#in_order_of sorts all records with filter: false" do
result = @posts.in_order_of(:category, %w[tutorial], filter: false)

assert_equal 4, result.size
assert_equal "tutorial", result.first.category
end

test "#in_order_of returns empty relation for empty values" do
result = @posts.in_order_of(:category, [])

assert_instance_of Perron::Relation, result
assert_equal 0, result.size
end
end
Loading