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.27 app/models/workarea/navigable.rb
workarea-core-3.5.26 app/models/workarea/navigable.rb
workarea-core-3.4.45 app/models/workarea/navigable.rb
workarea-core-3.5.25 app/models/workarea/navigable.rb
workarea-core-3.5.23 app/models/workarea/navigable.rb
workarea-core-3.4.44 app/models/workarea/navigable.rb
workarea-core-3.5.22 app/models/workarea/navigable.rb
workarea-core-3.4.43 app/models/workarea/navigable.rb
workarea-core-3.5.21 app/models/workarea/navigable.rb
workarea-core-3.4.42 app/models/workarea/navigable.rb
workarea-core-3.5.20 app/models/workarea/navigable.rb
workarea-core-3.4.41 app/models/workarea/navigable.rb
workarea-core-3.5.19 app/models/workarea/navigable.rb
workarea-core-3.4.40 app/models/workarea/navigable.rb
workarea-core-3.5.18 app/models/workarea/navigable.rb
workarea-core-3.4.39 app/models/workarea/navigable.rb
workarea-core-3.5.17 app/models/workarea/navigable.rb
workarea-core-3.4.38 app/models/workarea/navigable.rb
workarea-core-3.5.16 app/models/workarea/navigable.rb
workarea-core-3.4.37 app/models/workarea/navigable.rb