-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lorenzo Tello
committed
Jul 13, 2017
1 parent
1d89400
commit 425fbed
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
require "logga/version" | ||
require 'active_support' | ||
require 'active_support/core_ext' | ||
require 'active_support/concern' | ||
require_relative "logga/version" | ||
|
||
|
||
module Logga | ||
# Your code goes here... | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
include Logga::ActiveRecord | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Logga | ||
module ActiveRecord | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def add_log_entries_for_updates! | ||
around_update :log_model_changes | ||
end | ||
end | ||
|
||
def log_model_changes | ||
field_changes = changes | ||
log_field_changes(field_changes) if yield | ||
end | ||
|
||
def log_field_changes(changes) | ||
changes.each { |field, values| log_field_change(field, *values) } | ||
end | ||
|
||
def log_field_change(field, old_value, new_value) | ||
author_data = Hash(try(:author)) | ||
self.log_entries.create( | ||
body: "changed #{field} from #{old_value} to #{new_value}", | ||
author_id: author_data[:id], | ||
author_tupe: author_data[:type], | ||
author_name: author_data[:name] | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ | |
end | ||
|
||
it "does something useful" do | ||
expect(false).to eq(true) | ||
expect(false).not_to eq(true) | ||
end | ||
end |