<commit_before>module Formbuilder
  module Entry
    extend ActiveSupport::Concern

    included do
      attr_accessor :skip_validation

      before_validation :normalize_responses
      validates_with Formbuilder::EntryValidator
      before_save :calculate_responses_text, if: :responses_column_changed?

      scope :order_by_response_field_value, -> (response_field, direction) {
        if response_field.sort_as_numeric
          order("(responses -> '#{response_field.id}_sortable_value') ::numeric #{direction} NULLS LAST")
        else
          order("LOWER(responses -> '#{response_field.id}_sortable_value') #{direction} NULLS LAST")
        end
      }

      scope :order_by_response_field_checkbox_value, -> (response_field, option, direction) {
        order("(CASE WHEN (responses -> '#{response_field.id}_sortable_values_#{option}') = 'true' THEN
                  1
                ELSE
                  0
                END) #{direction}".squish)
      }

      scope :order_by_response_field_table_sum, -> (response_field, column, direction) {
        order("(responses -> '#{response_field.id}_sum_#{column}') ::numeric #{direction}")
      }
    end

    def responses_column
      @responses_column || 'responses'
    end

    def responses_column=(x)
      @responses_column = x
    end

    def get_responses
      send(responses_column)
    end

    def set_responses(x)
      send("#{responses_column}=", x)
    end

    def mark_responses_as_changed!
      send("#{responses_column}_will_change!")
    end

    def responses_column_changed?
      send("#{responses_column}_changed?")
    end

    def responses_column_was
      send("#{responses_column}_was")
    end

    def value_present?(response_field)
      value = self.response_value(response_field)

      # value isn't blank (ignore hashes)
      return true if (value && value.present? && !value.is_a?(Hash))

      # no options are available
      return true if (response_field.options_field && Array(response_field.field_options["options"]).empty?)

      # there is at least one value (for hashes)
      # reject select fields
      return true if (value.is_a?(Hash) && value.reject { |k, v| k.in? ['am_pm', 'country'] }.find { |k, v| v.present? })

      # otherwise, it's not present
      return false
    end

    def response_value(response_field)
      value = get_responses[response_field.id.to_s]

      if value
        response_field.serialized ? YAML::load(value) : value
      elsif !value && response_field.serialized && response_field.field_type != 'checkboxes'
        {}
      else # for checkboxes, we need to know the difference between no value and none selected
        nil
      end
    end<commit_msg>Fix rails 4 incompatibility (fixes #7)<commit_after>module Formbuilder
  module Entry
    extend ActiveSupport::Concern

    included do
      attr_accessor :skip_validation

      before_validation :normalize_responses
      validates_with Formbuilder::EntryValidator
      before_save :calculate_responses_text, if: :responses_column_changed?

      scope :order_by_response_field_value, -> (response_field, direction) {
        if response_field.sort_as_numeric
          order("(responses -> '#{response_field.id}_sortable_value') ::numeric #{direction} NULLS LAST")
        else
          order("LOWER(responses -> '#{response_field.id}_sortable_value') #{direction} NULLS LAST")
        end
      }

      scope :order_by_response_field_checkbox_value, -> (response_field, option, direction) {
        order("(CASE WHEN (responses -> '#{response_field.id}_sortable_values_#{option}') = 'true' THEN
                  1
                ELSE
                  0
                END) #{direction}".squish)
      }

      scope :order_by_response_field_table_sum, -> (response_field, column, direction) {
        order("(responses -> '#{response_field.id}_sum_#{column}') ::numeric #{direction}")
      }
    end

    def responses_column
      @responses_column || 'responses'
    end

    def responses_column=(x)
      @responses_column = x
    end

    def get_responses
      send(responses_column)
    end

    def set_responses(x)
      send("#{responses_column}=", x)
    end

    def mark_responses_as_changed!
      send("#{responses_column}_will_change!")
    end

    def responses_column_changed?
      send("#{responses_column}_changed?")
    end

    def responses_column_was
      send("#{responses_column}_was")
    end

    def value_present?(response_field)
      value = self.response_value(response_field)

      # value isn't blank (ignore hashes)
      return true if (value && value.present? && !value.is_a?(Hash))

      # no options are available
      return true if (response_field.options_field && Array(response_field.field_options["options"]).empty?)

      # there is at least one value (for hashes)
      # reject select fields
      return true if (value.is_a?(Hash) && value.reject { |k, v| k.in? ['am_pm', 'country'] }.find { |k, v| v.present? })

      # otherwise, it's not present
      return false
    end

    def response_value(response_field)
      r = get_responses || {}
      value = r[response_field.id.to_s]

      if value
        response_field.serialized ? YAML::load(value) : value
      elsif !value && response_field.serialized && response_field.field_type != 'checkboxes'
        {}
      else # for checkboxes, we need to know the difference between no value and none selected
        nil
      end
    end
{'fraction_non_alphanumeric': 0.10428441203281677, 'fraction_numerical': 0.0010938924339106654, 'mean_word_length': 2.893541518807665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '2609645', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1609, 'n_words': 370}