Skip to content

Commit

Permalink
error handlers for crud responses
Browse files Browse the repository at this point in the history
  • Loading branch information
linojon committed Dec 27, 2009
1 parent 155ae18 commit 2b75963
Show file tree
Hide file tree
Showing 10 changed files with 4,839 additions and 66 deletions.
6 changes: 5 additions & 1 deletion lib/gridify/grid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 18 additions & 36 deletions lib/gridify/grid_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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],
#
# });
# });
14 changes: 14 additions & 0 deletions lib/gridify/grid_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
15 changes: 15 additions & 0 deletions lib/gridify/grid_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
63 changes: 49 additions & 14 deletions lib/gridify/grid_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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');
Expand All @@ -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={} )
Expand Down
Binary file modified spec/db/gridify.sqlite3.db
Binary file not shown.
Loading

0 comments on commit 2b75963

Please sign in to comment.