Sha256: 1c9681303cce3c89f41ef739b6bd1ec34e84d2790f69e71597743ac3d31c9d54

Contents?: true

Size: 1.31 KB

Versions: 15

Compression:

Stored size: 1.31 KB

Contents

module Billing
  class Modifier < ActiveRecord::Base
    include BillItem
    belongs_to :bill, inverse_of: :modifiers#, validate: true
    belongs_to :charge, inverse_of: :modifier
    monetize :fixed_value_cents
    
    scope :global, -> { where(charge_id: nil) }
    
    validate :percent_or_value
    validates_uniqueness_of :charge, scope: :bill_id, allow_nil: true
    validates_uniqueness_of :bill, scope: :charge_id
    
    def percentage
      percent_ratio.nil? ? "" : "#{(percent_ratio * 100).to_i}%" 
    end
    
    def positive?
      percent_ratio.nil? ? fixed_value_cents > 0 : percent_ratio > 0
    end
    
    private
      def percent_or_value
        errors.add :percent_or_value, I18n.t('errors.messages.blank') if percent_ratio.blank? and fixed_value.zero?
        errors.add :percent_or_value, I18n.t('errors.messages.present') if percent_ratio.present? and !fixed_value.zero?
      end
      
    class << self
      def wild_args(*args)
        case when args.blank? || args.first.kind_of?(Hash) then
          {}.merge(*args)
        when args.first.kind_of?(String) then
          d = args.shift
          if d.index('%')
            #TODO parse
          end
        else
          h = { fixed_value: args.shift.to_money }
          args.any? ? h.merge(*args) : h
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
billing-0.2.2 app/models/billing/modifier.rb
billing-0.2.1 app/models/billing/modifier.rb
billing-0.2.0 app/models/billing/modifier.rb
billing-0.1.9 app/models/billing/modifier.rb
billing-0.1.8 app/models/billing/modifier.rb
billing-0.1.7 app/models/billing/modifier.rb
billing-0.1.6 app/models/billing/modifier.rb
billing-0.1.5 app/models/billing/modifier.rb
billing-0.1.4 app/models/billing/modifier.rb
billing-0.1.3 app/models/billing/modifier.rb
billing-0.1.2a app/models/billing/modifier.rb
billing-0.1.2 app/models/billing/modifier.rb
billing-0.1.1 app/models/billing/modifier.rb
billing-0.1.0a app/models/billing/modifier.rb
billing-0.1.0 app/models/billing/modifier.rb