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



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

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

.ransackable_attributes(_auth_object = nil) ⇒ Object

Ransack configuration for secure filtering



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

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)


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

def available?
  active? && quantity > 0
end

#can_be_managed_by?(business) ⇒ Boolean

Returns:

  • (Boolean)


158
159
160
# File 'app/models/dscf/marketplace/listing.rb', line 158

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

#cheapest?Boolean

Does this listing have the lowest price for its product?

Returns:

  • (Boolean)


82
83
84
85
86
87
# File 'app/models/dscf/marketplace/listing.rb', line 82

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



117
118
119
120
121
# File 'app/models/dscf/marketplace/listing.rb', line 117

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

#marginObject



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

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

#margin_percentageObject



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

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)



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

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



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

def product
  supplier_product&.product
end

#sole_provider?Boolean

Is this listing the only active listing for its product?

Returns:

  • (Boolean)


74
75
76
77
78
79
# File 'app/models/dscf/marketplace/listing.rb', line 74

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

#supplier_businessObject



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

def supplier_business
  supplier_product&.business
end

#total_listings_for_productObject

Market insight: how many active listings exist for this product



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

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



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

def total_value
  price * quantity
end

#visibility_reasonObject

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



102
103
104
105
106
107
# File 'app/models/dscf/marketplace/listing.rb', line 102

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

#visible?Boolean

Should this listing be shown to end users?

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
99
# File 'app/models/dscf/marketplace/listing.rb', line 90

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