Sha256: 7dc9fcbc56f1910ebf6820f30da5979ffd2d9e78bbe93af69d09891f75ba12a4

Contents?: true

Size: 1.43 KB

Versions: 16

Compression:

Stored size: 1.43 KB

Contents

require 'kaui/error_helper'

class Kaui::AccountEmail < Kaui::Base
  include Kaui::ErrorHelper

  define_attr :account_id
  define_attr :email

  def initialize(attributes = {})
    super(attributes)

    # We make Rails believe the id for the AccountEmail is actually the account_id
    # (this is handy for routes and form_for helpers). Make sure though to mark it as not persisted
    # (even though @id is not nil)
    @persisted = false
  end

  def self.where(conditions)
    begin
      account_emails = Kaui::KillbillHelper.get_account_emails(conditions[:account_id]) || []
      return account_emails.sort unless conditions[:email].present?

      account_emails.each do |account_email|
        return account_email if account_email.email == conditions[:email]
      end
    rescue => e
      @errors.add(:where, "Error while getting account emails: #{as_string(e)}")
    end
    []
  end

  def save
    begin
      Kaui::KillbillHelper.add_account_email(self)
      true
    rescue => e
      @errors.add(:save, "Error while trying to add an account email: #{as_string(e)}")
      false
    end
  end

  def destroy
    begin
      Kaui::KillbillHelper.remove_account_email(self)
      true
    rescue => e
      @errors.add(:destroy, "Error while trying to delete an account email: #{as_string(e)}")
    end
  end

  def id
    to_param
  end

  def to_param
    @account_id
  end

  def <=>(account_email)
    @email <=> account_email.email
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
kaui-0.2.6 app/models/kaui/account_email.rb
kaui-0.2.5 app/models/kaui/account_email.rb
kaui-0.2.4 app/models/kaui/account_email.rb
kaui-0.2.3 app/models/kaui/account_email.rb
kaui-0.2.2 app/models/kaui/account_email.rb
kaui-0.2.1 app/models/kaui/account_email.rb
kaui-0.2.0 app/models/kaui/account_email.rb
kaui-0.1.18 app/models/kaui/account_email.rb
kaui-0.1.17 app/models/kaui/account_email.rb
kaui-0.1.16 app/models/kaui/account_email.rb
kaui-0.1.15 app/models/kaui/account_email.rb
kaui-0.1.14 app/models/kaui/account_email.rb
kaui-0.1.12 app/models/kaui/account_email.rb
kaui-0.1.11 app/models/kaui/account_email.rb
kaui-0.1.10 app/models/kaui/account_email.rb
kaui-0.1.9 app/models/kaui/account_email.rb