Created
October 8, 2010 13:48
-
-
Save dwaynemac/616825 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# == Arguments | |
# object: object to evaluate | |
# status: true if updates successfully false if failed | |
def jeditable_result(object, status,options = {}) | |
param = options[:param] || params[:wants] | |
tokens = param.split('.') | |
result = object | |
if status | |
tokens.each do |t| | |
result = result.send(t) | |
end | |
else | |
result = t('jeditable.error_saving') | |
end | |
result | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Render jeditable in place edit field. | |
# TODO: h for safety | |
# TODO: select include_blank | |
# | |
# == Valid Options: | |
# - object: defaults to @object_name | |
# - type: default to text. If 'select' then select_collection is required. | |
# - select_collection: json array of options for select dropdown. | |
# - select_loadurl: url to get json for collection. overrides select_collection | |
# - rows: jeditable rows defaults to 1 | |
# - print: this will be called on object and printed. defaults to field_name. Example: category.name will call object.category.name | |
# - url: URL for request | |
# - style: jeditable style | |
# | |
# Will submit to url_for(object) | |
# While data transfer is in progress CSS class 'editing' is assigned. | |
# | |
# In controller response should be | |
# <tt>format.json { render :json => {:result => jeditable_result(@transaction, true)}}</tt> for success | |
# <tt>format.json { render :json => {:result => jeditable_result(@transaction, false}}</tt> for failure | |
# | |
def jeditable_field(object_name, field_name, options = {}) | |
options.reverse_merge!({:type => 'text', :rows => '1'}) | |
object = options[:object] || self.instance_variable_get("@#{object_name}") | |
if options[:print].nil? | |
field = object.send(field_name) | |
wants = field_name | |
else | |
tokens = options[:print].split('.') | |
field = object | |
tokens.each do |t| | |
field = field.send(t) unless field.nil? | |
end | |
wants = options[:print] | |
end | |
case options[:type] | |
when 'select' | |
submit = %( | |
submit : '#{t('jeditable.submit')}', | |
cancel : '#{t('jeditable.cancel')}', | |
onblur : function() { | |
$(this).removeClass('editing'); | |
}, | |
) | |
when 'text' | |
submit = %( | |
onblur : function() { | |
this.reset(); | |
$(this).removeClass('editing'); | |
}, | |
) | |
end | |
output = content_tag(:span, field, {:id => "jeditable_#{dom_id object}_#{field_name}", :class => 'jeditable'}) | |
output << javascript_tag do %( | |
$(document).ready(function() { | |
$('#jeditable_#{dom_id object}_#{field_name}').editable( | |
function(value, settings) { | |
var result; | |
$.ajax({ | |
type: 'PUT', | |
url: '#{options[:url] || url_for(object)}' , | |
async: false, | |
data: { | |
authenticity_token: #{form_authenticity_token.to_json }, | |
wants: '#{wants}', | |
'#{object_name}[#{field_name}]' : value | |
}, | |
dataType: 'json', | |
success: function(data) { | |
result = data.result; | |
} | |
}); | |
return(result); | |
}, { | |
#{(options[:loadurl].nil?? "data: '#{options[:select_collection]}'," : "loadurl: '#{options[:loadurl]}',") if options[:type]=='select'} | |
type : '#{options[:type]}', | |
#{"rows : '#{options[:rows]}'," if options[:type] == 'text'} | |
tooltip : ' #{t('jeditable.tooltip')}', | |
placeholder: '#{t('jeditable.tooltip')}', | |
style : #{options[:style] || "'display: inline;'"}, | |
#{submit} | |
onedit : function() { | |
$(this).addClass('editing'); | |
} | |
}); | |
}); | |
) | |
end | |
output | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment