<commit_before># Element represents a section, question or content element on the question sheet
module Fe
  class Element < ActiveRecord::Base
    self.table_name = self.table_name.sub('fe_', Fe.table_name_prefix)

    belongs_to :question_grid,
               :class_name => "Fe::QuestionGrid",
               :foreign_key => "question_grid_id"

    belongs_to :choice_field,
               :class_name => "Fe::ChoiceField",
               :foreign_key => "choice_field_id"

    belongs_to :question_sheet, :foreign_key => "related_question_sheet_id"

    belongs_to :conditional, :polymorphic => true

    self.inheritance_column = :kind

    has_many :page_elements, :dependent => :destroy
    has_many :pages, :through => :page_elements

    scope :active, -> { select("distinct(#{Fe::Element.table_name}.id), #{Fe::Element.table_name}.*").where(Fe::QuestionSheet.table_name + '.archived' => false).joins({:pages => :question_sheet}) }

    validates_presence_of :kind
    validates_presence_of :style
    # validates_presence_of :label, :style, :on => :update

    validates_length_of :kind, :maximum => 40, :allow_nil => true
    validates_length_of :style, :maximum => 40, :allow_nil => true
    # validates_length_of :label, :maximum => 255, :allow_nil => true

    before_validation :set_defaults, :on => :create
    before_save :set_conditional_element
    after_save :update_any_previous_conditional_elements

    # HUMANIZED_ATTRIBUTES = {
    #   :slug => "Variable"
    # }changed.include?('address1')
    #
    # def self.human_attrib_name(attr)
    #   HUMANIZED_ATTRIBUTES[attr.to_sym] || super
    # end

    def has_response?(answer_sheet = nil)
      false
    end

    def limit(answer_sheet = nil)
      if answer_sheet && object_name.present? && attribute_name.present?
        puts 'Fe::Element.limit 1'
        begin
          puts 'Fe::Element.limit 2'
          unless eval("answer_sheet." + self.object_name + ".nil?")
            puts 'Fe::Element.limit 3'
            klass = eval("answer_sheet." + self.object_name + ".class")
            puts "Fe::Element.limit 4 klass: #{klass.inspect}"
            column = klass.columns_hash[self.attribute_name]
            puts "Fe::Element.limit 5 column: #{column.inspect}"
            column.limit
            puts "Fe::Element.limit 6 column.limit: #{column.limit}"
          end
        rescue
          nil
        end
      end
    end

    # assume each element is on a question sheet only once to make things simpler. if not, just take the first one
    def previous_element(question_sheet)
      page_element = page_elements.joins(page: :question_sheet).where("#{Fe::QuestionSheet.table_name}.id" => question_sheet.id).first
      return unless page_element
      index = page_element.page.elements.index(self)
      if index > 0 && prev_el = page_element.page.elements[index-1]
        return prev_el
      end
    end

    def required?(answer_sheet = nil)
      if answer_sheet && 
        self.question_grid.nil? && 
        (prev_el = previous_element(answer_sheet.question_sheet)) && 
        prev_el.is_a?(Fe::Question) && <commit_msg>try an explicit return column.limit to get test passing<commit_after># Element represents a section, question or content element on the question sheet
module Fe
  class Element < ActiveRecord::Base
    self.table_name = self.table_name.sub('fe_', Fe.table_name_prefix)

    belongs_to :question_grid,
               :class_name => "Fe::QuestionGrid",
               :foreign_key => "question_grid_id"

    belongs_to :choice_field,
               :class_name => "Fe::ChoiceField",
               :foreign_key => "choice_field_id"

    belongs_to :question_sheet, :foreign_key => "related_question_sheet_id"

    belongs_to :conditional, :polymorphic => true

    self.inheritance_column = :kind

    has_many :page_elements, :dependent => :destroy
    has_many :pages, :through => :page_elements

    scope :active, -> { select("distinct(#{Fe::Element.table_name}.id), #{Fe::Element.table_name}.*").where(Fe::QuestionSheet.table_name + '.archived' => false).joins({:pages => :question_sheet}) }

    validates_presence_of :kind
    validates_presence_of :style
    # validates_presence_of :label, :style, :on => :update

    validates_length_of :kind, :maximum => 40, :allow_nil => true
    validates_length_of :style, :maximum => 40, :allow_nil => true
    # validates_length_of :label, :maximum => 255, :allow_nil => true

    before_validation :set_defaults, :on => :create
    before_save :set_conditional_element
    after_save :update_any_previous_conditional_elements

    # HUMANIZED_ATTRIBUTES = {
    #   :slug => "Variable"
    # }changed.include?('address1')
    #
    # def self.human_attrib_name(attr)
    #   HUMANIZED_ATTRIBUTES[attr.to_sym] || super
    # end

    def has_response?(answer_sheet = nil)
      false
    end

    def limit(answer_sheet = nil)
      if answer_sheet && object_name.present? && attribute_name.present?
        puts 'Fe::Element.limit 1'
        begin
          puts 'Fe::Element.limit 2'
          unless eval("answer_sheet." + self.object_name + ".nil?")
            puts 'Fe::Element.limit 3'
            klass = eval("answer_sheet." + self.object_name + ".class")
            puts "Fe::Element.limit 4 klass: #{klass.inspect}"
            column = klass.columns_hash[self.attribute_name]
            puts "Fe::Element.limit 5 column: #{column.inspect}"
            puts "Fe::Element.limit 6 column.limit: #{column.limit}"
            return column.limit
          end
        rescue
          nil
        end
      end
    end

    # assume each element is on a question sheet only once to make things simpler. if not, just take the first one
    def previous_element(question_sheet)
      page_element = page_elements.joins(page: :question_sheet).where("#{Fe::QuestionSheet.table_name}.id" => question_sheet.id).first
      return unless page_element
      index = page_element.page.elements.index(self)
      if index > 0 && prev_el = page_element.page.elements[index-1]
        return prev_el
      end
    end

    def required?(answer_sheet = nil)
      if answer_sheet && 
        self.question_grid.nil? && 
        (prev_el = previous_element(answer_sheet.question_sheet)) && 
        prev_el.is_a?(Fe::Question) && 
{'fraction_non_alphanumeric': 0.11449896776242655, 'fraction_numerical': 0.005081784976973162, 'mean_word_length': 3.0089115213239976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '5251995', 'n_tokens_mistral': 1959, 'n_tokens_neox': 1912, 'n_words': 421}