Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit c285b71f912f9186e8139823166c860273b04ab5 Ismael Celis committed Nov 4, 2008
@@ -0,0 +1,5 @@
+*~
+*.cache
+*.log
+.DS_Store
+*.db
@@ -0,0 +1,4 @@
+== 0.0.1 2008-11-03
+
+* 1 major enhancement:
+ * Initial release
@@ -0,0 +1,17 @@
+History.txt
+Manifest.txt
+PostInstall.txt
+README.rdoc
+Rakefile
+lib/sluggable_finder.rb
+lib/sluggable_finder/core_ext.rb
+lib/sluggable_finder/finder.rb
+lib/sluggable_finder/orm.rb
+script/console
+script/destroy
+script/generate
+spec/sluggable_finder_spec.rb
+spec/spec.opts
+spec/spec_helper.rb
+tasks/rspec.rake
+tasks/db.rake
@@ -0,0 +1,7 @@
+
+For more information on sluggable_finder, see http://sluggable_finder.rubyforge.org
+
+NOTE: Change this information in PostInstall.txt
+You can also delete it if you don't want it.
+
+
@@ -0,0 +1,48 @@
+= sluggable_finder
+
+* FIX (url)
+
+== DESCRIPTION:
+
+FIX (describe your package)
+
+== FEATURES/PROBLEMS:
+
+* FIX (list of features or problems)
+
+== SYNOPSIS:
+
+ FIX (code sample of usage)
+
+== REQUIREMENTS:
+
+* FIX (list of requirements)
+
+== INSTALL:
+
+* FIX (sudo gem install, anything else)
+
+== LICENSE:
+
+(The MIT License)
+
+Copyright (c) 2008 Ismael Celis
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
+%w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
+require File.dirname(__FILE__) + '/lib/sluggable_finder'
+
+# Generate all the Rake tasks
+# Run 'rake -T' to see list of generated tasks (from gem root directory)
+$hoe = Hoe.new('sluggable_finder', SluggableFinder::VERSION) do |p|
+ p.developer('Ismael Celis', 'ismaelct@gmail.com')
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
+ p.rubyforge_name = p.name # TODO this is default value
+ # p.extra_deps = [
+ # ['activesupport','>= 2.0.2'],
+ # ]
+ p.extra_dev_deps = [
+ ['newgem', ">= #{::Newgem::VERSION}"]
+ ]
+
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
+ p.rsync_args = '-av --delete --ignore-errors'
+end
+
+require 'newgem/tasks' # load /tasks/*.rake
+Dir['tasks/**/*.rake'].each { |t| load t }
+
+# TODO - want other tests/tasks run by default? Add them to the list
+# task :default => [:spec, :features]
@@ -0,0 +1,37 @@
+$:.unshift(File.dirname(__FILE__)) unless
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
+
+module SluggableFinder
+ VERSION = '2.0.0'
+
+ class << self
+
+ def enable_activerecord
+ ActiveRecord::Base.extend Finder
+ ActiveRecord::Base.class_eval{include Orm}
+ # support for associations
+ a = ActiveRecord::Associations
+ returning([ a::AssociationCollection ]) { |classes|
+ # detect http://dev.rubyonrails.org/changeset/9230
+ unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation
+ classes << a::HasManyThroughAssociation
+ end
+ }.each do |klass|
+ klass.send :include, Finder
+ klass.class_eval { alias_method_chain :find, :slug }
+ end
+
+ end
+
+ end
+
+end
+
+require 'rubygems'
+require 'active_record'
+
+Dir.glob('lib/sluggable_finder/*.rb').each do |file|
+ require file
+end
+
+SluggableFinder.enable_activerecord if (defined?(ActiveRecord) && !ActiveRecord::Base.respond_to?(:sluggable_finder))
@@ -0,0 +1,8 @@
+require 'iconv'
+class String
+ # Iconv use borrowed from http://svn.robertrevans.com/plugins/Permalize/
+ # Thanks!
+ def to_slug
+ (Iconv.new('US-ASCII//TRANSLIT', 'utf-8').iconv self).gsub(/[^\w\s\-\—]/,'').gsub(/[^\w]|[\_]/,' ').split.join('-').downcase
+ end
+end
@@ -0,0 +1,19 @@
+module SluggableFinder
+ # This module is included by the base class as well as AR asociation collections
+ #
+ module Finder
+
+ def find_with_slug(*args)
+ if (args.first.is_a?(String) and !(args.first =~ /\A\d+\Z/))#only contain digits
+ logger.info "++++++++++++++++ #{args.inspect}"
+ options = {:conditions => ["#{ sluggable_finder_options[:to]} = ?", args.first]}
+ first(options) or
+ raise ActiveRecord::RecordNotFound.new("There is no #{sluggable_finder_options[:sluggable_type]} with #{sluggable_finder_options[:to]} '#{args.first}'")
+ else
+ logger.info "++++++++++++++++ NOOOOOOO #{args.inspect}"
+ find_without_slug(*args)
+ end
+ end
+ end
+
+end
@@ -0,0 +1,111 @@
+module SluggableFinder
+ module Orm
+
+ def self.included(base)
+ base.extend ClassMethods
+ base.class_eval{include InstanceMethods}
+ class << base
+ alias_method_chain :find, :slug
+ end
+ end
+
+ module ClassMethods
+
+ def sluggable_finder(field = :title, options = {})
+ write_inheritable_attribute(:sluggable_finder_options, {
+ :sluggable_type => ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s,
+ :from => field,
+ :scope => nil,
+ :to => :slug,
+ :reserved_slugs => []
+ }.merge( options ))
+ class_inheritable_reader :sluggable_finder_options
+
+ if sluggable_finder_options[:scope]
+ scope_condition_method = %(
+ def scope_condition
+ "#{sluggable_finder_options[:scope].to_s} = \#{#{sluggable_finder_options[:scope].to_s}}"
+ end
+ )
+ else
+ scope_condition_method = %(
+ def scope_condition
+ '1 = 1'
+ end
+ )
+ end
+
+ class_eval <<-EOV
+
+ def slugable_class
+ ::#{self.name}
+ end
+
+ def source_column
+ "#{sluggable_finder_options[:from]}"
+ end
+
+ def destination_column
+ "#{sluggable_finder_options[:to]}"
+ end
+
+ def the_scope
+ "#{sluggable_finder_options[:scope]}"
+ end
+
+ def to_param
+ send("#{sluggable_finder_options[:to]}")
+ end
+
+ #{scope_condition_method}
+
+ after_validation :set_slug
+ EOV
+
+ end
+
+ end
+
+ module InstanceMethods
+
+
+ def set_slug
+ s = self.create_sluggable_slug
+ write_attribute(destination_column, s)
+ end
+
+ def create_sluggable_slug
+ suffix = ''
+ begin
+ proposed_slug = if self.send(destination_column.to_sym).blank?
+ self.send(source_column.to_sym).to_slug
+ else
+ self.send(destination_column.to_sym).to_slug
+ end
+ rescue Exception => e
+ raise e
+ end
+ cond = if new_record?
+ ''
+ else
+ "id != #{id} AND "
+ end
+ slugable_class.transaction do
+ #case insensitive
+ existing = slugable_class.find(:first, :conditions => ["#{cond}#{destination_column} LIKE ? and #{scope_condition}", proposed_slug + suffix])
+ while existing != nil or sluggable_finder_options[:reserved_slugs].include?(proposed_slug + suffix)
+ if suffix.empty?
+ suffix = "-2"
+ else
+ suffix.succ!
+ end
+ existing = slugable_class.find(:first, :conditions => ["#{cond}#{destination_column} = ? and #{scope_condition}", proposed_slug + suffix])
+ end
+ end # end of transaction
+ proposed_slug + suffix
+ end
+
+ end
+
+ end
+end
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+# File: script/console
+irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
+
+libs = " -r irb/completion"
+# Perhaps use a console_lib to store any extra methods I may want available in the cosole
+# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
+libs << " -r #{File.dirname(__FILE__) + '/../lib/sluggable_finder.rb'}"
+puts "Loading sluggable_finder gem"
+exec "#{irb} #{libs} --simple-prompt"
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/destroy'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Destroy.new.run(ARGV)
@@ -0,0 +1,14 @@
+#!/usr/bin/env ruby
+APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+begin
+ require 'rubigen'
+rescue LoadError
+ require 'rubygems'
+ require 'rubigen'
+end
+require 'rubigen/scripts/generate'
+
+ARGV.shift if ['--help', '-h'].include?(ARGV[0])
+RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
+RubiGen::Scripts::Generate.new.run(ARGV)
Oops, something went wrong.

0 comments on commit c285b71

Please sign in to comment.