Class: Dscf::Marketplace::Listing

Inherits:
ApplicationRecord show all
Defined in:
app/models/dscf/marketplace/listing.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



29
30
31
# File 'app/models/dscf/marketplace/listing.rb', line 29

def self.ransackable_associations(_auth_object = nil)
  %w[business supplier_product order_items]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object

Ransack configuration for secure filtering



25
26
27
# File 'app/models/dscf/marketplace/listing.rb', line 25

def self.ransackable_attributes(_auth_object = nil)
  %w[id business_id supplier_product_id price quantity status created_at updated_at]
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/dscf/marketplace/listing.rb', line 48

def available?
  active? && quantity > 0
end

#can_be_managed_by?(business) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/dscf/marketplace/listing.rb', line 60

def can_be_managed_by?(business)
  self.business == business
end

#marginObject



38
39
40
41
# File 'app/models/dscf/marketplace/listing.rb', line 38

def margin
  return nil unless supplier_product&.supplier_price
  price - supplier_product.supplier_price
end

#margin_percentageObject



43
44
45
46
# File 'app/models/dscf/marketplace/listing.rb', line 43

def margin_percentage
  return nil unless supplier_product&.supplier_price && supplier_product.supplier_price > 0
  ((margin / supplier_product.supplier_price) * 100).round(2)
end

#productObject



56
57
58
# File 'app/models/dscf/marketplace/listing.rb', line 56

def product
  supplier_product&.product
end

#supplier_businessObject



52
53
54
# File 'app/models/dscf/marketplace/listing.rb', line 52

def supplier_business
  supplier_product&.business
end

#total_valueObject

Custom methods



34
35
36
# File 'app/models/dscf/marketplace/listing.rb', line 34

def total_value
  price * quantity
end