<commit_before>require 'surveyor/common'
require 'rabl'

module Surveyor
  module Models
    module SurveyMethods
      extend ActiveSupport::Concern
      include ActiveModel::Validations
      include ActiveModel::ForbiddenAttributesProtection

      included do
        # Associations
        has_many :sections, class_name: 'SurveySection', :dependent => :destroy
        has_many :response_sets
        has_many :translations, :class_name => "SurveyTranslation"
        attr_accessible *PermittedParams.new.survey_attributes if defined? ActiveModel::MassAssignmentSecurity

        # Validations
        validates_presence_of :title
        validates_uniqueness_of :survey_version, :scope => :access_code, :message => "survey with matching access code and version already exists"

        # Derived attributes
        before_save :generate_access_code
        before_save :increment_version
      end

      module ClassMethods
        def to_normalized_string(value)
          # replace non-alphanumeric with "-". remove repeat "-"s. don't start or end with "-"
          value.to_s.downcase.gsub(/[^a-z0-9]/,"-").gsub(/-+/,"-").gsub(/-$|^-/,"")
        end
      end

      # Instance methods
      def initialize(*args)
        super(*args)
        default_args
      end

      def default_args
        self.api_id ||= Surveyor::Common.generate_api_id
        self.display_order ||= Survey.count
      end

      def active?
        self.active_as_of?(DateTime.now)
      end
      def active_as_of?(date)<commit_msg>Added latest_versions scope to Survey class<commit_after>require 'surveyor/common'
require 'rabl'

module Surveyor
  module Models
    module SurveyMethods
      extend ActiveSupport::Concern
      include ActiveModel::Validations
      include ActiveModel::ForbiddenAttributesProtection

      included do
        # Associations
        has_many :sections, class_name: 'SurveySection', :dependent => :destroy
        has_many :response_sets
        has_many :translations, :class_name => "SurveyTranslation"
        attr_accessible *PermittedParams.new.survey_attributes if defined? ActiveModel::MassAssignmentSecurity

        # Validations
        validates_presence_of :title
        validates_uniqueness_of :survey_version, :scope => :access_code, :message => "survey with matching access code and version already exists"

        # Derived attributes
        before_save :generate_access_code
        before_save :increment_version

        # Scopes
        scope :latest_versions, -> { order('access_code asc, survey_version desc').select('distinct on ("access_code") *') }
      end

      module ClassMethods
        def to_normalized_string(value)
          # replace non-alphanumeric with "-". remove repeat "-"s. don't start or end with "-"
          value.to_s.downcase.gsub(/[^a-z0-9]/,"-").gsub(/-+/,"-").gsub(/-$|^-/,"")
        end
      end

      # Instance methods
      def initialize(*args)
        super(*args)
        default_args
      end

      def default_args
        self.api_id ||= Surveyor::Common.generate_api_id
        self.display_order ||= Survey.count
      end

      def active?
        self.active_as_of?(DateTime.now)
      end
      def active_as_of?(date)
{'fraction_non_alphanumeric': 0.09364341085271317, 'fraction_numerical': 0.00124031007751938, 'mean_word_length': 2.746806039488966, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '5434263', 'n_tokens_mistral': 919, 'n_tokens_neox': 904, 'n_words': 209}