Sha256: ff13c2e4b467f8bb7b35b4bf252acacaf82dd1085381d8d5336f97d574202f19

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 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?(String) and !(key =~ /\A\d+\Z/))#only contain digits
          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
      else
        find_without_slug(*args)
      end
    end
  end
  
  module BaseFinder
    
    def find_with_slug(*args)
      return find_without_slug(*args) unless respond_to?(:sluggable_finder_options)
      options = sluggable_finder_options
      find_sluggable(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

3 entries across 3 versions & 2 rubygems

Version Path
ismasan-sluggable_finder-2.0.5 lib/sluggable_finder/finder.rb
ismasan-sluggable_finder-2.0.6 lib/sluggable_finder/finder.rb
sluggable_finder-2.1.1 lib/sluggable_finder/finder.rb