Sha256: cb801d9c96c9ea23784b0538adf41b674f0baadc4a0a378524a1466703b9b248

Contents?: true

Size: 1000 Bytes

Versions: 62

Compression:

Stored size: 1000 Bytes

Contents

module Workarea
  module Navigable
    extend ActiveSupport::Concern

    included do
      field :slug, type: String

      index({ slug: 1 }, { unique: true })

      validates :slug, presence: true, uniqueness: true

      has_one :taxon,
        inverse_of: :navigable,
        class_name: 'Workarea::Navigation::Taxon',
        dependent: :destroy

      before_validation :generate_slug, if: proc { |l| l.slug.blank? }
      after_validation :reset_slug!, if: proc { |l| l.errors.present? }
      after_save :update_taxon_slug
    end

    def to_param
      slug
    end

    def slug=(val)
      if val.nil?
        super(val)
      else
        super(val.to_s.parameterize)
      end
    end

    private

    def generate_slug
      return nil unless name.present?
      self.slug = FindUniqueSlug.new(self, name.delete("'").parameterize).slug
    end

    def update_taxon_slug
      return if taxon.blank? || changes['slug'].blank?
      taxon.set(navigable_slug: slug)
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-core-3.5.15 app/models/workarea/navigable.rb
workarea-core-3.4.36 app/models/workarea/navigable.rb
workarea-core-3.5.14 app/models/workarea/navigable.rb
workarea-core-3.4.35 app/models/workarea/navigable.rb
workarea-core-3.5.13 app/models/workarea/navigable.rb
workarea-core-3.4.34 app/models/workarea/navigable.rb
workarea-core-3.5.12 app/models/workarea/navigable.rb
workarea-core-3.4.33 app/models/workarea/navigable.rb
workarea-core-3.5.11 app/models/workarea/navigable.rb
workarea-core-3.5.10 app/models/workarea/navigable.rb
workarea-core-3.4.32 app/models/workarea/navigable.rb
workarea-core-3.5.9 app/models/workarea/navigable.rb
workarea-core-3.4.31 app/models/workarea/navigable.rb
workarea-core-3.5.8 app/models/workarea/navigable.rb
workarea-core-3.4.30 app/models/workarea/navigable.rb
workarea-core-3.5.7 app/models/workarea/navigable.rb
workarea-core-3.4.29 app/models/workarea/navigable.rb
workarea-core-3.5.6 app/models/workarea/navigable.rb
workarea-core-3.4.28 app/models/workarea/navigable.rb
workarea-core-3.5.5 app/models/workarea/navigable.rb