A PROBLEM
General information
source 'https://rubygems.org'
gem 'rails', '4.0.4' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'sass-rails', '~> 4.0.2' # Use SCSS for stylesheets
gem 'coffee-rails', '~> 4.0.0' # Use CoffeeScript for .js.coffee assets and views
gem 'jquery-rails' # Use jquery as the JavaScript library
gem 'jquery-ui-rails' # JQuery UI Library
gem 'jquery-datatables-rails' # Use datatables as default tables in the application
gem 'font-awesome-rails' # Icons
gem 'activerecord-oracle_enhanced-adapter', '~> 1.5.0' # Oracle database adapter
gem 'ruby-oci8' # Ruby interface for Oracle using OCI8 API
gem 'foundation-rails' # zurb foundation
gem 'simple_form' # Simple form builder
gem 'haml-rails' # Use haml processing in views
gem 'kaminari' # Pagination
gem 'turbolinks' # Turbolinks makes following links in your web application faster
gem 'jquery-turbolinks'
gem 'thin'
gem 'thin_service'
/*
*= require font-awesome
*= require_self
*= require foundation_and_overrides
*= require dataTables/jquery.dataTables.foundation
*= require_tree .
*/
//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require foundation
//= require dataTables/jquery.dataTables
//= require dataTables/jquery.dataTables.foundation
//= require turbolinks
//= require_tree .
Models
INDEX page, CONTROLLER, ROUTES, JAVASCRIPT
.row
.medium-12.columns
= form_tag products_index_path, method: 'get' do
.row
.medium-4.columns
%b Product code:
= text_field_tag :product_code, params[:product_code]
.medium-4.columns
%b Product name:
= text_field_tag :product_name, params[:product_name]
.medium-4.columns
.row
.medium-2.columns
= button_tag type: 'submit', style: 'color:#FFFFFF;', class: 'button tiny expand' do
%i.fa.fa-search.fa-lg
.medium-10.columns
.row
.separator
%br
.medium-12.columns
%table#product_table
%thead
%tr
%th{style: 'display: none'} ID
%th Code
%th Name
%th Description...
%th Category
%th
%th
%th
%tbody
class ProductsController < ApplicationController
def index
end
def datatable_ajax
render json: ProductsDatatable.new(view_context)
end
Test::Application.routes.draw do
resources :products
get 'products_ajax/datatable_ajax', to: 'products#datatable_ajax'
end
$ ->
product_table = $('#product_table').DataTable
processing: true
serverSide: true
ajax:
url: '/products_ajax/datatable_ajax'
data: (d) ->
d.product_code = $('#product_code').val()
d.product_name = $('#product_name').val()
return
columns: [
{ width: "0%", className: "dont_show", searchable: false, orderable: false }
{ width: "15%" }
{ width: "35%", className: "row_config" }
{ width: "null", className: "row_config", searchable: false, orderable: false }
{ width: "null", className: "row_config", searchable: false, orderable: false }
{ width: "5%", className: "center", searchable: false, orderable: false }
{ width: "5%", className: "center", searchable: false, orderable: false }
{ width: "5%", className: "center", searchable: false, orderable: false }
]
order: [ [1,'asc'] ]
.row_config {
max-width: 200px;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
.dont_show {
display: none;
}
AJAX PART
class ProductsDatatable
delegate :params, :fa_icon, :link_to, :products_path, :edit_products_path, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
data: data,
recordsTotal: my_search.count,
recordsFiltered: sort_order_filter.count
}
end
private
def data
products = []
display_on_page.map do |record|
product = []
product << record.id
product << record.code
product << record.name
product << record.description
product << (record.category.present? ? record.category.name : '')
product << link_to(fa_icon('info-circle lg'), products_path(record), class: 'label success round')
product << link_to(fa_icon('edit lg'), edit_products_path(record), class: 'label secondary round')
product << link_to(fa_icon('trash-o lg'), products_path(record), method: :delete, data: { confirm: 'Are you sure?' }, class: 'label alert round')
products << product
end
products
end
def my_search
@filtered_products = Products.filter_product_code(params[:product_code]).filter_product_name(params[:product_name]).some_additional_scope.distinct.includes(:category)
end
def sort_order_filter
records = my_search.order("#{sort_column} #{sort_direction}")
if params[:search][:value].present?
records = records.where("PRODUCTS.CODE like :search or lower(PRODUCTS.NAME) like :search", search: "%#{params[:search][:value]}%")
end
records
end
def display_on_page
sort_order_filter.page(page).per(per_page)
end
def page
params[:start].to_i/per_page + 1
end
def per_page
params[:length].to_i > 0 ? params[:length].to_i : 10
end
def sort_column
columns = %w[not_orderable PRODUCTS.CODE lower(PRODUCTS.NAME) not_orderable not_orderable]
columns[params[:order][:'0'][:column].to_i]
end
def sort_direction
params[:order][:'0'][:dir] == "desc" ? "desc" : "asc"
end
end
scope :filter_product_name, -> (product_name) {where("lower(PRODUCTS.NAME) like :search", search: "%#{product_name.downcase}%")}