Accept boolean for :verbose option

master
Guillaume DOTT 2013-05-30 09:30:10 +02:00
parent 7b1e4acf48
commit 77457be68f
2 changed files with 16 additions and 5 deletions

View File

@ -82,6 +82,7 @@ class Example < ActiveRecord::Base
big_brother_watch verbose: :third
end
```
`:verbose` option can take a boolean. If true, every updated fields values will be written in log file.
## Contributing

View File

@ -12,7 +12,9 @@ module RailsBigBrother
self.big_brother_options = {}
[:ignore, :only, :verbose].each do |k|
self.big_brother_options[k] = [options[k]].flatten.compact.map(&:to_s) if options.has_key?(k)
self.big_brother_options[k] = (
options[k] == true || options[k] == false ? options[k] : [options[k]].flatten.compact.map(&:to_s)
) if options.has_key?(k)
end
after_create :log_create if !options[:on] || options[:on].include?(:create)
@ -29,15 +31,23 @@ module RailsBigBrother
end
def log_update
options = self.class.big_brother_options
changed_fields = self.changed
changed_fields = changed_fields - self.class.big_brother_options[:ignore] if self.class.big_brother_options.has_key?(:ignore)
changed_fields = changed_fields & self.class.big_brother_options[:only] if self.class.big_brother_options.has_key?(:only)
# Remove fields from :ignore array
changed_fields = changed_fields -
options[:ignore] if options.has_key?(:ignore) && options[:ignore].is_a?(Array)
# Keep only fields from :only array
changed_fields = changed_fields &
options[:only] if options.has_key?(:only) && options[:only].is_a?(Array)
unless changed_fields.empty?
verbose = options[:verbose] if options.has_key?(:verbose)
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?(field) ? send(field).to_s : ''
(verbose.is_a?(Array) && verbose.include?(field)) ||
verbose == true ? send(field).to_s : ''
)
hash