Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Include a registry/structure setup to make grid creation more concise…

… and flexible
  • Loading branch information...
commit b14231c714b7aeef63ee7d4978a4a26c9827a520 1 parent dbe98fd
@ckuttruff ckuttruff authored
View
1  README.rdoc
@@ -47,6 +47,7 @@ jqgrid_rails was originally created by {Nick Ewing}[http://github.com/nickewing]
* {Nick Ewing}[http://github.com/nickewing]
* {David Cato}[http://github.com/davec]
+* {Christopher Kuttruff}[http://github.com/ckuttruff]
* {Ryan Golec}[http://github.com/ryangolec]
== Resources
View
58 examples/usage.rdoc
@@ -119,3 +119,61 @@ The first parameter is what we are actually querying against. For simple tables
end
end
end
+
+=== Registry/Structure
+
+As a convenient structure to contain and generate grids with sensible (and easily customizable) default options, JqGridRails provides registry and structure modules that aid in storage grid creation.
+
+Within the model that you want to associate the registry, provide the following...
+
+ # app/models/grid_model.rb
+
+ class GridModel < ActiveRecord::Base
+ include JqGridRails::JqGridStructureRegistry
+ self.register_grid(:index_grid)
+ end
+
+This will create a slot for a JqGridStructure object that can be retrieved later with GridModel.get_grid(:index_grid)
+
+ # app/controllers/grids_controller.rb
+ class GridsController < ApplicationController
+
+ def index
+ structure = GridModel.get_grid(:index_grid)
+
+ # columns
+ structure.add_column('Label', 'unique_field_id', { :columns => { :hidden => true }, :response => {} })
+ structure.add_scope(GridModel.scoped)
+
+ respond_to do |format|
+ @grid = gs.create_grid( { :sortname => "unique_field_id", :sortorder => "asc" } )
+
+ format.html
+ format.json { render :json => grid_response(structure.scope, params, structure.response) }
+ end
+ end
+
+JqGridStructure#add_column is very similar to JqGrid#add_column... the :columns key corresponds to the third argument of JqGrid#add_column and accepts all the same options. The :response key is used to pass on options to the grid_response (see "Populating the grid" above).
+
+These options along with the #add_scope provide all the necessary information to create the grid. The #create_grid method accepts an optional argument (see "Options Hash" above) that is merged into a hash of reasonable default options.
+
+The advantage of this registry structure, besides the convenience that it provides, is that it gives greater flexibility regarding where grids can be constructed. A good use case for this would involve adding columns in a model extension.
+
+ # lib/extensions/models/grid_model_extension.rb
+ # (this would be from a rails application using the above model, etc as an engine)
+ require_dependency 'grid_model'
+
+ GridModel.class_eval do
+
+ has_one :foo_join_table, :dependent => :destroy
+ has_one :foo, :through => :foo_join_table
+
+ # Add nifty_association to grid_model index grid
+
+ grid = GridModel.get_grid(:index_grid)
+ grid.add_scope(GridModel.include(:foo).
+ select("foos.name AS foo_name"))
+ gridd.add_column('Foo', 'foo_name', { :response => { :where => 'foos.name',
+ :order => 'foo_name' } } )
+
+ end
View
28 lib/jqgrid_rails/jqgrid_rails_registry.rb
@@ -0,0 +1,28 @@
+require 'jqgrid_rails/jqgrid_rails_structure'
+
+module JqGridRails
+ # JqGridStructureRegistry is a module that, once included, adds some class methods to enable
+ # registering and fetching of grid structures by the grid's name
+ # see examples/usage.rb file for more details
+ module JqGridStructureRegistry
+
+ def self.included(base)
+ base.extend(ClassMethods)
+ end
+
+ module ClassMethods
+ # Creates a new GridStructure object and stores it in a slot with grid_name as key
+ # grid_name:: grid name symbol (first arg of JqGrid.new(...))
+ def register_grid(grid_name)
+ @grids ||= {}
+ @grids[grid_name.to_sym] = JqGridRails::JqGridStructure.new(self, grid_name)
+ end
+
+ # Fetches grid from grid_name symbol
+ def get_grid(grid_name)
+ @grids[grid_name.to_sym]
+ end
+ end
+
+ end
+end
View
91 lib/jqgrid_rails/jqgrid_rails_structure.rb
@@ -0,0 +1,91 @@
+module JqGridRails
+ # GridStructure acts as a container for the various jqgrid_rails elements. It is used to store
+ # the grid's scopes, column data, and response options. These elements can then be used to
+ # generate the ultimate, grid, response and scope meant to be used in the controller
+ class JqGridStructure
+ # base_class:: name of model containing GridStructure (used to generate some grid defaults)
+ # grid_name:: used to identify grid (convention is action_name_grid; eg: index_grid)
+ def initialize(base_class, grid_name)
+ @grid_name = grid_name
+ @base_class = base_class
+ @columns = []
+ @scopes = []
+ @response = {}
+ end
+
+ # Get scope (1st arg of grid_response) by merging @scopes together (first added is the base)
+ def scope
+ scope = @scopes.first
+ last_index = @scopes.length - 1
+ 1.upto(last_index) { |i| scope = scope.merge(@scopes[i]) }
+ scope
+ end
+
+ # Returns response for grid (last argument in json grid_response call)
+ def response
+ @response
+ end
+
+ # Adds column info for later grid creation; arguments accepted are similar to Grid#add_column
+ # Only adds column if the unique key (field_name) does not exist in the response hash
+ # label:: label for grid column
+ # field_name:: used during the grid response
+ # opts:: hash containing :columns and :response keys to specify these options for column
+ def add_column(label, field_name, opts = {})
+ unless(@response.has_key?(field_name))
+ @columns << { :label => label, :field_name => field_name, :column_opts => (opts[:columns] || {}) }
+ @response[field_name] = opts[:response]
+ end
+ end
+
+ # Remove grid element from columns and response based on field_name
+ # field_name:: element to delete from columns and response
+ def remove_column(field_name)
+ @columns.delete_if { |col_hash| col_hash[:field_name] == field_name }
+ @response.delete(field_name)
+ end
+
+ # Adds scope to be used in grid response
+ # scope:: scope to be merged with the rest in the array (array acts as a queue)
+ # location:: optional location of scope (:first or :last (:last is default))
+ def add_scope(scope, location = :last)
+ insert_location = (location.to_sym == :first)? 0 : -1
+ @scopes.insert(insert_location, scope)
+ end
+
+ # Generates grid that will be used in controller
+ # custom_grid_options:: hash that will be added to default options for grid
+ def create_grid(custom_grid_options = {})
+ grid = JqGridRails::JqGrid.new(@grid_name, default_options(custom_grid_options))
+ @columns.each { |c| grid.add_column(c[:label], c[:field_name], c[:column_opts]) }
+ grid
+ end
+
+ # Main grid options (last arg or Grid#new); contains reasonable defaults that may be overriden
+ # options:: custom options to override any defaults or to add other options not specified
+ def default_options(options = {})
+ tbl = @base_class.table_name
+ { :caption => "#{@base_class.to_s.pluralize} List",
+ :url => "/#{tbl}/",
+ :ondbl_click_row => { :url => "#{tbl.singularize}_path".to_sym},
+ :link_toolbar => true,
+ :filter_toolbar => true,
+
+ :sortname => "#{tbl}.id",
+ :sortorder => "desc",
+
+ :height => '100%',
+ :width => '100%',
+ :autowidth => true,
+ :ignore_case => true,
+
+ :row_id => :id,
+ :row_num => 50,
+ :pager => true,
+ :multiselect => true }.merge(options)
+ end
+
+ end
+end
+
+
View
2  lib/jqgrid_rails/version.rb
@@ -13,5 +13,5 @@ def to_s
end
end
- VERSION = Version.new('1.2.3')
+ VERSION = Version.new('1.2.4')
end
Please sign in to comment.
Something went wrong with that request. Please try again.