Skip to content
Browse files

error handlers for crud responses

  • Loading branch information...
1 parent 155ae18 commit 2b75963abfd06eb87b68a1f42f6a4235af905fe2 @linojon committed Dec 27, 2009
View
6 lib/gridify/grid.rb
@@ -47,12 +47,16 @@ def update( options )
def column( name, options={} )
name = name.to_s
klass = resource.classify.constantize
+ # TODO: set edit options based on ar_column
+ # TODO: edit
+ # TODO: handle file input types
+ # TODO: custom input types
if col = columns_hash[name]
# update an existing column
col.update options
elsif ar = klass.columns.detect {|c| c.name==name}
- debugger
+ #debugger
# create column from database schema
edit = editable &&
# only edit accessible attributes
View
54 lib/gridify/grid_column.rb
@@ -4,30 +4,32 @@ class GridColumn
attr_accessor :name, # attribute name (required)
:label,
:ar_column, # active_record column
- :value_type, # active_record data type :string, :text, :integer, :float, :boolean, + :currency
+ :value_type, # active_record data type :string, :text, :integer, :float, :boolean, + :currency
:key, # true if this is the key (only :id)
# column formatting
:width, # initial width (150)
:fixed_width, # true = not resized when recalc fit to width
- #:formatter, # see jqGrid doc (based on value_type)
- #:format_options, # hash, see jqGrid doc (based on value_type)
+ #:formatter, # see jqGrid doc (based on value_type)
+ #:format_options, # hash, see jqGrid doc (based on value_type)
:align, # 'left', 'right', 'center' (left for strings, right for numerics)
- #:classes, # optional classes to add to column
+ #:classes, # optional classes to add to column
# column actions
- :resizable, # t/f (true)
+ :resizable, # t/f (true)
:sortable, # t/f (true) or jqGrid sorttype: :integer, :float, :currency, :date, :text (true)
:searchable, # true/false (true) or text or select
#:search_options,
:editable, # true/false (false) or text, textarea, select, checkbox, password, button, image and file (based on value_type)
- #:edit_options,
-
+ :edit_options, # html input field options, depends on editable type (eg {"size" => 10, "maxlength" => 15}) see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules
+ :validations, # jqGrid editrules (TODO derive from AR model validations) see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:common_rules
+ # "required" "integer" "email" "url" etc
+ :form_options, # jqGrid formoptions for formatting input forms
# select types
- #:select_url, # url to dynamically get select options
+ #:select_url, # url to dynamically get select options
# visiblity
- #:always_hidden, # (false)
+ #:always_hidden, # (false)
:hidden # initial hide state (false)
def initialize(options)
@@ -84,9 +86,12 @@ def jqgrid_type
def jqgrid_properties
vals = {
:name => name,
- :index => name,
- :xmlmap => name
+ :index => name
}
+ #xmlmap not required when same as :name
+ # vals[:xmlmap] = name if data_type == :xml
+ # vals[:jsonmap] = name if data_type == :json
+
vals[:label] = label || name.titleize
vals[:resizable] = false if resizable==false
vals[:fixed] = fixed_width unless fixed_width==false
@@ -102,6 +107,8 @@ def jqgrid_properties
end
vals[:hidden] = true if hidden
vals[:width] = width if width
+ vals[:editoptions] = edit_options if editable && edit_options
+ vals[:editrules] = validations if editable && validations
# and more...
vals
@@ -112,28 +119,3 @@ def jqgrid_properties
#<ActiveRecord::ConnectionAdapters::SQLiteColumn:0x2515a98 @sql_type="varchar(255)", @name="title", @precision=nil, @primary=false, @default=nil, @limit=255, @null=true, @type=:string, @scale=nil>
-# grid_col :title, :searchable => false
-# grid_col :id, :hidden => :always, :key => true
-# grid_col :created_at, :formatter => 'date', :format_options => { :source_format => 'UniversalSortableDateTime', new_format: 'FullDateTime' }
-# grid_model attributes
-# grid_columns, searchable, sortable, editable
-
-
-
- # colNames: ['Title', 'Body', 'Created'],
- # colModel: [
- # { name:'title', index:'title', xmlmap: "title", search: true, editable: true },
- # { name:'body', index:'body', xmlmap: "body", search: true, editable: true },
- # { name:'created', index:'created', xmlmap: "created_at",
- # formatter: 'date',
- # formatoptions: { srcformat: 'UniversalSortableDateTime', newformat: 'FullDateTime' }
- # }
- # ],
- #
- # sortname: 'title', // sort column of initial data request
- # sortorder: 'asc', // sort direction of initial data request
- #
- # rowList: [10, 20, 30],
- #
- # });
- # });
View
14 lib/gridify/grid_finder.rb
@@ -181,3 +181,17 @@ def rules_to_conditions
end
end
+
+
+# # If you need to display error messages
+# err = ""
+# if user
+# user.errors.entries.each do |error|
+# err << "<strong>#{error[0]}</strong> : #{error[1]}<br/>"
+# end
+# end
+#
+# render :text => "#{err}"
+
+
+# { :add => true, :edit => true, :inline_edit => false, :delete => true, :edit_url => "/users/post_data", :error_handler => "after_submit" }
View
15 lib/gridify/grid_options.rb
@@ -101,12 +101,15 @@ class Grid
# we dont know to preserve this so you also need to set in options)
:load_once, # true to use local data after first load (false)
+ :error_handler, # javacript: method for crud error handling (default to "after_submit")
+ :error_container, # selector for posting error/flash messages (.errorExplanation)
:z
# ----------------------
# attribute defaults and special value handling
+ # (sure it'd be easier to initialize defaults using a hash but we want nil to mean the jqGrid default - might be true - and not pass a value at all)
def restful
@restful==false ? false : true
@@ -241,6 +244,18 @@ def data_format
}
end
end
+
+ def error_handler
+ @error_handler || 'gridify_action_error_handler'
+ end
+
+ def error_handler_return_value
+ error_handler ? error_handler : 'true;'
+ end
+
+ def error_container
+ @error_container || '.errorExplanation'
+ end
end
end
View
63 lib/gridify/grid_view.rb
@@ -54,17 +54,31 @@ def to_s( options={} )
# get the button options
def edit_button_options
# 'url' => '/notes/{id}', 'mtype' => 'PUT'
- merge_options_defaults( edit_button, 'reloadAfterSubmit' => false, 'closeAfterEdit' => true)
+ # {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'edit');}},
+
+ # note, closeAfterEdit will not close if response returns a non-empty string (even if "success" message)
+ merge_options_defaults( edit_button,
+ 'reloadAfterSubmit' => false,
+ 'closeAfterEdit' => true,
+ 'afterSubmit' => "javascript: function(r,data){return #{error_handler_return_value}(r,data,'edit');}"
+ )
end
def add_button_options
# 'url' => '/notes', 'mtype' => 'POST'
- merge_options_defaults( add_button, 'reloadAfterSubmit' => false, 'closeAfterEdit' => true)
+ merge_options_defaults( add_button,
+ 'reloadAfterSubmit' => false,
+ 'closeAfterEdit' => true,
+ 'afterSubmit' => "javascript: function(r,data){return #{error_handler_return_value}(r,data,'add');}"
+ )
end
def delete_button_options
# 'url' => '/notes/{id}', 'mtype' => 'DELETE'
- merge_options_defaults( delete_button, 'reloadAfterSubmit' => false)
+ merge_options_defaults( delete_button,
+ 'reloadAfterSubmit' => false,
+ 'afterSubmit' => "javascript: function(r,data){return #{error_handler_return_value}(r,data,'delete');}"
+ )
end
def search_button_options
@@ -129,7 +143,7 @@ def jqgrid_properties
#vals['shrinkToFit'] = true #default
vals['forceFit'] = true
#vals['width'] = is ignored
- vals['resizeStop'] = 'javascript: jqgrid_fluid_recalc_width'
+ vals['resizeStop'] = 'javascript: gridify_fluid_recalc_width'
end
vals['sortable'] = true if arranger_type.include?(:sortable)
@@ -219,13 +233,6 @@ def jqgrid_javascript( options={} )
)^
end
- # {edit:#{edit_button},add:#{options[:add]},del:#{options[:delete]},search:false,refresh:true},
- # {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'edit');}},
- # {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'add');}},
- # {afterSubmit:function(r,data){return #{options[:error_handler_return_value]}(r,data,'delete');}}
- # )
-
-
if arranger_type.include?(:hide_show)
s << %Q^ .jqGrid('navButtonAdd','##{pager}',{
caption: "Columns",
@@ -260,6 +267,10 @@ def jqgrid_javascript( options={} )
.jqGrid('filterToolbar')^
end
+ # TODO: built in event handlers, eg
+ # loadError
+ # onSelectRow, onDblClickRow, onRightClickRow etc
+
s << '; '
unless search_toolbar == :visible
@@ -275,10 +286,14 @@ def jqgrid_javascript( options={} )
# ------------------
def js_helpers
# just move this into appliaction.js?
- # allow grid resize on window resize events
+
+ # gridify_fluid_recalc_width: allow grid resize on window resize events
# recalculate width of grid based on parent container; handles multiple grids
# ref: http://www.trirand.com/blog/?page_id=393/feature-request/Resizable%20grid/
- %Q^ function jqgrid_fluid_recalc_width(){
+
+ # afterSubmit: display error message in response
+
+ %Q^ function gridify_fluid_recalc_width(){
if (grids = jQuery('.fluid.ui-jqgrid-btable:visible')) {
grids.each(function(index) {
gridId = jQuery(this).attr('id');
@@ -287,9 +302,29 @@ def js_helpers
});
}
};
- jQuery(window).bind('resize', jqgrid_fluid_recalc_width);
+
+ jQuery(window).bind('resize', gridify_fluid_recalc_width);
+
+ function gridify_action_error_handler(r, data, action){
+ if (r.responseText != '') {
+ return [false, r.responseText];
+ } else {
+ return true;
+ }
+ }
^
end
+
+
+ # if(r.responseText != "") {
+ # $('#{error_container}').html(r.responseText);
+ # $('#{error_container}').slideDown();
+ # //window.setTimeout(function() { // Hide error div after 6 seconds
+ # // $('#{error_container}').slideUp();
+ # //}, 6000);
+ # return false;
+ # }
+ # return true;
# lets options be true or a hash, merges into defaults and returns a hash
def merge_options_defaults( options, defaults={} )
View
BIN spec/db/gridify.sqlite3.db
Binary file not shown.
View
4,716 spec/debug.log
4,716 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
8 spec/grid_column_spec.rb
@@ -59,10 +59,10 @@ def new_grid( options={} )
it "should generate column_model" do
new_grid
- @grid.columns[0].properties.should == {:name => "id", :label => "Id", :index => "id", :xmlmap => "id", :align => "right", :sorttype => "integer", :hidden => true}
- @grid.columns[1].properties.should == {:name => "name", :label => "Name", :index => "name", :xmlmap => "name", :sorttype => "text" }
- @grid.columns[2].properties.should == {:name => "age", :label => "Age", :index => "age", :xmlmap => "age", :align => "right", :sorttype => "integer"}
- @grid.columns[3].properties.should == {:name => "created_at", :label => "Created At", :index => "created_at", :xmlmap => "created_at", :sorttype => "date", :formatter=>"date", :formatoptions=>{:newformat=>"FullDateTime", :srcformat=>"UniversalSortableDateTime"} }
+ @grid.columns[0].properties.should == {:name => "id", :label => "Id", :index => "id", :align => "right", :sorttype => "integer", :hidden => true}
+ @grid.columns[1].properties.should == {:name => "name", :label => "Name", :index => "name", :sorttype => "text" }
+ @grid.columns[2].properties.should == {:name => "age", :label => "Age", :index => "age", :align => "right", :sorttype => "integer"}
+ @grid.columns[3].properties.should == {:name => "created_at", :label => "Created At", :index => "created_at", :sorttype => "date", :formatter=>"date", :formatoptions=>{:newformat=>"FullDateTime", :srcformat=>"UniversalSortableDateTime"} }
end
it "should set current visiblity"
View
21 spec/grid_finder_spec.rb
@@ -153,13 +153,20 @@ def params( p={} )
it "should encode json" do
new_grid :pager => true, :current_page => 1, :data_type => :json
json = @grid.encode_records @records
- json.should == %Q^{"total_records": 2, "page": 1, \
-"chickens": [\
-{"chicken": {"name": "Achick", "created_at": "#{@records[0].created_at.iso8601}", "id": 1, "age": 10}}, \
-{"chicken": {"name": "Bchick", "created_at": "#{@records[1].created_at.iso8601}", "id": 2, "age": 20}}\
-], \
-"total_pages": 1}^
-
+# json.should == %Q^{"total_records": 2, "page": 1, \
+# "chickens": [\
+# {"chicken": {"name": "Achick", "created_at": "#{@records[0].created_at.iso8601}", "id": 1, "age": 10}}, \
+# {"chicken": {"name": "Bchick", "created_at": "#{@records[1].created_at.iso8601}", "id": 2, "age": 20}}\
+# ], \
+# "total_pages": 1}^
+ json.should include('"total_records": 2')
+ json.should include('"page": 1')
+ json.should include('"total_pages": 1')
+ json.should include(
+%Q^"chickens": [\
+{"name": "Achick", "created_at": "#{@records[0].created_at.iso8601}", "id": 1, "age": 10}, \
+{"name": "Bchick", "created_at": "#{@records[1].created_at.iso8601}", "id": 2, "age": 20}\
+]^)
end
end
View
8 spec/grid_view_spec.rb
@@ -26,7 +26,7 @@ def new_grid( options={} )
it "should extract embedded javascript:" do
json = @grid.to_json
- @grid.to_json.should include('"resizeStop": jqgrid_fluid_recalc_width')
+ @grid.to_json.should include('"resizeStop": gridify_fluid_recalc_width')
@grid.to_json.should include('"beforeSelectRow": function(){ false; }')
end
end
@@ -89,11 +89,11 @@ def new_grid( options={} )
@grid.to_json.should include('"autowidth": true')
@grid.to_json.should include('"forceFit": true')
# recalc width after resizing columns
- @grid.to_json.should include('"resizeStop": jqgrid_fluid_recalc_width')
- # tag the grid as fluid for the jqgrid_fluid_recalc_width function
+ @grid.to_json.should include('"resizeStop": gridify_fluid_recalc_width')
+ # tag the grid as fluid for the gridify_fluid_recalc_width function
@grid.to_javascript.should include('.addClass("fluid")' )
# include the recalc function
- @grid.to_javascript.should include('function jqgrid_fluid_recalc_width(){')
+ @grid.to_javascript.should include('function gridify_fluid_recalc_width(){')
end
it "should set :width in pixels" do

0 comments on commit 2b75963

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