ajaxlibs 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.document +5 -0
  2. data/.gitignore +21 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +97 -0
  5. data/Rakefile +45 -0
  6. data/VERSION +1 -0
  7. data/lib/ajaxlibs/constants.rb +3 -0
  8. data/lib/ajaxlibs/exceptions.rb +8 -0
  9. data/lib/ajaxlibs/includes_helper.rb +90 -0
  10. data/lib/ajaxlibs/libraries/jquery.rb +10 -0
  11. data/lib/ajaxlibs/libraries/jqueryui.rb +16 -0
  12. data/lib/ajaxlibs/libraries/prototype.rb +5 -0
  13. data/lib/ajaxlibs/libraries/scriptaculous.rb +9 -0
  14. data/lib/ajaxlibs/library.rb +73 -0
  15. data/lib/ajaxlibs/versions_tools.rb +16 -0
  16. data/lib/ajaxlibs.rb +34 -0
  17. data/public/jquery/1.2.3/jquery.js +3408 -0
  18. data/public/jquery/1.2.6/jquery.js +3549 -0
  19. data/public/jquery/1.3.0/jquery.js +4241 -0
  20. data/public/jquery/1.3.1/jquery.js +4241 -0
  21. data/public/jquery/1.3.2/jquery.js +4376 -0
  22. data/public/jquery/1.4.0/jquery.js +5999 -0
  23. data/public/jquery/1.4.1/jquery.js +6078 -0
  24. data/public/jquery/1.4.2/jquery.js +6240 -0
  25. data/public/jqueryui/1.5.2/jquery-ui.js +7598 -0
  26. data/public/jqueryui/1.5.3/jquery-ui.js +7616 -0
  27. data/public/jqueryui/1.6/jquery-ui.js +8862 -0
  28. data/public/jqueryui/1.7.0/jquery-ui.js +9049 -0
  29. data/public/jqueryui/1.7.1/jquery-ui.js +9074 -0
  30. data/public/jqueryui/1.7.2/jquery-ui.js +9133 -0
  31. data/public/mootools/1.1.1/mootools.js +7102 -0
  32. data/public/mootools/1.1.2/mootools.js +7102 -0
  33. data/public/mootools/1.2.1/mootools.js +3946 -0
  34. data/public/mootools/1.2.2/mootools.js +4003 -0
  35. data/public/mootools/1.2.3/mootools.js +4036 -0
  36. data/public/mootools/1.2.4/mootools.js +4329 -0
  37. data/public/prototype/1.6.0.2/prototype.js +4221 -0
  38. data/public/prototype/1.6.0.3/prototype.js +4320 -0
  39. data/public/prototype/1.6.1.0/prototype.js +4874 -0
  40. data/public/scriptaculous/1.8.1/builder.js +136 -0
  41. data/public/scriptaculous/1.8.1/controls.js +965 -0
  42. data/public/scriptaculous/1.8.1/dragdrop.js +974 -0
  43. data/public/scriptaculous/1.8.1/effects.js +1122 -0
  44. data/public/scriptaculous/1.8.1/scriptaculous.js +58 -0
  45. data/public/scriptaculous/1.8.1/slider.js +275 -0
  46. data/public/scriptaculous/1.8.1/sound.js +55 -0
  47. data/public/scriptaculous/1.8.2/builder.js +136 -0
  48. data/public/scriptaculous/1.8.2/controls.js +965 -0
  49. data/public/scriptaculous/1.8.2/dragdrop.js +975 -0
  50. data/public/scriptaculous/1.8.2/effects.js +1130 -0
  51. data/public/scriptaculous/1.8.2/scriptaculous.js +60 -0
  52. data/public/scriptaculous/1.8.2/slider.js +275 -0
  53. data/public/scriptaculous/1.8.2/sound.js +55 -0
  54. data/public/scriptaculous/1.8.3/builder.js +136 -0
  55. data/public/scriptaculous/1.8.3/controls.js +965 -0
  56. data/public/scriptaculous/1.8.3/dragdrop.js +974 -0
  57. data/public/scriptaculous/1.8.3/effects.js +1123 -0
  58. data/public/scriptaculous/1.8.3/scriptaculous.js +68 -0
  59. data/public/scriptaculous/1.8.3/slider.js +275 -0
  60. data/public/scriptaculous/1.8.3/sound.js +59 -0
  61. data/spec/includes_helper_spec.rb +139 -0
  62. data/spec/library_spec.rb +84 -0
  63. data/spec/spec.opts +1 -0
  64. data/spec/spec_helper.rb +14 -0
  65. data/spec/versions_tools_spec.rb +27 -0
  66. metadata +143 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -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 Fabien Jakimowicz
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.
data/README.rdoc ADDED
@@ -0,0 +1,97 @@
1
+ = Ajaxlibs
2
+
3
+ == Description
4
+
5
+ Ajaxlibs provides helpers to load various javascript libraries, specifying version number, locally served or using google CDN.
6
+
7
+ In development environment, it provides local javascript files for the development server. In production environment, it will automatically use google CDN to serve your standard javascript libraries.
8
+
9
+ You can specify a specific version of each library to use, or the latest available.
10
+
11
+ == Examples
12
+
13
+ === simple library load, under the development environment
14
+
15
+ <%= ajaxlibs_include :jquery %>
16
+
17
+ will produce, under the development environment :
18
+
19
+ <script src="/javascripts/ajaxlibs/jquery/1.4.2/jquery.js?1267013480" type="text/javascript"></script>
20
+
21
+ and in production :
22
+
23
+ <script type="text/javascript" src="http://www.google.com/jsapi"></script>
24
+ <script type="text/javascript">
25
+ //<![CDATA[
26
+ google.load('jquery', '1.4.2');
27
+ //]]>
28
+ </script>
29
+
30
+ === multiple libraries load
31
+
32
+ <%= ajaxlibs_include :jquery, :jqueryui %>
33
+
34
+ in development environment :
35
+
36
+ <script src="/javascripts/ajaxlibs/jquery/1.4.2/jquery.js?1267013480" type="text/javascript"></script>
37
+ <script src="/javascripts/ajaxlibs/jqueryui/1.7.2/jqueryui.js" type="text/javascript"></script>
38
+
39
+ this time in production :
40
+
41
+ <script type="text/javascript" src="http://www.google.com/jsapi"></script>
42
+ <script type="text/javascript">
43
+ //<![CDATA[
44
+ google.load('jquery', '1.4.2');
45
+ google.load('jqueryui', '1.7.2');
46
+ //]]>
47
+ </script>
48
+
49
+ === forcing a version
50
+
51
+ <%= ajaxlibs_include :prototype, :version => '1.6.0.3' %>
52
+
53
+ will produce :
54
+
55
+ <script src="/javascripts/ajaxlibs/prototype/1.6.0.3/prototype.js?1267013480" type="text/javascript"></script>
56
+
57
+ === automatic dependencies
58
+
59
+ <%= ajaxlibs_include :scriptaculous %>
60
+
61
+ will produce :
62
+
63
+ <script src="/javascripts/ajaxlibs/prototype/1.6.1.0/prototype.js?1267013480" type="text/javascript"></script>
64
+ <script src="/javascripts/ajaxlibs/scriptaculous/1.8.3/scriptaculous.js?1267013481" type="text/javascript"></script>
65
+
66
+ == Installation
67
+
68
+ First, install gem on your system and servers. You can also specify it as a dependency on your rails enviroment :
69
+
70
+ config.gem 'ajaxlibs'
71
+
72
+ Then, just restart your rails server to copy files from gem the public directory of your rails application.
73
+
74
+ Finally, include some javascript libraries to your views or layouts :
75
+
76
+ <%= ajaxlibs_include :scriptaculous %>
77
+
78
+ == TODO
79
+
80
+ * support more javascript libraries : available though Google CDN (mootools, ...), not available through Google CDN (jrails, ...), available only from google (google maps, ...).
81
+ * support plain/minified versions.
82
+ * provide a rake task to copy only required javascript libraries.
83
+ * automatic support for jrails.
84
+
85
+ == Note on Patches/Pull Requests
86
+
87
+ * Fork the project.
88
+ * Make your feature addition or bug fix.
89
+ * Add tests for it. This is important so I don't break it in a
90
+ future version unintentionally.
91
+ * Commit, do not mess with rakefile, version, or history.
92
+ (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)
93
+ * Send me a pull request. Bonus points for topic branches.
94
+
95
+ == Copyright
96
+
97
+ Copyright (c) 2010 Fabien Jakimowicz. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "ajaxlibs"
8
+ gem.summary = %Q{helper to load various javascript libraries, locally or through google CDN}
9
+ gem.description = %Q{ajaxlibs provides helpers to load various javascript libraries, specifying version number, locally served or using google CDN}
10
+ gem.email = "fabien@jakimowicz.com"
11
+ gem.homepage = "http://github.com/jakimowicz/ajaxlibs"
12
+ gem.authors = ["Fabien Jakimowicz"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
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 'spec/rake/spectask'
22
+ Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ spec.libs << 'lib' << 'spec'
24
+ spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ end
26
+
27
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ spec.libs << 'lib' << 'spec'
29
+ spec.pattern = 'spec/**/*_spec.rb'
30
+ spec.rcov = true
31
+ end
32
+
33
+ task :spec => :check_dependencies
34
+
35
+ task :default => :spec
36
+
37
+ require 'rake/rdoctask'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "ajaxlibs #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,3 @@
1
+ module Ajaxlibs
2
+ GoogleJSAPI = "http://www.google.com/jsapi"
3
+ end
@@ -0,0 +1,8 @@
1
+ class Ajaxlibs::Exception < StandardError
2
+ end
3
+
4
+ class Ajaxlibs::Exception::LibraryNotFound < Ajaxlibs::Exception
5
+ end
6
+
7
+ class Ajaxlibs::Exception::VersionNotFound < Ajaxlibs::Exception
8
+ end
@@ -0,0 +1,90 @@
1
+ module Ajaxlibs::IncludesHelper
2
+ # Returns an html script tag for each javascript library name provided.
3
+ # By default, javascript files are loaded locally for development and test environment,
4
+ # and through Google CDN on production environment. Basic dependencies are automatically handled.
5
+ #
6
+ # == Options
7
+ # * <tt>version</tt> : specify the version to use for each library
8
+ # * <tt>local</tt> : if true, always serve file locally, if false, use Google CDN
9
+ # * <tt>remote</tt> : if false, always serve file locally, if true, use Google CDN
10
+ #
11
+ # == Exceptions
12
+ # * <tt>Ajaxlibs::Exception::LibraryNotFound</tt> : raised if one or more of the given library is not available
13
+ # * <tt>Ajaxlibs::Exception::VersionNotFound</tt> : raised if given version is not available for this/these library/libraries
14
+ #
15
+ # == Examples
16
+ # * Simple library load, under the development environment
17
+ # ajaxlibs_include :jquery
18
+ # <script src="/javascripts/ajaxlibs/jquery/1.4.2/jquery.js?1267013480" type="text/javascript"></script>
19
+ #
20
+ # ajaxlibs_include :jquery, :jqueryui
21
+ # <script src="/javascripts/ajaxlibs/jquery/1.4.2/jquery.js?1267013480" type="text/javascript"></script>
22
+ # <script src="/javascripts/ajaxlibs/jqueryui/1.7.2/jqueryui.js" type="text/javascript"></script>
23
+ #
24
+ # * Same examples as above, this time in production
25
+ # ajaxlibs_include :jquery
26
+ # <script src="/javascripts/ajaxlibs/jquery/1.4.2/jquery.js?1267013480" type="text/javascript"></script>
27
+ #
28
+ # ajaxlibs_include :jquery, :jqueryui
29
+ # <script type="text/javascript" src="http://www.google.com/jsapi"></script>
30
+ # <script type="text/javascript">
31
+ # //<![CDATA[
32
+ # google.load('jquery', '1.4.2');
33
+ # google.load('jqueryui', '1.7.2');
34
+ # //]]>
35
+ # </script>
36
+ #
37
+ # * Specifying version
38
+ # ajaxlibs_include :prototype, :version => '1.6.0.3'
39
+ # <script src="/javascripts/ajaxlibs/prototype/1.6.0.3/prototype.js?1267013480" type="text/javascript"></script>
40
+ #
41
+ # * Automatic dependencies
42
+ # ajaxlibs_include :scriptaculous
43
+ # <script src="/javascripts/ajaxlibs/prototype/1.6.1.0/prototype.js?1267013480" type="text/javascript"></script>
44
+ # <script src="/javascripts/ajaxlibs/scriptaculous/1.8.3/scriptaculous.js?1267013481" type="text/javascript"></script>
45
+ #
46
+ def ajaxlibs_include(*args)
47
+ options = (Hash === args.last) ? args.pop : {}
48
+
49
+ includes = args.collect {|library| javascript_include_library library, options}.compact.join("\n")
50
+
51
+ if options[:local] === false or RAILS_ENV == 'production'
52
+ <<-EOB
53
+ <script type="text/javascript" src="#{Ajaxlibs::GoogleJSAPI}"></script>
54
+ #{javascript_tag includes}
55
+ EOB
56
+ else
57
+ includes
58
+ end
59
+ end
60
+
61
+ private
62
+ def javascript_include_library(library, options)
63
+ library = library.to_sym
64
+
65
+ @included_javascript_libraries ||= []
66
+
67
+ return if @included_javascript_libraries.include?(library)
68
+
69
+ version = options.delete(:version)
70
+ ajaxlib = Ajaxlibs::Library.by_name(library)
71
+
72
+ result = []
73
+
74
+ # Handle dependencies between libraries
75
+ if ajaxlib.requires and !@included_javascript_libraries.include?(ajaxlib.requires.to_sym)
76
+ result << javascript_include_library(ajaxlib.requires, options)
77
+ end
78
+
79
+ @included_javascript_libraries << library
80
+
81
+ # Javascript load code
82
+ if options[:local] === true or RAILS_ENV != 'production'
83
+ result << javascript_include_tag(ajaxlib.local_path(version))
84
+ else
85
+ result << ajaxlib.google_cdn_load_code(version)
86
+ end
87
+
88
+ result.join("\n")
89
+ end
90
+ end
@@ -0,0 +1,10 @@
1
+ class Ajaxlibs::Library::Jquery < Ajaxlibs::Library
2
+ Versions = ['1.2.3',
3
+ '1.2.6',
4
+ '1.3.0',
5
+ '1.3.1',
6
+ '1.3.2',
7
+ '1.4.0',
8
+ '1.4.1',
9
+ '1.4.2']
10
+ end
@@ -0,0 +1,16 @@
1
+ class Ajaxlibs::Library::Jqueryui < Ajaxlibs::Library
2
+ Versions = ['1.5.2',
3
+ '1.5.3',
4
+ '1.6' ,
5
+ '1.7.0',
6
+ '1.7.1',
7
+ '1.7.2']
8
+
9
+ def requires #:nodoc:
10
+ 'jquery'
11
+ end
12
+
13
+ def filename #:nodoc:
14
+ "jquery-ui"
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ class Ajaxlibs::Library::Prototype < Ajaxlibs::Library
2
+ Versions = ['1.6.0.2',
3
+ '1.6.0.3',
4
+ '1.6.1.0']
5
+ end
@@ -0,0 +1,9 @@
1
+ class Ajaxlibs::Library::Scriptaculous < Ajaxlibs::Library
2
+ Versions = ['1.8.1',
3
+ '1.8.2',
4
+ '1.8.3']
5
+
6
+ def requires #:nodoc:
7
+ 'prototype'
8
+ end
9
+ end
@@ -0,0 +1,73 @@
1
+ require 'ajaxlibs/exceptions'
2
+ require 'ajaxlibs/versions_tools'
3
+
4
+ # Represents an ajaxlib library, regroups available version of a particular library
5
+ # and a few functions to generate either a filepath to local library or javascript loading code
6
+ # for Google CDN.
7
+ class Ajaxlibs::Library
8
+ # Available versions for this library.
9
+ Versions = []
10
+
11
+ @@subclasses = {}
12
+
13
+ def self.inherited(child) #:nodoc:
14
+ @@subclasses[child.library_name.to_sym] = child
15
+ end
16
+
17
+ # Returns all available libraries (instance of Ajaxlibs::Library).
18
+ def self.all
19
+ @@subclasses.values
20
+ end
21
+
22
+ # Search a specific library by its name, could by either a string or a symbol.
23
+ def self.by_name(name)
24
+ @@subclasses[name.to_sym].new
25
+ rescue NoMethodError
26
+ raise Ajaxlibs::Exception::LibraryNotFound
27
+ end
28
+
29
+ # Library name based on class name
30
+ def self.library_name
31
+ name.match(/::(\w+)$/)[1].downcase
32
+ end
33
+
34
+ # Returns requirements for a library (for example, prototype for scriptaculous)
35
+ def requires
36
+ nil
37
+ end
38
+
39
+ # Library name based on class name
40
+ def library_name
41
+ self.class.library_name
42
+ end
43
+
44
+ # Javascript library filename, can be different from library_name (jqueryui / jquery-ui for example)
45
+ def file_name
46
+ library_name
47
+ end
48
+
49
+ # Search for the latest version available using given Versions
50
+ def latest_version
51
+ self.class::Versions.max {|a, b| Ajaxlibs::VersionsTools.compare a, b}
52
+ end
53
+
54
+ # Local path for a particular version, or the latest if given version is nil.
55
+ def local_path(version = nil)
56
+ File.join('ajaxlibs', library_name, check_version_or_latest_version(version), file_name)
57
+ end
58
+
59
+ # Javascript load code through google jsapi for a particular version, or the latest if given version is nil.
60
+ def google_cdn_load_code(version = nil)
61
+ "google.load('#{library_name}', '#{check_version_or_latest_version(version)}');"
62
+ end
63
+
64
+ private
65
+ # Checks if given version is available for this library,
66
+ # raises Ajaxlibs::Exception::VersionNotFound if not and returns it.
67
+ # Passing a nil value will returns the latest available version
68
+ def check_version_or_latest_version(version = nil)
69
+ version ||= latest_version
70
+ raise Ajaxlibs::Exception::VersionNotFound unless self.class::Versions.include?(version)
71
+ version
72
+ end
73
+ end
@@ -0,0 +1,16 @@
1
+ # Tools to manipulate versions strings (for example 1.2.3)
2
+ module Ajaxlibs::VersionsTools
3
+ # Compare two versions and returns either :
4
+ # * 1 if a > b
5
+ # * 0 if a == b
6
+ # * -1 if a < b
7
+ def self.compare(a, b)
8
+ return 0 if a == b
9
+ splitted_a, splitted_b = a.split('.'), b.split('.')
10
+ splitted_a.each_with_index do |node, i|
11
+ break if node < splitted_b[i]
12
+ return 1 if node > splitted_b[i]
13
+ end
14
+ return -1
15
+ end
16
+ end
data/lib/ajaxlibs.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'ajaxlibs/constants'
2
+ require 'ajaxlibs/exceptions'
3
+ require 'ajaxlibs/versions_tools'
4
+ require 'ajaxlibs/library'
5
+ require 'ajaxlibs/libraries/jquery'
6
+ require 'ajaxlibs/libraries/jqueryui'
7
+ require 'ajaxlibs/libraries/prototype'
8
+ require 'ajaxlibs/libraries/scriptaculous'
9
+ require 'ajaxlibs/includes_helper'
10
+
11
+ if Object.const_defined?(:ActionView)
12
+ # Make Ajaxlibs helpers available in views
13
+ ActionView::Base.send(:include, Ajaxlibs::IncludesHelper)
14
+
15
+ # Copy all available js libraries to rails public folder
16
+ if Object.const_defined?(:Rails) and File.directory?(File.join(Rails.root, 'public'))
17
+ ajaxlibs_js_path = File.join(Rails.root, 'public', 'javascripts', 'ajaxlibs')
18
+
19
+ # We do not have already copied local javascript files, copying them right away
20
+ # OPTIMIZE : we should copy only necessary libraries
21
+ unless File.directory?(ajaxlibs_js_path)
22
+ FileUtils.mkdir_p(ajaxlibs_js_path)
23
+ Ajaxlibs::Library.all.each do |library|
24
+ library::Versions.each do |version|
25
+ source = File.join(File.dirname(__FILE__), '..', 'public', library.library_name, version, '*.*')
26
+ destination = File.join(ajaxlibs_js_path, library.library_name, version)
27
+ FileUtils.mkdir_p(destination)
28
+ FileUtils.cp(Dir.glob(source), destination)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ end