summaryrefslogtreecommitdiff
path: root/rubocop/cop/activerecord_serialize.rb
blob: bfa0cff9a67709a20baa0eba1319bcce60cdf973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module RuboCop
  module Cop
    # Cop that prevents the use of `serialize` in ActiveRecord models.
    class ActiverecordSerialize < RuboCop::Cop::Cop
      MSG = 'Do not store serialized data in the database, use separate columns and/or tables instead'.freeze

      def on_send(node)
        return unless in_models?(node)

        add_offense(node, :selector) if node.children[1] == :serialize
      end

      def models_path
        File.join(Dir.pwd, 'app', 'models')
      end

      def in_models?(node)
        path = node.location.expression.source_buffer.name

        path.start_with?(models_path)
      end
    end
  end
end