pivotal-honkster-jelly 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/.gitignore +3 -0
  2. data/MIT-LICENSE +21 -0
  3. data/README.markdown +112 -0
  4. data/Rakefile +39 -0
  5. data/VERSION +1 -0
  6. data/generators/jelly/USAGE +11 -0
  7. data/generators/jelly/jelly_generator.rb +12 -0
  8. data/generators/jelly/templates/javascripts/ajax_with_jelly.js +26 -0
  9. data/generators/jelly/templates/javascripts/jelly.js +77 -0
  10. data/generators/jelly/templates/javascripts/jquery/jquery-1.3.2.js +4376 -0
  11. data/generators/jelly/templates/javascripts/jquery/jquery.protify-0.3.js +345 -0
  12. data/honkster-jelly.gemspec +76 -0
  13. data/install.rb +1 -0
  14. data/lib/jelly/jelly_controller.rb +20 -0
  15. data/lib/jelly/jelly_helper.rb +21 -0
  16. data/lib/jelly.rb +16 -0
  17. data/spec/controllers/jelly_controller_spec.rb +88 -0
  18. data/spec/helpers/jelly_helper_spec.rb +33 -0
  19. data/spec/rails_root/app/controllers/application_controller.rb +10 -0
  20. data/spec/rails_root/app/helpers/application_helper.rb +3 -0
  21. data/spec/rails_root/config/boot.rb +110 -0
  22. data/spec/rails_root/config/environment.rb +41 -0
  23. data/spec/rails_root/config/environments/development.rb +17 -0
  24. data/spec/rails_root/config/environments/production.rb +28 -0
  25. data/spec/rails_root/config/environments/test.rb +28 -0
  26. data/spec/rails_root/config/initializers/backtrace_silencers.rb +7 -0
  27. data/spec/rails_root/config/initializers/inflections.rb +10 -0
  28. data/spec/rails_root/config/initializers/mime_types.rb +5 -0
  29. data/spec/rails_root/config/initializers/new_rails_defaults.rb +19 -0
  30. data/spec/rails_root/config/initializers/session_store.rb +15 -0
  31. data/spec/rails_root/config/routes.rb +43 -0
  32. data/spec/rails_root/test/performance/browsing_test.rb +9 -0
  33. data/spec/rails_root/test/test_helper.rb +38 -0
  34. data/spec/spec_helper.rb +22 -0
  35. data/tasks/jelly_tasks.rake +4 -0
  36. data/uninstall.rb +1 -0
  37. metadata +97 -0
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ .idea
2
+ *.svn
3
+ pkg
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2009 Pivotal Labs, Inc.
2
+ with contributions by: Brian Takita, Nate Clark, Eric Metens
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,112 @@
1
+ Jelly
2
+ =====
3
+
4
+ INSTALLATION
5
+ ------------
6
+
7
+ If you haven't already, add GitHub to your gem sources:
8
+
9
+ gem sources -a http://gems.github.com
10
+
11
+ Then run:
12
+
13
+ sudo gem install pivotal-jelly
14
+
15
+
16
+ GETTING STARTED
17
+ ---------------
18
+
19
+ In your `environment.rb` in the `Rails::Initializer.run` block, be sure to require jelly:
20
+
21
+ config.gem "jelly"
22
+
23
+ Then install the required JavaScript files by running this command in your Rails project:
24
+
25
+ script/generate jelly
26
+
27
+ Then, in your layout, add the following:
28
+
29
+ <%= javascript_include_tag :jelly, *application_jelly_files %>
30
+ <%= spread_jelly %>
31
+
32
+ This will include the required JavaScripts for jelly and activate the current page. The `:jelly` javascript expansion
33
+ includes jQuery. If you already have jQuery included in the page, use the `:only_jelly` expansion instead.
34
+
35
+ EXAMPLE USAGE
36
+ -------------
37
+
38
+ Assuming you have controller named `fun` with an action called `index` and that you have a layout called `fun.html.erb`
39
+ that is already setup as described above. In your fun index view (`index.html.erb`), put:
40
+
41
+ <h1>Your page's 'index' function did not run. Jelly is not configured correctly.</h1>
42
+ <span class="all">Your page's 'all' function did not run. Jelly is not configured correctly.</span>
43
+
44
+ Then, in `public/javascripts/pages/fun.js`, put:
45
+
46
+ Jelly.add("Fun", {
47
+ all: function() {
48
+ $('span.all').text("I am displayed on every action in this controller.");
49
+ },
50
+ index: function() {
51
+ $('h1').text("Welcome to the index page.");
52
+ }
53
+ });
54
+
55
+ Now goto `/fun/index` and see Jelly in action!
56
+
57
+ AJAX CALLBACKS WITH JELLY
58
+ -------------------------
59
+
60
+ You can trigger callbacks on the page object from Rails with the `jelly_callback` method.
61
+ Adding to the `index.html.erb` file from above:
62
+
63
+ <a href="#" id="jelly_ajax_link">Click me for Jelly Ajax Action</a>
64
+ <span id="jelly_callback_element">This gets filled in by the Jelly Ajax callback</span>
65
+
66
+ And update your controller:
67
+
68
+ class FunController < ApplicationController
69
+ def index
70
+ end
71
+
72
+ def ajax_action
73
+ jelly_callback do
74
+ [
75
+ render(:partial => 'fun_partial'),
76
+ "second_parameter"
77
+ ]
78
+ end
79
+ end
80
+ end
81
+
82
+ Update your page object in `fun.js`:
83
+
84
+ Jelly.add("Fun", {
85
+ all: function() {
86
+ $('title').text("Hello! Isn't this fun?");
87
+ },
88
+ index: function() {
89
+ $('h1').text("Welcome to the index page.");
90
+ $("#jelly_ajax_link").click(function() {
91
+ $.ajaxWithJelly({
92
+ type: "GET",
93
+ url: "/fun/ajax_action"
94
+ });
95
+ });
96
+ },
97
+ on_ajax_action: function(html, second_parameter) {
98
+ $('#jelly_callback_element').html(html);
99
+ }
100
+ });
101
+
102
+ And finally, make the partial `_fun_partial.html.erb` and just put "Hello from the server!" in it, then visit your page
103
+ and watch the ajax callbacks in action.
104
+
105
+ The `jelly_callback` method takes an optional parameter for the name of the callback, and the provided block can return
106
+ either one parameter, or an array of parameters.
107
+
108
+ DEVELOPMENT
109
+ -----------
110
+
111
+ To run ruby tests, run `rake spec`.
112
+ To run JavaScript tests, open `jelly/spec/jasmine_runner.html` in a web browser.
data/Rakefile ADDED
@@ -0,0 +1,39 @@
1
+ require 'spec/version'
2
+ require 'spec/rake/spectask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run specs'
6
+ task :default => :spec
7
+
8
+ desc 'Test the jelly plugin with Rspec.'
9
+ Spec::Rake::SpecTask.new(:spec) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'spec'
12
+ t.pattern = 'spec/**/*_spec.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the jelly plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Jelly.'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "honkster-jelly"
29
+ gemspec.summary = "a sweet unobtrusive javascript framework for jQuery and Rails"
30
+ gemspec.description = "Jelly provides a set of tools and conventions for creating rich ajax/javascript web applications with jQuery and Ruby on Rails."
31
+ gemspec.email = "opensource@pivotallabs.com"
32
+ gemspec.homepage = "http://github.com/pivotal/jelly"
33
+ gemspec.authors = ["Pivotal Labs, Inc"]
34
+ gemspec.files.exclude 'spec/**/*'
35
+ gemspec.add_dependency('rails', '>= 2.3.0')
36
+ end
37
+ rescue LoadError
38
+ puts "Jeweler not available. Install it with: sudo gem install jeweler"
39
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.0
@@ -0,0 +1,11 @@
1
+ Description:
2
+ Creates 'jelly.js' and dependent jQuery libraries
3
+
4
+ Example:
5
+ ./script/generate jelly
6
+
7
+ This will create:
8
+ public/javascripts/jelly.js
9
+ public/javascripts/jquery-1.3.2.js
10
+ public/javascripts/jquery.protify-0.3.js
11
+
@@ -0,0 +1,12 @@
1
+ class JellyGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file 'javascripts/jelly.js', "public/javascripts/jelly.js"
5
+ m.file 'javascripts/ajax_with_jelly.js', "public/javascripts/ajax_with_jelly.js"
6
+ m.directory('public/javascripts/jquery')
7
+ m.file 'javascripts/jquery/jquery-1.3.2.js', "public/javascripts/jquery/jquery-1.3.2.js"
8
+ m.file 'javascripts/jquery/jquery.protify-0.3.js', "public/javascripts/jquery/jquery.protify-0.3.js"
9
+ m.directory('public/javascripts/pages')
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,26 @@
1
+ (function($) {
2
+ $.ajaxWithJelly = function(params) {
3
+ $.ajax($.ajaxWithJelly.params(params));
4
+ };
5
+
6
+ $.ajaxWithJelly.params = function(otherParams) {
7
+ otherParams = otherParams || {};
8
+
9
+ if (otherParams.type && otherParams.type != "GET") {
10
+ otherParams['data'] = $.extend(otherParams['data'], {
11
+ authenticity_token: window._token
12
+ });
13
+ }
14
+ return $.extend({
15
+ dataType: 'json',
16
+ cache: false,
17
+ success : $.ajaxWithJelly.onSuccess
18
+ }, otherParams);
19
+ };
20
+
21
+ $.ajaxWithJelly.onSuccess = function(json) {
22
+ var context = json.on ? eval(json.on) : page;
23
+ context[json.method].apply(context, json.arguments);
24
+ return true;
25
+ };
26
+ })(jQuery);
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Jelly. a sweet unobtrusive javascript framework
3
+ * for jQuery and Rails
4
+ *
5
+ * version 0.5
6
+ *
7
+ * Copyright (c) 2009 Pivotal Labs
8
+ * Licensed under the MIT license.
9
+ *
10
+ * * Date: 2009-07-20 9:50:50 (Mon, 20 Jul 2009)
11
+ *
12
+ */
13
+
14
+ var Jelly = new Object();
15
+ Jelly.all = {};
16
+
17
+ Jelly.add = function(name) {
18
+ var page = new Jelly.Page(name);
19
+ for(var i=1; i < arguments.length; i++) {
20
+ $.extend(page, arguments[i]);
21
+ }
22
+ return page;
23
+ };
24
+
25
+ var page;
26
+ Jelly.activatePage = function(controllerName, actionName) {
27
+ page = Jelly.all[controllerName] || new Jelly.Page(controllerName);
28
+ $(document).ready(function() {
29
+ Jelly.Page.all();
30
+ if(page.all) page.all();
31
+ if(page[actionName]) page[actionName].call(page);
32
+ page.loaded = true;
33
+ });
34
+ };
35
+
36
+ Jelly.Page = function(name) {
37
+ this.name = name;
38
+ Jelly.all[name] = this;
39
+ };
40
+
41
+ Jelly.Page.all = function() {
42
+ $.protify(Jelly.Page.components).each(function(componentAndArgs) {
43
+ var component = componentAndArgs[0];
44
+ var args = componentAndArgs[1] || [];
45
+ if(component.init) component.init.apply(component, args);
46
+ });
47
+ };
48
+ Jelly.Page.components = [];
49
+ Jelly.Page.prototype.loaded = false;
50
+ Jelly.Page.prototype.documentHref = function() {
51
+ return document.location.href;
52
+ };
53
+ Jelly.Page.prototype.on_redirect = function(location){
54
+ top.location.href = location;
55
+ };
56
+
57
+ Jelly.Page.prototype.attach = function(component, args) {
58
+ var methodNames = [];
59
+ // TODO: Figure out a better way to do this.
60
+ for(var methodName in component.pageMixin) {
61
+ methodNames.push(methodName);
62
+ }
63
+ var self = this;
64
+ $.protify(methodNames).each(function(methodName) {
65
+ self[methodName] = function() {
66
+ if(this['before_' + methodName]) {
67
+ this['before_' + methodName].apply(this, arguments);
68
+ }
69
+ var returnValue = component.pageMixin[methodName].apply(this, arguments);
70
+ if(this['after_' + methodName]) {
71
+ this['after_' + methodName].apply(this, arguments);
72
+ }
73
+ return returnValue;
74
+ };
75
+ });
76
+ Jelly.Page.components.push([component, args]);
77
+ };