Class: FriendlyId::TaskRunner
- Inherits:
-
Object
- Object
- FriendlyId::TaskRunner
- Extended by:
- Forwardable
- Defined in:
- lib/friendly_id/datamapper_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.
16 17 18 19 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 16 def initialize(&block) self.klass = ENV["MODEL"] self.days = ENV["DAYS"] end |
Instance Attribute Details
#days ⇒ Object
Returns the value of attribute days.
8 9 10 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 8 def days @days end |
#klass ⇒ Object
Returns the value of attribute klass.
9 10 11 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 9 def klass @klass end |
#task_options ⇒ Object
Returns the value of attribute task_options.
10 11 12 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 10 def @task_options end |
Instance Method Details
#delete_old_slugs ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 51 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
43 44 45 46 47 48 49 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 43 def delete_slugs validate_uses_slugs Slug.all(:sluggable_type => klass).destroy! if column = friendly_id_config.cache_column klass.all.update!(column => nil) end end |
#make_slugs ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 29 def make_slugs validate_uses_slugs = {:limit => 100, :slugs => nil, :order => [:id.asc]}.merge( || {}) while records = klass.all() do break if records.size == 0 records.each do |record| record.send(:build_slug) # FIXME: DataMapper Hack: this hook is not getting called record.save yield(record) if block_given? end .merge!(:id.gt => records.last.id) end end |
#validate_uses_slugs ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/friendly_id/datamapper_adapter/tasks.rb', line 60 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 |