Skip to content

Commit

Permalink
Bugfix scope given as AR association
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Jan 15, 2013
1 parent bb3346e commit 7a4545c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/datagrid/drivers/active_record.rb
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions spec/datagrid/drivers/active_record_spec.rb
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/support/active_record.rb
Expand Up @@ -32,5 +32,6 @@ class ::Entry < ActiveRecord::Base
belongs_to :group
end
class ::Group < ActiveRecord::Base
has_many :entries
end
end

0 comments on commit 7a4545c

Please sign in to comment.