Class: AmazonCompetitor

Inherits:
ApplicationRecord show all
Defined in:
app/models/amazon_competitor.rb

Overview

== Schema Information

Table name: amazon_competitors
Database name: primary

id :bigint not null, primary key
name :string
notes :text
created_at :datetime not null
updated_at :datetime not null
seller_id :string

Indexes

index_amazon_competitors_on_seller_id (seller_id) UNIQUE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#seller_idObject (readonly)



19
# File 'app/models/amazon_competitor.rb', line 19

validates :seller_id, presence: true, uniqueness: true

Class Method Details

.name_for(seller_id) ⇒ Object

Look up competitor name by seller ID
Returns the name if found, otherwise returns the raw seller ID



28
29
30
31
32
33
# File 'app/models/amazon_competitor.rb', line 28

def self.name_for(seller_id)
  return seller_id if seller_id.blank?

  competitor = find_by(seller_id: seller_id)
  competitor&.name.presence || seller_id
end

.namedActiveRecord::Relation<AmazonCompetitor>

A relation of AmazonCompetitors that are named. Active Record Scope

Returns:

See Also:



23
# File 'app/models/amazon_competitor.rb', line 23

scope :named, -> { where.not(name: [nil, '']) }

.named?(seller_id) ⇒ Boolean

Check if a seller ID is named (has a known name in the database)

Returns:

  • (Boolean)


36
37
38
39
40
# File 'app/models/amazon_competitor.rb', line 36

def self.named?(seller_id)
  return false if seller_id.blank?

  named.exists?(seller_id: seller_id)
end

.storefront_url(seller_id, marketplace: 'amazon.com') ⇒ Object

Get storefront URL for a seller



43
44
45
# File 'app/models/amazon_competitor.rb', line 43

def self.storefront_url(seller_id, marketplace: 'amazon.com')
  "https://www.#{marketplace}/sp?seller=#{seller_id}"
end

.unnamedActiveRecord::Relation<AmazonCompetitor>

A relation of AmazonCompetitors that are unnamed. Active Record Scope

Returns:

See Also:



24
# File 'app/models/amazon_competitor.rb', line 24

scope :unnamed, -> { where(name: [nil, '']) }

Instance Method Details

#display_nameObject

Display name (name or seller ID if unnamed)



52
53
54
# File 'app/models/amazon_competitor.rb', line 52

def display_name
  name.presence || seller_id
end

#storefront_url(marketplace: 'amazon.com') ⇒ Object



47
48
49
# File 'app/models/amazon_competitor.rb', line 47

def storefront_url(marketplace: 'amazon.com')
  self.class.storefront_url(seller_id, marketplace: marketplace)
end