Skip to content

Commit

Permalink
Add ActiveTouch::Network and create a map of touches as application i…
Browse files Browse the repository at this point in the history
…s initialized.
  • Loading branch information
Kevin Pheasey committed Dec 19, 2015
1 parent 19d6c81 commit dab603d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/active_touch.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
6 changes: 6 additions & 0 deletions lib/active_touch/define_touch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 35 additions & 0 deletions lib/active_touch/network.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dab603d

Please sign in to comment.