ref: master
plugins/currency/lib/ext/products_plugin/product.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
require_dependency 'products_plugin/product' module ProductsPlugin class Product has_many :product_currencies, -> { includes(:currency).order('price IS NOT NULL, discount IS NOT NULL, id ASC') }, class_name: 'CurrencyPlugin::ProductCurrency' has_many :currencies, through: :product_currencies CurrencyFields = [:price, :discount] def available_currencies_for_price self.enterprise.currencies - self.currencies.with_price end def available_currencies_for_discount self.enterprise.currencies - self.currencies.with_discount end def available_currencies zip = CurrencyFields.zip CurrencyFields.map{ |field| self.send "available_currencies_for_#{field}" } Hash[zip] end def currencies= currencies ActiveRecord::Base.transaction do self.product_currencies.destroy_all currencies.each do |id, attrs| next unless attrs.values.select{ |v| v.empty? }.empty? self.product_currencies.create attrs.merge(currency_id: id) end end end alias_method :orig_price_with_discount, :price_with_discount def price_with_discount currency = nil return orig_price_with_discount unless currency currency.discount ? (currency.price - currency.discount) : currency.price end end end |