rails-async 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adam Bachman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,47 @@
1
+ This is Alpha, naive, itch scratching bozo code.
2
+
3
+ Good luck.
4
+
5
+ ## Install
6
+
7
+ Get the gem:
8
+
9
+ gem install rails-async
10
+
11
+ Or add it to your gemfile:
12
+
13
+ gem "rails-async", :require => "async"
14
+
15
+ ## Setup
16
+
17
+
18
+
19
+ ## Use
20
+
21
+ Slow page:
22
+
23
+ <h1>Accounts</h1>
24
+ <ul>
25
+ <% sleep 3.8 %>
26
+ <% @accounts.each do |account| %>
27
+ <li>
28
+ <%= render 'account', :account => account %>
29
+ </li>
30
+ <% end %>
31
+ </ul>
32
+
33
+ Fast page:
34
+
35
+ <h1>Accounts</h1>
36
+ <ul>
37
+ <% asynchronously do %>
38
+ <% sleep 3.8 %>
39
+ <% @accounts.each do |account| %>
40
+ <li>
41
+ <%= render 'account', :account => account %>
42
+ </li>
43
+ <% end %>
44
+ <% end %>
45
+ </ul>
46
+
47
+ ##
@@ -0,0 +1,17 @@
1
+ = async-rails
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Adam Bachman. See LICENSE for details.
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rails-async"
8
+ gem.summary = %Q{Simple async blocks.}
9
+ gem.description = %Q{Don't wait for your pages to render.}
10
+ gem.email = "adam.bachman@gmail.com"
11
+ gem.homepage = "http://github.com/abachman/rails-async"
12
+ gem.authors = ["Adam Bachman"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
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
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rails-async #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,7 @@
1
+ puts "CONTROLLER"
2
+ class AsyncController < ApplicationController
3
+ def show
4
+ @as_cache = AsyncCache.read(params[:tag])
5
+ render :json => {:content => @as_cache}
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ puts "HELPER"
2
+ module AsyncHelper
3
+ def asynchronously
4
+ id = Time.new.to_i
5
+
6
+ # insert javascript callback
7
+ safe_concat %{
8
+ <div id="#{id}">
9
+ Loading... #{ image_tag('spinner.gif') }
10
+ </div>
11
+ <script>
12
+ $(function () {
13
+ $.getJSON(
14
+ "/async/#{id}",
15
+ function (data) {
16
+ $('##{id}').html(data.content)
17
+ }
18
+ );
19
+ });
20
+ </script>
21
+ }
22
+
23
+ connection = ActiveRecord::Base.remove_connection
24
+ child = Process.fork do
25
+ begin
26
+ ActiveRecord::Base.establish_connection((connection || {}).merge({:allow_concurrency => true})) if defined?(ActiveRecord)
27
+ # anything rendered within capture will be stored in _content
28
+ _content = capture do
29
+ yield
30
+ end
31
+ AsyncCache.write(id, _content)
32
+ ensure
33
+ ActiveRecord::Base.remove_connection
34
+ end
35
+ end
36
+ ensure
37
+ ActiveRecord::Base.establish_connection((connection || {}).merge({:allow_concurrency => true}))
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ puts "MODEL"
2
+ class AsyncCache
3
+ def self.read(tag)
4
+ @content = nil
5
+ status = Timeout.timeout(5) do
6
+ until File.exists?(path(tag))
7
+ sleep 0.2
8
+ end
9
+ @content = File.read(path(tag))
10
+ end
11
+ Rails.logger.debug("Returned from timeout with status #{ status.inspect }")
12
+
13
+ @content
14
+ end
15
+
16
+ def self.write(tag, content)
17
+ if !File.exists?(parent)
18
+ setup
19
+ end
20
+
21
+ File.open(path(tag), 'w') do |f|
22
+ f.write(content)
23
+ end
24
+ end
25
+
26
+ private
27
+ def self.setup
28
+ FileUtils.mkdir_p parent
29
+ end
30
+
31
+ def self.parent
32
+ File.join(Rails.root, "tmp", "async")
33
+ end
34
+
35
+ def self.path tag
36
+ File.join(parent, tag.to_s)
37
+ end
38
+ end
39
+
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do |map|
2
+ match 'async/:tag' => 'async#show'
3
+ end
@@ -0,0 +1,4 @@
1
+ module Async
2
+ require 'async/engine' if defined?(Rails)
3
+ end
4
+
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+ require 'async'
3
+
4
+ # puts "ENGINE UP"
5
+
6
+ module Async
7
+ class Engine < Rails::Engine
8
+ engine_name :async
9
+ end
10
+ end
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rails-async}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adam Bachman"]
12
+ s.date = %q{2010-09-19}
13
+ s.description = %q{Don't wait for your pages to render.}
14
+ s.email = %q{adam.bachman@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md",
18
+ "README.rdoc"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".gitignore",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "app/controllers/async_controller.rb",
28
+ "app/helpers/async_helper.rb",
29
+ "app/models/async_cache.rb",
30
+ "config/routes.rb",
31
+ "lib/async.rb",
32
+ "lib/async/engine.rb",
33
+ "rails-async.gemspec",
34
+ "test/helper.rb",
35
+ "test/test_async-rails.rb"
36
+ ]
37
+ s.homepage = %q{http://github.com/abachman/rails-async}
38
+ s.rdoc_options = ["--charset=UTF-8"]
39
+ s.require_paths = ["lib"]
40
+ s.rubygems_version = %q{1.3.7}
41
+ s.summary = %q{Simple async blocks.}
42
+ s.test_files = [
43
+ "test/helper.rb",
44
+ "test/test_async-rails.rb"
45
+ ]
46
+
47
+ if s.respond_to? :specification_version then
48
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
49
+ s.specification_version = 3
50
+
51
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
52
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ end
56
+ else
57
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -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 'async-rails'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestAsyncRails < 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
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-async
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Adam Bachman
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-19 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ description: Don't wait for your pages to render.
34
+ email: adam.bachman@gmail.com
35
+ executables: []
36
+
37
+ extensions: []
38
+
39
+ extra_rdoc_files:
40
+ - LICENSE
41
+ - README.md
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.rdoc
48
+ - Rakefile
49
+ - VERSION
50
+ - app/controllers/async_controller.rb
51
+ - app/helpers/async_helper.rb
52
+ - app/models/async_cache.rb
53
+ - config/routes.rb
54
+ - lib/async.rb
55
+ - lib/async/engine.rb
56
+ - rails-async.gemspec
57
+ - test/helper.rb
58
+ - test/test_async-rails.rb
59
+ - README.md
60
+ has_rdoc: true
61
+ homepage: http://github.com/abachman/rails-async
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options:
66
+ - --charset=UTF-8
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple async blocks.
92
+ test_files:
93
+ - test/helper.rb
94
+ - test/test_async-rails.rb