Skip to content

Commit

Permalink
Added config, enable method and more tests (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
schinery authored Aug 22, 2022
1 parent 0c2eaf4 commit cf78758
Show file tree
Hide file tree
Showing 11 changed files with 487 additions and 132 deletions.
99 changes: 2 additions & 97 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,98 +1,3 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore gem building folder: pkg
pkg

# Ignore log and tmp files
log/*
*.log
tmp
tmp/**/*

# Documentation
/doc/
doc/api
doc/app
.yardoc
/_yardoc/
.yardopts

# Public Uploads
public/system/*
public/themes/*

# Public Cache
public/javascripts/cache
public/stylesheets/cache

# Vendor Cache
vendor/cache

# Acts as Indexed
index/**/*

# Refinery Specific
*.tmproj
*.autobackupbyrefinery.*
refinerycms-*.gem
.autotest

# Mac
.DS_Store

# Windows
Thumbs.db

# NetBeans
nbproject

# Eclipse
.project

# Redcar
.redcar

# Rubinius
*.rbc

# RVM / rbenv
.ruby-gemset
.rvmrc

# Vim
*.swp
*.swo

# RubyMine
.idea

# E-texteditor
.eprj

# Backup
*~

# Capybara Bug
capybara-*html

# sass
.sass-cache
.sass-cache/*

# SimpleCov (tests code converage)
/coverage

# RSpec
/spec/reports/
.rspec_status
.byebug_history
coverage
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ source "https://rubygems.org"
gemspec

group :development do
gem "activerecord", "~> 7.0"
gem "boxt_rubocop", "0.0.21"
gem "byebug", "~> 11.0"
gem "rake", "~> 13.0"
gem "rspec", "~> 3.9"
gem "simplecov", "~> 0.17"
gem "sqlite3", "~> 1.4"
gem "timecop", "~> 0.9"
gem "with_model", "~> 2.1"
end
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,28 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
sqlite3 (1.4.4)
timecop (0.9.5)
tzinfo (2.0.5)
concurrent-ruby (~> 1.0)
unicode-display_width (2.2.0)
with_model (2.1.6)
activerecord (>= 5.2)

PLATFORMS
ruby

DEPENDENCIES
activerecord (~> 7.0)
boxt_rubocop (= 0.0.21)
byebug (~> 11.0)
logga!
rake (~> 13.0)
rspec (~> 3.9)
simplecov (~> 0.17)
sqlite3 (~> 1.4)
timecop (~> 0.9)
with_model (~> 2.1)

BUNDLED WITH
2.3.10
86 changes: 76 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,94 @@ gem install logga

## Usage

Add this to your model:
Add the following to your model:

```ruby
class Order < ApplicationRecord
add_log_entries_for :create, :update
class Thing < ApplicationRecord
# Optional author accessor. See #author
attr_accessor :author

# Association to :log_entries, which the loggable object must response to for logging.
has_many :log_entries, as: :loggable, dependent: :destroy

add_log_entries_for(
:create, # Log on object create
:delete, # Log on object delete
:update, # Log on object update
allowed_fields: [], # set an array of fields allowed to be logged
exclude_fields: [], # set an array of fields excluded from logging. Ignored if allowed_fields is set
fields: {}, # Custom messages for fields. See #fields
to: nil
)
end
```

So that new LogEntry records attached to a given Order instance will be created whenever a new one is created or
So that new `LogEntry` records attached to a given `Thing` instance will be created whenever a new one is created or
modified.

## Author

If you want to log the author of the changes you can do so by setting:

```ruby
thing.author = { id: "1", name: "Barry", type: "User" }
```

## Fields

You can override the default messages per field by using:

```ruby
add_log_entries_for(
:update,
fields: {
name: lambda { |record, field, old_value, new_value|
"Name changed from #{old_value} to #{new_value}"
}
}
)
```

This is with the exeception on `:created_at` which only takes the created record.

```ruby
add_log_entries_for(
:create,
fields: {
created_at: lambda { |record|
"Created object with id: #{record.id}"
}
}
)
```

## Configuration

Add an initializer to your project:

```ruby
Logga.configure do |config|
enabled: true,
excluded_fields: [], # Default array of excluded fields i.e. [:id] to ignore all :id fields for every object
excluded_suffixes: [] # Array of excluded suffixes i.e. [:_id] to ignore all fields that end in :_id for every object
end
```

For example:

```ruby
Logga.configure do |config|
excluded_fields: [:id], # Don't log any id changes
excluded_suffixes: [_id] # Don't log any column that ends in _id
end
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run `bundle exec rake install`.

## Versioning

The version of the gem should be set in the `VERSION` file found in the root of the project. This is then read by the `lib/boxt_aasm_ext/version.rb` file to set in the gem.

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/boxt/logga.
Expand All @@ -62,6 +129,5 @@ The gem is available as open source under the terms of the [MIT License](http://

## TODOs

- Write some tests
- Improve the documentation
- Remove explicit `EXCLUDED_KEYS` and `EXCLUDED_SUFFIXES` for something that can be set in an initializer
- Add migration generator for `:log_entries`
28 changes: 27 additions & 1 deletion lib/logga.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,37 @@
require "active_support"
require "active_support/core_ext"
require "active_support/concern"
require_relative "logga/version"

require_relative "logga/active_record"
require_relative "logga/config"
require_relative "logga/version"

module Logga
ActiveSupport.on_load(:active_record) do
include Logga::ActiveRecord
end

class << self
def configuration
@configuration ||= Config.new
end

def configure
yield(configuration)
end

# Switches Logga on or off
def enabled=(value)
configuration.enabled = value
end

# Returns `true` if Logga is on, `false` otherwise
def enabled?
!!configuration.enabled
end

def version
Logga::VERSION
end
end
end
Loading

0 comments on commit cf78758

Please sign in to comment.