-
Notifications
You must be signed in to change notification settings - Fork 20
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
Showing
5 changed files
with
98 additions
and
47 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
This file was deleted.
Oops, something went wrong.
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module Logux | ||
module Model | ||
class UpdatesDeprecator | ||
EVENT = 'logux.insecure_update' | ||
|
||
class << self | ||
def watch(args = {}, &block) | ||
new(args).watch(&block) | ||
end | ||
end | ||
|
||
def initialize(level: :warn) | ||
@level = level | ||
end | ||
|
||
def watch(&block) | ||
callback = lambda(&method(:handle_insecure_update)) | ||
ActiveSupport::Notifications.subscribed(callback, EVENT, &block) | ||
end | ||
|
||
private | ||
|
||
# rubocop:disable Naming/UncommunicativeMethodParamName | ||
def handle_insecure_update(_, _, _, _, args) | ||
attributes = args[:changed].map(&:to_sym) - [:logux_fields_updated_at] | ||
insecure_attributes = | ||
attributes & args[:model_class].logux_crdt_mapped_attributes | ||
return if insecure_attributes.empty? | ||
|
||
notify_about_insecure_update(insecure_attributes) | ||
end | ||
# rubocop:enable Naming/UncommunicativeMethodParamName | ||
|
||
def notify_about_insecure_update(insecure_attributes) | ||
pluralized_attributes = 'attribute'.pluralize(insecure_attributes.count) | ||
|
||
message = <<~TEXT | ||
Logux tracked #{pluralized_attributes} (#{insecure_attributes.join(', ')}) should be updated using model.logux.update(...) | ||
TEXT | ||
|
||
case @level | ||
when :warn | ||
ActiveSupport::Deprecation.warn(message) | ||
when :error | ||
raise InsecureUpdateError, message | ||
end | ||
end | ||
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Logux::Model::UpdatesDeprecator do | ||
let(:post) { create(:post) } | ||
|
||
context 'with error level' do | ||
it 'raises error when insecure update is detected' do | ||
expect do | ||
described_class.watch(level: :error) do | ||
post.update_attributes(title: 'new title') | ||
end | ||
end.to raise_error(Logux::Model::InsecureUpdateError) | ||
end | ||
|
||
it 'does not raise error when update is secure' do | ||
expect do | ||
described_class.watch(level: :error) do | ||
post.update_attributes(updated_at: Time.now) | ||
end | ||
end.not_to raise_error(Logux::Model::InsecureUpdateError) | ||
end | ||
end | ||
|
||
context 'with warn level' do | ||
it 'outputs deprecation warning when update is detected' do | ||
expect do | ||
described_class.watch(level: :warn) do | ||
post.update_attributes(title: 'new title') | ||
end | ||
end.to output(/DEPRECATION WARNING/).to_stderr | ||
end | ||
|
||
it 'uses warn level by default' do | ||
expect do | ||
described_class.watch do | ||
post.update_attributes(title: 'new title') | ||
end | ||
end.to output(/DEPRECATION WARNING/).to_stderr | ||
end | ||
end | ||
end |