Class: FriendlyId::TaskRunner
- Inherits:
-
Object
- Object
- FriendlyId::TaskRunner
- Extended by:
- Forwardable
- Defined in:
- lib/friendly_id/active_record_adapter/tasks.rb
Constant Summary
- OLD_SLUG_DAYS =
45
Instance Attribute Summary (collapse)
-
- (Object) days
Returns the value of attribute days.
-
- (Object) klass
Returns the value of attribute klass.
-
- (Object) task_options
Returns the value of attribute task_options.
Instance Method Summary (collapse)
- - (Object) delete_old_slugs
- - (Object) delete_slugs
-
- (TaskRunner) initialize(&block)
constructor
A new instance of TaskRunner.
- - (Object) make_slugs
- - (Object) validate_uses_slugs
Constructor Details
- (TaskRunner) initialize(&block)
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
- (Object) days
Returns the value of attribute days
6 7 8 |
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 6 def days @days end |
- (Object) klass
Returns the value of attribute klass
7 8 9 |
# File 'lib/friendly_id/active_record_adapter/tasks.rb', line 7 def klass @klass end |
- (Object) task_options
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
- (Object) delete_old_slugs
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 |
- (Object) delete_slugs
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 |
- (Object) make_slugs
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 |
- (Object) validate_uses_slugs
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 |