Skip to content
Browse files

Refactor generated controller code for #index and #post_data into jqg…

…rid_index_for and jqgrid_post_data_for methods and include them in ActionController::Base
  • Loading branch information...
1 parent 9bfb564 commit 2e0d7788fc9039aa248e11c8ea50fcc466d97ee1 @jsy committed May 5, 2011
Showing with 91 additions and 46 deletions.
  1. +12 −1 README.rdoc
  2. +1 −0 lib/2dc_jqgrid.rb
  3. +3 −45 lib/generators/jqgrid/templates/controller.rb
  4. +75 −0 lib/jqgrid/controller_extensions.rb
View
13 README.rdoc
@@ -15,6 +15,8 @@ Following features are supported :
- And more ...
= Install and Uninstall
+
+ $ rails plugin install git://github.com/jsy/jqgrid-rails3
$ rake jqgrid:install
@@ -47,4 +49,13 @@ You can use it simple,eg;
Copyright (c) 2010 Anthony Heukmes, released under the MIT license
-My gmail account : doinsist@gmail.com
+My gmail account : doinsist@gmail.com
+
+= JsY Changes
+
+ o Add controller methods to ActionController::Base:
+
+ jqgrid_post_data_for
+ jqgrid_index_for
+
+Released under the MIT license.
View
1 lib/2dc_jqgrid.rb
@@ -484,6 +484,7 @@ def filter_by_conditions(columns)
end
end
+require 'jqgrid/controller_extensions'
=begin
View
48 lib/generators/jqgrid/templates/controller.rb
@@ -14,53 +14,11 @@ class <%= class_name.pluralize %>Controller < ApplicationController
GRID_COLUMNS = [<%= columns.map {|x| ":#{x}"}.join(', ') %>]
def post_data
- error_message = ""
- payment_params = {}
- GRID_COLUMNS.each {|c| <%= model_name %>_params[c] = params[c] if params[c]}
- case params[:oper]
- when 'add'
- if params["id"] == "_empty"
- <%= model_name %> = <%= camel %>.create(<%= model_name %>_params)
- end
- when 'edit'
- <%= model_name %> = <%= camel %>.find(params[:id])
- <%= model_name %>.update_attributes(<%= model_name %>_params)
- record_data = {}
- GRID_COLUMNS.each {|c| record_data[c] = <%= model_name %>.send(c)}
- when 'del'
- <%= camel %>.destroy_all(:id => params[:id].split(","))
- else
- error_message = 'unknown action'
- end
-
- if <%= model_name %> && <%= model_name %>.errors.empty?
- render :json => [false, error_message, record_data]
- else
- <%= model_name %>.errors.entries.each do |error|
- message << "<strong>#{<%= camel %>.human_attribute_name(error[0])}</strong> : #{error[1]}<br/>"
- end
- render :json =>[true, error_message, record_data]
- end
+ jqgrid_post_data_for <%= model_name %>, params, GRID_COLUMNS
end
-
def index
- current_page = params[:page] ? params[:page].to_i : 1
- rows_per_page = params[:rows] ? params[:rows].to_i : 10
-
- conditions={:page => current_page, :per_page => rows_per_page}
- conditions[:order] = params["sidx"] + " " + params["sord"] unless (params[:sidx].blank? || params[:sord].blank?)
-
- if params[:_search] == "true"
- conditions[:conditions]=filter_by_conditions(GRID_COLUMNS)
- end
-
- @<%= plural_name %>=<%= camel %>.paginate(conditions)
- total_entries=@<%= plural_name %>.total_entries
-
- respond_with(@<%= plural_name %>) do |format|
- format.json { render :json => @<%= plural_name %>.to_jqgrid_json(GRID_COLUMNS, current_page, rows_per_page, total_entries)}
- end
+ jqgrid_index_for <%= model_name %>, params, GRID_COLUMNS
end
-end
+end
View
75 lib/jqgrid/controller_extensions.rb
@@ -0,0 +1,75 @@
+module JQGrid
+ module ControllerExtensions
+ module InstanceMethods
+
+ # call from Controller#index action
+ #
+ # options
+ # :rows_per_page ==> Integer ==> defaults to 20, number of rows to dispaly in single page
+ # :current_page ==> Integer ==> Defaults to 1. The page to display
+ def jqgrid_index_for model, params, columns, options={}
+ current_page = params[:page] ? params[:page].to_i : options.fetch(:current_page, 1)
+ rows_per_page = params[:rows] ? params[:rows].to_i : options.fetch(:rows_per_page, 20)
+ conditions = {:page=>current_page, :per_page=>rows_per_page}
+ conditions[:order] = params["sidx"] + " " + params["sord"] unless (params[:sidx].blank? || params[:sord].blank?)
+
+ if params[:_search] == "true"
+ conditions[:conditions] = filter_by_conditions(columns) # TODO: this needs to support .where look
+ end
+
+ records = model.paginate(conditions)
+ total_entries= records.total_entries
+
+ respond_with(records) do |format|
+ format.json { render :json => records.to_jqgrid_json(columns, current_page, rows_per_page, total_entries)}
+ end
+ end
+
+ # call from Controller#post_data action
+ #
+ # options
+ # :add_ok ==> Boolean ==> default to false. Set to true to allow user to add records
+ # :delete_ok ==> Boolean ==> default to false. Set to true to allow user to delete records
+ def jqgrid_post_data_for(model, params, columns, options={})
+ add_ok = options.fetch(:add_ok, false)
+ delete_ok = options.fetch(:delete_ok, false)
+
+ error_message = ""
+ model_attributes = {}
+ columns.each {|c| model_attributes[c] = params[c] if params[c]}
+
+ object = nil
+
+ case params[:oper]
+ when 'add'
+ object = model.create(model_attributes) if params["id"] == "_empty" && add_ok
+ when 'edit'
+ object = model.find(params[:id])
+ object.update_attributes(model_attributes)
+ record_data = {}
+ columns.each {|c| record_data[c] = object.send(c)}
+ when 'del'
+ model.destroy_all(:id => params[:id].split(",")) if delete_ok
+ else
+ error_message = 'unknown action'
+ end
+
+ if not object.nil? && object.errors.empty?
+ render :json => [false, error_message, record_data]
+ else
+ object.errors.entries.each do |error|
+ message << "<strong>#{model.human_attribute_name(error[0])}</strong> : #{error[1]}<br/>"
+ end
+ render :json =>[true, error_message, record_data]
+ end
+ end
+ end
+ end
+end
+
+# Add jqgrid_*_for methods to ActionController::Base
+class ActionController::Base
+ include JQGrid::ControllerExtensions::InstanceMethods
+end
+
+

0 comments on commit 2e0d778

Please sign in to comment.
Something went wrong with that request. Please try again.