Sha256: 56db17d573bb9d0527b1456317b5baa0be9bb5edcc976765347562ed9b596f09

Contents?: true

Size: 1.15 KB

Versions: 8

Compression:

Stored size: 1.15 KB

Contents

module SluggableFinder
  # This module is included by the base class as well as AR asociation collections
  #
  module Finder
    def find_sluggable(opts,*args)
      key = args.first
      if key.is_a?(Symbol) || key.kind_of?(Array) || (key.to_s =~ /\A\d+\Z/ && opts[:allow_integer_ids]) # normal INT find
        find_without_slug(*args)
      else # sluggable find
        options = {:conditions => ["#{ opts[:to]} = ?", key]}
        error = "There is no #{opts[:sluggable_type]} with #{opts[:to]} '#{key}'"
        with_scope(:find => options) do
          find_without_slug(:first) or 
          raise SluggableFinder.not_found_exception.new(error)
        end
      end
    end
  end
  
  module BaseFinder
    
    def find_with_slug(*args)
      return find_without_slug(*args) unless respond_to?(:sluggable_finder_options)
      find_sluggable(sluggable_finder_options,*args)
    end
  end
  
  module AssociationProxyFinder
    def find_with_slug(*args)
      return find_without_slug(*args) unless @reflection.klass.respond_to?(:sluggable_finder_options)
      options = @reflection.klass.sluggable_finder_options
      find_sluggable(options,*args)
    end
  end
  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sluggable_finder-2.3.5 lib/sluggable_finder/finder.rb
sluggable_finder-2.3.4 lib/sluggable_finder/finder.rb
sluggable_finder-2.3.3 lib/sluggable_finder/finder.rb
sluggable_finder-2.3.2 lib/sluggable_finder/finder.rb
sluggable_finder-2.3.1 lib/sluggable_finder/finder.rb
sluggable_finder-2.3.0 lib/sluggable_finder/finder.rb
sluggable_finder-2.2.2 lib/sluggable_finder/finder.rb
sluggable_finder-2.2.1 lib/sluggable_finder/finder.rb