Skip to content
Jan Berdajs edited this page Jan 13, 2015 · 5 revisions

You can create an actual Datasource::Base subclass instead of using datasource_module. This makes sense if your model is already big or you have a lot of loaders. If you don't create the subclass yourself, it will actually be generated when datasource is first used, so there is no difference in performance or complexity. Because Rails autoloads all folders in app/, you can simply put them into app/datasources.

The datasource subclass will be found by name:

# app/datasources/user_datasource.rb
class UserDatasource < Datasource::From(User)
  loader :my # ...
end
class User < ActiveRecord::Base
end

Or you can specify it explicitly:

class User < ActiveRecord::Base
  def self.default_datasource
    UserDatasource
  end
end

You could also have multiple datasources for the same model, and specify which one to use with the scope:

render json: User.with_datasource(UserDatasource)
Clone this wiki locally