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

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

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::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#seller_idString

Returns:

  • (String)


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

validates :seller_id, presence: true, uniqueness: true

Class Method Details

.name_for(seller_id) ⇒ String

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

Parameters:

  • seller_id (String)

Returns:

  • (String)


33
34
35
36
37
38
# File 'app/models/amazon_competitor.rb', line 33

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:



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

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

.named?(seller_id) ⇒ Boolean

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

Parameters:

  • seller_id (String)

Returns:

  • (Boolean)


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

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') ⇒ String

Get storefront URL for a seller.

Parameters:

  • seller_id (String)
  • marketplace (String) (defaults to: 'amazon.com')

Returns:

  • (String)


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

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:



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

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

Instance Method Details

#display_nameObject

Display name (name or seller ID if unnamed)



64
65
66
# File 'app/models/amazon_competitor.rb', line 64

def display_name
  name.presence || seller_id
end

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

Parameters:

  • marketplace (String) (defaults to: 'amazon.com')

Returns:

  • (String)


59
60
61
# File 'app/models/amazon_competitor.rb', line 59

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