Sha256: 9adfbe4a8e11cd3cd5b711c2a140227414eed23bf3f2f857f376e75762d1b97d

Contents?: true

Size: 931 Bytes

Versions: 1

Compression:

Stored size: 931 Bytes

Contents

# cart_item
# good
class ServeFee
  attr_reader :good, :number, :buyer,
              :extra, :charges

  def initialize(good_type, good_id, number = 1, buyer_id = nil, extra = {})
    @good = good_type.constantize.unscoped.find good_id
    @number = number
    @buyer = RailsTrade.buyer_class.find(buyer_id) if buyer_id
    @extra = extra.merge! good.extra
    verbose_fee
  end

  def verbose_fee
    @charges = []

    Serve.single.overall.default.each do |serve|
      charge = get_charge(serve)
      @charges << charge
    end

    @charges
  end

  def subtotal
    @subtotal ||= self.charges.map(&:subtotal).sum
  end

  def get_charge(serve)
    if serve.is_a? QuantityServe
      charge = serve.compute_price(good.unified_quantity * number, extra)
    elsif serve.is_a? NumberServe
      charge = serve.compute_price(number, extra)
    else
      charge = serve.compute_price(number, extra)
    end
    charge
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_trade-0.0.1 app/models/rails_trade/concerns/serve_fee.rb