<commit_before>module ExpressTemplates
  module Components
    module Forms
      # Provides a form component with knowledge of any association
      # on the field and an means of loading the collection for supplying
      # options to the user.
      module OptionSupport
        # Reflect on any association and return it if the association type
        # is :belongs_to.  Returns false if the association is not :belongs_to.
        # Returns nil if there was a problem reflecting.
        def belongs_to_association
          begin
            reflection = resource_name.classify.constantize.reflect_on_association(field_name.to_sym)
            if reflection.macro.eql?(:belongs_to)
              return reflection
            end
          rescue
            nil
          end
        end

        # Provide ActiveRecord code to load the associated collection as
        # options for display.
        def related_collection
          if reflection = belongs_to_association
            "#{reflection.klass}.all.select(:#{option_value_method}, :#{option_name_method}).order(:name)"
          end
        end

        protected

          def option_value_method
            :id
          end

          def option_name_method
            :name
          end<commit_msg>[#95327494] Fix select behavior on belongs_to association<commit_after>module ExpressTemplates
  module Components
    module Forms
      # Provides a form component with knowledge of any association
      # on the field and an means of loading the collection for supplying
      # options to the user.
      module OptionSupport
        # Reflect on any association and return it if the association type
        # is :belongs_to.  Returns false if the association is not :belongs_to.
        # Returns nil if there was a problem reflecting.
        def belongs_to_association
          begin
            # assumes the belongs_to association uses <name>_id
            reflection = resource_name.classify.constantize.reflect_on_association(field_name.gsub(/_id$/, '').to_sym)
            if reflection.macro.eql?(:belongs_to)
              return reflection
            end
          rescue
            nil
          end
        end

        # Provide ActiveRecord code to load the associated collection as
        # options for display.
        def related_collection
          if reflection = belongs_to_association
            "#{reflection.klass}.all.select(:#{option_value_method}, :#{option_name_method}).order(:name)"
          end
        end

        protected

          def option_value_method
            :id
          end

          def option_name_method
            :name
          end
{'fraction_non_alphanumeric': 0.04822429906542056, 'fraction_numerical': 0.0029906542056074765, 'mean_word_length': 1.9866071428571428, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '253067', 'n_tokens_mistral': 655, 'n_tokens_neox': 644, 'n_words': 224}