Skip to content

Commit

Permalink
Add documentation for either_order/3
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeKaraszi committed May 28, 2018
1 parent 8e9a363 commit fe9d6c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,23 @@ User.either_joins(:profile_l, :profile_r) #=> [alice, bob]

#### Either Order

The `#either_order/3` method is a base ActiveRecord querying method that will order a set of columns that may or may not exist for each record.
This works similar to how [Either Join](#either-join) works. This does not however exclude records that do not have relationships.

```ruby
alice = User.create!
bob = User.create!
ProfileL.create!(user_id: alice.id, left_turns: 100)
ProfileR.create!(user_id: bob.id, right_turns: 50)

User.either_order(:asc, profile_l: :left_turns, profile_r: :right_turns) #=> [bob, alice]
User.either_order(:desc, profile_l: :left_turns, profile_r: :right_turns) #=> [alice, bob]

randy = User.create!
User.either_order(:asc, profile_l: :left_turns, profile_r: :right_turns) #=> [bob, alice, randy]
User.either_order(:desc, profile_l: :left_turns, profile_r: :right_turns) #=> [randy, alice, bob]
```

## Installation

Add this line to your application's Gemfile:
Expand Down
16 changes: 12 additions & 4 deletions spec/query_methods/either_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@
describe ".either_order/2" do
it "Should not exclude anyone who does not have a relationship" do
query = Person.either_order(:asc, profile_l: :likes, profile_r: :dislikes)
expect(query).to include(one, two, three)
expect(query.count).to eq(3)
expect(query[0]).to eq(two)
expect(query[1]).to eq(one)
expect(query[2]).to eq(three)
end

it "Should order people based on their likes and dislikes in ascended order" do
query = Person.either_order(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
expect(query).to match_array([two, one])
expect(query.count).to eq(2)
expect(query.first).to eq(two)
expect(query.last).to eq(one)
end

it "Should order people based on their likes and dislikes in descending order" do
query = Person.either_order(:desc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
expect(query).to match_array([one, two])
expect(query.first).to eq(one)
expect(query.last).to eq(two)
end

context "Alias .either_order/2" do
it "Should order people based on their likes and dislikes in ascended order" do
query = Person.either_orders(:asc, profile_l: :likes, profile_r: :dislikes).where(id: [one.id, two.id])
expect(query).to match_array([two, one])
expect(query.count).to eq(2)
expect(query.first).to eq(two)
expect(query.last).to eq(one)
end
end
end
Expand Down

0 comments on commit fe9d6c5

Please sign in to comment.