Module: GooglePlusHelper

Defined in:
lib/google_plus_helper.rb,
lib/google_plus_helper/engine.rb,
lib/google_plus_helper/helper.rb,
lib/google_plus_helper/railtie.rb,
lib/google_plus_helper/version.rb,
lib/google_plus_helper/settings.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: Rails Classes: Engine, Railtie, Settings

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config {|settings| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/google_plus_helper.rb', line 19

def self.config
  yield(settings)
end

.settingsObject



5
6
7
8
# File 'lib/google_plus_helper.rb', line 5

def settings
  @_settings ||= Settings.new
  @_settings
end

Instance Method Details

#google_plusone_button(url, *args) ⇒ Object

Generates the +1 button for specific URL.

Arguments:

  • url: the URL to +1.

  • options: (optional) a hash that is identical to the 1 Button attributes listed here.

Example - A +1 button in medium size and without annotation:

<%= google_plusone_button("http://example.com", :size => :medium, :annotation => :none)


78
79
80
81
82
83
84
# File 'lib/google_plus_helper/helper.rb', line 78

def google_plusone_button(url, *args)
  options = args.extract_options!
  options[:href] = url
  html_options = options.delete(:html_options) || {}

  google_social_plugin(:plusone, options, html_options)
end

#google_plusone_javascript_include_tag(*args) ⇒ Object

Generates a <script> tag to load Google Plusone JavaScript file.

The trivial usage is to load synchonously:

<%= google_plusone_javascript_include_tag %> #=> <script src="..."></script>

To load the script asynchronously, use:

<%= google_plusone_javascript_include_tag(:async => true)

By default it takes “en-US” as display language. Add :lang option to change, e.g. :lang => "zh-TW"

To load in explicit mode, add :explicit => true option.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/google_plus_helper/helper.rb', line 24

def google_plusone_javascript_include_tag(*args)
  options = {
    :async => false,
    :lang => "en-US",
    :explicit => false
  }.merge(args.extract_options!)

  explicit = options.delete(:explicit)
  options.merge!(:parsetags => "explicit") if explicit

  # explicit parsing can only be applied to synchronized loading
  async = options.delete(:async) && !explicit

  params = options.to_json

  if async
    javascript_tag <<-EOJS
      window.___gcfg = #{options.to_json};
      (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();
    EOJS
  else
    javascript_tag options.to_json, :src => "https://apis.google.com/js/plusone.js"
  end
end

#google_share_button(url, *args) ⇒ Object

Generates the “Share on Google+” button for specific URL.

Arguments:

  • url: the URL to +1.

  • options: (optional) a hash that is identical to the Share Button attributes listed here.

Example - A Share button aligned to right and with vertical-bubble annotation:

<%= google_share_button("http://example.com", :align => :right, :annotation => "vertical-bubble")


96
97
98
99
100
101
102
# File 'lib/google_plus_helper/helper.rb', line 96

def google_share_button(url, *args)
  options = args.extract_options!
  options.merge! :href => url, :action => :share
  html_options = options.delete(:html_options) || {}

  google_social_plugin(:plus, options, html_options)
end

#google_social_plugin(plugin_name, options, html_options = {}) ⇒ Object

Generates Google+ Social Plugin html structure.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/google_plus_helper/helper.rb', line 55

def google_social_plugin(plugin_name, options, html_options={}) #:nodoc:
  case GooglePlusHelper.settings.social_plugin_version
  when :html5
    html_options.merge! :class => "g-#{plugin_name.to_s}", :data => options
     "div", "", html_options
  when :xml
    html_options.merge! options
     "g:#{plugin_name.to_s}", "", html_options
  else
    raise "Unknown Social Plugin Version: #{GooglePlusHelper.settings.social_plugin_version}"
  end
end