Skip to content

Instantly share code, notes, and snippets.

@spq24
Last active August 29, 2015 14:05
Show Gist options
  • Save spq24/bd87b9d2968e6e6739ad to your computer and use it in GitHub Desktop.
Save spq24/bd87b9d2968e6e6739ad to your computer and use it in GitHub Desktop.
filterrifc-ruby
Lead Model
class Lead < ActiveRecord::Base
filterrific(
default_settings: { sorted_by: 'created_at_asc' },
filter_names: [
:sorted_by,
:with_nonleadaction_id,
:with_reasoninquiry_id
]
)
belongs_to :user
has_one :actiontype
has_one :reasoninquiry
has_one :nonleadaction
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /asc$/) ? 'asc' : 'desc'
order("leads.created_at #{ direction }").where(reviewed: true)
}
scope :with_nonleadaction_id, lambda { |nonleadaction_ids|
where(nonleadaction_id: [*nonleadaction_ids], reviewed: true)
}
scope :with_reasoninquiry_id, lambda { |reasoninquiry_ids|
where(reasoninquiry_id: [*reasoninquiry_ids], reviewed: true)
}
end
Nonleadaction model
class Nonleadaction < ActiveRecord::Base
belongs_to :lead
def self.options_for_select
order('id').map { |e| [e.non_lead_action_type, e.id] }
end
end
reasoninquiry model
class Reasoninquiry < ActiveRecord::Base
belongs_to :lead
def self.options_for_select
order('id').map { |e| [e.reason] }
end
end
Leads Controller
class LeadsController < ApplicationController
def index
@user = current_user
@filterrific = Filterrific.new(Lead, params[:filterrific] || session[:filterrific_leads])
@filterrific.select_options = { sorted_by: Reasoninquiry.options_for_sorted_by, with_reasoninquiry_id: Reasoninquiry.options_for_select, with_nonleadaction_id: Nonleadaction.options_for_select }
@user_leads = @user.leads.filterrific_find(@filterrific).page(params[:page])
session[:filterrific_leads] = @filterrific.to_hash
respond_to do |format|
format.html
format.js
end
rescue ActiveRecord::RecordNotFound
# There is an issue with the persisted param_set. Reset it.
redirect_to(action: :reset_filterrific) and return
end
index.html.erb
<%= form_for @filterrific do |f| %>
<% @filterrific.select_options[:with_reasoninquiry_id].each do |c| %>
<%= h c[0] %>
<%= check_box_tag("filterrific[with_reasoninquiry_id][]",c, true) %>
<% end %>
<% @filterrific.select_options[:with_nonleadaction_id].each do |c| %>
<%= h c[0] %>
<%= check_box_tag("filterrific[with_nonleadaction_id][]",c) %>
<% end %>
<% end %>
_list.html.erb
<table cellpadding="0" cellspacing="0" class="table table-expand table-responsive" id="leads_table">
<thead>
<tr>
<th style="width:15%">Date</th>
<th style="width:35%">Customer Name</th>
<th style="width:15%">Lead Source</th>
<th style="width:15%">lead Type</th>
<th style="width:15%">Status</th>
<th style="width:5%">&nbsp;</th>
</tr>
</thead>
<tbody>
<%= render_filterrific_spinner %>
<% @user_leads.each do |lead| %>
<tr id="edit_<%= lead.id %>">
<td><%= lead.created_at.strftime("%m/%d/%Y") %></td>
<td><%= lead.opportunity_name %></td>
<td><%= lead.message_type.upcase %></td>
<td>
<% if [1, 2, 3].include? lead.reasoninquiry_id %>
<span class="request request-quality">Quality</span>
<% elsif lead.spam? %>
<span class="request request-quality">Spam</span>
<% elsif lead.actiontype_id == 2 %>
<span class="request request-existing">Existing Customer</span>
<% elsif lead.nonleadaction_id == 7 %>
<span class="request request-hangup">Hangup</span>
<% else %>
--
<% end %>
</td>
<td>
<% if lead.status == 'p' %>
<span class="status status-pending">Pending</span>
<% elsif lead.status == 'u' %>
<span class="status status-unreported">Unreported</span>
<% elsif lead.status == 's' %>
<span class="status status-jom-complete">Job Complete</span>
<% elsif lead.status == 'x' %>
<span class="status status-no-business">No Business</span>
<% else %>
<span class="status status-new">** New **</span>
<% end %>
</td>
<td><a class="btn btn-action accordion-toggle" data-parent="#accordion" data-toggle="collapse" data-target="#edit_<%= lead.id %>"></a></td>
</tr>
</tbody>
</table>
index.js.erb
<% js = escape_javascript(render(partial: 'leads/list', locals: { leads: @user_leads })) %>
$("#filterrific_results").html("<%= js %>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment