Class: FriendlyId::TaskRunner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/friendly_id/active_record_adapter/tasks.rb

Constant Summary collapse

OLD_SLUG_DAYS =
45

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ TaskRunner

Returns a new instance of TaskRunner.



14
15
16
17
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 14

def initialize(&block)
  self.klass = ENV["MODEL"]
  self.days  = ENV["DAYS"]
end

Instance Attribute Details

#daysObject

Returns the value of attribute days.



6
7
8
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 6

def days
  @days
end

#klassObject

Returns the value of attribute klass.



7
8
9
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 7

def klass
  @klass
end

#task_optionsObject

Returns the value of attribute task_options.



8
9
10
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 8

def task_options
  @task_options
end

Instance Method Details

#delete_old_slugsObject



54
55
56
57
58
59
60
61
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 54

def delete_old_slugs
  conditions = ["created_at < ?", DateTime.now - days]
  if klass
    conditions[0] << " AND sluggable_type = ?"
    conditions << klass.to_s
  end
  Slug.all(:conditions => conditions).select(&:outdated?).map(&:destroy)
end

#delete_slugsObject



46
47
48
49
50
51
52
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 46

def delete_slugs
  validate_uses_slugs
  Slug.destroy_all(["sluggable_type = ?", klass.to_s])
  if column = friendly_id_config.cache_column
    update_all("#{column} = NULL")
  end
end

#make_slugsObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 27

def make_slugs
  validate_uses_slugs
  options = {
    :include => :slug,
    :limit   => (ENV["LIMIT"] || 100).to_i,
    :offset  => 0,
    :order   => ENV["ORDER"] || "#{klass.table_name}.#{klass.primary_key} ASC",
  }.merge(task_options || {})

  while records = find(:all, options) do
    break if records.size == 0
    records.each do |record|
      record.save(:validate => false) unless record.slug?
      yield(record) if block_given?
    end
    options[:offset] += options[:limit]
  end
end

#validate_uses_slugsObject



63
64
65
66
67
68
69
70
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 63

def validate_uses_slugs
  (raise "You need to pass a MODEL=<model name> argument to rake") if klass.blank?
  unless friendly_id_config.use_slug?
    raise "Class '%s' doesn't use slugs" % klass.to_s
  end
rescue NoMethodError
  raise "Class '%s' doesn't use FriendlyId" % klass.to_s
end