From dbf7660407d1d4a8fbba25e82cff3d746763e4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Nie=C5=82acny?= Date: Sun, 3 May 2020 14:08:16 +0200 Subject: [PATCH] Remove deprecation warnings for rails 6 In rails 6 there is deprecation warning for `update_attributes!` method. It will be replaced with `update!` method. In order to remove deprecation I have wrapped method call inside `rails_api` facade. --- .rubocop_todo.yml | 7 ++++--- lib/acts_as_messageable.rb | 5 ++++- lib/acts_as_messageable/message.rb | 8 ++++---- lib/acts_as_messageable/model.rb | 4 ++-- lib/acts_as_messageable/rails4.rb | 6 +++--- lib/acts_as_messageable/rails6.rb | 31 ++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 13 deletions(-) create mode 100644 lib/acts_as_messageable/rails6.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 7a96c5a75..21142df65 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-04-19 08:04:49 +0200 using RuboCop version 0.82.0. +# on 2020-05-03 14:16:43 +0200 using RuboCop version 0.82.0. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -11,12 +11,12 @@ Metrics/AbcSize: Max: 44 -# Offense count: 1 +# Offense count: 4 # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: Max: 28 -# Offense count: 12 +# Offense count: 13 Style/Documentation: Exclude: - 'spec/**/*' @@ -26,6 +26,7 @@ Style/Documentation: - 'lib/acts_as_messageable/model.rb' - 'lib/acts_as_messageable/rails3.rb' - 'lib/acts_as_messageable/rails4.rb' + - 'lib/acts_as_messageable/rails6.rb' - 'lib/acts_as_messageable/railtie.rb' - 'lib/acts_as_messageable/relation.rb' - 'lib/acts_as_messageable/scopes.rb' diff --git a/lib/acts_as_messageable.rb b/lib/acts_as_messageable.rb index 9ca73cbac..61b0da88f 100644 --- a/lib/acts_as_messageable.rb +++ b/lib/acts_as_messageable.rb @@ -7,9 +7,12 @@ module ActsAsMessageable autoload :Relation, 'acts_as_messageable/relation' autoload :Rails3, 'acts_as_messageable/rails3' autoload :Rails4, 'acts_as_messageable/rails4' + autoload :Rails6, 'acts_as_messageable/rails6' def self.rails_api - if Rails::VERSION::MAJOR >= 4 + if Rails::VERSION::MAJOR >= 6 + Rails6 + elsif Rails::VERSION::MAJOR >= 4 Rails4 else Rails3 diff --git a/lib/acts_as_messageable/message.rb b/lib/acts_as_messageable/message.rb index d5b621b64..28440729f 100644 --- a/lib/acts_as_messageable/message.rb +++ b/lib/acts_as_messageable/message.rb @@ -27,16 +27,16 @@ def opened? end def open - update_attributes!(opened_at: DateTime.now) - update_attributes!(opened: true) + ActsAsMessageable.rails_api.new(self).update_attributes!(opened_at: DateTime.now) + ActsAsMessageable.rails_api.new(self).update_attributes!(opened: true) end alias mark_as_read open alias read open def close - update_attributes!(opened_at: nil) - update_attributes!(opened: false) + ActsAsMessageable.rails_api.new(self).update_attributes!(opened_at: nil) + ActsAsMessageable.rails_api.new(self).update_attributes!(opened: false) end alias mark_as_unread close diff --git a/lib/acts_as_messageable/model.rb b/lib/acts_as_messageable/model.rb index f77c21e8e..a66c56874 100644 --- a/lib/acts_as_messageable/model.rb +++ b/lib/acts_as_messageable/model.rb @@ -155,7 +155,7 @@ def delete_message(message) raise "#{current_user} can't delete this message" end - message.update_attributes!(attribute => true) + ActsAsMessageable.rails_api.new(message).update_attributes!(attribute => true) end # Mark message as restored @@ -171,7 +171,7 @@ def restore_message(message) raise "#{current_user} can't restore this message" end - message.update_attributes!(attribute => false) + ActsAsMessageable.rails_api.new(message).update_attributes!(attribute => false) end end end diff --git a/lib/acts_as_messageable/rails4.rb b/lib/acts_as_messageable/rails4.rb index 6b7b19ab7..46d0d3b6e 100644 --- a/lib/acts_as_messageable/rails4.rb +++ b/lib/acts_as_messageable/rails4.rb @@ -6,6 +6,8 @@ def initialize(subject) @subject = subject end + def attr_accessible(*); end + def default_scope(order_by) @subject.send(:default_scope) { order(order_by) } end @@ -14,14 +16,12 @@ def scoped @subject.scope end - def attr_accessible(*); end - def method_missing(name, *args) @subject.send(name, *args) || super end def respond_to_missing?(method_name, include_private = false) - %w[default_scope scoped].include?(method_name) || super + %w[default_scope scoped attr_accessible].include?(method_name) || super end end end diff --git a/lib/acts_as_messageable/rails6.rb b/lib/acts_as_messageable/rails6.rb new file mode 100644 index 000000000..2b458bdbe --- /dev/null +++ b/lib/acts_as_messageable/rails6.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module ActsAsMessageable + class Rails6 + def initialize(subject) + @subject = subject + end + + def attr_accessible(*); end + + def default_scope(order_by) + @subject.send(:default_scope) { order(order_by) } + end + + def scoped + @subject.scope + end + + def update_attributes!(*args) + @subject.update!(*args) + end + + def method_missing(name, *args) + @subject.send(name, *args) || super + end + + def respond_to_missing?(method_name, include_private = false) + %w[attr_accessible default_scope scoped update_attributes!].include?(method_name) || super + end + end +end