Sha256: ca81d0c8e8aba350abf728cf1fb664f7492f684cd1bd3facb35ccd390531dbcb
Contents?: true
Size: 1.59 KB
Versions: 2
Compression:
Stored size: 1.59 KB
Contents
class UnderConstructionEmailStorage include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor :email FILE_PATH = Rails.root + "db/under_construction_mails.txt" class UniquenessValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) emails_path = Rails.root + "db/under_construction_mails.txt" if File.exist? emails_path File.open(emails_path, "r") do |f| if f.readlines.include? value + "\n" record.errors[attribute] << I18n.t('activemodel.errors.models.under_construction_email_storage.attributes.email.uniqueness', default: "Already submitted") end end end end end validates :email, presence: true, uniqueness: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, message: I18n.t('activemodel.errors.models.under_construction_email_storage.attributes.email.format', default: "doesn't looks like correct") } def initialize(attributes = {}) attributes.each do |name, value| send("#{name}=", value) end end def persisted? false end # save submitted mail to a text file def save_to_file File.open(FILE_PATH, "a") do |f| f.puts email end end # return emails in a array def self.emails File.open(FILE_PATH, "r") do |f| f.readlines end rescue Errno::ENOENT [] end def self.no_emails? emails.empty? end def self.remove_file if File.exist? FILE_PATH FileUtils.rm FILE_PATH end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
under_construction-0.2.2 | app/models/under_construction_email_storage.rb |
under_construction-0.2.1 | app/models/under_construction_email_storage.rb |