Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bugfix scope given as AR association

  • Loading branch information...
commit 7a4545c4299f2be87000284226dea4d3e125abf5 1 parent bb3346e
@bogdan authored
View
12 lib/datagrid/drivers/active_record.rb
@@ -12,7 +12,17 @@ def self.match?(scope)
end
def to_scope(scope)
- scope.scoped({})
+ # Model class or Active record association
+ # ActiveRecord association class hides itself under an Array
+ # We can only reveal it by checking if it respond to some specific
+ # to ActiveRecord method like #scoped
+ if scope.is_a?(Class)
+ scope.scoped({})
+ elsif (scope.is_a?(Array) && scope.respond_to?(:scoped))
+ scope.scoped
+ else
+ scope
+ end
end
def where(scope, condition)
View
7 spec/datagrid/drivers/active_record_spec.rb
@@ -9,5 +9,12 @@
it {should be_match(Entry.scoped)}
it {should_not be_match(MongoidEntry)}
end
+
+ it "should convert any scope to AR::Relation" do
+ subject.to_scope(Entry).should be_a(ActiveRecord::Relation)
+ subject.to_scope(Entry.limit(5)).should be_a(ActiveRecord::Relation)
+ subject.to_scope(Entry.limit(5)).should be_a(ActiveRecord::Relation)
+ subject.to_scope(Group.create!.entries).should be_a(ActiveRecord::Relation)
+ end
end
View
1  spec/support/active_record.rb
@@ -32,5 +32,6 @@ class ::Entry < ActiveRecord::Base
belongs_to :group
end
class ::Group < ActiveRecord::Base
+ has_many :entries
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.