-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new functionality for querying, filtering, and sorting changes
- Loading branch information
Showing
3 changed files
with
59 additions
and
0 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
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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/concern' | ||
|
||
module Bemi::ChangeQueryHelpers | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
scope :before, ->(hash) { where('before @> ?', hash.to_json) } | ||
scope :after, ->(hash) { where('after @> ?', hash.to_json) } | ||
scope :context, ->(hash) { where('context @> ?', hash.to_json) } | ||
|
||
scope :created, -> { where(operation: 'CREATE') } | ||
scope :updated, -> { where(operation: 'UPDATE') } | ||
scope :deleted, -> { where(operation: 'DELETE') } | ||
|
||
scope :asc, -> { order(committed_at: :asc) } | ||
scope :desc, -> { order(committed_at: :desc) } | ||
|
||
self.filter_attributes = [] | ||
end | ||
|
||
def diff | ||
result = {} | ||
|
||
(before.keys | after.keys).each do |key| | ||
next if before[key] == after[key] | ||
result[key] = [before[key], after[key]] | ||
end | ||
|
||
result | ||
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'active_support/concern' | ||
|
||
module Bemi::RecordQueryHelpers | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
# Setter | ||
def bemi_change_class(bemi_change_class_name) | ||
@@bemi_change_class_name = bemi_change_class_name | ||
end | ||
|
||
# Getter | ||
def __bemi_change_class | ||
raise "Please set the change class name with `bemi_change_class 'BemiChange'`" if !@@bemi_change_class_name | ||
@@__bemi_change_class ||= @@bemi_change_class_name.constantize | ||
end | ||
end | ||
|
||
def bemi_changes | ||
self.class.__bemi_change_class.where(table: self.class.table_name, primary_key: attributes[self.class.primary_key]) | ||
end | ||
end |