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



64
65
66
# File 'app/models/dscf/marketplace/listing.rb', line 64

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

.ransackable_attributes(_auth_object = nil) ⇒ Object

Ransack configuration for secure filtering



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

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

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/dscf/marketplace/listing.rb', line 141

def available?
  active? && quantity > 0
end

#can_be_managed_by?(business) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/dscf/marketplace/listing.rb', line 153

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

#cheapest?Boolean

Does this listing have the lowest price for its product?

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'app/models/dscf/marketplace/listing.rb', line 77

def cheapest?
  self.class.active
    .joins(:supplier_product)
    .where(supplier_product: { product_id: supplier_product.product_id })
    .minimum(:price) == price
end

#cheapest_price_for_productObject

Market insight: lowest price across all businesses for this product



112
113
114
115
116
# File 'app/models/dscf/marketplace/listing.rb', line 112

def cheapest_price_for_product
  self.class.active.joins(:supplier_product)
    .where(supplier_product: { product_id: supplier_product.product_id })
    .minimum(:price)
end

#marginObject



131
132
133
134
# File 'app/models/dscf/marketplace/listing.rb', line 131

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

#margin_percentageObject



136
137
138
139
# File 'app/models/dscf/marketplace/listing.rb', line 136

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

#price_rankObject

Market insight: this listing’s price rank (1 = cheapest)



119
120
121
122
123
124
# File 'app/models/dscf/marketplace/listing.rb', line 119

def price_rank
  self.class.active.joins(:supplier_product)
    .where(supplier_product: { product_id: supplier_product.product_id })
    .where("price < ?", price)
    .count + 1
end

#productObject



149
150
151
# File 'app/models/dscf/marketplace/listing.rb', line 149

def product
  supplier_product&.product
end

#sole_provider?Boolean

Is this listing the only active listing for its product?

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'app/models/dscf/marketplace/listing.rb', line 69

def sole_provider?
  self.class.active
    .joins(:supplier_product)
    .where(supplier_product: { product_id: supplier_product.product_id })
    .count == 1
end

#supplier_businessObject



145
146
147
# File 'app/models/dscf/marketplace/listing.rb', line 145

def supplier_business
  supplier_product&.business
end

#total_listings_for_productObject

Market insight: how many active listings exist for this product



105
106
107
108
109
# File 'app/models/dscf/marketplace/listing.rb', line 105

def total_listings_for_product
  self.class.active.joins(:supplier_product)
    .where(supplier_product: { product_id: supplier_product.product_id })
    .count
end

#total_valueObject

Custom methods



127
128
129
# File 'app/models/dscf/marketplace/listing.rb', line 127

def total_value
  price * quantity
end

#visibility_reasonObject

Returns :orchestrator_promoted, :sole_provider, :cheapest, or nil



97
98
99
100
101
102
# File 'app/models/dscf/marketplace/listing.rb', line 97

def visibility_reason
  return nil unless visible?
  return :orchestrator_promoted if orchestrator_promoted?
  return :sole_provider if sole_provider?
  return :cheapest if cheapest?
end

#visible?Boolean

Should this listing be shown to end users?

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
# File 'app/models/dscf/marketplace/listing.rb', line 85

def visible?
  return false unless active? && quantity > 0
  return true if orchestrator_promoted?

  product_id = supplier_product.product_id
  active_listings = self.class.active.joins(:supplier_product)
    .where(supplier_product: { product_id: product_id })

  active_listings.count == 1 || active_listings.minimum(:price) == price
end