<commit_before>module CoronavirusPages
  class ModelBuilder
    def self.call(*args)
      new(*args).page
    end

    attr_reader :slug, :action

    def initialize(slug, action = nil)
      @slug = slug
      @action = action
    end

    def page
      @page ||=
        CoronavirusPage.find_or_create_by!(slug: slug) do |coronavirus_page|
          coronavirus_page.attributes = coronavirus_page_attributes
        end
    end

    def discard_changes
      if live_content.any?
        store_live_subsections
        page.update!(state: "published")
      end
    end

    def sections_heading
      data["sections_heading"]
    end

    def title
      data["title"]
    end

  private

    def store_live_subsections
      page.sub_sections.destroy_all
      page.sub_sections = sub_sections
    end

    def page_config
      CoronavirusPages::Configuration.page(slug)
    end

    def coronavirus_page_attributes
      page_config.merge(
        sections_title: sections_heading,
        title: title,
      )
    end
<commit_msg>Wrap discard tasks in a transaction<commit_after>module CoronavirusPages
  class ModelBuilder
    def self.call(*args)
      new(*args).page
    end

    attr_reader :slug, :action

    def initialize(slug, action = nil)
      @slug = slug
      @action = action
    end

    def page
      @page ||=
        CoronavirusPage.find_or_create_by!(slug: slug) do |coronavirus_page|
          coronavirus_page.attributes = coronavirus_page_attributes
        end
    end

    def discard_changes
      if live_content.any?
        CoronavirusPage.transaction do
          store_live_subsections
          page.update!(state: "published")
        end
      end
    end

    def sections_heading
      data["sections_heading"]
    end

    def title
      data["title"]
    end

  private

    def store_live_subsections
      page.sub_sections.destroy_all
      page.sub_sections = sub_sections
    end

    def page_config
      CoronavirusPages::Configuration.page(slug)
    end

    def coronavirus_page_attributes
      page_config.merge(
        sections_title: sections_heading,
        title: title,
      )
    end

{'fraction_non_alphanumeric': 0.06351414000927214, 'fraction_numerical': 0.0, 'mean_word_length': 2.3098159509202456, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7312648', 'n_tokens_mistral': 645, 'n_tokens_neox': 623, 'n_words': 142}