app/models/concerns/geopolitocracy.rb in geopolitical-0.9.3 vs app/models/concerns/geopolitocracy.rb in geopolitical-0.9.5

- old
+ new

@@ -1,11 +1,11 @@ # Geopolitical Helpers module Geopolitocracy extend ActiveSupport::Concern included do - field :gid, type: Integer # geonames id + # field :gid, type: Integer # geonames id field :name, type: String, localize: true field :abbr, type: String field :nick, type: String @@ -21,31 +21,33 @@ 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) - txt.gsub!(/\s/, '-') - where(slug: /^#{ActiveSupport::Inflector.transliterate(txt)}/i) + 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(/\s/, '-').downcase + .gsub(/\W/, '-').downcase end def to_s name || slug end