summaryrefslogtreecommitdiff
path: root/app/models/concerns/ignorable_column.rb
blob: 5c1f7dfcd2ad6bc96f1399405023c9622a1df6a9 (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
25
26
27
28
29
30
# frozen_string_literal: true

# Module that can be included into a model to make it easier to ignore database
# columns.
#
# Example:
#
#     class User < ActiveRecord::Base
#       include IgnorableColumn
#
#       ignore_column :updated_at
#     end
#
module IgnorableColumn
  extend ActiveSupport::Concern

  class_methods do
    def columns
      super.reject { |column| ignored_columns.include?(column.name) }
    end

    def ignored_columns
      @ignored_columns ||= Set.new
    end

    def ignore_column(*names)
      ignored_columns.merge(names.map(&:to_s))
    end
  end
end