rubymood-jintastic 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ #Jintastic
2
+
3
+ Jintastic is a **j**Query based **in**-place editor generated by [Form**tastic**](http://github.com/justinfrench/formtastic/tree/master).
4
+
5
+ ## Features
6
+
7
+ * easy usage
8
+ * you can reuse your partials and controller actions
9
+ * you can edit more than one attribute at once
10
+ * you can use nested resources
11
+
12
+ ## Install
13
+
14
+ **Gem**
15
+
16
+ sudo gem install rubymood-jintastic
17
+
18
+ Add Jintastic as a dependency in your environment.rb file:
19
+
20
+ config.gem 'rubymood-jintastic', :lib => 'jintastic'
21
+
22
+ **Plugin**
23
+
24
+ script/plugin install git://github.com/rubymood/jintastic.git
25
+
26
+ ## Usage
27
+
28
+ Having downloaded jQuery(>=1.3.2) include javascript files in your template:
29
+
30
+ <%= javascripts_include_tag 'jquery', 'jintastic' %>
31
+
32
+ **Example**
33
+
34
+ Let's say you have a bookmark resource and you have this haml files:
35
+
36
+ # bookmarks/index.html.haml
37
+ %ul
38
+ = render @bookmarks
39
+
40
+ # bookmarks/_bookmark.html.haml
41
+ %li
42
+ = bookmark.name
43
+
44
+ # bookmarks/edit.html.haml
45
+ -# with formtastic
46
+ - semantic_form_for @bookmark do |f|
47
+ -f.inputs
48
+ = render :partial=>'form', :locals=>{:f=>f}
49
+ = f.commit_button
50
+ -# or with default form_for helper
51
+ - form_for @bookmark do |f|
52
+ = render :partial=>'form'
53
+ = f.submit_tag 'Save'
54
+
55
+ # bookmarks/_form.html.haml
56
+ -# with formtastic
57
+ = f.input :name
58
+ = f.input :url
59
+ -# or with default form_for helper
60
+ = error_messages_for :bookmark
61
+ = f.text_field :name
62
+ = f.text_field :url
63
+
64
+ This is simple. If you organize your views in this way you just follows the rails conventions. Now if you want to in-place editing a bookmark inside your index page just simply do one of the below:
65
+
66
+ # bookmarks/_bookmark.html.haml
67
+ %li{:id=>dom_id(bookmark)} # we are referencing the id later in a js file (see below)
68
+ -# this shows name attribute, and if you click on it, you can edit it
69
+ = in_place_editor_for bookmark, :name
70
+ -# this shows name attribute, and if you click on it, it will show your bookmark form partial
71
+ = in_place_editor_for bookmark, :name=>:form
72
+ -# this shows name attribute, and if you click on it, it will show your custom form partial
73
+ = in_place_editor_for bookmark, :name=>'bookmarks/custom_form'
74
+ -# this shows name attribute, and if you click on it, you can edit both the name and url attributes
75
+ = in_place_editor_for bookmark, :name=>[:name, :bookmark]
76
+ -# so you can edit something else (this shows name attribute, but when you click on it, you will edit the url attribute)
77
+ = in_place_editor_for bookmark, :name=>:url
78
+ -# and finally, you can add nested resource
79
+ = in_place_editor_for [:admin, bookmark], :name
80
+
81
+ Inside your bookmark controller you have to make nothing new. The only thing you have to do is some javascript magic:
82
+
83
+ # bookmarks/update.js.erb
84
+ $('#<%= dom_id @bookmark %>').replaceWith('<%= escape_javascript render @bookmark %>')
85
+
86
+ ## And finally, what happens at validation error?
87
+
88
+ With formtastic input helper the validation errors will be displayed automatic. If you use rails default input helpers you could display the errors in your form partial (see above in the example).
89
+
90
+ ## TODO
91
+
92
+ * some options to customize in place editor form
93
+ * make some tests
94
+
95
+ Copyright (c) 2009 Nandor Komzak, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "jintastic"
8
+ gem.summary = %Q{jQuery based in-place editor generated by Formtastic}
9
+ gem.email = "nandor.komzak@gmail.com"
10
+ gem.homepage = "http://github.com/rubymood/jintastic"
11
+ gem.authors = ["Nandor Komzak"]
12
+ gem.description = gem.summary
13
+ gem.files.include FileList["{assets}/**/*"]
14
+ gem.add_dependency("justinfrench-formtastic")
15
+ gem.has_rdoc = false
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION.yml')
47
+ config = YAML.load(File.read('VERSION.yml'))
48
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "jintastic #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
58
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 0
4
+ :major: 1
@@ -0,0 +1,16 @@
1
+ <div class="in_place_editor_wrapper">
2
+ <%= content_tag container_tag, instance[attribute], content_tag_options %>
3
+ <% semantic_form_for path_spec_or_object, form_tag_options do |f| %>
4
+ <% f.inputs do %>
5
+ <% if input_attributes %>
6
+ <% input_attributes.each do |attr| %>
7
+ <%= f.input attr %>
8
+ <% end %>
9
+ <% else %>
10
+ <%= render :partial=>form_partial, :locals=>{:f=>f} %>
11
+ <% end %>
12
+ <% end %>
13
+ <%= submit_tag 'Save', :class=>'in_place_save' %>
14
+ <input type='button' value='Cancel' class='in_place_cancel' />
15
+ <% end %>
16
+ </div>
@@ -0,0 +1,36 @@
1
+ (function($){
2
+ $(function(){
3
+ $.inPlaceEditor = {
4
+ toggle: function() {
5
+ $([this.attribute, this.form]).toggle()
6
+
7
+ if (this.form.is(':visible'))
8
+ $.inPlaceEditor.form.find('fieldset li:nth-child(1)')
9
+ }
10
+ }
11
+
12
+ $('.in_place_attribute').live('click', function() {
13
+ $.inPlaceEditor.attribute = $(this)
14
+ $.inPlaceEditor.form = $.inPlaceEditor.attribute.next()
15
+ $.inPlaceEditor.toggle()
16
+ })
17
+
18
+ $('.in_place_save').live('click', function() {
19
+ $.inPlaceEditor.form = $(this).parent('form')
20
+ $.inPlaceEditor.attribute = $.inPlaceEditor.form.prev()
21
+ $.ajax({
22
+ url: $.inPlaceEditor.form[0].action,
23
+ type: "POST",
24
+ dataType: "script",
25
+ data: $.inPlaceEditor.form.serializeArray()
26
+ })
27
+ return false
28
+ })
29
+
30
+ $('.in_place_cancel').live('click', function() {
31
+ $.inPlaceEditor.form = $(this).parent('form')
32
+ $.inPlaceEditor.attribute = $.inPlaceEditor.form.prev()
33
+ $.inPlaceEditor.toggle()
34
+ })
35
+ })
36
+ })(jQuery)
data/lib/jintastic.rb ADDED
@@ -0,0 +1,62 @@
1
+ require 'formtastic'
2
+
3
+ class ActionView::Base
4
+ def in_place_editor_for(path_spec_or_object, attributes)
5
+ instance = case path_spec_or_object
6
+ when ActiveRecord::Base: path_spec_or_object
7
+ when Array: path_spec_or_object.last
8
+ end
9
+
10
+ if attributes.class==Symbol
11
+ #simple one attribute in place editor
12
+ #in_place_editor_for @user, :name
13
+ attribute = attributes.to_sym
14
+ input_attributes = Array(attribute)
15
+
16
+ elsif attributes.values.first==:form
17
+ #render default form partial
18
+ #in_place_editor_for @user, :name=>:form
19
+ attribute = attributes.keys.first
20
+
21
+ elsif attributes.values.first.class==String
22
+ #render custom form partial
23
+ #in_place_editor_for @user, :name=>'users/custom_form'
24
+ attribute = attributes.keys.first
25
+ form_partial = attributes[attribute]
26
+
27
+ else
28
+ #attribute is an array
29
+ #in_place_editor_for @user, :name=>[:name,:address]
30
+ attribute = attributes.keys.first
31
+ input_attributes = attributes.values
32
+ end
33
+
34
+ container_tag = instance.column_for_attribute(attribute).type == :text ? :pre : :span
35
+
36
+ form_tag_options = {:id=>dom_id(path_spec_or_object, attribute)}
37
+ content_tag_options = {:class=>'in_place_attribute'}
38
+
39
+ form_partial ||= "#{instance.class.to_s.downcase.pluralize}/form" unless input_attributes
40
+
41
+ if instance.valid?
42
+ form_tag_options.merge!({:html=>{:style=>"display: none"}})
43
+ else
44
+ content_tag_options.merge!({:style=>"display: none"})
45
+ end
46
+
47
+ render :partial => 'jintastic/in_place_editor',
48
+ :locals => {:container_tag=>container_tag,
49
+ :input_attributes=>input_attributes,
50
+ :attribute=>attribute,
51
+ :path_spec_or_object=>path_spec_or_object,
52
+ :instance=>instance,
53
+ :content_tag_options=>content_tag_options,
54
+ :form_tag_options=>form_tag_options,
55
+ :form_partial=>form_partial}
56
+ end
57
+ end
58
+
59
+ # hack for assets because gems hasn't got rake tasks
60
+ #
61
+ # copy assets at every server start because after update maybe they have to update too
62
+ FileUtils.cp_r File.join(File.dirname(__FILE__), *%w{.. assets .}), Rails.root
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class JintasticTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'jintastic'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubymood-jintastic
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nandor Komzak
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: justinfrench-formtastic
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: jQuery based in-place editor generated by Formtastic
26
+ email: nandor.komzak@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - README.md
33
+ files:
34
+ - README.md
35
+ - Rakefile
36
+ - VERSION.yml
37
+ - assets/app/views/jintastic/_in_place_editor.html.erb
38
+ - assets/public/javascripts/jintastic.js
39
+ - lib/jintastic.rb
40
+ - test/jintastic_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: false
43
+ homepage: http://github.com/rubymood/jintastic
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.2.0
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: jQuery based in-place editor generated by Formtastic
68
+ test_files:
69
+ - test/test_helper.rb
70
+ - test/jintastic_test.rb