bogdan/datagrid — Issue #21: class.scope prevents controller from assigning scope
Author: avit
Created: 2012-02-28 23:48:48+00:00
State: open

In our usage we need to scope a lot of things by the `current_user`, which means passing an argument into the Datagrid model from the controller.

First attempt:

```
class ThingReport
include Datagrid

attr_accessor :current_user

scope do
@current_user.things
end
end

# Controller:
@things = ThingReport.new(params[:things])
@things.current_user = current_user
```

Unfortunately the scope is evaluated in the context of the class, not the instance.

Our current solution:

```
class ThingReport
include Datagrid

attr_accessor :current_user

scope do
# Just a dummy scope to satisfy the class & load the driver.
# We just override our own scope instance method.
Thing
end

def scope
@current_user.things
end

# assets needs to be redefined here because `Association#scoped` and `CollectionProxy#scoped`
# take 0 arguments. Only ClassMethods#scoped takes options, which driver#to_scope breaks.
def assets
scope.scoped
end
end
```

I'm wondering if there are any better solutions for this or any thoughts on adding an allowance to Datagrid for handling more dynamic scopes.