Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow again overriding filter_records, sort_records... #228

Closed
panmari opened this issue Jun 5, 2017 · 10 comments
Closed

Allow again overriding filter_records, sort_records... #228

panmari opened this issue Jun 5, 2017 · 10 comments
Assignees

Comments

@panmari
Copy link
Contributor

panmari commented Jun 5, 2017

Currently, when overriding for example filter_records in a custom datatable, it's ignored due to 'load_orm_extension' being called on initialize, which overrides the custom method by the one provided in the orm.

def initialize(view, options = {})
  @view    = view
  @options = options
  load_orm_extension
end

According to the rails generator datatable template, such use should be possible:

  # ==== These methods represent the basic operations to perform on records
  # and feel free to override them

  # def filter_records(records)
  # end

  # def sort_records(records)
  # end

  # def paginate_records(records)
  # end

As a workaround I'm currently overriding retrieve_records in my custom datatable

  def retrieve_records
    records = fetch_records
    records = filter_records_with_my_awesome_custom_logic(records)
    records = sort_records(records)     if datatable.orderable?
    records = paginate_records(records) if datatable.paginate?
    records
  end
@natebird
Copy link
Contributor

natebird commented Jul 7, 2017

I ran into the same issue. 👍

Thanks for the workaround.

@aldefouw
Copy link

Thanks for posting this. I just ran into the exact same issue.

@maengkom
Copy link

@panmari thanks, same issue and now solved.

@maengkom
Copy link

@panmari oh this make search column on associated model not working correctly, 2 records of associated model with different content always retrieves and displayed. If I am not override this way, it can search column and filter nested content correctly.

@GUI
Copy link
Contributor

GUI commented Nov 8, 2017

One thing to note about @panmari's workaround is that it doesn't affect the recordsFiltered count returned by the API, since the default records_filtered_count implementation is still calling the original filter_records method (rather than the custom one). While you could also override records_filtered_count in a similar fashion, here's a slightly different approach to this workaround that forces the replacement filter_records method to be re-defined after the ORM's version:

class ExampleDatatable < AjaxDatatablesRails::Base
  private

  def load_orm_extension
    super
    extend CustomOverrides
  end

  module CustomOverrides
    def filter_records(records)
      records = super(records)
      records.where(:other => "stuff")
    end
  end
end

@n-rodriguez n-rodriguez self-assigned this Feb 18, 2018
@n-rodriguez
Copy link
Member

n-rodriguez commented Apr 18, 2018

An other way to fix this would be to make the gem a Rails engine. This way we could hook on Rails to extend the AjaxDatatablesRails::Base class in early loading stage and not on runtime.

@n-rodriguez
Copy link
Member

But you loose the ability of having datatables plugged on Mongoid models.

@n-rodriguez
Copy link
Member

But Mongoid models are not really supported. There is no implementation for this adapter. So what do we do? What do you suggest? What do you prefer?

@n-rodriguez
Copy link
Member

But Mongoid models are not really supported. There is no implementation for this adapter. So what do we do? What do you suggest? What do you prefer?

See #288 (comment) and https://github.com/jbox-web/ajax-datatables-rails/tree/feat/ar_class

@n-rodriguez
Copy link
Member

Hi there! It's now fixed : 719d623

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants