Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine a source class of touch in after_touch callback?

Is there a way to get a source class inside after_touch callback? My situation is that I must not process after_touch callback if its source class is BoardItemPosition, but for remaining 8 classes that also use belongs_to :item, touch: true relationship I must process callback as usual.

I can not simply delete touch: true from BoardItemPosition class because this touch is also used by Audited gem to watch and store object versions.

class BoardItemPosition < ActiveRecord::Base
  belongs_to :item, touch: true
end

class Comment < ActiveRecord::Base
  belongs_to :item, touch: true
end

# ... 8 more classes that belongs to Item

class Item < ActiveRecord::Base
  after_touch :rtn_item_updated


  def rtn_item_updated
    return if self.touch_class == BoardItemPosition # <--- How to check a class?

    # unrelated method logic below...      
  end
end
like image 374
yaru Avatar asked Dec 06 '25 05:12

yaru


1 Answers

What you are asking may not be possible. As shown in Rails guides, The only parameter touch callback accepts is the entity itself, you will have to use below form of callback implementation

after_touch do |record|
    puts "#{record.class}"
end
#=> Item
like image 190
Wand Maker Avatar answered Dec 09 '25 00:12

Wand Maker