Skip to content

Instantly share code, notes, and snippets.

@karmi
Created August 31, 2008 15:25
Show Gist options
  • Save karmi/8188 to your computer and use it in GitHub Desktop.
Save karmi/8188 to your computer and use it in GitHub Desktop.
# = Encapsulating Script.aculo.us methods
# Why? Because an In-Place-Editor with Auto-Completer „widget“ is needed
# Editor Sends updates to standard RESTful +update+ method [=> admin_compositions_path(composition) ]
# Autocompleter expects +autocomplete+ method for your resource collection [=> autocomplete_admin_compositions_path]
module Admin::ScriptaculousHelper
# Puts an "in-place-editor" for specified +attribute+ of +model+ into HTML
# See http://github.com/madrobby/scriptaculous/wikis/ajax-inplaceeditor
def inplace_editor_for(object, method, options={})
element_dom_id = "#{dom_id(object)}___#{method}"
string_value = object.send(method.to_sym).blank? ? '—' : object.send(method.to_sym)
url = send("admin_#{object.class.name.underscore}_path".to_sym, object)
authenticity_token_param = protect_against_forgery? ? "authenticity_token=#{form_authenticity_token}" : '' # Beware of tests :)
input_rows = options[:textarea] ? '5' : '1'
in_place_editor=<<HTML
<p id="#{element_dom_id}">#{h(string_value)}</p>
<script type="text/javascript">
editor = new Ajax.InPlaceEditor('#{element_dom_id}',
'#{url}',
{
clickToEditText : 'Klikněte pro editaci',
cancelText : 'zrušit',
okText : 'uložit',
savingText : 'ukládám',
rows : '#{input_rows}',
callback: function(form, value) {
return '#{object.class.name.underscore}[#{method}]=' + encodeURIComponent(value) +
'&value=' + encodeURIComponent(value) +
'&#{authenticity_token_param}'
},
onEnterEditMode: function(form, value) { $(form.element).up('tr').addClassName('edited') },
onLeaveEditMode: function(form, value) { $(form.element).up('tr').removeClassName('edited') },
ajaxOptions: { method:'put' }
}
);
</script>
HTML
end
# Puts an "in-place-editor" enhanced with "autocompleter" for specified +attribute+ of +model+ into HTML
def inplace_editor_with_autocompleter_for(object, method, options={})
in_place_editor = inplace_editor_for(object, method)
authenticity_token_param = protect_against_forgery? ? "authenticity_token=#{form_authenticity_token}" : '' # Beware of tests :)
url = send( "autocomplete_admin_#{object.class.name.pluralize.underscore}_path".to_sym )
autocompleter=<<HTML
<div id="#{object.class.name.underscore}_#{method.to_s.underscore}_autocomplete_choices" class="autocomplete"></div>
<script type="text/javascript">
$('#{object.class.name.underscore}_#{method.to_s.underscore}_autocomplete_choices').hide()
// See http://groups.google.com/group/rubyonrails-spinoffs/browse_thread/thread/8030897c7664b3c8 for credits!
Object.extend(editor, {
_createEditField: editor.createEditField,
createEditField: function() {
this._createEditField();
console.log(this._controls.editor)
new Ajax.Autocompleter(this._controls.editor,
'#{object.class.name.underscore}_#{method.to_s.underscore}_autocomplete_choices',
'#{url}',
{ 'minChars':2,
callback: function(form, value) {
return '#{object.class.name.underscore}[#{method}]=' + encodeURIComponent(value.gsub('value=', '')) +
'&' + value +
'&#{authenticity_token_param}'
},
}
);
}
});
</script>
HTML
in_place_editor << autocompleter
end
end
require File.join(File.dirname(__FILE__), '..', '..', 'test_helper.rb')
require 'action_view/test_case'
# Let's hack a little bit....
module Admin::ScriptaculousHelper
def protect_against_forgery?
false
end
end
class Admin::ScriptaculousHelperTest < ActionView::TestCase
def test_inplace_editor_should_return_properly
assert_nothing_thrown { inplace_editor_for(compositions(:ma_vlast), :title_cs) }
assert inplace_editor_for(compositions(:ma_vlast), :title_cs).include?('/admin/compositions/1')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment