Class: ActiveTouch::DefineTouch
- Inherits:
-
Object
- Object
- ActiveTouch::DefineTouch
- Defined in:
- lib/active_touch/define_touch.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add_active_record_callback(event, method, args = {}) ⇒ Object
- #add_to_network ⇒ Object
- #default_options ⇒ Object
- #define ⇒ Object
- #define_after_commit_method ⇒ Object
- #define_before_commit_method ⇒ Object
- #define_before_destroy ⇒ Object
-
#initialize(klass, association, options) ⇒ DefineTouch
constructor
A new instance of DefineTouch.
Constructor Details
#initialize(klass, association, options) ⇒ DefineTouch
Returns a new instance of DefineTouch.
10 11 12 13 14 15 16 17 |
# File 'lib/active_touch/define_touch.rb', line 10 def initialize(klass, association, ) @klass = klass @association = association @options = .merge() @before_commit_method = "touch_#{SecureRandom.uuid}" @after_commit_method = "touch_#{SecureRandom.uuid}" @before_destroy_method = "touch_#{SecureRandom.uuid}" end |
Class Method Details
.on(klass, association, options) ⇒ Object
4 5 6 7 8 |
# File 'lib/active_touch/define_touch.rb', line 4 def self.on(klass, association, ) new(klass, association, ).define if ActiveRecord::Base.connection.data_source_exists?(klass.table_name) rescue ActiveRecord::NoDatabaseError # do nothing end |
Instance Method Details
#add_active_record_callback(event, method, args = {}) ⇒ Object
101 102 103 |
# File 'lib/active_touch/define_touch.rb', line 101 def add_active_record_callback(event, method, args = {}) @klass.send(event) { send(method, args) } end |
#add_to_network ⇒ Object
105 106 107 108 |
# File 'lib/active_touch/define_touch.rb', line 105 def add_to_network touched = @options[:class_name] || @association.to_s.camelize ActiveTouch.network.add_touch(@klass.to_s, touched, @options[:watch]) end |
#default_options ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/active_touch/define_touch.rb', line 110 def { touch_in_transaction: ActiveTouch.configuration.touch_in_transaction, async: ActiveTouch.configuration.async, watch: @klass.column_names.map(&:to_sym) - ActiveTouch.configuration.ignored_attributes, after_touch: nil, if: Proc.new { true }, unless: Proc.new { false }, touch_updates: ActiveTouch.configuration.touch_updates } end |
#define ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_touch/define_touch.rb', line 19 def define if @options[:touch_in_transaction] define_before_commit_method add_active_record_callback(:before_commit, @before_commit_method) end if !@options[:touch_in_transaction] || !@options[:after_touch].nil? define_after_commit_method add_active_record_callback(:after_commit, @after_commit_method) end define_before_destroy add_active_record_callback(:before_destroy, @before_destroy_method, prepend: true) add_to_network end |
#define_after_commit_method ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_touch/define_touch.rb', line 52 def define_after_commit_method association = @association = @options @klass.send :define_method, @after_commit_method do |*args| changed_attributes = self.previous_changes.keys.map(&:to_sym) watched_changes = ([:watch] & changed_attributes) # watched values changed and conditional procs evaluate to true if watched_changes.any? && [:if].call(self) && ![:unless].call(self) Rails.logger.debug "Touch After Commit: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}" if [:async] TouchJob .set(queue: ActiveTouch.configuration.queue) .perform_later(self, association.to_s, { after_touch: [:after_touch].to_s, is_destroy: false, is_touched: [:touch_in_transaction], touch_updates: [:touch_updates] }) else TouchJob.perform_now(self, association.to_s, { after_touch: [:after_touch].to_s, is_destroy: false, is_touched: [:touch_in_transaction], touch_updates: [:touch_updates] }) end end end end |
#define_before_commit_method ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/active_touch/define_touch.rb', line 36 def define_before_commit_method association = @association = @options @klass.send :define_method, @before_commit_method do |*args| changed_attributes = self.previous_changes.keys.map(&:to_sym) watched_changes = ([:watch] & changed_attributes) # watched values changed and conditional procs evaluate to true if watched_changes.any? && [:if].call(self) && ![:unless].call(self) Rails.logger.debug "Touch Before Commit: #{self.class}(#{self.id}) => #{association} due to changes in #{watched_changes}" TouchJob.perform_now(self, association.to_s, touch_updates: [:touch_updates]) end end end |
#define_before_destroy ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/active_touch/define_touch.rb', line 87 def define_before_destroy association = @association = @options @klass.send :define_method, @before_destroy_method do |*args| Rails.logger.debug "Touch Before Destroy: #{self.class}(#{self.id}) => #{association} due to destroy" TouchJob.perform_now(self, association.to_s, { after_touch: [:after_touch].to_s, is_destroy: true, touch_updates: [:touch_updates] }) end end |