googlets 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +24 -0
- data/Gemfile +4 -0
- data/README.md +0 -0
- data/Rakefile +2 -0
- data/googlets.gemspec +21 -0
- data/lib/generators/googlets/config/config_generator.rb +13 -0
- data/lib/generators/googlets/config/templates/google.yml +9 -0
- data/lib/googlets.rb +20 -0
- data/lib/googlets/analytics.rb +47 -0
- data/lib/googlets/googlet.rb +29 -0
- data/lib/googlets/libraries.rb +96 -0
- data/lib/googlets/version.rb +3 -0
- metadata +59 -0
data/.gitignore
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
*.gem
|
2
|
+
.bundle
|
3
|
+
Gemfile.lock
|
4
|
+
pkg/*
|
5
|
+
|
6
|
+
# IntelliJ #############################################################################################################
|
7
|
+
.idea
|
8
|
+
/*.ids
|
9
|
+
|
10
|
+
# Linux ################################################################################################################
|
11
|
+
*~
|
12
|
+
|
13
|
+
# NetBeans #############################################################################################################
|
14
|
+
nbproject
|
15
|
+
|
16
|
+
# Windows ##############################################################################################################
|
17
|
+
Thumbs.db
|
18
|
+
Desktop.ini
|
19
|
+
|
20
|
+
# Mac ##################################################################################################################
|
21
|
+
.DS_Store
|
22
|
+
|
23
|
+
# Keep it! (and keep this pattern at the end of .gitignore) ############################################################
|
24
|
+
!.gitkeep
|
data/Gemfile
ADDED
data/README.md
ADDED
File without changes
|
data/Rakefile
ADDED
data/googlets.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'googlets/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'googlets'
|
7
|
+
s.version = Googlets::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['sodercober']
|
10
|
+
s.email = ['sodercober@gmail.com']
|
11
|
+
s.homepage = 'https://github.com/sodercober/googlets'
|
12
|
+
s.summary = %q{Google Snippets}
|
13
|
+
s.description = %q{Several Google snippets for Rails 3}
|
14
|
+
|
15
|
+
s.rubyforge_project = 'googlets'
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ['lib']
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Googlets
|
2
|
+
module Generators
|
3
|
+
class ConfigGenerator < ::Rails::Generators::Base
|
4
|
+
desc 'This generator generates sample configuration for Google Snippets'
|
5
|
+
source_root File.expand_path('../templates', __FILE__)
|
6
|
+
|
7
|
+
def generate_config
|
8
|
+
copy_file 'google.yml', 'config/google.yml'
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# Include configuration for Google services here.
|
2
|
+
|
3
|
+
development:
|
4
|
+
# Disable remote CDN in development:
|
5
|
+
libraries_local_only: true
|
6
|
+
|
7
|
+
production:
|
8
|
+
# Configure Google Analytics Web Property ID (UA number). Leave this blank to disable Google Analytics.
|
9
|
+
analytics_web_property_id: UA-XXXXX-YY
|
data/lib/googlets.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module Googlets
|
2
|
+
|
3
|
+
class Railtie < Rails::Railtie
|
4
|
+
|
5
|
+
initializer 'googlets.action_view' do |app|
|
6
|
+
ActiveSupport.on_load :action_view do
|
7
|
+
require 'googlets/googlet'
|
8
|
+
require 'googlets/analytics'
|
9
|
+
require 'googlets/libraries'
|
10
|
+
|
11
|
+
Googlets::Googlet.subclasses.each(&:parse_config)
|
12
|
+
|
13
|
+
include Googlets::Libraries::ViewHelpers::ActionView
|
14
|
+
include Googlets::Analytics::ViewHelpers::ActionView
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Googlets
|
2
|
+
|
3
|
+
class Analytics < Googlet
|
4
|
+
@@analytics_web_property_id = nil
|
5
|
+
cattr_accessor :analytics_web_property_id, :instance_writer => false
|
6
|
+
|
7
|
+
def self.enabled?
|
8
|
+
!analytics_web_property_id.blank?
|
9
|
+
end
|
10
|
+
|
11
|
+
module ViewHelpers
|
12
|
+
module ActionView
|
13
|
+
|
14
|
+
# Google Analytics Asynchronous Tracking
|
15
|
+
# http://code.google.com/intl/pl-PL/apis/analytics/docs/tracking/asyncUsageGuide.html
|
16
|
+
def include_google_analytics(part = nil)
|
17
|
+
return unless Googlets::Analytics.enabled?
|
18
|
+
|
19
|
+
javascript_tag do
|
20
|
+
case part
|
21
|
+
when :head then Googlets::Analytics.head_javascript
|
22
|
+
when :body then Googlets::Analytics.body_javascript
|
23
|
+
else Googlets::Analytics.head_javascript + Googlets::Analytics.body_javascript
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
protected ##########################################################################################################
|
32
|
+
|
33
|
+
def self.head_javascript
|
34
|
+
"var _gaq = _gaq || []; _gaq.push(['_setAccount', '#{analytics_web_property_id}'], ['_trackPageview']);".html_safe
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.body_javascript
|
38
|
+
"(function() { " +
|
39
|
+
"var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; " +
|
40
|
+
"ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; " +
|
41
|
+
"var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); " +
|
42
|
+
"})();".html_safe
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Googlets
|
2
|
+
|
3
|
+
class Googlet
|
4
|
+
|
5
|
+
def self.parse_config
|
6
|
+
read_config.each do |key, value|
|
7
|
+
send("#{key}=", value) if respond_to?("#{key}=")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.read_config
|
12
|
+
return {} unless File.exist?(config_file)
|
13
|
+
|
14
|
+
config = YAML::load(ERB.new(IO.read(config_file)).result)[Rails.env] || {}
|
15
|
+
config.symbolize_keys!
|
16
|
+
config
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.config_file
|
20
|
+
Rails.root.join('config', 'google.yml')
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.enabled?
|
24
|
+
true
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Googlets
|
2
|
+
|
3
|
+
class Libraries < Googlet
|
4
|
+
LIBRARIES = {
|
5
|
+
'chrome-frame' => {:path => 'chrome-frame/%s/CFInstall.min.js'},
|
6
|
+
'dojo' => {:path => 'dojo/%s/dojo/dojo.xd.js'},
|
7
|
+
'ext-core' => {:path => 'ext-core/%s/ext-core.js'},
|
8
|
+
'jquery' => {:path => 'jquery/%s/jquery.min.js', :local_fallback_if => '!window.jQuery'},
|
9
|
+
'jquery-ui' => {:path => 'jqueryui/%s/jquery-ui.min.js', :local_fallback_if => 'window.jQuery && !window.jQuery.ui'},
|
10
|
+
'mootools' => {:path => 'mootools/%s/mootools-yui-compressed.js'},
|
11
|
+
'prototype' => {:path => 'prototype/%s/prototype.js'},
|
12
|
+
'scriptaculous' => {:path => 'scriptaculous/%s/scriptaculous.js'},
|
13
|
+
'swfobject' => {:path => 'swfobject/%s/swfobject.js', :local_fallback_if => '!window.swfobject'},
|
14
|
+
'yui' => {:path => 'yui/%s/build/yui/yui-min.js'},
|
15
|
+
'webfont' => {:path => 'webfont/%s/webfont.js'}
|
16
|
+
}
|
17
|
+
|
18
|
+
@@libraries_local_only = false
|
19
|
+
cattr_accessor :libraries_local_only, :instance_writer => false
|
20
|
+
|
21
|
+
@@libraries_api_key = nil
|
22
|
+
cattr_accessor :libraries_api_key, :instance_writer => false
|
23
|
+
|
24
|
+
@@libraries_api_url = '//www.google.com/jsapi'
|
25
|
+
cattr_accessor :libraries_api_url, :instance_writer => false
|
26
|
+
|
27
|
+
@@libraries_url = '//ajax.googleapis.com/ajax/libs/'
|
28
|
+
cattr_accessor :libraries_url, :instance_writer => false
|
29
|
+
|
30
|
+
# This must be relative to ActionController::Base.javascripts_dir
|
31
|
+
@@local_libraries_path = 'lib'
|
32
|
+
cattr_accessor :local_libraries_path, :instance_writer => false
|
33
|
+
|
34
|
+
@@local_libraries_matcher = /^([\w-]+)-(\d+(\.\d+)*)(\.\w+)*.js$/i
|
35
|
+
cattr_accessor :local_libraries_matcher, :instance_writer => false
|
36
|
+
|
37
|
+
@@libraries = LIBRARIES
|
38
|
+
cattr_reader :libraries
|
39
|
+
|
40
|
+
def self.libraries=(libraries = {})
|
41
|
+
libraries.each do |key, value|
|
42
|
+
@@libraries[key.to_s].merge!(value.symbolize_keys) if @@libraries.has_key?(key.to_s) && value.is_a?(Hash)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
module ViewHelpers
|
47
|
+
module ActionView
|
48
|
+
|
49
|
+
def include_javascript_libraries(*args)
|
50
|
+
options = {}.merge(args.extract_options!)
|
51
|
+
library_names = [*args].flatten
|
52
|
+
api_key = options.delete(:libraries_api_key) || Googlets::Libraries::libraries_api_key
|
53
|
+
api_url = options.delete(:libraries_api_url) || Googlets::Libraries::libraries_api_url
|
54
|
+
local_only = options.delete(:libraries_local_only) || Googlets::Libraries::libraries_local_only
|
55
|
+
|
56
|
+
''.html_safe.tap do |html|
|
57
|
+
html << content_tag('script', '', 'type' => Mime::JS, 'src' => api_url + '?' + {:key => api_key}.to_query) unless local_only || api_key.blank? || api_url.blank?
|
58
|
+
library_names.each{|library_name| html << include_javascript_library(library_name, {:local => local_only}.merge(options))}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def include_javascript_library(library_name, options = {})
|
63
|
+
library_name = library_name.to_s
|
64
|
+
library = Googlets::Libraries::libraries[library_name]
|
65
|
+
local_library = local_javascript_libraries[library_name]
|
66
|
+
local_libraries_path = options[:local_libraries_path] || Googlets::Libraries::local_libraries_path
|
67
|
+
|
68
|
+
if (options[:local] || !library) && local_library
|
69
|
+
javascript_include_tag(local_libraries_path + '/' + local_library[:path]).html_safe
|
70
|
+
elsif local_library || !library[:version].blank?
|
71
|
+
libraries_url = options[:libraries_url] || Googlets::Libraries::libraries_url
|
72
|
+
local_fallback_if = options[:local_fallback_if] || library[:local_fallback_if]
|
73
|
+
''.html_safe.tap do |html|
|
74
|
+
html << content_tag('script', '', 'type' => Mime::JS, 'src' => libraries_url + (library[:path] % (library[:version] || local_library[:version])))
|
75
|
+
if local_library && !local_fallback_if.blank?
|
76
|
+
html << javascript_tag(%{(#{local_fallback_if}) && document.write(unescape('%3Cscript src="#{javascript_path(local_libraries_path + '/' + local_library[:path])}"%3E%3C/script%3E'));})
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def local_javascript_libraries
|
83
|
+
@@local_javascript_libraries ||= {}.tap do |libraries|
|
84
|
+
Dir.entries(File.join(ActionController::Base.javascripts_dir, Googlets::Libraries::local_libraries_path)).
|
85
|
+
map{|entry| entry.match(Googlets::Libraries::local_libraries_matcher)}.compact.
|
86
|
+
each{|match| libraries[match[1]] = {:version => match[2], :path => match.to_s}}
|
87
|
+
end
|
88
|
+
end
|
89
|
+
private :local_javascript_libraries
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: googlets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.4
|
5
|
+
prerelease: !!null
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- sodercober
|
9
|
+
autorequire: !!null
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-03-04 00:00:00.000000000 +00:00
|
13
|
+
default_executable: !!null
|
14
|
+
dependencies: []
|
15
|
+
description: Several Google snippets for Rails 3
|
16
|
+
email:
|
17
|
+
- sodercober@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- googlets.gemspec
|
27
|
+
- lib/generators/googlets/config/config_generator.rb
|
28
|
+
- lib/generators/googlets/config/templates/google.yml
|
29
|
+
- lib/googlets.rb
|
30
|
+
- lib/googlets/analytics.rb
|
31
|
+
- lib/googlets/googlet.rb
|
32
|
+
- lib/googlets/libraries.rb
|
33
|
+
- lib/googlets/version.rb
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: https://github.com/sodercober/googlets
|
36
|
+
licenses: []
|
37
|
+
post_install_message: !!null
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project: googlets
|
55
|
+
rubygems_version: 1.5.0
|
56
|
+
signing_key: !!null
|
57
|
+
specification_version: 3
|
58
|
+
summary: Google Snippets
|
59
|
+
test_files: []
|