Module: TreasureData::Logger::Agent::Rails::ModelExtension::ClassMethods

Defined in:
lib/td/logger/agent/rails/model.rb

Instance Method Summary collapse

Instance Method Details

#td_enable_model_tracer(tag, options = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
86
87
88
89
90
91
# File 'lib/td/logger/agent/rails/model.rb', line 37

def td_enable_model_tracer(tag, options={})
  only = nil
  except = nil
  static = {}

  if o = options[:only]
    only = case o
           when Array
             o
           else
             [o]
           end.map {|e| e.to_s }
  end

  if o = options[:except]
    except = case o
             when Array
               o
             else
               [o]
             end.map {|e| e.to_s }
  end

  if o = options[:static]
    o.each_pair {|k,v|
      static[k.to_s] = v
    }
  end

  if defined?(after_commit)
    # Rails 3
    m = :after_commit
  else
    # Rails 2
    m = :after_save
  end

  __send__(m) do |record|
    data = {}
    record.attribute_names.each {|name|
      name = name.to_s
      if (!only || only.include?(name)) && (!except || !except.include?(name))
        data[name] = record.read_attribute(name)
      end
    }
    static.each_pair {|k,v|
      data[k] = v
    }
    if time = data['updated_at'] && time.is_a?(Time)
      data['time'] = time.to_i
      data.delete('updated_at')
    end
    TreasureData.log(tag, data)
  end
end