Module: Primer::Watcher::ActiveRecordMacros

Defined in:
lib/primer/watcher/active_record_macros.rb

Defined Under Namespace

Modules: InstanceMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/primer/watcher/active_record_macros.rb', line 5

def self.extended(klass)
  klass.watch_calls_to(*klass.attributes_watchable_by_primer)
  klass.__send__(:include, InstanceMethods)
  klass.after_create(:notify_primer_after_create)
  klass.after_update(:notify_primer_after_update)
  klass.after_destroy(:notify_primer_after_destroy)
end

Instance Method Details

#attributes_watchable_by_primerObject



23
24
25
26
# File 'lib/primer/watcher/active_record_macros.rb', line 23

def attributes_watchable_by_primer
  attributes = columns + reflect_on_all_associations
  attributes.map { |c| c.name.to_s }
end

#belongs_to(name, *args) ⇒ Object



18
19
20
21
# File 'lib/primer/watcher/active_record_macros.rb', line 18

def belongs_to(name, *args)
  watch_calls_to(name)
  super
end

#has_many(name, *args) ⇒ Object



13
14
15
16
# File 'lib/primer/watcher/active_record_macros.rb', line 13

def has_many(name, *args)
  watch_calls_to(name)
  super
end

#patch_method_for_primer(method_name) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/primer/watcher/active_record_macros.rb', line 38

def patch_method_for_primer(method_name)
  method = instance_method(method_name) rescue nil
  return super if method
  class_eval <<-RUBY
    def #{method_name}
      result = read_attribute(:#{method_name})
      Primer::Watcher.log(self, :#{method_name}, [], nil, result)
      result
    end
  RUBY
end

#primer_foreign_key_mappingsObject



28
29
30
31
32
33
34
35
36
# File 'lib/primer/watcher/active_record_macros.rb', line 28

def primer_foreign_key_mappings
  return @primer_foreign_key_mappings if defined?(@primer_foreign_key_mappings)
  
  foreign_keys = reflect_on_all_associations.
                 select { |a| a.macro == :belongs_to }.
                 map { |a| [a.primary_key_name.to_s, a.name] }
  
  @primer_foreign_key_mappings = Hash[foreign_keys]
end

#unpatch_method_for_primer(method_name) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/primer/watcher/active_record_macros.rb', line 50

def unpatch_method_for_primer(method_name)
  alias_name = Macros.alias_name(method_name)
  method = instance_method(alias_name) rescue nil
  return super if method
  class_eval <<-RUBY
    undef_method :#{method_name}
  RUBY
end