Sha256: 252e0e0f1ac2911fae814b2c5c51edc31bec8ff9731b7073ee877eb3562f65f3

Contents?: true

Size: 1005 Bytes

Versions: 31

Compression:

Stored size: 1005 Bytes

Contents

# frozen_string_literal: true

module Spree
  class State < Spree::Base
    belongs_to :country, class_name: 'Spree::Country', optional: true
    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

    self.whitelisted_ransackable_attributes = %w[name]

    # 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 { |hash, key| hash[key] = [] }
      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

    def state_with_country
      "#{name} (#{country})"
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
solidus_core-3.2.9 app/models/spree/state.rb
solidus_core-3.2.8 app/models/spree/state.rb
solidus_core-3.2.7 app/models/spree/state.rb
solidus_core-3.2.6 app/models/spree/state.rb
solidus_core-3.1.9 app/models/spree/state.rb
solidus_core-3.2.5 app/models/spree/state.rb
solidus_core-3.2.4 app/models/spree/state.rb
solidus_core-3.2.3 app/models/spree/state.rb
solidus_core-3.1.8 app/models/spree/state.rb
solidus_core-3.0.8 app/models/spree/state.rb
solidus_core-3.2.2 app/models/spree/state.rb
solidus_core-3.2.1 app/models/spree/state.rb
solidus_core-3.2.0 app/models/spree/state.rb
solidus_core-3.2.0.alpha app/models/spree/state.rb
solidus_core-3.0.7 app/models/spree/state.rb
solidus_core-3.1.7 app/models/spree/state.rb
solidus_core-3.0.6 app/models/spree/state.rb
solidus_core-3.1.6 app/models/spree/state.rb
solidus_core-3.0.5 app/models/spree/state.rb
solidus_core-3.1.5 app/models/spree/state.rb