jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 124 forks source link

Sort_by filter not working #105

Open catmal opened 8 years ago

catmal commented 8 years ago

Hi, I am new to rails and filterrific. I started by setting only 1 filter sort_by and I can't get it to work. In view it does apply the default. And if I change the default the sorting changes when I refresh the page. But if I choose an option from sort dropdown it shows it's loading but then nothing change.

I noticed that if I change the default value only for example from name_asc to price_asc it throws an error Invalid sort options. But if I change both default and scope values to price

Note that I'm building the app on top of a database with capital column names.

rails (5.0.0.1) actioncable (= 5.0.0.1) actionmailer (= 5.0.0.1) actionpack (= 5.0.0.1) actionview (= 5.0.0.1) activejob (= 5.0.0.1) activemodel (= 5.0.0.1) activerecord (= 5.0.0.1) activesupport (= 5.0.0.1) bundler (>= 1.3.0, < 2.0) railties (= 5.0.0.1) sprockets-rails (>= 2.0.0)

filterrific (2.0.5) rails (>= 3.1.0)

ruby 2.3.1p112

Model Products

class Product < ActiveRecord::Base
filterrific(
    default_filter_params: { sorted_by: 'name_asc' },
    available_filters: [
    :sorted_by  
    ]
  )

belongs_to :category, :foreign_key => 'category'
belongs_to :extra1_detail, :foreign_key => 'extra1_id'
belongs_to :taxcategory, :foreign_key => 'taxcat'

scope :sorted_by, lambda { |sort_options|
direction = (sort_options =~ /desc$/) ? 'desc' : 'asc'

    case sort_options.to_s
    when /^name_/
      order("products.name #{ direction }")

    else
      raise(ArgumentError, "Invalid sort option")
    end
  }
  def self.options_for_sorted_by
    [
      ['Name (AZ)', 'name_asc'],
      ['Name (ZA)', 'name_desc']
     ]
  end
end

Controller

class ProductsController < ApplicationController

 def index

    @filterrific = initialize_filterrific(
      Product,
      params[:filterrific],
      select_options: {
      sorted_by: Product.options_for_sorted_by,

      },
      persistence_id: false,
      default_filter_params: {},
      available_filters: [],
    ) or return
    @products = @filterrific.find.page(params[:page])

    # Respond to html for initial page load and to js for AJAX filter updates.
    respond_to do |format|
      format.html
      format.js
    end

  rescue ActiveRecord::RecordNotFound => e
    # There is an issue with the persisted param_set. Reset it.
    puts "Had to reset filterrific params: #{ e.message }"
    redirect_to(reset_filterrific_url(format: :html)) and return
  end

<div id="filterrific_results">

View List

<table class="table" summary="Product list">
    <tr class="header">

      <th>Code</th>
      <th>Barcode</th>
      <th>Name</th>
      <th>Color</th>
      <th>Size</th>
      <th>Pricebuy</th>
      <th>Pricesell</th>
      <th>Tax Category</th>
      <th>Category</th>

    </tr>
    <% @products.eager_load(:category, :extra1_detail, :taxcategory).each do |product| %>

      <tr>
      <td><%= product.REFERENCE %></td>
      <td><%= product.CODE %></td>
      <td><%= product.NAME %></td>
      <td><%= product.extra1_detail %></td>
      <td><%= product.EXTRA2_ID  %></td>
      <td><%= product.PRICEBUY %></td>
      <td><%= product.PRICESELL %></td>
      <td><%= product.taxcategory.NAME %></td>
      <td><%= product.category.NAME %></td>

      <td class="actions">

        <%= button_to("Edit", {:action => 'edit', :id => product.id}, :class => 'action edit') %>
        <%= button_to("Delete", {:action => 'delete', :id => product.id}, :class => 'action delete') %>
      </td>
    </tr>
    <% end %>
  </table>

</div>

<%= will_paginate products %>

View index

<div class="products index">
  <h2>Products</h2>
<%= form_for_filterrific @filterrific do |f| %>
<%= link_to("Add New Product", {:action => 'new'}, :class => 'action new') %>

<div><%= pluralize(@products.size, 'product') %> found</div>

    Sorted by
    <%= f.select(:sorted_by, @filterrific.select_options[:sorted_by]) %>
  </div>

  <div>
    <%= link_to(
      'Reset filters',
      reset_filterrific_url,
    ) %>
  </div>

  <%= render_filterrific_spinner %>
<% end %>

<%= render(
  partial: 'products/list',
  locals: { products: @products }
) %>

</div>

What could be the issue?

dom-dom-dom commented 7 years ago

Try removing available_filters: [] line from your controller. It's overriding your model.