<commit_before>module Locomotive
  module ActionController
    class Responder < ::ActionController::Responder

      include ::Responders::FlashResponder

      # by default flash_now messages if the resource has errors
      def set_flash_now?
        super || has_errors?
      end

      def options
        current_site    = self.controller.send(:current_site)
        current_account = self.controller.send(:current_locomotive_account)
        ability         = current_site.nil? || current_account.nil? ? nil : self.controller.send(:current_ability)

        super.merge({
          current_site:     current_site,
          current_account:  current_account,
          ability:          ability
        })
      end

      def to_json
        if get?
          display resource
        elsif has_errors?
          with_flash_message(:alert) do
            display resource.errors, status: :unprocessable_entity
          end
        elsif post?
          in_header = controller.request.headers['X-Flash'] == 'true'
          with_flash_message(:notice, in_header) do
            display resource, location: api_location
          end
        elsif put?
          with_flash_message do |message|
            display resource, status: :ok, location: api_location
          end
        elsif delete?
          with_flash_message do |message|
            display resource, status: :ok, location: api_location
          end
        elsif has_empty_resource_definition?
          display empty_resource, status: :ok
        else
          with_flash_message do
            head :ok
          end
        end
      end

      protected

      def with_flash_message(type = :notice, in_header = true)
        if in_header
          set_flash_message!
          message = URI::escape controller.flash[type].to_str

          unless message.blank?
            controller.headers['X-Message']       = ActiveSupport::JSON::Encoding.escape(message)
            controller.headers['X-Message-Type']  = type.to_s<commit_msg>quick fix for API respond with flash issue<commit_after>module Locomotive
  module ActionController
    class Responder < ::ActionController::Responder

      include ::Responders::FlashResponder

      # by default flash_now messages if the resource has errors
      def set_flash_now?
        super || has_errors?
      end

      def options
        current_site    = self.controller.send(:current_site)
        current_account = self.controller.send(:current_locomotive_account)
        ability         = current_site.nil? || current_account.nil? ? nil : self.controller.send(:current_ability)

        super.merge({
          current_site:     current_site,
          current_account:  current_account,
          ability:          ability
        })
      end

      def to_json
        if get?
          display resource
        elsif has_errors?
          with_flash_message(:alert) do
            display resource.errors, status: :unprocessable_entity
          end
        elsif post?
          in_header = controller.request.headers['X-Flash'] == 'true'
          with_flash_message(:notice, in_header) do
            display resource, location: api_location
          end
        elsif put?
          with_flash_message do |message|
            display resource, status: :ok, location: api_location
          end
        elsif delete?
          with_flash_message do |message|
            display resource, status: :ok, location: api_location
          end
        elsif has_empty_resource_definition?
          display empty_resource, status: :ok
        else
          with_flash_message do
            head :ok
          end
        end
      end

      protected

      def with_flash_message(type = :notice, in_header = true)
        if in_header
          set_flash_message!
          message = URI::escape(controller.flash[type].to_str) if controller.flash[type] 

          unless message.blank?
            controller.headers['X-Message']       = ActiveSupport::JSON::Encoding.escape(message)
            controller.headers['X-Message-Type']  = type.to_s
{'fraction_non_alphanumeric': 0.07015399657785383, 'fraction_numerical': 0.0, 'mean_word_length': 2.0514541387024607, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1780226', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1038, 'n_words': 259}