jquery_tag 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,6 @@
1
1
  pkg/*
2
+ doc
3
+ .yardoc
2
4
  *.gem
3
5
  .bundle
4
6
  .rvmrc
data/README.markdown CHANGED
@@ -1,22 +1,36 @@
1
1
  # Jquery_tag
2
- a Rails Helper for togglin' between a local jquery.js file located in /public/javascripts/jquery.js or the hosted script on Google's CDN.
3
- Also supports the same behaviour for a local jquery-ui.js file or the CDN version.
2
+ A Helper gem for both Rails and Sinatra for toggling between a local jquery.js file or the hosted script on Google's CDN based on your application environment, avoiding network dependency during development and bandwidth usage when your application goes live.
3
+ It also supports the same behavior for a loading the jQuery UI script.
4
4
 
5
5
  To know more about the benefits of using a CDN for your common Javascript frameworks, check [this Chris Coyier post](http://css-tricks.com/google-cdn-naming/) at [CSS-Tricks](http://css-tricks.com).
6
6
 
7
7
  ## Installation
8
- Just add the `jquery_tag` to your Gemfile and run `bundle install`.
9
- To download the latest jQuery scripts, check the [jquery-rails](https://github.com/indirect/jquery-rails) gem.
8
+ If you're using [Bundler](http://gembundler.com), just add the `jquery_tag` to your Gemfile and run `bundle install`. If not, `gem install jquery_tag` and `require 'jquery_tag'` inside your application code.
9
+
10
+ To download the latest jQuery script to your application just run `jquery_tag` command inside your Terminal and the script will be downloaded to `public/javascripts/jquery.js`. You can check the available options running it with the `-h` flag.
10
11
 
11
12
  ## Usage
12
- Inside your views
13
+ Inside your views, you can just call the `jquery_tag` method.
13
14
 
14
15
  <%= jquery_tag %>
15
16
 
16
- Accepted options:
17
+ It accepts a some configuration options by using the following symbols:
17
18
 
18
- :version # Overrides the script version on the CDN URL. Defaults do '1.4.4'
19
+ :version # Overrides the script version on the CDN URL. Defaults do '1.5.0'
19
20
  :file # Path for the local script. Defaults do 'jquery.js'
20
- :ui # Loads the jquery-ui file. Accepts a true value for loading a 'jquery-ui.js' file or a String for the local path.
21
+ :ui # Loads jQuery UI. Accepts a true value for loading a 'jquery-ui.js' file or a String for the local path.
22
+
23
+ Any other arguments will be passed along to the `javascript_include_tag` helper, if inside a Rails application. The [Sinatra Helper](https://github.com/lucasmazza/jquery-tag/blob/master/lib/jquery_tag/helpers/sinatra_helper.rb) currently ignores any extra option.
24
+
25
+ ## Sinatra
26
+ To use the `jquery_tag` on your [Sinatra](http://www.sinatrarb.com/) applications, just include the `JqueryTag::SinatraHelper` on your application
27
+
28
+ require 'jquery_tag' # if your not using Bundler.
29
+
30
+ class Application < Sinatra::Base
31
+ helpers do
32
+ include JqueryTag::SinatraHelper
33
+ end
34
+ end
21
35
 
22
- Any other arguments will be passed along to the `javascript_include_tag` helper.
36
+ Just call the `jquery_tag` helper inside your views or layouts, as in the example above. Your local scripts should be located inside a `./public/javascripts/` folder (or whatever the Sinatra public path is configured on your application).
data/bin/jquery_tag ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby -*-
3
+ require 'jquery_tag/installer'
4
+
5
+ JqueryTag::Installer.new(ARGV.dup).start
data/jquery_tag.gemspec CHANGED
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Lucas Mazza"]
9
9
  s.email = ["luc4smazza@gmail.com"]
10
10
  s.homepage = "http://rubygems.org/gems/jquery_tag"
11
- s.summary = "jQuery script tag helper for Rails"
11
+ s.summary = "jQuery script tag helper for Rails and Sinatra"
12
12
  s.description = "Helper gem that toggles between local jquery script and Google CDN version based on your app environment"
13
13
 
14
14
  s.required_rubygems_version = ">= 1.3.6"
@@ -0,0 +1,67 @@
1
+ module JqueryTag # :nodoc:
2
+ ## +jQueryTag::Helper+ is the abstract base for real Helper module
3
+ ## that should be used.
4
+ ##
5
+ ## This Module is responsible for the argument parsing and the public api
6
+ ## of the +jquery_tag+ method, that you should use inside your views and layouts.
7
+ ##
8
+ ## == Usage
9
+ ## inside your application.html.erb, just above the closing body tag.
10
+ ## <%= jquery_tag %>
11
+ ## During development, the given helper will render something like this:
12
+ ## <script type="text/javascript" src="/javascripts/jquery.js"></script>
13
+ ## Your application will be using a local version of jQuery, avoiding any network usage. Once you
14
+ ## shift to production mode, the ouput will be:
15
+ ## <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
16
+ ## Reducing your server bandwidth load and sharing the same cached file from many other sites.
17
+ ##
18
+ ## == Dependencies
19
+ ## +jQueryTag::Helper+ relies on two other methods that should be implemented:
20
+ ##
21
+ ## * +production?+ - it should return +true+ if the application is on production mode. Otherwise, false.
22
+ ## * +javascript_tag(paths, options)+ - responsible for building the tags for the given paths array. Any other option should be passed on the second argument, a options hash.
23
+ module Helper
24
+
25
+ ## Builds the proper script tags for a suitable url or path for both jQuery or jQuery UI scripts.
26
+ ##
27
+ ## @option :file [String] an alternative path for the local jquery.js file. Defaults to `jquery.js`
28
+ ## @option :version [String] tells the helper to target a different version of jQuery hosted on Google's CDN. Defaults to JqueryTag.version
29
+ ## @option :ui [String, TrueClass] loads the jQuery UI File. Can be a [TrueClass] or a [String], overriding the local path for the script.
30
+ ## Any other argument will be delivered to the +javascript_tag+ method.
31
+ ##
32
+ ## @return [String] the html tags to include scripts
33
+ def jquery_tag(*arguments)
34
+ options = extract_options(arguments)
35
+ paths = []
36
+ paths << jquery_file(options[:file], options[:version])
37
+ paths << jquery_ui_file(options[:ui]) if options[:ui]
38
+
39
+ javascript_tag(paths, arguments)
40
+ end
41
+
42
+ private
43
+
44
+ def extract_options(arguments)
45
+ keys = [:file, :version, :ui]
46
+ if arguments.last.is_a?(Hash)
47
+ options = Hash[keys.map { |o| [o, arguments.last.delete(o)] }]
48
+ arguments.pop if arguments.last.empty?
49
+ else
50
+ options = {}
51
+ end
52
+ options
53
+ end
54
+
55
+ def jquery_file(path, version)
56
+ path ||= 'jquery.js'
57
+ version ||= JqueryTag.version
58
+ production? ? "//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js" : path
59
+ end
60
+
61
+ def jquery_ui_file(path)
62
+ path = path.is_a?(String) ? path : "jquery-ui.js"
63
+ version = JqueryTag.ui_version
64
+ production? ? "//ajax.googleapis.com/ajax/libs/jqueryui/#{version}/jquery-ui.min.js" : path
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,22 @@
1
+ module JqueryTag # :nodoc:
2
+ ## The Helper implementation for +Ruby on Rails+ applications.
3
+ ## Will be included on +ActionView::Base+ automatically on gem loading.
4
+ module RailsHelper
5
+ include JqueryTag::Helper
6
+
7
+ ## Delegates the verification to +Rails.env+
8
+ ## @return [Boolean] true if the current Rails environment is production.
9
+ def production?
10
+ Rails.env.production?
11
+ end
12
+
13
+ ## Builds the script tags using the +javascript_included_tag+ method from Rails.
14
+ ## @param [Array] paths_or_urls The paths for the local files or the CDN urls.
15
+ ## @param [Hash] options any other option that should be delivered to +javascript_include_tag+.
16
+ ## @return [String] the concatenated html tags
17
+ def javascript_tag(paths_or_urls, options = {})
18
+ arguments = paths_or_urls + [options].flatten
19
+ javascript_include_tag(arguments)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ module JqueryTag # :nodoc:
2
+ ## The Helper implementation for +Sinatra+ applications.
3
+ ## you should manually include this Module as a helper inside your application
4
+ ## class MyApp < Sinatra::Base
5
+ ## helpers do
6
+ ## include JqueryTag::SinatraHelper
7
+ ## end
8
+ ## end
9
+ module SinatraHelper
10
+ include JqueryTag::Helper
11
+
12
+ ## Delegates the verification to Sinatra +settings+ object and check it's environment value.
13
+ ## @return [Boolean] true if the application is in the production environment.
14
+ def production?
15
+ settings.environment == :production
16
+ end
17
+
18
+ ## A Simple helper that concatenates the html for the script tags.
19
+ ## @param [Array] paths_or_urls The paths for the local files or the CDN urls.
20
+ ## @param [Hash] options the current implementation doesn't use the options hash.
21
+ ## @return [String] the concatenated html tags
22
+ def javascript_tag(paths_or_urls, options = {})
23
+ paths_or_urls.map { |path| build_tag(path) }.join
24
+ end
25
+
26
+ private
27
+ def build_tag(path_or_url)
28
+ path_or_url = "/javascripts/#{path_or_url}" unless production?
29
+ %Q{<script type="text/javascript" src="#{path_or_url}"></script>}
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,43 @@
1
+ require 'jquery_tag'
2
+ require 'optparse'
3
+ require 'open-uri'
4
+
5
+ module JqueryTag #:nodoc:
6
+ # +JqueryTag::Installer+ is the class called by the +jquery_tag+ command line
7
+ # to download the latest version of the jQuery script to your public/javascripts folder.
8
+ class Installer
9
+
10
+ attr_reader :path, :version, :arguments
11
+
12
+ def initialize(arguments)
13
+ @version = JqueryTag.version
14
+ @path = File.expand_path("public/javascripts/jquery.js")
15
+ @arguments = arguments
16
+ end
17
+
18
+ # Parses the arguments given by arguments array using +OptionParser+
19
+ # and downloads the script file.
20
+ def start
21
+ parse!
22
+ run
23
+ end
24
+
25
+ private
26
+
27
+ def parse!
28
+ OptionParser.new do |opts|
29
+ opts.banner = "Usage: jquery_tag [-v VERSION] [-p PATH]"
30
+ opts.on("-v", "--version VERSION", "Sets the target version to download. Defaults '#{JqueryTag.version}'.") { |v| @version = v }
31
+ opts.on("-p", "--path PATH", "The path to save the script file. Defaults to 'public/javascripts/jquery.js'.") { |path| @path = path }
32
+ opts.on("-h", "--help", "Outputs this message.") { puts opts; exit }
33
+ end.parse!(arguments)
34
+ end
35
+
36
+ def run
37
+ content = open("http://ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.js") { |io| io.binmode.read }
38
+ FileUtils.mkdir_p(File.dirname(path))
39
+ File.open(path, "w") { |file| file.write(content) }
40
+ puts "jQuery #{version} downloaded to #{path}"
41
+ end
42
+ end
43
+ end
@@ -1,3 +1,3 @@
1
- module JqueryTag
2
- VERSION = "0.1.3"
1
+ module JqueryTag #:nodoc:
2
+ VERSION = "0.2.0" # :nodoc:
3
3
  end
data/lib/jquery_tag.rb CHANGED
@@ -1,3 +1,12 @@
1
- require 'jquery_tag/view_helpers'
1
+ require 'jquery_tag/helper'
2
+ require 'jquery_tag/helpers/rails_helper'
3
+ require 'jquery_tag/helpers/sinatra_helper'
2
4
 
3
- ActionView::Base.send(:include, JqueryTag::ViewHelpers)
5
+ module JqueryTag # :nodoc:
6
+ ## @return [String] the current (mostly latest) version of jQuery.
7
+ def self.version; '1.5.0'; end
8
+ ## @return [String] the current (mostly latest) version of jQuery UI.
9
+ def self.ui_version; '1.8.9'; end
10
+ end
11
+
12
+ ActionView::Base.send(:include, JqueryTag::RailsHelper) if defined? Rails
@@ -1,9 +1,9 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe JqueryTag::ViewHelpers do
4
- include JqueryTag::ViewHelpers
3
+ describe JqueryTag::RailsHelper do
4
+ include JqueryTag::RailsHelper
5
5
 
6
- context "development" do
6
+ context "in development environment" do
7
7
  before { development! }
8
8
 
9
9
  it "renders a local jquery.js file" do
@@ -11,9 +11,14 @@ describe JqueryTag::ViewHelpers do
11
11
  jquery_tag
12
12
  end
13
13
 
14
+ it "delegates any other arguments and options" do
15
+ expects_include_with ['jquery.js', {:cached => true}]
16
+ jquery_tag :cached => true
17
+ end
18
+
14
19
  it "delegates any other arguments" do
15
- expects_include_with ['jquery.js', 'application.js']
16
- jquery_tag 'application.js'
20
+ expects_include_with ['jquery.js', 'other.js', {:cached => true}]
21
+ jquery_tag 'other.js', :cached => true
17
22
  end
18
23
 
19
24
  it "accepts a different path for the local jquery file" do
@@ -32,7 +37,7 @@ describe JqueryTag::ViewHelpers do
32
37
  end
33
38
  end
34
39
 
35
- context "production" do
40
+ context "in production environment" do
36
41
  before { production! }
37
42
 
38
43
  it "uses the google CDN path" do
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe JqueryTag::SinatraHelper do
4
+ include JqueryTag::SinatraHelper
5
+
6
+ context "in development environment" do
7
+ before { development! }
8
+
9
+ it "renders a local jquery.js file" do
10
+ jquery_tag.should == %Q{<script type="text/javascript" src="/javascripts/jquery.js"></script>}
11
+ end
12
+
13
+ it "delegates any other arguments"
14
+
15
+ it "accepts a different path for the local jquery file" do
16
+ jquery_tag(:file => "frameworks/jquery.min.js").should == %Q{<script type="text/javascript" src="/javascripts/frameworks/jquery.min.js"></script>}
17
+ end
18
+
19
+ it "renders the jquery-ui.js file" do
20
+ expected = %Q{<script type="text/javascript" src="/javascripts/jquery.js"></script>}
21
+ expected << %Q{<script type="text/javascript" src="/javascripts/jquery-ui.js"></script>}
22
+ jquery_tag(:ui => true).should == expected
23
+ end
24
+
25
+ it "receives a path to the jquery-ui.js file" do
26
+ expected = %Q{<script type="text/javascript" src="/javascripts/jquery.js"></script>}
27
+ expected << %Q{<script type="text/javascript" src="/javascripts/frameworks/jquery-ui.js"></script>}
28
+ jquery_tag(:ui => 'frameworks/jquery-ui.js').should == expected
29
+ end
30
+ end
31
+
32
+ context "in production environment" do
33
+ before { production! }
34
+
35
+ it "uses the google CDN path" do
36
+ expected = %Q{<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>}
37
+ jquery_tag.should == expected
38
+ end
39
+
40
+ it "accepts a different version for the jquery script" do
41
+ expected = %Q{<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.0.0/jquery.min.js"></script>}
42
+ jquery_tag(:version => '1.0.0').should == expected
43
+ end
44
+
45
+ it "uses the google CDN path for the jquery ui script" do
46
+ expected = %Q{<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>}
47
+ expected << %Q{<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>}
48
+ jquery_tag(:ui => true).should == expected
49
+ end
50
+ end
51
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,17 +1,24 @@
1
- require 'jquery_tag/view_helpers'
1
+ require 'jquery_tag'
2
2
 
3
3
  module Rails
4
4
  end
5
5
 
6
6
  module SpecHelper
7
+
8
+ def settings
9
+ @_settings ||= mock
10
+ end
11
+
7
12
  def production!
13
+ settings.stub(:environment) { :production }
8
14
  Rails.stub_chain(:env, :production?) { true }
9
15
  end
10
-
16
+
11
17
  def development!
18
+ settings.stub(:environment) { :development }
12
19
  Rails.stub_chain(:env, :production?) { false }
13
20
  end
14
-
21
+
15
22
  def expects_include_with(*args)
16
23
  should_receive(:javascript_include_tag).with(*args)
17
24
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jquery_tag
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 3
10
- version: 0.1.3
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lucas Mazza
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-03 00:00:00 -02:00
18
+ date: 2011-02-17 00:00:00 -02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -51,8 +51,8 @@ dependencies:
51
51
  description: Helper gem that toggles between local jquery script and Google CDN version based on your app environment
52
52
  email:
53
53
  - luc4smazza@gmail.com
54
- executables: []
55
-
54
+ executables:
55
+ - jquery_tag
56
56
  extensions: []
57
57
 
58
58
  extra_rdoc_files: []
@@ -62,11 +62,16 @@ files:
62
62
  - Gemfile
63
63
  - README.markdown
64
64
  - Rakefile
65
+ - bin/jquery_tag
65
66
  - jquery_tag.gemspec
66
67
  - lib/jquery_tag.rb
68
+ - lib/jquery_tag/helper.rb
69
+ - lib/jquery_tag/helpers/rails_helper.rb
70
+ - lib/jquery_tag/helpers/sinatra_helper.rb
71
+ - lib/jquery_tag/installer.rb
67
72
  - lib/jquery_tag/version.rb
68
- - lib/jquery_tag/view_helpers.rb
69
- - spec/jquery_tag_spec.rb
73
+ - spec/rails_helper_spec.rb
74
+ - spec/sinatra_helper_spec.rb
70
75
  - spec/spec_helper.rb
71
76
  has_rdoc: true
72
77
  homepage: http://rubygems.org/gems/jquery_tag
@@ -100,9 +105,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
100
105
  requirements: []
101
106
 
102
107
  rubyforge_project:
103
- rubygems_version: 1.4.1
108
+ rubygems_version: 1.4.2
104
109
  signing_key:
105
110
  specification_version: 3
106
- summary: jQuery script tag helper for Rails
111
+ summary: jQuery script tag helper for Rails and Sinatra
107
112
  test_files: []
108
113
 
@@ -1,26 +0,0 @@
1
- module JqueryTag
2
- module ViewHelpers
3
- def jquery_tag(*args)
4
-
5
- options = args.first.is_a?(Hash) ? args.pop : {}
6
-
7
- args.unshift(jquery_ui_file(options[:ui])) if options[:ui]
8
- args.unshift(jquery_file(options[:file], options[:version]))
9
-
10
- javascript_include_tag args
11
- end
12
-
13
- private
14
- def jquery_file(path, version)
15
- path ||= 'jquery.js'
16
- version ||= '1.5.0'
17
- Rails.env.production? ? "//ajax.googleapis.com/ajax/libs/jquery/#{version}/jquery.min.js" : path
18
- end
19
-
20
- def jquery_ui_file(path)
21
- path = path.is_a?(String) ? path : "jquery-ui.js"
22
- version = '1.8.9'
23
- Rails.env.production? ? "//ajax.googleapis.com/ajax/libs/jqueryui/#{version}/jquery-ui.min.js" : path
24
- end
25
- end
26
- end