Sha256: 5f9642b8b663e33b4ebd2eba1cef5cb265c7cb7f7d7cd3e77fd943e8bc6a9380

Contents?: true

Size: 1.8 KB

Versions: 9

Compression:

Stored size: 1.8 KB

Contents

require "net/smtp"

# The Confirmation code is a sort of join table that links mailing lists
# with addresses, and also stores confirmation status.
class Confirmationcode < ActiveRecord::Base
    belongs_to :mailinglist
    belongs_to :address

    # Creates a new confirmation code, and then freezes it so that it
    # can't be modified.
    def initialize
        super
        @attributes["code"]=("%16s" % rand(36**16).to_s(36)).gsub(/ /,"0")
        @attributes["code"].freeze
    end

    validates_presence_of :mailinglist
    validates_presence_of :address

    def code= _
        raise TypeError, "can't modify code string"
    end

    # Given a +mailinglist_id+ and an +address_id+, returns whether the
    # address has been confirmed for that mailing list or not.
    def self.confirmed? mailinglist_id, address_id
        confirmation_record = find( :first, :conditions => 
            [ 'mailinglist_id = ? and address_id = ?', 
                mailinglist_id, address_id ] )
        if confirmation_record then
            confirmation_record.confirmed
        else
            nil
        end
    end

    # Given a +mailinglist+, an +address+ and a confirmation code in
    # +code+, it confirms the address in that mailing list if the
    # confirmation code is correct.
    # 
    # If the confirmation code is incorrect, returns false.
    def self.confirm mailinglist, address, code
        confirmation_record = 
            find(:first, :conditions => [
                 'mailinglist_id = ? and address_id = ? and code = ?',
                 mailinglist.id,
                 address.id,
                 code
        ])

        if confirmation_record then
            confirmation_record.confirmed = true
            if confirmation_record.save then
                true
            end
        end

    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sugoi-mail-0.0.3 app/models/confirmationcode.rb
sugoi-mail-0.1.5 app/models/confirmationcode.rb
sugoi-mail-0.0.1 app/models/confirmationcode.rb
sugoi-mail-0.0.0 app/models/confirmationcode.rb
sugoi-mail-0.0.2 app/models/confirmationcode.rb
sugoi-mail-0.0.5 app/models/confirmationcode.rb
sugoi-mail-0.1.0 app/models/confirmationcode.rb
sugoi-mail-0.3.0 app/models/confirmationcode.rb
sugoi-mail-0.3.2 app/models/confirmationcode.rb