1module SharingHelper |
|
|
2 def share_via_facebook(petition, options = {}) |
3 link_to(share_button(:facebook), share_via_facebook_url(petition), options) |
|
4 end |
|
|
6 def share_via_facebook_url(petition) |
7 "https://www.facebook.com/sharer/sharer.php?#{share_via_facebook_params(petition)}" |
|
8 end |
|
|
10 def share_via_email(petition, options = {}) |
11 link_to(share_button(:email), share_via_email_url(petition), options) |
|
12 end |
|
|
14 def share_via_email_url(petition) |
15 "mailto:?#{share_via_email_params(petition)}" |
|
16 end |
|
|
18 def share_via_twitter(petition, options = {}) |
19 link_to(share_button(:twitter), share_via_twitter_url(petition), options) |
|
20 end |
|
|
22 def share_via_twitter_url(petition) |
23 "https://twitter.com/intent/tweet?#{share_via_twitter_params(petition)}" |
|
24 end |
|
|
26 def share_via_whatsapp(petition, options = {}) |
27 link_to(share_button(:whatsapp), share_via_whatsapp_url(petition), options) |
|
28 end |
|
|
30 def share_via_whatsapp_url(petition) |
31 "whatsapp://send?#{share_via_whatsapp_params(petition)}" |
|
32 end |
|
34 private
|
|
|
36 def share_via_facebook_params(petition) |
37 share_params(u: petition_url(petition), ref: "responsive") |
|
38 end |
|
|
40 def share_via_email_params(petition) |
41 share_params(subject: share_title(petition), body: petition_url(petition)) |
|
42 end |
|
|
44 def share_via_twitter_params(petition) |
45 share_params(text: share_title(petition), url: petition_url(petition)) |
|
46 end |
|
|
48 def share_via_whatsapp_params(petition) |
49 share_params(text: "#{share_title(petition)}\n#{petition_url(petition)}") |
|
50 end |
|
|
52 def share_title(petition) |
53 t(:share_title, scope: :petitions, petition: petition.action) |
|
54 end |
|
|
56 def share_params(hash) |
57 hash.to_query.gsub('+', '%20') |
|
58 end |
|
|
60 def share_button(service) |
61 t(:"#{service}.html", scope: :"petitions.sharing") |
|
62 end |
|
63end |