Skip to content

[RFC] Remove dependency on view_context #288

Closed
@n-rodriguez

Description

Hi there!

In order to have a cleaner API for ajax-datatables-rails and in a long term view for this gem, I'd like to remove the view_context dependency by directly injecting the params hash.

It would have some advantages :

  • start to reduce coupling with Rails (there's still room for improvements but it would be a start)
  • ajax-datatables-rails would be compatible with Rails API which doesn't have view_context (Using in API only mode? #240)
  • it would make tests easier for users (you have only one hash to pass to build and test a datatable, don't need Capybara tests anymore)
  • the view_context is a quite big object to pass around, passing a hash would be lighter
  • it encourages developers to have a clean object design

But it also have some drawbacks :

  • it changes the API :
# before
  respond_to do |format|
    format.json { render json: UserDatatable.new(view_context) }
  end

# after
  respond_to do |format|
    format.json { render json: UserDatatable.new(params) }
  end

If you really want to inject the view_context as before you can still use the options hash :

# Controller
  respond_to do |format|
    format.json { render json: UserDatatable.new(params, view_context: view_context) }
  end

# Datatable
class ApplicationDatatable < AjaxDatatablesRails::Base
  extend Forwardable
  attr_reader :view
  def initialize(params, opts = {})
    @view = opts[:view_context]
    super
  end
end

class MyCustomDatatable < ApplicationDatatable
end

I'm testing this idea on a real world application (45 datatables) with this branch : https://github.com/jbox-web/ajax-datatables-rails/tree/feat/rails-api and so far it works very well :) (I made the migration on Draper before)

Tests are passing : https://travis-ci.org/jbox-web/ajax-datatables-rails/builds/375203557 🎉

The idea was a lot inspired by Trailblazer which I use in my app 👍

What do you think? It would be for a 0.5 release.

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions