Add hash_to_s and array_to_s options to define proc

master
Guillaume DOTT 2013-05-29 15:33:52 +02:00
parent a83de02b0f
commit 7b1e4acf48
3 changed files with 26 additions and 9 deletions

View File

@ -22,6 +22,8 @@ Or install it yourself as:
RailsBigBrother.config do |config| RailsBigBrother.config do |config|
config.format = "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s" config.format = "big_brother;%<user>s;%<controller_info>s;%<class>s;%<id>s;%<action>s;%<args>s"
config.logger = Rails.logger 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 end
``` ```

View File

@ -4,7 +4,19 @@ require "rails_big_brother/model"
module RailsBigBrother module RailsBigBrother
class << self 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 def logger
@logger ||= Rails.logger @logger ||= Rails.logger
@ -37,9 +49,9 @@ module RailsBigBrother
def controller_info_string def controller_info_string
case controller_info case controller_info
when Array when Array
controller_info.join(',') array_to_s.call(controller_info)
when Hash when Hash
controller_info.map { |k,v| "#{k}:#{v}" }.join(',') hash_to_s.call(controller_info)
else else
controller_info controller_info
end end

View File

@ -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) changed_fields = changed_fields & self.class.big_brother_options[:only] if self.class.big_brother_options.has_key?(:only)
unless changed_fields.empty? unless changed_fields.empty?
changed_fields_string = changed_fields.map do |f| fields_hash = changed_fields.inject({}) do |hash, field|
f + ( hash[field] = (
self.class.big_brother_options.has_key?(:verbose) && self.class.big_brother_options.has_key?(:verbose) &&
self.class.big_brother_options[:verbose].include?(f) ? ":#{send(f)}" : '') self.class.big_brother_options[:verbose].include?(field) ? send(field).to_s : ''
end.join(',') )
big_brother_log 'update', changed_fields_string
hash
end
big_brother_log 'update', RailsBigBrother.hash_to_s.call(fields_hash)
end end
end end
@ -55,7 +58,7 @@ module RailsBigBrother
class: self.class.name, class: self.class.name,
id: self.to_param, id: self.to_param,
action: action, action: action,
args: args.join(',') args: RailsBigBrother.array_to_s.call(args)
} }
end end
end end