Sha256: d5b2be1cfe543922bce3a7c8967aa0dad2c9c08ebafae789b9594b83a738465c

Contents?: true

Size: 1.26 KB

Versions: 2

Compression:

Stored size: 1.26 KB

Contents

require "beetle_reporter/version"
require "beetle_reporter/engine"
require 'beetle_reporter/configuration'

require 'aasm'
require 'pagy'
require 'hamlit'
require 'discard'
require 'turbo-rails'

module BeetleReporter
  class << self
    def config
      return @config if defined?(@config)

      @config = Configuration.new
      @config.user_class = 'User'
      @config.current_user_method = 'current_user'
      @config.user_name_method = 'name'
      @config.main_app_root_path_method = 'root_path'
      @config.main_app_user_path_method = 'user_path'

      @config
    end

    def configure
      yield(config)
    end

    def add(record, user_id, reason)
      report = BeetleReporter::Report.find_by(record: record)
      if report
        report.user_ids << user_id unless report.user_ids.include?(user_id)
        report.reasons << reason unless report.reasons.include?(reason)
        # 未登录用户也有可能
        report.reported_count += 1
        report.user_ids = report.user_ids.uniq
        report.reasons = report.reasons.uniq
        report.save
      else
        report = BeetleReporter::Report.create(record: record,
          user_ids: [user_id],
          reasons: [reason],
          reported_count: 1,
        )
      end
      report
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
beetle_reporter-0.1.2 lib/beetle_reporter.rb
beetle_reporter-0.1.1 lib/beetle_reporter.rb