Created
August 29, 2013 08:16
-
-
Save zealot128/6375481 to your computer and use it in GitHub Desktop.
Rails: Two Click Image helper for easy dropin daily refreshed Facebook/Twitter/SocialPlugins as static images (to comply with German data protection standards).
Needs wkhtmltoimage installed on server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .two-click{data: { iframe: url, height: height, width: width} } | |
| = link_to url, title: t("two-click.info"), target: "_blank", rel: "nofollow" do | |
| = image_tag image_url, class: "two-click-img" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| = two_click_image "https://www.facebook.com/plugins/like_box.php?..., 600, 300 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module SomeHelper | |
| def two_click_image(url, width, height) | |
| return "" if Rails.env.test? | |
| hash = Digest::MD5.hexdigest(url + width.to_s + height.to_s) | |
| image_path = "/system/two-click/#{hash}.jpg" | |
| folder = Rails.root.join "public/system/two-click" | |
| FileUtils.mkdir_p folder | |
| filename = "public" + image_path | |
| if !File.exists?(filename) || File.ctime(filename) < 1.day.ago | |
| cmd = "wkhtmltoimage --width #{Shellwords.escape(width)} --height #{Shellwords.escape(height)} #{Shellwords.escape(url)} #{Shellwords.escape(filename)} && mogrify - ↪ colorspace Gray #{Shellwords.escape(filename)} 2>&1" | |
| logger.warn cmd | |
| log = `#{cmd}` | |
| if $?.to_i > 0 | |
| logger.error "WARNING Two factor image could not be created: #{log}" | |
| end | |
| end | |
| render "/layouts/two_click", image_url: image_path, url: url, width: width, height: height | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ -> | |
| $(".two-click").each -> | |
| element = $(this) | |
| element.find("a").click -> | |
| $(this).remove() | |
| iframe = """ | |
| <iframe src="#{element.data("iframe")}" height="#{element.data("height")}" width="#{element.data("width")}" scrolling="none" border="0" frameborder="0"> | |
| Your browser does not support Iframes. | |
| <a href="#{element.data("iframe")}" rel="nofollow" target="_blank">The Social Plugin can be found here</a> | |
| </iframe> | |
| """ | |
| element.append iframe | |
| false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment