Sha256: a9a5955b9bd02de5e48dcaf7c6ef1e91f7108506314a2add807d5cc09088e78c

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

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

    has_many :zone_members,
             -> { where(zoneable_type: 'Spree::State') },
             class_name: 'Spree::ZoneMember',
             dependent: :destroy,
             foreign_key: :zoneable_id

    has_many :zones, through: :zone_members, class_name: 'Spree::Zone'

    validates :country, :name, presence: true

    self.whitelisted_ransackable_attributes = %w(abbr)

    def self.find_all_by_name_or_abbr(name_or_abbr)
      where('name = ? OR abbr = ?', name_or_abbr, name_or_abbr)
    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] = [] }
      self.order('name ASC').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

6 entries across 6 versions & 1 rubygems

Version Path
spree_core-3.0.10 app/models/spree/state.rb
spree_core-3.0.9 app/models/spree/state.rb
spree_core-3.0.8 app/models/spree/state.rb
spree_core-3.0.7 app/models/spree/state.rb
spree_core-3.0.6.1 app/models/spree/state.rb
spree_core-3.0.6 app/models/spree/state.rb