module Dunlop::UserModel
  extend ActiveSupport::Concern

  included do
    serialize :role_names, JSONColumnCoder.new(Array)
    serialize :role_names_admin, JSONColumnCoder.new(Array)
    serialize :settings_storage, JSONColumnCoder.new(Hash)
    has_many :source_files, as: :creator
  end

  def settings
    @settings ||= Dunlop::UserSettings.new(self, settings_storage)
  end

  def all_role_names
    role_names + role_names_admin
  end

  def presentation_name
    email
  end

  module ClassMethods

    def current
      RequestStore.store[:current_user]
    end

    def current=(user)
      RequestStore.store[:current_user] = user
    end
  end
end
