Sha256: 1fa8a0851c49ca27ce5122fbed1c3227bcaf8f906b83f1082d5889af9bd73ae2

Contents?: true

Size: 960 Bytes

Versions: 1

Compression:

Stored size: 960 Bytes

Contents

# Geopolitical Helpers
module Geopolitocracy
  extend ActiveSupport::Concern

  included do
    field :name,   type: String, localize: true
    field :abbr,   type: String
    field :nick,   type: String
    field :gid,    type: Integer # geonames id

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

    validates :name, presence: true
    validates :slug, presence: true, uniqueness: true

    index slug: 1
    index name: 1

    before_validation :ensure_slug

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

    def self.search(txt)
      txt.gsub!(/\s/, '-')
      where(slug: /^#{ActiveSupport::Inflector.transliterate(txt)}/i)
    end
  end

  def ensure_slug
    self.slug ||= name
  end

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

  def to_s
    name || slug
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
geopolitical-0.8.8 app/models/concerns/geopolitocracy.rb