Sha256: 6ff6f40481b0c55749f11e6164f42531fc8aa610730d5b9adf8d7d2cfae582af

Contents?: true

Size: 1.94 KB

Versions: 14

Compression:

Stored size: 1.94 KB

Contents

module TreasureData
module Logger
module Agent::Rails
  module ModelExtension

    def self.init
      # disable model extension ActiveRecord is not loaded
      # on Rails > 3 (other ORM is used)
      if !defined?(::ActiveRecord) &&
          ::Rails.respond_to?(:version) && ::Rails.version =~ /^3/
        return
      end
      require 'active_record'
      ::ActiveRecord::Base.send(:include, self)
    end

    def self.included(mod)
      cm = ClassMethods
      mod.class_eval do
        extend cm
      end
    end

    module ClassMethods
      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
          }
          time = data['updated_at']
          if time.is_a?(Time)
            data.delete('updated_at')
          else
            time = Time.now
          end
          TreasureData::Logger.post_with_time(tag, data, time)
        end
      end
    end

  end
end
end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
td-logger-0.3.28 lib/td/logger/agent/rails/model.rb
td-logger-0.3.27 lib/td/logger/agent/rails/model.rb
td-logger-0.3.26 lib/td/logger/agent/rails/model.rb
td-logger-0.3.25 lib/td/logger/agent/rails/model.rb
td-logger-0.3.24 lib/td/logger/agent/rails/model.rb
td-logger-0.3.23 lib/td/logger/agent/rails/model.rb
td-logger-0.3.22 lib/td/logger/agent/rails/model.rb
td-logger-0.3.21 lib/td/logger/agent/rails/model.rb
td-logger-0.3.20 lib/td/logger/agent/rails/model.rb
td-logger-0.3.19 lib/td/logger/agent/rails/model.rb
td-logger-0.3.18 lib/td/logger/agent/rails/model.rb
td-logger-0.3.17 lib/td/logger/agent/rails/model.rb
td-logger-0.3.16 lib/td/logger/agent/rails/model.rb
td-logger-0.3.15 lib/td/logger/agent/rails/model.rb