Sha256: 2a264d64e22cdcc5f200659d49b4fc850c2c8383f13b3bdaa43340de4647dd97

Contents?: true

Size: 1.55 KB

Versions: 9

Compression:

Stored size: 1.55 KB

Contents

class Wco::Leadset
  include Mongoid::Document
  include Mongoid::Timestamps
  include Mongoid::Paranoia
  store_in collection: 'wco_leadsets'

  field     :company_url
  validates :company_url, presence: true, uniqueness: true
  index({ company_url: 1 }, { name: 'company_url' })

  field :email
  index({ email: 1 }, { name: 'email' })
  validates :email, uniqueness: { allow_nil: true } # presence: true

  has_many :leads,         class_name: 'Wco::Lead'
  has_many :profiles,      class_name: 'Wco::Profile',           inverse_of: :leadset
  has_many :appliances,    class_name: 'WcoHosting::Appliance',  inverse_of: :leadset
  has_many :subscriptions, class_name: 'Wco::Subscription',      inverse_of: :leadset
  has_and_belongs_to_many :tags, class_name: 'Wco::Tag'

  has_many :invoices,     class_name: 'Wco::Invoice'
  field :next_invoice_number, type: :integer, default: 100

  has_and_belongs_to_many :serverhosts, class_name: 'WcoHosting::Serverhost' # , inverse_of: :leadset
  def next_serverhost
    serverhosts.first
  end

  ##
  ## stripe
  ##
  field :customer_id
  def customer_id
    if self[:customer_id].blank?
      return nil if !email
      existing = Stripe::Customer.search({ query: "email: '#{email}'" })
      # puts! existing, 'existing'
      if existing.data.present?
        update_attributes( customer_id: existing.data[0][:id] )
      else
        customer = Stripe::Customer.create({ email: email })
        # puts! customer, 'customer'
        update_attributes( customer_id: customer[:id] )
      end
    end
    self[:customer_id]
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
wco_models-3.1.0.46 app/models/wco/leadset.rb
wco_models-3.1.0.45 app/models/wco/leadset.rb
wco_models-3.1.0.44 app/models/wco/leadset.rb
wco_models-3.1.0.43 app/models/wco/leadset.rb
wco_models-3.1.0.42 app/models/wco/leadset.rb
wco_models-3.1.0.41 app/models/wco/leadset.rb
wco_models-3.1.0.40 app/models/wco/leadset.rb
wco_models-3.1.0.39 app/models/wco/leadset.rb
wco_models-3.1.0.38 app/models/wco/leadset.rb