SocialShareRails
SocialShareRails is a Ruby on Rails gem that simplifies the process of adding social sharing buttons to your Rails applications. It supports multiple social platforms, customizable styles, and an easy-to-use API for sharing.
Features
- Supports popular social platforms: Twitter, Facebook, Pinterest, LinkedIn, Reddit, Telegram, WhatsApp, and more.
- Customizable styles with rounded icons.
- JavaScript-based popup for sharing links.
- Easy integration with Rails views and assets pipeline.
Installation
Add this line to your application's Gemfile
:
gem 'social_share_rails'
Then, execute:
bundle install
Configuration
Include the Assets
You can require the CSS and JavaScript files directly in your application.js
and application.css
(or .scss
) files:
In app/assets/stylesheets/application.css
(or application.scss
):
@import "social_share_rails/social_share";
In app/assets/javascripts/application.js
:
//= require social_share_rails/social_share
If you're using Rails 7 with importmap
, pin the JavaScript in your config/importmap.rb
:
pin "social_share_rails", to: "social_share_rails/social_share.js"
And import it in your app/javascript/application.js
:
import "social_share_rails"
Usage
Basic Example
Add the social_share
helper to your views:
<%= social_share("Share this post", {
url: "https://example.com",
allow_sites: %w[twitter facebook linkedin],
rounded: true
}) %>
Fallback to Current Page URL
If no url is provided, the helper will automatically use the current page URL (window.location.href) as the fallback. For example:
<%= social_share("Share this post", {
allow_sites: %w[twitter facebook linkedin],
rounded: true
}) %>
In this case, the current page URL will be shared.
Options
- title (String): The title of the post to share.
- url (String): The URL to share (falls back to the current page URL if not provided).
- desc (String): Optional description of the content.
- allow_sites (Array): List of social platforms to include.
-
rounded (Boolean): If
true
, uses rounded icon styles.
Example with more options:
<%= social_share("Check out this article", {
url: "https://example.com/article",
desc: "This is an amazing article!",
allow_sites: %w[twitter facebook pinterest],
rounded: false
}) %>
Supported Platforms
- Google Bookmarks
- Telegram
- WhatsApp (App & Web)
- VKontakte
I18n Support
SocialShareRails supports multiple languages using Rails' built-in I18n system. The gem provides default translations for the following keys:
en:
social_share_rails:
share_to: Share to %{name}
twitter: Twitter
facebook: Facebook
google_bookmark: Google Bookmark
pinterest: Pinterest
email: Email
linkedin: Linkedin
vkontakte: Vkontakte
reddit: Reddit
telegram: Telegram
whatsapp_app: WhatsApp
whatsapp_web: WhatsApp
To customize or add translations, create or modify the appropriate YAML file in your Rails application under config/locales
. For example, to add translations in Portuguese:
pt-BR:
social_share_rails:
share_to: Compartilhar no %{name}
twitter: Twitter
facebook: Facebook
google_bookmark: Favoritos do Google
pinterest: Pinterest
email: Email
linkedin: LinkedIn
vkontakte: VKontakte
reddit: Reddit
telegram: Telegram
whatsapp_app: WhatsApp (App)
whatsapp_web: WhatsApp (Web)
After adding your translations, Rails will automatically use them based on the current locale.
Customization
Custom Styles
You can override the default styles provided by the gem. Add your own styles in app/assets/stylesheets/application.scss
:
.social-share .share-icon {
width: 30px;
height: 30px;
background-size: cover;
}
.social-share .share-twitter {
background-image: url('/path/to/your/custom-twitter-icon.svg');
}
Development
Running Tests
To run the tests locally, use:
bundle exec rspec
Contributing
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration.
License
The gem is available as open source under the terms of the MIT License.