Sha256: 44f99783db399061700b8ad500dca0f804a840cbcb10f15daac2933ada2f22a8

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

module Slugs
  module ActiveRecord 
    module Translatable
      extend ActiveSupport::Concern
 
      protected

      def generate_slugs
        old_locale = current_locale
        I18n.available_locales.each do |locale|
          I18n.locale = locale
          with_locale locale
          generate_slug
        end
        I18n.locale = old_locale
        with_locale old_locale
      end

      module ClassMethods

        def find_previous_slug(slug)
          t = reflect_on_association(:translations)
          joins(
            "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
          ).where(
            "(t.slug LIKE '#{slug}-%' OR t.slug = '#{slug}') AND t.locale = '#{I18n.locale}'"
          ).order(
            'LENGTH(t.slug) DESC, t.slug DESC'
          ).pluck('t.slug').select{ |r| r =~ /^#{slug}(-\d+)?$/ }.first
        end

        def find_by_slug(id)
          t = reflect_on_association(:translations)
          joins(
            "INNER JOIN #{t.table_name} t ON t.#{t.foreign_key} = #{table_name}.#{t.active_record_primary_key}"
          ).where(
            "t.slug = '#{id}' AND t.locale = '#{I18n.locale}'"
          ).readonly(false).first
        end
 
        def exists_by_slug(id)
          t = reflect_on_association(:translations)
          joins(:translations).exists? t.table_name.to_sym => { slug: id, locale: I18n.locale }
        end

      end      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slugs-1.2.3 lib/slugs/active_record/translatable.rb