Sha256: 2003e451b34ff6a077c00c2b4217b04ba929e3585690b66a89f20ece9736b9d3

Contents?: true

Size: 1023 Bytes

Versions: 14

Compression:

Stored size: 1023 Bytes

Contents

module Spree
  class Store < Spree::Base
    has_many :orders, class_name: "Spree::Order"

    validates :code, presence: true, uniqueness: { allow_blank: true }
    validates :name, presence: true
    validates :url, presence: true
    validates :mail_from_address, presence: true

    before_save :ensure_default_exists_and_is_unique
    before_destroy :validate_not_default

    scope :by_url, lambda { |url| where("url like ?", "%#{url}%") }

    def self.current(domain = nil)
      current_store = domain ? Store.by_url(domain).first : nil
      current_store || Store.default
    end

    def self.default
      where(default: true).first || new
    end

    private

    def ensure_default_exists_and_is_unique
      if default
        Store.where.not(id: id).update_all(default: false)
      elsif Store.where(default: true).count == 0
        self.default = true
      end
    end

    def validate_not_default
      if default
        errors.add(:base, :cannot_destroy_default_store)
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
spree_core-3.0.10 app/models/spree/store.rb
spree_core-3.0.9 app/models/spree/store.rb
spree_core-3.0.8 app/models/spree/store.rb
spree_core-3.0.7 app/models/spree/store.rb
spree_core-3.0.6.1 app/models/spree/store.rb
spree_core-3.0.6 app/models/spree/store.rb
spree_core-3.0.5 app/models/spree/store.rb
spree_core-3.0.4 app/models/spree/store.rb
spree_core-3.0.3 app/models/spree/store.rb
spree_core-3.0.2 app/models/spree/store.rb
spree_core-3.0.1 app/models/spree/store.rb
spree_core-3.0.0 app/models/spree/store.rb
spree_core-3.0.0.rc4 app/models/spree/store.rb
spree_core-3.0.0.rc3 app/models/spree/store.rb