Sha256: 76c465630deb7f61c59380ac37c4432b5c81cfa34bd6cfed89703affe8fb246f

Contents?: true

Size: 1.28 KB

Versions: 5

Compression:

Stored size: 1.28 KB

Contents

# Geopolitical Helpers
module Geopolitocracy
  extend ActiveSupport::Concern

  included do
    # field :gid,    type: Integer # geonames id

    field :name,   type: String, localize: true
    field :abbr,   type: String
    field :nick,   type: String

    field :souls,  type: Integer  # Population

    field :ascii,  type: String
    field :code,   type: String
    field :slug,   type: String  # , default: -> { name }

    field :postal,  type: String  # , default: -> { name }
    field :phone,   type: String  # , default: -> { name }

    alias_method :population, :souls
    alias_method :iso_3166_2, :code

    validates :name, presence: true
    validates :slug, presence: true, uniqueness: true
    validates :code, uniqueness: { allow_nil: true }

    index slug: 1
    index name: 1

    before_validation :ensure_slug

    scope :ordered, -> { order_by(name: 1) }

    def self.search(txt, lazy = false)
      key = ActiveSupport::Inflector.transliterate(txt).gsub(/\W/, '-')
      char = lazy ? nil : '$'
      where(slug: /^#{key}#{char}/i)
    end
  end

  def ensure_slug
    self.slug ||= name
  end

  def slug=(txt)
    return unless txt
    self[:slug] = ActiveSupport::Inflector.transliterate(txt)
                  .gsub(/\W/, '-').downcase
  end

  def to_s
    name || slug
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
geopolitical-1.0.2 app/models/concerns/geopolitocracy.rb
geopolitical-1.0.0 app/models/concerns/geopolitocracy.rb
geopolitical-0.9.9 app/models/concerns/geopolitocracy.rb
geopolitical-0.9.7 app/models/concerns/geopolitocracy.rb
geopolitical-0.9.5 app/models/concerns/geopolitocracy.rb