Skip to content

Commit

Permalink
Treat ActiveRecord::Result as Array driver
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Aug 20, 2024
1 parent 342b1a1 commit fc7d31d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Treat true/false as YES/NO when assigned as strings for xboolean filter
* Support infinite ranges for date, datetime and integer filters
* Treat `ActiveRecord::Result` class as `Array` driver

## 1.8.1

Expand Down
5 changes: 4 additions & 1 deletion lib/datagrid/drivers/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ module Drivers
class Array < AbstractDriver

def self.match?(scope)
!Datagrid::Drivers::ActiveRecord.match?(scope) && (scope.is_a?(::Array) || scope.is_a?(Enumerator))
!Datagrid::Drivers::ActiveRecord.match?(scope) && (
scope.is_a?(::Array) || scope.is_a?(Enumerator) ||
(defined?(::ActiveRecord::Result) && scope.is_a?(::ActiveRecord::Result))
)
end

def to_scope(scope)
Expand Down
17 changes: 9 additions & 8 deletions spec/datagrid/drivers/array_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
subject { described_class }

it {should be_match(Array.new)}
it {should be_match(ActiveRecord::Result.new([], []))}
it {should_not be_match({})}
end

describe "api" do

class ArrayGrid
class User < Struct.new(:name, :age); end
include Datagrid
Expand All @@ -36,8 +37,8 @@ class User < Struct.new(:name, :age); end
[ first, second, third ]
end
end


describe '#assets' do
subject { super().assets }
describe '#size' do
Expand All @@ -55,13 +56,13 @@ class User < Struct.new(:name, :age); end
subject { super().header }
it {should ==[ "Name", "Age"]}
end

describe '#data' do
subject { super().data }
it {should == [[ "Name", "Age"], ["Vasya", 15], ["Petya", 12], ["Vova", 13]]}
end


describe "when some filters specified" do
let(:_attributes) { {:age => [12,14]} }

Expand All @@ -80,7 +81,7 @@ class User < Struct.new(:name, :age); end
it {should include(third)}
end
end

describe "when reverse ordering is specified" do
let(:_attributes) { {:order => :name, :descending => true} }

Expand Down

0 comments on commit fc7d31d

Please sign in to comment.