Skip to content

Commit

Permalink
Fix ordering of Hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Oct 20, 2024
1 parent bd96368 commit f766b10
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
18 changes: 12 additions & 6 deletions lib/datagrid/drivers/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ def to_scope(scope)

def where(scope, attribute, value)
scope.select do |object|
object.public_send(attribute) == value
get(object, attribute) == value
end
end

def asc(scope, order)
return scope unless order
return scope if order.empty?
scope.sort_by do |object|
object.public_send(order)
get(object, order)
end
end

Expand All @@ -42,13 +42,13 @@ def reverse_order(scope)

def greater_equal(scope, field, value)
scope.select do |object|
object.public_send(field) >= value
get(object, field) >= value
end
end

def less_equal(scope, field, value)
scope.select do |object|
object.public_send(field) <= value
get(object, field) <= value
end
end

Expand All @@ -58,12 +58,12 @@ def has_column?(scope, column_name)

def is_timestamp?(scope, column_name)
has_column?(scope, column_name) &&
timestamp_class?(scope.first.public_send(column_name).class)
timestamp_class?(get(scope.first, column_name).class)
end

def contains(scope, field, value)
scope.select do |object|
object.public_send(field).to_s.include?(value)
get(object, field).to_s.include?(value)
end
end

Expand All @@ -82,6 +82,12 @@ def default_cache_key(asset)
def can_preload?(scope, association)
false
end

protected

def get(object, property)
object.is_a?(Hash) ? object[property] : object.public_send(property)
end
end
end
end
51 changes: 39 additions & 12 deletions spec/datagrid/drivers/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ class User < Struct.new(:name, :age); end
let(:first) { ArrayGrid::User.new("Vasya", 15) }
let(:second) { ArrayGrid::User.new("Petya", 12) }
let(:third) { ArrayGrid::User.new("Vova", 13) }
let(:_attributes) { {} }

subject do
ArrayGrid.new(
defined?(_attributes) ? _attributes : {}
).scope do
ArrayGrid.new(_attributes).scope do
[ first, second, third ]
end
end
Expand Down Expand Up @@ -92,16 +91,44 @@ class User < Struct.new(:name, :age); end
end

end
describe "when using enumerator scope" do

it "should work fine" do
grid = test_report(to_enum: true) do
scope {[]}
filter(:to_enum, :boolean) do |_, scope|
scope.to_enum
end

describe "when using enumerator scope" do

it "should work fine" do
grid = test_report(to_enum: true) do
scope {[]}
filter(:to_enum, :boolean) do |_, scope|
scope.to_enum
end
grid.assets.should_not be_any
end
grid.assets.should_not be_any
end
end

describe "array of hashes" do
class HashGrid
include Datagrid
scope do
[{name: 'Bogdan', age: 30}, {name: 'Brad', age: 32}]
end

filter(:name)
filter(:age, :integer, :range => true)

column(:name)
column(:age)
end

let(:_attributes) { {} }

subject do
HashGrid.new(_attributes)
end

context "ordered" do
let(:_attributes) { { order: :name, descending: true }}

it { subject.assets.should == [ {name: 'Brad', age: 32}, {name: 'Bogdan', age: 30},] }
end
end
end

0 comments on commit f766b10

Please sign in to comment.