Skip to content

Instantly share code, notes, and snippets.

@zoki
Created June 4, 2019 20:40
Show Gist options
  • Save zoki/fc238c20ce3b872a322e63fbd91ec995 to your computer and use it in GitHub Desktop.
Save zoki/fc238c20ce3b872a322e63fbd91ec995 to your computer and use it in GitHub Desktop.
Devise and ActsAsTaggableOn - prevent loading tags with user
# frozen_string_literal: true
#
# Devise & ActsAsTaggableOn gems combination
# This fix redefine inspect method by excluding tag_lists form it.
#
# Because the way Devise redefines inspect method tag_lists are getting
# loaded on every user query.
#
# From Devise:
#
# Redefine serializable_hash in models for more secure defaults.
# By default, it removes from the serializable model all attributes that
# are *not* accessible. You can remove this default by using :force_except
# and passing a new list of attributes you want to exempt. All attributes
# given to :except will simply add names to exempt to Devise internal list.
#
# Redefine inspect using serializable_hash, to ensure we don't accidentally
# leak passwords into exceptions.
#
module DeviseTagsFix
def inspect
inspection = serializable_hash(except: tag_lists).collect do |k, v|
"#{k}: #{respond_to?(:attribute_for_inspect) ? attribute_for_inspect(k) : v.inspect}"
end
"#<#{self.class} #{inspection.join(', ')}>"
end
def tag_lists
self.class.tag_types.map { |type| "#{type.to_s.singularize}_list" }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment