Sha256: a0a4b9c90445fc1bbbe2d3931ac4e8b23f47b8758ba1e3519af9c7ef478c17b5

Contents?: true

Size: 1.95 KB

Versions: 5

Compression:

Stored size: 1.95 KB

Contents

module FriendlyId

  module ActiveRecordAdapter

    # Extends FriendlyId::Configuration with some implementation details and
    # features specific to ActiveRecord.
    class Configuration < FriendlyId::Configuration

      # The column used to cache the friendly_id string. If no column is specified,
      # FriendlyId will look for a column named +cached_slug+ and use it automatically
      # if it exists. If for some reason you have a column named +cached_slug+
      # but don't want FriendlyId to modify it, pass the option
      # +:cache_column => false+ to {FriendlyId::ActiveRecordAdapter#has_friendly_id has_friendly_id}.
      attr_accessor :cache_column

      # An array of classes for which the configured class serves as a
      # FriendlyId scope.
      attr_reader :child_scopes

      attr_reader :custom_cache_column

      def cache_column
        return @cache_column if defined?(@cache_column)
        @cache_column = autodiscover_cache_column
      end

      def cache_column?
        !! cache_column
      end

      def cache_column=(cache_column)
        @cache_column = cache_column
        @custom_cache_column = cache_column
      end

      def child_scopes
        @child_scopes ||= associated_friendly_classes.select { |klass| klass.friendly_id_config.scopes_over?(configured_class) }
      end

      def custom_cache_column?
        !! custom_cache_column
      end

      def scope_for(record)
        scope? ? record.send(scope).to_param : nil
      end

      def scopes_over?(klass)
        scope? && scope == klass.to_s.underscore.to_sym
      end

      private

      def autodiscover_cache_column
        :cached_slug if configured_class.columns.any? { |column| column.name == 'cached_slug' }
      end

      def associated_friendly_classes
        configured_class.reflect_on_all_associations.select { |assoc|
          !assoc.options[:polymorphic] &&
          assoc.klass.uses_friendly_id?
        }.map(&:klass)
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
cmassimo-friendly_id-3.0.4.2 lib/friendly_id/active_record_adapter/configuration.rb
friendly_id-3.0.6 lib/friendly_id/active_record_adapter/configuration.rb
friendly_id-3.0.5 lib/friendly_id/active_record_adapter/configuration.rb
friendly_id-3.0.4 lib/friendly_id/active_record_adapter/configuration.rb
friendly_id-3.0.3 lib/friendly_id/active_record_adapter/configuration.rb