stay 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest ADDED
@@ -0,0 +1,10 @@
1
+ Manifest
2
+ README.md
3
+ README.rdoc
4
+ Rakefile
5
+ lib/assets/javascripts/stay.js
6
+ lib/stay.rb
7
+ lib/stay/controller_extensions.rb
8
+ lib/stay/engine.rb
9
+ lib/stay/helper.rb
10
+ lib/stay/string_extensions.rb
data/README.md ADDED
File without changes
data/README.rdoc ADDED
File without changes
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('stay', '0.1.2') do |p|
6
+ p.description = "Form ajax helper with tinymce support"
7
+ p.summary = "Helper for building ajax form, inspired by best_in_place, with some modifications, support tinymce as editor"
8
+ p.url = "http://github.com/tejanium/stay"
9
+ p.author = "Teja Sophista"
10
+ p.email = "tejanium@yahoo.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*"]
12
+ p.runtime_dependencies = ["jquery-rails", "tinymce-rails"]
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,116 @@
1
+ jQuery.fn.stay = function(){
2
+ this.each(function(){
3
+ new Stay(this);
4
+ });
5
+ }
6
+
7
+ jQuery.fn.disable = function(){
8
+ this.attr("disabled", "")
9
+ }
10
+
11
+ jQuery.fn.enable = function(){
12
+ this.removeAttr("disabled")
13
+ }
14
+
15
+ function Stay(e){
16
+ this.init(e);
17
+ }
18
+
19
+ Stay.prototype = {
20
+ init: function(e){
21
+ var self = this;
22
+ console.info("Stay initializing...");
23
+
24
+ self.stayWrapper = jQuery(e);
25
+ self.stayObject = self.stayWrapper.find(".stay-object");
26
+ self.stayForm = self.stayWrapper.find(".stay-form");
27
+ self.stayFormForm = self.stayForm.find("form");
28
+ self.stayTinyMCE = self.stayForm.find(".stay-input.stay-tiny-mce")
29
+ self.tinyMCEUsed = self.stayTinyMCE.length > 0;
30
+ self.stayFormInput = self.stayFormForm.find(".stay-input");
31
+
32
+ self.tinyMCETheme = self.stayWrapper.data("tinymce-theme") || "simple"
33
+
34
+ self.activator_id = self.stayWrapper.data("activator")
35
+ self.activator = (typeof self.activator_id === "undefined") ? self.stayObject : jQuery(self.activator_id)
36
+
37
+ self.submitter_id = this.stayWrapper.data("submitter")
38
+ self.submitter = (typeof self.submitter_id === "undefined") ? self.stayFormInput : jQuery(self.submitter_id)
39
+
40
+ if(self.tinyMCEUsed){
41
+ console.info("TinyMCE found! Initailize editor...");
42
+ this.stayTinyMCE.tinymce({
43
+ theme: self.tinyMCETheme,
44
+ setup : function(ed) {
45
+ ed.onInit.add(function(ed, evt) {
46
+ var dom = ed.dom;
47
+ var doc = ed.getDoc();
48
+ if(typeof self.submitter_id === "undefined"){
49
+ tinymce.dom.Event.add(doc, 'blur', function(e) {
50
+ self.stayFormForm.submit();
51
+ });
52
+ }else{
53
+ self.submitter.click(function(){
54
+ self.stayFormForm.submit();
55
+ });
56
+ }
57
+ });
58
+ }
59
+ });
60
+ }
61
+ self.bindTrigger();
62
+ self.bindCallback();
63
+
64
+ console.info("Stay successfully initialized");
65
+ },
66
+
67
+
68
+ bindTrigger: function(){
69
+ var self = this;
70
+
71
+ console.log("Activator used: ");
72
+ console.log(self.activator);
73
+
74
+ self.activator.click(function(){
75
+ self.stayObject.hide();
76
+ self.stayForm.show();
77
+ if(self.tinyMCEUsed){
78
+ self.stayTinyMCE.tinymce().focus();
79
+ }else{
80
+ self.stayFormInput.focus();
81
+
82
+ var ev = (typeof self.submitter_id === "undefined") ? "blur" : "click";
83
+ console.log("Submitter event: " + ev + ", on:");
84
+ console.log(self.submitter);
85
+
86
+ self.submitter.bind(ev, function(){
87
+ self.stayFormForm.submit();
88
+ self.stayFormInput.disable();
89
+ });
90
+ }
91
+ });
92
+ },
93
+
94
+ bindCallback: function(){
95
+ var self = this;
96
+
97
+ self.stayFormForm.live("ajax:success", function(event, data, status, xhr){
98
+ self.stayFormInput.val(data.input);
99
+ self.stayObject.html(data.display);
100
+ self.reShowObject();
101
+ });
102
+
103
+ self.stayFormForm.live("ajax:error", function(event, data, status, xhr){
104
+ var cb = jQuery.parseJSON(data.responseText);
105
+ self.stayFormInput.val(cb.input);
106
+ self.stayObject.html(cb.display);
107
+ self.reShowObject();
108
+ });
109
+ },
110
+
111
+ reShowObject: function(){
112
+ this.stayForm.hide();
113
+ this.stayObject.show();
114
+ this.stayFormInput.enable();
115
+ }
116
+ };
@@ -0,0 +1,20 @@
1
+ module Stay
2
+ module ControllerExtensions
3
+ def stay_response(obj)
4
+ obj.changed? ? response_error(obj) : response_ok(obj)
5
+ end
6
+
7
+ private
8
+ def response_ok(obj)
9
+ field = params[obj.class.to_s.underscore].keys.first
10
+ value = obj.send(field.to_sym)
11
+ render json: { :display => value.to_html, :input => value }, status: :ok
12
+ end
13
+
14
+ def response_error(obj)
15
+ field = params[obj.class.to_s.underscore].keys.first
16
+ value = obj.send("#{field}_was".to_sym)
17
+ render json: { :display => value.to_html, :input => value }, status: :unprocessable_entity
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ module Stay
2
+ class Engine < Rails::Engine
3
+ initializer "setup for rails" do
4
+ ActionView::Base.send(:include, Stay::StayHelpers)
5
+ ActionController::Base.send(:include, Stay::ControllerExtensions)
6
+ String.send(:include, Stay::StringExtensions)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ module Stay
2
+ module FormHelper
3
+ def generate_form(object, field, type = :text_field)
4
+ form_for(object, remote: true, format: :json) do |f|
5
+ if type == :tiny_mce || type.is_a?(Array)
6
+ f.send(:text_area, field, class: "stay-input stay-tiny-mce")
7
+ else
8
+ f.send(type, field, class: "stay-input")
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ ActionView::Helpers::FormHelper.send(:include, FormHelper)
15
+
16
+ module StayHelpers
17
+ def stay(record, field, opts = {})
18
+ html = "<span class='stay' "
19
+ html << "data-activator='#{ opts[:activator] }' " if opts[:activator]
20
+ html << "data-submitter='#{ opts[:submitter] }' " if opts[:submitter]
21
+ html << "data-tinymce-theme='#{ opts[:type] }' " if opts[:type] == :tiny_mce
22
+ html << "data-tinymce-theme='#{ opts[:type][1] }' " if opts[:type].is_a?(Array)
23
+ html << ">"
24
+ html << "<span class='stay-object'>"
25
+ html << (record.is_a?(Array) ? record.last : record).send(field).to_html
26
+ html << "</span>"
27
+ html << "<span class='stay-form' style='display:none'>"
28
+ html << generate_form(record, field, opts[:type])
29
+ html << "</span>"
30
+ html << "</span>"
31
+ html.html_safe
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,10 @@
1
+ module Stay
2
+ module StringExtensions
3
+ def to_html
4
+ html = "<p>"
5
+ html << self.gsub(" ", "&nbsp;").gsub("\n", "<br />").gsub("<p>", "").gsub("</p>","")
6
+ html << "</p>"
7
+ html
8
+ end
9
+ end
10
+ end
data/lib/stay.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "stay/helper"
2
+ require "stay/controller_extensions"
3
+ require "stay/string_extensions"
4
+ require "stay/engine"
5
+ require "action_view"
6
+
7
+ module Stay
8
+ class ActionView::Base
9
+ include Stay::StayHelpers
10
+ end
11
+ end
data/stay.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "stay"
5
+ s.version = "0.1.2"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Teja Sophista"]
9
+ s.date = "2012-02-04"
10
+ s.description = "Form ajax helper with tinymce support"
11
+ s.email = "tejanium@yahoo.com"
12
+ s.extra_rdoc_files = ["README.md", "README.rdoc", "lib/assets/javascripts/stay.js", "lib/stay.rb", "lib/stay/controller_extensions.rb", "lib/stay/engine.rb", "lib/stay/helper.rb", "lib/stay/string_extensions.rb"]
13
+ s.files = ["Manifest", "README.md", "README.rdoc", "Rakefile", "lib/assets/javascripts/stay.js", "lib/stay.rb", "lib/stay/controller_extensions.rb", "lib/stay/engine.rb", "lib/stay/helper.rb", "lib/stay/string_extensions.rb", "stay.gemspec"]
14
+ s.homepage = "http://github.com/tejanium/stay"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Stay", "--main", "README.md"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "stay"
18
+ s.rubygems_version = "1.8.10"
19
+ s.summary = "Helper for building ajax form, inspired by best_in_place, with some modifications, support tinymce as editor"
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_runtime_dependency(%q<jquery-rails>, [">= 0"])
26
+ s.add_runtime_dependency(%q<tinymce-rails>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<jquery-rails>, [">= 0"])
29
+ s.add_dependency(%q<tinymce-rails>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<jquery-rails>, [">= 0"])
33
+ s.add_dependency(%q<tinymce-rails>, [">= 0"])
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Teja Sophista
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-04 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jquery-rails
16
+ requirement: &13965200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *13965200
25
+ - !ruby/object:Gem::Dependency
26
+ name: tinymce-rails
27
+ requirement: &13964740 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *13964740
36
+ description: Form ajax helper with tinymce support
37
+ email: tejanium@yahoo.com
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files:
41
+ - README.md
42
+ - README.rdoc
43
+ - lib/assets/javascripts/stay.js
44
+ - lib/stay.rb
45
+ - lib/stay/controller_extensions.rb
46
+ - lib/stay/engine.rb
47
+ - lib/stay/helper.rb
48
+ - lib/stay/string_extensions.rb
49
+ files:
50
+ - Manifest
51
+ - README.md
52
+ - README.rdoc
53
+ - Rakefile
54
+ - lib/assets/javascripts/stay.js
55
+ - lib/stay.rb
56
+ - lib/stay/controller_extensions.rb
57
+ - lib/stay/engine.rb
58
+ - lib/stay/helper.rb
59
+ - lib/stay/string_extensions.rb
60
+ - stay.gemspec
61
+ homepage: http://github.com/tejanium/stay
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --line-numbers
66
+ - --inline-source
67
+ - --title
68
+ - Stay
69
+ - --main
70
+ - README.md
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '1.2'
85
+ requirements: []
86
+ rubyforge_project: stay
87
+ rubygems_version: 1.8.10
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: Helper for building ajax form, inspired by best_in_place, with some modifications,
91
+ support tinymce as editor
92
+ test_files: []