Skip to content

Commit

Permalink
Remove deprecation warnings for rails 6
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LTe committed May 3, 2020
1 parent 90b50c1 commit dbf7660
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 13 deletions.
7 changes: 4 additions & 3 deletions .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
Expand All @@ -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/**/*'
Expand All @@ -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'
Expand Down
5 changes: 4 additions & 1 deletion lib/acts_as_messageable.rb
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/acts_as_messageable/message.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/acts_as_messageable/model.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/acts_as_messageable/rails4.rb
Expand Up @@ -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
Expand All @@ -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
31 changes: 31 additions & 0 deletions 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

0 comments on commit dbf7660

Please sign in to comment.