Module: Spree::CustomDomains::StoreDecorator

Defined in:
app/models/spree/custom_domains/store_decorator.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/models/spree/custom_domains/store_decorator.rb', line 4

def self.prepended(base)
  base.has_many :custom_domains, class_name: 'Spree::CustomDomain', dependent: :destroy
  base.has_one :default_custom_domain, -> { where(default: true) }, class_name: 'Spree::CustomDomain'

  # Host-based resolution: +by_custom_domain+ matches an attached custom
  # domain, +by_url+ matches the store's internal URL. Both are consumed
  # by +Spree::Stores::FindCurrent+.
  base.scope :by_custom_domain, ->(url) { left_joins(:custom_domains).where("#{Spree::CustomDomain.table_name}.url" => url) }
  base.scope :by_url, ->(url) { where(url: url).or(where("#{base.table_name}.url like ?", "%#{url}%")) }

  base.singleton_class.prepend(ClassMethods)
end

Instance Method Details

#formatted_custom_domainObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/spree/custom_domains/store_decorator.rb', line 41

def formatted_custom_domain
  return unless default_custom_domain

  @formatted_custom_domain ||= if Rails.env.development? || Rails.env.test?
    URI::Generic.build(
      scheme: Rails.application.routes.default_url_options[:protocol] || 'http',
      host: default_custom_domain.url,
      port: Rails.application.routes.default_url_options[:port]
    ).to_s
  else
    URI::HTTPS.build(host: default_custom_domain.url).to_s
  end
end

#formatted_url_or_custom_domainObject



33
34
35
# File 'app/models/spree/custom_domains/store_decorator.rb', line 33

def formatted_url_or_custom_domain
  formatted_custom_domain || super
end

#storefront_urlObject



37
38
39
# File 'app/models/spree/custom_domains/store_decorator.rb', line 37

def storefront_url
  formatted_custom_domain || super
end

#url_or_custom_domainObject

Prefer the store's default custom domain over its internal URL.



29
30
31
# File 'app/models/spree/custom_domains/store_decorator.rb', line 29

def url_or_custom_domain
  default_custom_domain&.url || super
end