module Dunlop
  class UserFilter < ApplicationRecord
    belongs_to :user
    if Rails.application.config.database_configuration[Rails.env]['adapter'] != 'postgesql' # postgesql uses jsonb
      serialize :filters, JSONColumnCoder.new(Hash)
    end

    before_save do
      if changes['is_default'] == [false, true]
        self.class.where(user_id: user_id, scope_key: scope_key).update_all(is_default: false)
      end
    end
  end
end
