Class: Dscf::Marketplace::Listing
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Dscf::Marketplace::Listing
- Defined in:
- app/models/dscf/marketplace/listing.rb
Class Method Summary collapse
- .ransackable_associations(_auth_object = nil) ⇒ Object
-
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration for secure filtering.
Instance Method Summary collapse
- #available? ⇒ Boolean
- #can_be_managed_by?(business) ⇒ Boolean
-
#cheapest? ⇒ Boolean
Does this listing have the lowest price for its product?.
-
#cheapest_price_for_product ⇒ Object
Market insight: lowest price across all businesses for this product.
- #margin ⇒ Object
- #margin_percentage ⇒ Object
-
#price_rank ⇒ Object
Market insight: this listing’s price rank (1 = cheapest).
- #product ⇒ Object
-
#sole_provider? ⇒ Boolean
Is this listing the only active listing for its product?.
- #supplier_business ⇒ Object
-
#total_listings_for_product ⇒ Object
Market insight: how many active listings exist for this product.
-
#total_value ⇒ Object
Custom methods.
-
#visibility_reason ⇒ Object
Returns :orchestrator_promoted, :sole_provider, :cheapest, or nil.
-
#visible? ⇒ Boolean
Should this listing be shown to end users?.
Class Method Details
.ransackable_associations(_auth_object = nil) ⇒ Object
65 66 67 |
# File 'app/models/dscf/marketplace/listing.rb', line 65 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
59 60 61 62 63 |
# File 'app/models/dscf/marketplace/listing.rb', line 59 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
142 143 144 |
# File 'app/models/dscf/marketplace/listing.rb', line 142 def available? active? && quantity > 0 end |
#can_be_managed_by?(business) ⇒ Boolean
154 155 156 |
# File 'app/models/dscf/marketplace/listing.rb', line 154 def can_be_managed_by?(business) self.business == business end |
#cheapest? ⇒ Boolean
Does this listing have the lowest price for its product?
78 79 80 81 82 83 |
# File 'app/models/dscf/marketplace/listing.rb', line 78 def cheapest? self.class.active .joins(:supplier_product) .where(supplier_product: {product_id: supplier_product.product_id}) .minimum(:price) == price end |
#cheapest_price_for_product ⇒ Object
Market insight: lowest price across all businesses for this product
113 114 115 116 117 |
# File 'app/models/dscf/marketplace/listing.rb', line 113 def cheapest_price_for_product self.class.active.joins(:supplier_product) .where(supplier_product: {product_id: supplier_product.product_id}) .minimum(:price) end |
#margin ⇒ Object
132 133 134 135 |
# File 'app/models/dscf/marketplace/listing.rb', line 132 def margin return nil unless supplier_product&.supplier_price price - supplier_product.supplier_price end |
#margin_percentage ⇒ Object
137 138 139 140 |
# File 'app/models/dscf/marketplace/listing.rb', line 137 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_rank ⇒ Object
Market insight: this listing’s price rank (1 = cheapest)
120 121 122 123 124 125 |
# File 'app/models/dscf/marketplace/listing.rb', line 120 def price_rank self.class.active.joins(:supplier_product) .where(supplier_product: {product_id: supplier_product.product_id}) .where("price < ?", price) .count + 1 end |
#product ⇒ Object
150 151 152 |
# File 'app/models/dscf/marketplace/listing.rb', line 150 def product supplier_product&.product end |
#sole_provider? ⇒ Boolean
Is this listing the only active listing for its product?
70 71 72 73 74 75 |
# File 'app/models/dscf/marketplace/listing.rb', line 70 def sole_provider? self.class.active .joins(:supplier_product) .where(supplier_product: {product_id: supplier_product.product_id}) .count == 1 end |
#supplier_business ⇒ Object
146 147 148 |
# File 'app/models/dscf/marketplace/listing.rb', line 146 def supplier_business supplier_product&.business end |
#total_listings_for_product ⇒ Object
Market insight: how many active listings exist for this product
106 107 108 109 110 |
# File 'app/models/dscf/marketplace/listing.rb', line 106 def total_listings_for_product self.class.active.joins(:supplier_product) .where(supplier_product: {product_id: supplier_product.product_id}) .count end |
#total_value ⇒ Object
Custom methods
128 129 130 |
# File 'app/models/dscf/marketplace/listing.rb', line 128 def total_value price * quantity end |
#visibility_reason ⇒ Object
Returns :orchestrator_promoted, :sole_provider, :cheapest, or nil
98 99 100 101 102 103 |
# File 'app/models/dscf/marketplace/listing.rb', line 98 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?
86 87 88 89 90 91 92 93 94 95 |
# File 'app/models/dscf/marketplace/listing.rb', line 86 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 |