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.4.27 app/models/workarea/navigable.rb
workarea-core-3.5.4 app/models/workarea/navigable.rb
workarea-core-3.4.26 app/models/workarea/navigable.rb
workarea-core-3.5.3 app/models/workarea/navigable.rb
workarea-core-3.4.25 app/models/workarea/navigable.rb
workarea-core-3.5.2 app/models/workarea/navigable.rb
workarea-core-3.4.24 app/models/workarea/navigable.rb
workarea-core-3.5.1 app/models/workarea/navigable.rb
workarea-core-3.4.23 app/models/workarea/navigable.rb
workarea-core-3.4.22 app/models/workarea/navigable.rb
workarea-core-3.5.0 app/models/workarea/navigable.rb
workarea-core-3.4.21 app/models/workarea/navigable.rb
workarea-core-3.5.0.beta.1 app/models/workarea/navigable.rb
workarea-core-3.4.20 app/models/workarea/navigable.rb
workarea-core-3.4.19 app/models/workarea/navigable.rb
workarea-core-3.4.18 app/models/workarea/navigable.rb
workarea-core-3.4.17 app/models/workarea/navigable.rb
workarea-core-3.4.16 app/models/workarea/navigable.rb
workarea-core-3.4.15 app/models/workarea/navigable.rb
workarea-core-3.4.14 app/models/workarea/navigable.rb