Module: SocialShareRails::SocialShareHelper

Defined in:
lib/social_share_rails/social_share_helper.rb

Instance Method Summary collapse

Instance Method Details

#social_share(title = "", opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/social_share_rails/social_share_helper.rb', line 3

def social_share(title = "", opts = {})
  opts[:allow_sites] ||= %w[twitter facebook google_bookmark pinterest email linkedin vkontakte reddit telegram whatsapp_app whatsapp_web]
  is_rounded = opts.delete(:rounded) || false

  html = []
  html << "<div class='social-share' data-title='#{ERB::Util.html_escape(title)}'"
  html << "data-url='#{opts[:url]}' data-desc='#{opts[:desc]}'>"

  opts[:allow_sites].each do |name|
    special_data = opts.select { |k, _| k.to_s.start_with?("data-" + name) }

    # Generate CSS class based on rounded option
    css_class = "share-icon share-#{name}"
    css_class += " rounded-#{name}" if is_rounded

    link_title = I18n.t("social_share.share_to", name: I18n.t("social_share.#{name.downcase}"))
    html << link_to("", "#", {
      rel: ["nofollow", opts[:rel]],
      "data-site": name,
      class: css_class,
      onclick: "return SocialShare.share(this);",
      title: ERB::Util.html_escape(link_title)
    }.merge(special_data))
  end

  html << "</div>"
  raw html.join("\n")
end