Class: FriendlyId::TaskRunner
- Extended by:
- Forwardable
- Defined in:
- lib/friendly_id/active_record_adapter/tasks.rb
Constant Summary collapse
- OLD_SLUG_DAYS =
45
Instance Attribute Summary collapse
-
#days ⇒ Object
Returns the value of attribute days.
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#task_options ⇒ Object
Returns the value of attribute task_options.
Instance Method Summary collapse
- #delete_old_slugs ⇒ Object
- #delete_slugs ⇒ Object
-
#initialize(&block) ⇒ TaskRunner
constructor
A new instance of TaskRunner.
- #make_slugs ⇒ Object
- #validate_uses_slugs ⇒ Object
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
#days ⇒ Object
Returns the value of attribute days.
6 7 8 |
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 6 def days @days end |
#klass ⇒ Object
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_options ⇒ Object
Returns the value of attribute task_options.
8 9 10 |
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 8 def @task_options end |
Instance Method Details
#delete_old_slugs ⇒ Object
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_slugs ⇒ Object
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_slugs ⇒ Object
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 = { :include => :slug, :limit => (ENV["LIMIT"] || 100).to_i, :offset => 0, :order => ENV["ORDER"] || "#{klass.table_name}.#{klass.primary_key} ASC", }.merge( || {}) while records = find(:all, ) do break if records.size == 0 records.each do |record| record.save(:validate => false) unless record.slug? yield(record) if block_given? end [:offset] += [:limit] end end |
#validate_uses_slugs ⇒ Object
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 |