From f766b108f823fe47817dbf21d17f705c44d8d207 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Sun, 20 Oct 2024 13:05:09 +0200 Subject: [PATCH] Fix ordering of Hashes --- lib/datagrid/drivers/array.rb | 18 ++++++---- spec/datagrid/drivers/array_spec.rb | 51 ++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/lib/datagrid/drivers/array.rb b/lib/datagrid/drivers/array.rb index 9ca0fae..99d320c 100644 --- a/lib/datagrid/drivers/array.rb +++ b/lib/datagrid/drivers/array.rb @@ -16,7 +16,7 @@ def to_scope(scope) def where(scope, attribute, value) scope.select do |object| - object.public_send(attribute) == value + get(object, attribute) == value end end @@ -24,7 +24,7 @@ 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 @@ -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 @@ -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 @@ -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 diff --git a/spec/datagrid/drivers/array_spec.rb b/spec/datagrid/drivers/array_spec.rb index c565a43..1a82a27 100644 --- a/spec/datagrid/drivers/array_spec.rb +++ b/spec/datagrid/drivers/array_spec.rb @@ -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 @@ -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