Sha256: 5f1a80b6b2b53582184a39b704191553f7b0e8d2119b6ceb20cc4c23b23f65d4

Contents?: true

Size: 1 KB

Versions: 30

Compression:

Stored size: 1 KB

Contents

#
# This module builds permalink when the record is created for the very first time.
#
# Usage:
#
# class Product < ActiveRecord::Base
#   include BuildPermalink
#   build_permalink allow_nil: true
# end
#
# build_permalink line is needed only if you are passing parameter otherwise that line can be omitted
#
module Permalink
  module Builder
    extend ActiveSupport::Concern

    included do
      before_create :set_permalink
      class_attribute :permalink_options
      self.permalink_options = {}
    end

    module ClassMethods
      def build_permalink(options = {})
        self.permalink_options = options
      end
    end

    def to_param
      self.permalink
    end

    def set_permalink
      return if self.name.blank? && permalink_options[:allow_nil]
      permalink = self.name.parameterize
      counter = 2

      while self.class.exists?(permalink: permalink) do
        permalink = "#{permalink}-#{counter}"
        counter = counter + 1
      end

      self.permalink ||= permalink
    end
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
nimbleshop_core-0.0.23 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.21 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.20 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.19 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.17 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.16 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.15 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.14 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.14.rc2 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.14.rc1 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.13 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.12 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.11 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.10 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.9 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.8 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.7 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.5 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.4.beta1 lib/nimbleshop/permalink/builder.rb
nimbleshop_core-0.0.4 lib/nimbleshop/permalink/builder.rb