Sha256: 315fca4260c9c39143579cb59b4e93a2de20673efa305c889634cbdfbce11330

Contents?: true

Size: 1.16 KB

Versions: 23

Compression:

Stored size: 1.16 KB

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

    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

23 entries across 23 versions & 1 rubygems

Version Path
solidus_core-2.11.17 app/models/spree/state.rb
solidus_core-2.11.16 app/models/spree/state.rb
solidus_core-2.11.15 app/models/spree/state.rb
solidus_core-2.11.14 app/models/spree/state.rb
solidus_core-2.11.13 app/models/spree/state.rb
solidus_core-2.11.12 app/models/spree/state.rb
solidus_core-2.11.11 app/models/spree/state.rb
solidus_core-2.10.5 app/models/spree/state.rb
solidus_core-2.11.10 app/models/spree/state.rb
solidus_core-2.11.9 app/models/spree/state.rb
solidus_core-2.11.8 app/models/spree/state.rb
solidus_core-2.11.7 app/models/spree/state.rb
solidus_core-2.11.6 app/models/spree/state.rb
solidus_core-2.11.5 app/models/spree/state.rb
solidus_core-2.11.4 app/models/spree/state.rb
solidus_core-2.11.3 app/models/spree/state.rb
solidus_core-2.11.2 app/models/spree/state.rb
solidus_core-2.11.1 app/models/spree/state.rb
solidus_core-2.10.3 app/models/spree/state.rb
solidus_core-2.11.0 app/models/spree/state.rb