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

has_scope gem support #280

Closed
emilenriquez opened this issue Feb 21, 2018 · 15 comments
Closed

has_scope gem support #280

emilenriquez opened this issue Feb 21, 2018 · 15 comments
Assignees
Labels

Comments

@emilenriquez
Copy link

https://github.com/plataformatec/has_scope

this gem simplifies the need to create long hard coded queries that makes the function bloated.
is there any chance we can use this to query the data?

@n-rodriguez
Copy link
Member

n-rodriguez commented Feb 21, 2018

format.json { render json: InvoiceDatatable.new(view_context, { current_org: current_organization }) }
class Foo < AjaxDatatablesRails::Base
  include HasScope

  has_scope :featured, :type => :boolean
  has_scope :by_degree
  has_scope :by_period, :using => [:started_at, :ended_at], :type => :hash

  def get_raw_records
    apply_scopes(current_organization.invoices).all
  end

  private

  def current_organization
    options[:current_org]
  end
end

Don't use :only or :except options or it will crash with Unknow method :action_name

@n-rodriguez n-rodriguez self-assigned this Feb 21, 2018
@emilenriquez
Copy link
Author

emilenriquez commented Feb 21, 2018

what I did was I passed in the query to the the a datatable

format.json { render json: InvoiceDatatable.new(view_context, {current_org: current_organization, query: apply_scopes(current_organization.invoices.order(created_at: :desc)).all}) }

and in the datatable file


  def get_raw_records    
    options[:query]
  end

@n-rodriguez
Copy link
Member

It sounds like an anti pattern. Does the solution I propose work? (I didn't try it, just read the code of has_scope)

@emilenriquez
Copy link
Author

ohh I see. will try your code above

I'm guessing it will loop through the columns and wouldn't be able to read the params defined in the has_scope?

@emilenriquez
Copy link
Author

NoMethodError in InvoicesController#index

undefined method `to_unsafe_h' for nil:NilClass
Extracted source (around line #74):

@n-rodriguez
Copy link
Member

I've updated the example above

@emilenriquez
Copy link
Author

this worked like a charm!

and neat code!!

@n-rodriguez
Copy link
Member

n-rodriguez commented Feb 22, 2018

Ok. I will take a look when I can.

@emilenriquez
Copy link
Author

thanks @n-rodriguez

@n-rodriguez
Copy link
Member

@emilenriquez does it work?

@emilenriquez
Copy link
Author

it did work @n-rodriguez thanks!

@cesar82
Copy link

cesar82 commented Mar 4, 2019

Hi @emilenriquez, I would like to use has_scope with ajax call, so I don't need to reload my page, do you have any idea how can do it?

For example, I would add a checkbox for show/hide inactive users (it is a boolean in the table), if user check or uncheck the checkbox I would like to trigger the call and refresh my table and also when a user clicks in sort arrows in a specific column this value for inactive users can be included in the ajax call.

Thanks for any help here.

@cesar82
Copy link

cesar82 commented Mar 4, 2019

@emilenriquez it looks it should be a good option for including my custom params into the ajax call, but i do not find how to trigger the datatable ajax call when the user clicks the show the inactive users

https://datatables.net/examples/server_side/custom_vars.html

@andreibondarev
Copy link

@cesar82 Have you been able to figure out how to refresh your DataTable without a page reload?

@cesar82
Copy link

cesar82 commented Sep 16, 2019

@andreibondarev yes!

This method DataTable().ajax.reload() reloads the table and take the user to page 1, if you want to keep the current page use this way DataTable().ajax.reload(null, false)

Thanks

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

No branches or pull requests

4 participants