Sha256: 3dfd37949c25d47ab3ffcb44b2ea4b8cb1169a91e26724b3abb5c5d44c8f6ed8

Contents?: true

Size: 1.02 KB

Versions: 34

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Spree
  class State < Spree::Base
    belongs_to :country, class_name: 'Spree::Country'
    has_many :addresses, dependent: :nullify

    validates :country, :name, presence: true

    scope :with_name_or_abbr, ->(name_or_abbr) do
      where(
        arel_table[:name].matches(name_or_abbr).or(
          arel_table[:abbr].matches(name_or_abbr)
        )
      )
    end
    class << self
      alias_method :find_all_by_name_or_abbr, :with_name_or_abbr
      deprecate find_all_by_name_or_abbr: :with_name_or_abbr, deprecator: Spree::Deprecation
    end

    # table of { country.id => [ state.id , state.name ] }, arrays sorted by name
    # blank is added elsewhere, if needed
    def self.states_group_by_country_id
      state_info = Hash.new { |h, k| h[k] = [] }
      order(:name).each { |state|
        state_info[state.country_id.to_s].push [state.id, state.name]
      }
      state_info
    end

    def <=>(other)
      name <=> other.name
    end

    def to_s
      name
    end
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
solidus_core-2.8.6 app/models/spree/state.rb
solidus_core-2.6.6 app/models/spree/state.rb
solidus_core-2.7.4 app/models/spree/state.rb
solidus_core-2.8.5 app/models/spree/state.rb
solidus_core-2.7.3 app/models/spree/state.rb
solidus_core-2.6.5 app/models/spree/state.rb
solidus_core-2.8.4 app/models/spree/state.rb
solidus_core-2.8.3 app/models/spree/state.rb
solidus_core-2.7.2 app/models/spree/state.rb
solidus_core-2.6.4 app/models/spree/state.rb
solidus_core-2.8.2 app/models/spree/state.rb
solidus_core-2.8.1 app/models/spree/state.rb
solidus_core-2.6.3 app/models/spree/state.rb
solidus_core-2.8.0 app/models/spree/state.rb
solidus_core-2.7.1 app/models/spree/state.rb
solidus_core-2.6.2 app/models/spree/state.rb
solidus_core_devise_token_auth-2.8.0.alpha.14 app/models/spree/state.rb
solidus_core_devise_token_auth-2.8.0.alpha.13 app/models/spree/state.rb
solidus_core_devise_token_auth-2.8.0.alpha.12 app/models/spree/state.rb
solidus_core_devise_token_auth-2.8.0.alpha.11 app/models/spree/state.rb