Created
January 8, 2026 14:14
-
-
Save aslam/2c682b265dafa3e5dc93488d7f9ce1ac to your computer and use it in GitHub Desktop.
Rails mail_to obfuscation
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
| ```ruby | |
| # app/helpers/application_helper.rb | |
| def obfuscated_mail_to(email, options = {}, &block) | |
| user, domain = email.split('@') | |
| options[:data] ||= {} | |
| options[:data].merge!( | |
| controller: "mail-obfuscator", | |
| mail_obfuscator_user_value: user, | |
| mail_obfuscator_domain_value: domain, | |
| # The secret sauce: Trigger on everything | |
| action: "mouseover->mail-obfuscator#reveal focus->mail-obfuscator#reveal mouseout->mail-obfuscator#reset blur->mail-obfuscator#reset click->mail-obfuscator#reveal" | |
| ) | |
| options[:href] ||= "#" | |
| link_to(options[:href], options, &block) | |
| end | |
| ``` | |
| ```javascript | |
| // app/javascript/controllers/mail_obfuscator_controller.js | |
| import { Controller } from "@hotwired/stimulus" | |
| export default class extends Controller { | |
| static values = { user: String, domain: String } | |
| connect() { | |
| // Ensure it's dead on arrival | |
| this.element.href = "#" | |
| } | |
| reveal() { | |
| // Assemble only when needed | |
| this.element.href = `mailto:${this.userValue}@${this.domainValue}` | |
| } | |
| reset() { | |
| // Cover your tracks | |
| this.element.href = "#" | |
| } | |
| } | |
| ``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment