Sha256: 6b6ab9cf15af24d82d6be9936f29072fbf6288d562143c1453c803ff6f4b6225

Contents?: true

Size: 1.61 KB

Versions: 25

Compression:

Stored size: 1.61 KB

Contents

namespace :friendly_id do
  desc "Make slugs for a model."
  task :make_slugs => :environment do
    raise 'USAGE: rake friendly_id:make_slugs MODEL=MyModelName' if ENV["MODEL"].nil?
    if !sluggable_class.friendly_id_options[:use_slug]
      raise "Class \"#{sluggable_class.to_s}\" doesn't appear to be using slugs"
    end
    while records = sluggable_class.find(:all, :include => :slugs, :conditions => "slugs.id IS NULL", :limit => 1000) do
      break if records.size == 0
      records.each do |r|
        r.send(:set_slug)
        r.save!
        puts "#{sluggable_class.to_s}(#{r.id}) friendly_id set to \"#{r.slug.name}\""
      end
    end
  end

  desc "Regenereate slugs for a model."
  task :redo_slugs => :environment do
    raise 'USAGE: rake friendly_id:redo_slugs MODEL=MyModelName' if ENV["MODEL"].nil?
    if !sluggable_class.friendly_id_options[:use_slug]
      raise "Class \"#{sluggable_class.to_s}\" doesn't appear to be using slugs"
    end
    Slug.destroy_all(["sluggable_type = ?", sluggable_class.to_s])
    Rake::Task["friendly_id:make_slugs"].invoke
  end

  desc "Kill obsolete slugs older than 45 days."
  task :remove_old_slugs => :environment do
    if ENV["DAYS"].nil?
      @days = 45
    else
      @days = ENV["DAYS"].to_i
    end
    slugs = Slug.find(:all, :conditions => ["created_at < ?", DateTime.now - @days.days])
    slugs.each do |s|
      s.destroy if !s.is_most_recent?
    end
  end
end

def sluggable_class
  if (ENV["MODEL"].split('::').size > 1)
    ENV["MODEL"].split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
  else
    Object.const_get(ENV["MODEL"])
  end
end

Version data entries

25 entries across 25 versions & 5 rubygems

Version Path
dougcole-friendly_id-2.0.2 lib/tasks/friendly_id.rake
dougcole-friendly_id-2.0.3 lib/tasks/friendly_id.rake
dougcole-friendly_id-2.0.4 lib/tasks/friendly_id.rake
dougcole-friendly_id-2.0.5 lib/tasks/friendly_id.rake
dougcole-friendly_id-2.0.6 lib/tasks/friendly_id.rake
norman-friendly_id-2.0.1 lib/tasks/friendly_id.rake
norman-friendly_id-2.0.2 lib/tasks/friendly_id.rake
norman-friendly_id-2.0.3 lib/tasks/friendly_id.rake
norman-friendly_id-2.0.4 lib/tasks/friendly_id.rake
norman-friendly_id-2.1.0 lib/tasks/friendly_id.rake
norman-friendly_id-2.1.1 lib/tasks/friendly_id.rake
norman-friendly_id-2.1.2 lib/tasks/friendly_id.rake
norman-friendly_id-2.1.3 lib/tasks/friendly_id.rake
rakutenusa-friendly_id-2.0.6 lib/tasks/friendly_id.rake
rakutenusa-friendly_id-2.0.7 lib/tasks/friendly_id.rake
alancse-friendly_id-2.0.1 lib/tasks/friendly_id.rake
friendly_id-2.0.2 lib/tasks/friendly_id.rake
friendly_id-2.0.3 lib/tasks/friendly_id.rake
friendly_id-2.0.0 lib/tasks/friendly_id.rake
friendly_id-2.0.4 lib/tasks/friendly_id.rake