Description:
[web] frozen in_place_editing
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@135 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r61:1417cdf16f4c - - 6 files changed: 203 inserted, 0 deleted
@@ -0,0 +1,14 | |||||
|
|
1 | + InPlaceEditing | ||
|
|
2 | + ============== | ||
|
|
3 | + | ||
|
|
4 | + Example: | ||
|
|
5 | + | ||
|
|
6 | + # Controller | ||
|
|
7 | + class BlogController < ApplicationController | ||
|
|
8 | + in_place_edit_for :post, :title | ||
|
|
9 | + end | ||
|
|
10 | + | ||
|
|
11 | + # View | ||
|
|
12 | + <%= in_place_editor_field :post, 'title' %> | ||
|
|
13 | + | ||
|
|
14 | + Copyright (c) 2007 David Heinemeier Hansson, released under the MIT license No newline at end of file |
@@ -0,0 +1,22 | |||||
|
|
1 | + require 'rake' | ||
|
|
2 | + require 'rake/testtask' | ||
|
|
3 | + require 'rake/rdoctask' | ||
|
|
4 | + | ||
|
|
5 | + desc 'Default: run unit tests.' | ||
|
|
6 | + task :default => :test | ||
|
|
7 | + | ||
|
|
8 | + desc 'Test in_place_editing plugin.' | ||
|
|
9 | + Rake::TestTask.new(:test) do |t| | ||
|
|
10 | + t.libs << 'lib' | ||
|
|
11 | + t.pattern = 'test/**/*_test.rb' | ||
|
|
12 | + t.verbose = true | ||
|
|
13 | + end | ||
|
|
14 | + | ||
|
|
15 | + desc 'Generate documentation for in_place_editing plugin.' | ||
|
|
16 | + Rake::RDocTask.new(:rdoc) do |rdoc| | ||
|
|
17 | + rdoc.rdoc_dir = 'rdoc' | ||
|
|
18 | + rdoc.title = 'InPlaceEditing' | ||
|
|
19 | + rdoc.options << '--line-numbers' << '--inline-source' | ||
|
|
20 | + rdoc.rdoc_files.include('README') | ||
|
|
21 | + rdoc.rdoc_files.include('lib/**/*.rb') | ||
|
|
22 | + end |
@@ -0,0 +1,2 | |||||
|
|
1 | + ActionController::Base.send :include, InPlaceEditing | ||
|
|
2 | + ActionController::Base.helper InPlaceMacrosHelper |
@@ -0,0 +1,25 | |||||
|
|
1 | + module InPlaceEditing | ||
|
|
2 | + def self.included(base) | ||
|
|
3 | + base.extend(ClassMethods) | ||
|
|
4 | + end | ||
|
|
5 | + | ||
|
|
6 | + # Example: | ||
|
|
7 | + # | ||
|
|
8 | + # # Controller | ||
|
|
9 | + # class BlogController < ApplicationController | ||
|
|
10 | + # in_place_edit_for :post, :title | ||
|
|
11 | + # end | ||
|
|
12 | + # | ||
|
|
13 | + # # View | ||
|
|
14 | + # <%= in_place_editor_field :post, 'title' %> | ||
|
|
15 | + # | ||
|
|
16 | + module ClassMethods | ||
|
|
17 | + def in_place_edit_for(object, attribute, options = {}) | ||
|
|
18 | + define_method("set_#{object}_#{attribute}") do | ||
|
|
19 | + @item = object.to_s.camelize.constantize.find(params[:id]) | ||
|
|
20 | + @item.update_attribute(attribute, params[:value]) | ||
|
|
21 | + render :text => @item.send(attribute).to_s | ||
|
|
22 | + end | ||
|
|
23 | + end | ||
|
|
24 | + end | ||
|
|
25 | + end |
@@ -0,0 +1,71 | |||||
|
|
1 | + module InPlaceMacrosHelper | ||
|
|
2 | + # Makes an HTML element specified by the DOM ID +field_id+ become an in-place | ||
|
|
3 | + # editor of a property. | ||
|
|
4 | + # | ||
|
|
5 | + # A form is automatically created and displayed when the user clicks the element, | ||
|
|
6 | + # something like this: | ||
|
|
7 | + # <form id="myElement-in-place-edit-form" target="specified url"> | ||
|
|
8 | + # <input name="value" text="The content of myElement"/> | ||
|
|
9 | + # <input type="submit" value="ok"/> | ||
|
|
10 | + # <a onclick="javascript to cancel the editing">cancel</a> | ||
|
|
11 | + # </form> | ||
|
|
12 | + # | ||
|
|
13 | + # The form is serialized and sent to the server using an AJAX call, the action on | ||
|
|
14 | + # the server should process the value and return the updated value in the body of | ||
|
|
15 | + # the reponse. The element will automatically be updated with the changed value | ||
|
|
16 | + # (as returned from the server). | ||
|
|
17 | + # | ||
|
|
18 | + # Required +options+ are: | ||
|
|
19 | + # <tt>:url</tt>:: Specifies the url where the updated value should | ||
|
|
20 | + # be sent after the user presses "ok". | ||
|
|
21 | + # | ||
|
|
22 | + # Addtional +options+ are: | ||
|
|
23 | + # <tt>:rows</tt>:: Number of rows (more than 1 will use a TEXTAREA) | ||
|
|
24 | + # <tt>:cols</tt>:: Number of characters the text input should span (works for both INPUT and TEXTAREA) | ||
|
|
25 | + # <tt>:size</tt>:: Synonym for :cols when using a single line text input. | ||
|
|
26 | + # <tt>:cancel_text</tt>:: The text on the cancel link. (default: "cancel") | ||
|
|
27 | + # <tt>:save_text</tt>:: The text on the save link. (default: "ok") | ||
|
|
28 | + # <tt>:loading_text</tt>:: The text to display while the data is being loaded from the server (default: "Loading...") | ||
|
|
29 | + # <tt>:saving_text</tt>:: The text to display when submitting to the server (default: "Saving...") | ||
|
|
30 | + # <tt>:external_control</tt>:: The id of an external control used to enter edit mode. | ||
|
|
31 | + # <tt>:load_text_url</tt>:: URL where initial value of editor (content) is retrieved. | ||
|
|
32 | + # <tt>:options</tt>:: Pass through options to the AJAX call (see prototype's Ajax.Updater) | ||
|
|
33 | + # <tt>:with</tt>:: JavaScript snippet that should return what is to be sent | ||
|
|
34 | + # in the AJAX call, +form+ is an implicit parameter | ||
|
|
35 | + # <tt>:script</tt>:: Instructs the in-place editor to evaluate the remote JavaScript response (default: false) | ||
|
|
36 | + # <tt>:click_to_edit_text</tt>::The text shown during mouseover the editable text (default: "Click to edit") | ||
|
|
37 | + def in_place_editor(field_id, options = {}) | ||
|
|
38 | + function = "new Ajax.InPlaceEditor(" | ||
|
|
39 | + function << "'#{field_id}', " | ||
|
|
40 | + function << "'#{url_for(options[:url])}'" | ||
|
|
41 | + | ||
|
|
42 | + js_options = {} | ||
|
|
43 | + js_options['cancelText'] = %('#{options[:cancel_text]}') if options[:cancel_text] | ||
|
|
44 | + js_options['okText'] = %('#{options[:save_text]}') if options[:save_text] | ||
|
|
45 | + js_options['loadingText'] = %('#{options[:loading_text]}') if options[:loading_text] | ||
|
|
46 | + js_options['savingText'] = %('#{options[:saving_text]}') if options[:saving_text] | ||
|
|
47 | + js_options['rows'] = options[:rows] if options[:rows] | ||
|
|
48 | + js_options['cols'] = options[:cols] if options[:cols] | ||
|
|
49 | + js_options['size'] = options[:size] if options[:size] | ||
|
|
50 | + js_options['externalControl'] = "'#{options[:external_control]}'" if options[:external_control] | ||
|
|
51 | + js_options['loadTextURL'] = "'#{url_for(options[:load_text_url])}'" if options[:load_text_url] | ||
|
|
52 | + js_options['ajaxOptions'] = options[:options] if options[:options] | ||
|
|
53 | + js_options['evalScripts'] = options[:script] if options[:script] | ||
|
|
54 | + js_options['callback'] = "function(form) { return #{options[:with]} }" if options[:with] | ||
|
|
55 | + js_options['clickToEditText'] = %('#{options[:click_to_edit_text]}') if options[:click_to_edit_text] | ||
|
|
56 | + function << (', ' + options_for_javascript(js_options)) unless js_options.empty? | ||
|
|
57 | + | ||
|
|
58 | + function << ')' | ||
|
|
59 | + | ||
|
|
60 | + javascript_tag(function) | ||
|
|
61 | + end | ||
|
|
62 | + | ||
|
|
63 | + # Renders the value of the specified object and method with in-place editing capabilities. | ||
|
|
64 | + def in_place_editor_field(object, method, tag_options = {}, in_place_editor_options = {}) | ||
|
|
65 | + tag = ::ActionView::Helpers::InstanceTag.new(object, method, self) | ||
|
|
66 | + tag_options = {:tag => "span", :id => "#{object}_#{method}_#{tag.object.id}_in_place_editor", :class => "in_place_editor_field"}.merge!(tag_options) | ||
|
|
67 | + in_place_editor_options[:url] = in_place_editor_options[:url] || url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id }) | ||
|
|
68 | + tag.to_content_tag(tag_options.delete(:tag), tag_options) + | ||
|
|
69 | + in_place_editor(tag_options[:id], in_place_editor_options) | ||
|
|
70 | + end | ||
|
|
71 | + end |
@@ -0,0 +1,69 | |||||
|
|
1 | + require File.expand_path(File.join(File.dirname(__FILE__), '../../../../test/test_helper')) | ||
|
|
2 | + require 'test/unit' | ||
|
|
3 | + | ||
|
|
4 | + class InPlaceEditingTest < Test::Unit::TestCase | ||
|
|
5 | + include InPlaceEditing | ||
|
|
6 | + include InPlaceMacrosHelper | ||
|
|
7 | + | ||
|
|
8 | + include ActionView::Helpers::UrlHelper | ||
|
|
9 | + include ActionView::Helpers::TagHelper | ||
|
|
10 | + include ActionView::Helpers::TextHelper | ||
|
|
11 | + include ActionView::Helpers::FormHelper | ||
|
|
12 | + include ActionView::Helpers::CaptureHelper | ||
|
|
13 | + | ||
|
|
14 | + def setup | ||
|
|
15 | + @controller = Class.new do | ||
|
|
16 | + def url_for(options) | ||
|
|
17 | + url = "http://www.example.com/" | ||
|
|
18 | + url << options[:action].to_s if options and options[:action] | ||
|
|
19 | + url | ||
|
|
20 | + end | ||
|
|
21 | + end | ||
|
|
22 | + @controller = @controller.new | ||
|
|
23 | + end | ||
|
|
24 | + | ||
|
|
25 | + def test_in_place_editor_external_control | ||
|
|
26 | + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {externalControl:'blah'})\n//]]>\n</script>), | ||
|
|
27 | + in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :external_control => 'blah'}) | ||
|
|
28 | + end | ||
|
|
29 | + | ||
|
|
30 | + def test_in_place_editor_size | ||
|
|
31 | + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {size:4})\n//]]>\n</script>), | ||
|
|
32 | + in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :size => 4}) | ||
|
|
33 | + end | ||
|
|
34 | + | ||
|
|
35 | + def test_in_place_editor_cols_no_rows | ||
|
|
36 | + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {cols:4})\n//]]>\n</script>), | ||
|
|
37 | + in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :cols => 4}) | ||
|
|
38 | + end | ||
|
|
39 | + | ||
|
|
40 | + def test_in_place_editor_cols_with_rows | ||
|
|
41 | + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {cols:40, rows:5})\n//]]>\n</script>), | ||
|
|
42 | + in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :rows => 5, :cols => 40}) | ||
|
|
43 | + end | ||
|
|
44 | + | ||
|
|
45 | + def test_inplace_editor_loading_text | ||
|
|
46 | + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {loadingText:'Why are we waiting?'})\n//]]>\n</script>), | ||
|
|
47 | + in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :loading_text => 'Why are we waiting?'}) | ||
|
|
48 | + end | ||
|
|
49 | + | ||
|
|
50 | + def test_in_place_editor_url | ||
|
|
51 | + assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value')", | ||
|
|
52 | + in_place_editor( 'id-goes-here', :url => { :action => "action_to_set_value" }) | ||
|
|
53 | + end | ||
|
|
54 | + | ||
|
|
55 | + def test_in_place_editor_load_text_url | ||
|
|
56 | + assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {loadTextURL:'http://www.example.com/action_to_get_value'})", | ||
|
|
57 | + in_place_editor( 'id-goes-here', | ||
|
|
58 | + :url => { :action => "action_to_set_value" }, | ||
|
|
59 | + :load_text_url => { :action => "action_to_get_value" }) | ||
|
|
60 | + end | ||
|
|
61 | + | ||
|
|
62 | + def test_in_place_editor_eval_scripts | ||
|
|
63 | + assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {evalScripts:true})", | ||
|
|
64 | + in_place_editor( 'id-goes-here', | ||
|
|
65 | + :url => { :action => "action_to_set_value" }, | ||
|
|
66 | + :script => true ) | ||
|
|
67 | + end | ||
|
|
68 | + | ||
|
|
69 | + end No newline at end of file |
You need to be logged in to leave comments.
Login now