diff --git a/lib/active_touch.rb b/lib/active_touch.rb index a199e77..9d4badc 100644 --- a/lib/active_touch.rb +++ b/lib/active_touch.rb @@ -1,6 +1,7 @@ require 'active_touch/version' require 'active_touch/configuration' require 'active_touch/define_touch' +require 'active_touch/network' require 'active_touch/touch_job' require 'active_support/concern' diff --git a/lib/active_touch/define_touch.rb b/lib/active_touch/define_touch.rb index 6e7c023..214ef94 100644 --- a/lib/active_touch/define_touch.rb +++ b/lib/active_touch/define_touch.rb @@ -15,6 +15,7 @@ def initialize(klass, association, options) def define define_touch_method add_active_record_callback + add_to_network end def define_touch_method @@ -47,6 +48,11 @@ def add_active_record_callback @klass.send(:after_commit) { send(touch_method) } end + def add_to_network + touched = @options[:class_name] || @association.to_s.camelize + ActiveTouch.network.add_touch(@klass.to_s, touched, @options[:watch]) + end + def default_options { async: ActiveTouch.configuration.async, diff --git a/lib/active_touch/network.rb b/lib/active_touch/network.rb new file mode 100644 index 0000000..73adc91 --- /dev/null +++ b/lib/active_touch/network.rb @@ -0,0 +1,35 @@ +module ActiveTouch + + class << self + attr_accessor :network + end + + def self.network + @network ||= Network.new + end + + class Network + + attr_accessor :map + + def initialize + @map = {} + end + + # @param caller [String] + # @param touched [String, Array(String)] + # @param watch [Array(Symbol)] + # @return [void] + def add_touch(caller, touched, watch) + map[caller] ||= [] + + if touched.is_a? Array + touched.each { |t| map[caller] << { class: t, attributes: watch } } + else + map[caller] << { class: touched, attributes: watch } + end + end + + end + +end \ No newline at end of file