Sha256: 34594d3f358cd9ded1c87b440be94037aa16d7f93b4510626fb373878f6ef348

Contents?: true

Size: 1.98 KB

Versions: 37

Compression:

Stored size: 1.98 KB

Contents

require 'stringex'

module Spree
  module Core
    module Permalinks
      extend ActiveSupport::Concern

      included do
        class_attribute :permalink_options
      end

      module ClassMethods
        def make_permalink(options = {})
          options[:field] ||= :permalink
          self.permalink_options = options

          before_validation(on: :create) { save_permalink }
        end

        def find_by_param(value, *args)
          send("find_by_#{permalink_field}", value, *args)
        end

        def find_by_param!(value, *args)
          send("find_by_#{permalink_field}!", value, *args)
        end

        def permalink_field
          permalink_options[:field]
        end

        def permalink_prefix
          permalink_options[:prefix] || ""
        end

        def permalink_length
          permalink_options[:length] || 9
        end

        def permalink_order
          order = permalink_options[:order]
          "#{order} ASC," if order
        end
      end

      def generate_permalink
        "#{self.class.permalink_prefix}#{Array.new(self.class.permalink_length){ rand(9) }.join}"
      end

      def save_permalink(permalink_value = to_param)
        with_lock do
          permalink_value ||= generate_permalink

          field = self.class.permalink_field
          # Do other links exist with this permalink?
          other = self.class.where("#{self.class.table_name}.#{field} LIKE ?", "#{permalink_value}%")
          if other.any?
            # Find the existing permalink with the highest number, and increment that number.
            # (If none of the existing permalinks have a number, this will evaluate to 1.)
            number = other.map { |o| o.send(field)[/-(\d+)$/, 1].to_i }.max + 1
            permalink_value += "-#{number}"
          end
          write_attribute(field, permalink_value)
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, Spree::Core::Permalinks
ActiveRecord::Relation.send :include, Spree::Core::Permalinks

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
solidus_core-2.4.2 lib/spree/core/permalinks.rb
solidus_core-2.3.1 lib/spree/core/permalinks.rb
solidus_core-2.4.1 lib/spree/core/permalinks.rb
solidus_core-2.2.2 lib/spree/core/permalinks.rb
solidus_core-2.1.1 lib/spree/core/permalinks.rb
solidus_core-2.0.3 lib/spree/core/permalinks.rb
solidus_core-1.4.2 lib/spree/core/permalinks.rb
solidus_core-1.3.2 lib/spree/core/permalinks.rb
solidus_core-2.4.0 lib/spree/core/permalinks.rb
solidus_core-2.4.0.rc1 lib/spree/core/permalinks.rb
solidus_core-2.4.0.beta1 lib/spree/core/permalinks.rb
solidus_core-2.3.0 lib/spree/core/permalinks.rb
solidus_core-2.3.0.rc3 lib/spree/core/permalinks.rb
solidus_core-2.3.0.rc2 lib/spree/core/permalinks.rb
solidus_core-2.3.0.rc1 lib/spree/core/permalinks.rb
solidus_core-2.3.0.beta1 lib/spree/core/permalinks.rb
solidus_core-2.0.2 lib/spree/core/permalinks.rb
solidus_core-1.4.1 lib/spree/core/permalinks.rb
solidus_core-2.2.1 lib/spree/core/permalinks.rb
solidus_core-2.2.0 lib/spree/core/permalinks.rb