From 7b1e4acf48b5326a61e3a2fe9dedb75c99c98374 Mon Sep 17 00:00:00 2001 From: Guillaume DOTT Date: Wed, 29 May 2013 15:33:52 +0200 Subject: [PATCH] Add hash_to_s and array_to_s options to define proc --- README.md | 2 ++ lib/rails_big_brother.rb | 18 +++++++++++++++--- lib/rails_big_brother/model.rb | 15 +++++++++------ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 4ff7441..02f5372 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Or install it yourself as: RailsBigBrother.config do |config| config.format = "big_brother;%s;%s;%s;%s;%s;%s" config.logger = Rails.logger + config.hash_to_s = proc { |hash| hash.map { |k,v| "#{k}:#{v}" }.join(',') } + config.array_to_s = proc { |array| array.join(',') } end ``` diff --git a/lib/rails_big_brother.rb b/lib/rails_big_brother.rb index 9b72993..4a073d7 100644 --- a/lib/rails_big_brother.rb +++ b/lib/rails_big_brother.rb @@ -4,7 +4,19 @@ require "rails_big_brother/model" module RailsBigBrother class << self - attr_writer :format, :logger + attr_writer :array_to_s, :hash_to_s, :format, :logger + + def array_to_s + @array_to_s ||= Proc.new do |array| + array.join(',') + end + end + + def hash_to_s + @hash_to_s ||= Proc.new do |hash| + hash.map { |k,v| "#{k}:#{v}" }.join(',') + end + end def logger @logger ||= Rails.logger @@ -37,9 +49,9 @@ module RailsBigBrother def controller_info_string case controller_info when Array - controller_info.join(',') + array_to_s.call(controller_info) when Hash - controller_info.map { |k,v| "#{k}:#{v}" }.join(',') + hash_to_s.call(controller_info) else controller_info end diff --git a/lib/rails_big_brother/model.rb b/lib/rails_big_brother/model.rb index 0892789..6b40f77 100644 --- a/lib/rails_big_brother/model.rb +++ b/lib/rails_big_brother/model.rb @@ -34,12 +34,15 @@ module RailsBigBrother changed_fields = changed_fields & self.class.big_brother_options[:only] if self.class.big_brother_options.has_key?(:only) unless changed_fields.empty? - changed_fields_string = changed_fields.map do |f| - f + ( + fields_hash = changed_fields.inject({}) do |hash, field| + hash[field] = ( self.class.big_brother_options.has_key?(:verbose) && - self.class.big_brother_options[:verbose].include?(f) ? ":#{send(f)}" : '') - end.join(',') - big_brother_log 'update', changed_fields_string + self.class.big_brother_options[:verbose].include?(field) ? send(field).to_s : '' + ) + + hash + end + big_brother_log 'update', RailsBigBrother.hash_to_s.call(fields_hash) end end @@ -55,7 +58,7 @@ module RailsBigBrother class: self.class.name, id: self.to_param, action: action, - args: args.join(',') + args: RailsBigBrother.array_to_s.call(args) } end end