Sha256: 163df683fe177c3b37730350422cb34e3e54a4f2ec35562b4a131a6ce43bc4f3

Contents?: true

Size: 1.68 KB

Versions: 9

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
module Thredded
  class MessageboardDestroyer
    def initialize(messageboard_name)
      @messageboard = Thredded::Messageboard.friendly.find(messageboard_name)
      @connection = ActiveRecord::Base.connection
    rescue
      say "No messageboard with the name '#{messageboard_name}' found."
    end

    def run
      return unless messageboard

      ActiveRecord::Base.transaction do
        destroy_all_lower_dependencies
        destroy_all_posts
        destroy_all_topics
        destroy_messageboard_and_everything_else
      end
    end

    private

    attr_reader :messageboard, :connection

    def destroy_all_lower_dependencies
      say 'Destroying lower level dependencies ...'
      [PostNotification.joins(non_private_post: :postable), UserTopicReadState.joins(:postable)].each do |child_t|
        child_t.merge(Topic.where(messageboard_id: messageboard.id)).delete_all
      end

      Thredded::MessageboardUser
        .where(thredded_messageboard_id: messageboard.id)
        .delete_all
    end

    def destroy_all_posts
      say 'Destroying all posts ...'

      connection.execute <<-SQL
        DELETE FROM thredded_posts
          WHERE messageboard_id = #{messageboard.id}
      SQL
    end

    def destroy_all_topics
      say 'Destroying all topics ...'

      connection.execute <<-SQL
        DELETE FROM thredded_topics
          WHERE messageboard_id = #{messageboard.id}
      SQL
    end

    def destroy_messageboard_and_everything_else
      say 'Destroying messageboard and everything else. Sit tight ...'

      @messageboard.destroy!
    end

    def say(message)
      Rails.logger.info message unless Rails.env.test?
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
thredded-0.7.0 app/commands/thredded/messageboard_destroyer.rb
thredded-0.6.3 app/commands/thredded/messageboard_destroyer.rb
thredded-0.6.2 app/commands/thredded/messageboard_destroyer.rb
thredded-0.6.1 app/commands/thredded/messageboard_destroyer.rb
thredded-0.6.0 app/commands/thredded/messageboard_destroyer.rb
thredded-0.5.1 app/commands/thredded/messageboard_destroyer.rb
thredded-0.5.0 app/commands/thredded/messageboard_destroyer.rb
thredded-0.4.0 app/commands/thredded/messageboard_destroyer.rb
thredded-0.3.2 app/commands/thredded/messageboard_destroyer.rb