Permalink
Browse files

Added jtable.rb initializer to add the from_jtable_query method to Ac…

…tiveRecord::Base, will make this a gem later and make it work correctly.
  • Loading branch information...
1 parent aa140ca commit 623d9d5a31ae8d8bd2d45cfca19f5b56927e6548 @yelvert committed Jan 25, 2011
@@ -2,7 +2,7 @@ class IcdCodesController < ApplicationController
# GET /icd_codes
# GET /icd_codes.xml
def index
- @icd_codes = IcdCode.from_query(params[:jTableQuery], true)
+ @icd_codes = IcdCode.from_jtable_query(params[:jTableQuery], true)
respond_to do |format|
format.html do
@@ -2,7 +2,7 @@ class WidgetsController < ApplicationController
# GET /widgets
# GET /widgets.xml
def index
- @widgets = Widget.from_query(params[:jTableQuery], true)
+ @widgets = Widget.from_jtable_query(params[:jTableQuery], true)
respond_to do |format|
format.html do
@@ -14,7 +14,7 @@ def index
end
def client_side
- @widgets = Widget.from_query(params[:jTableQuery], false)
+ @widgets = Widget.from_jtable_query(params[:jTableQuery], false)
respond_to do |format|
format.html do
View
@@ -1,4 +1,5 @@
class IcdCode < ActiveRecord::Base
+=begin
def self.from_query(query, serverSidePagination)
icd_codes = self
unless query.nil?
@@ -42,4 +43,5 @@ def self.from_query(query, serverSidePagination)
icd_codes
end
end
+=end
end
View
@@ -1,4 +1,5 @@
class Widget < ActiveRecord::Base
+=begin
def self.from_query(query, serverSidePagination)
widgets = self
unless query.nil?
@@ -42,4 +43,5 @@ def self.from_query(query, serverSidePagination)
widgets
end
end
+=end
end
@@ -0,0 +1,47 @@
+module ActiveRecord
+ class Base
+ def self.from_jtable_query(query, serverSidePagination)
+ rel = self
+ unless query.nil?
+ unless query[:search].blank?
+ search_terms = query[:search].split(" ")
+ search_terms.each do |term|
+ where_query = []
+ query[:searchable_columns].each do |column|
+ if rel.arel_table[column.to_sym].column.type == :integer
+ where_query << rel.arel_table[column.to_sym].eq(term.to_i)
+ else
+ where_query << rel.arel_table[column.to_sym].matches("%#{term}%")
+ end
+ end
+ rel = rel.where(where_query.inject(&:or))
+ end
+ end
+ unless query[:column_search].blank?
+ query[:column_search].each_pair do |column,search|
+ search.split(" ").each do |term|
+ rel = rel.where(rel.arel_table[column.to_sym].matches("%#{term}%"))
+ end
+ end
+ end
+ unless query[:sort_column].blank? and query[:sort_direction].blank?
+ rel = rel.order("#{query[:sort_column]} #{query[:sort_direction]}")
+ end
+ if serverSidePagination
+ total_items = rel.count
+ if query[:limit]
+ rel = rel.limit(query[:limit])
+ end
+ if query[:offset]
+ rel = rel.offset(query[:offset])
+ end
+ end
+ end
+ if serverSidePagination
+ {:total_items => total_items, :items => rel}
+ else
+ rel
+ end
+ end
+ end
+end

0 comments on commit 623d9d5

Please sign in to comment.