<commit_before>module WulinMaster
  module GridRelation
    extend ActiveSupport::Concern

    included do
      class_eval do
        class << self
          attr_reader :current_filter_column, :current_detail_model
        end
      end
    end
    
    module ClassMethods
      # Set master grid, invoked from grid.apply_custom_config method
      def master_grid(master_grid_klass, options={}, inclusion=true)
        @current_filter_column = nil

        if options[:screen]
          detail_model = self.model
          master_grid = master_grid_klass.constantize.new({screen: options[:screen], no_render: true})   # set no_render true to skip the config applying
          
          # master_model must has_many detail_model, detail_model may belongs_to master_model OR has_many master_model
          reflection = detail_model.reflections[master_grid.model.to_s.underscore.intern] || detail_model.reflections[master_grid.model.to_s.underscore.pluralize.intern]

          through = options[:through] || reflection.foreign_key

          # call affiliation or reverse_affiliation behavior for detail grid
          operator = if reflection.macro == :belongs_to
            inclusion ? 'equals' : 'not_equals'
          elsif reflection.macro == :has_many
            inclusion ? 'include' : 'exclude'
          end

          # add association column to self for filtering
          unless self.columns_pool.find {|c| c.full_name == reflection.foreign_key and c.valid_in_screen(options[:screen]) }
            column reflection.name, visible: false, editable: false, formable: false, option_text_attribute: "id", detail_relation_name: @current_detail_model, only: [options[:screen].intern]
            @current_filter_column = reflection.name
          end

          behavior :add_candidate_filter, filter: reflection.foreign_key, only: [options[:screen].intern]
          behavior :affiliation, master_grid_name: master_grid.name, only: [options[:screen].intern], through: through, operator: operator
          behavior :empty_detail, master_grid_name: master_grid.name, only: [options[:screen].intern]
          behavior :clear_detail_when_multi_select, master_grid_name: master_grid.name, only: [options[:screen].intern]
        end
      end

      # Inclusion-Exclusion relation, include of master grid
      def include_of(master_grid_klass, options={})
        self.master_grid(master_grid_klass, options, true)
        behavior :include_exclude_trivia, only: [options[:screen].intern] if options[:screen]
      end
<commit_msg>REFACTOR - grid_relation.rb<commit_after>module WulinMaster
  module GridRelation
    extend ActiveSupport::Concern

    included do
      class_eval do
        class << self
          attr_reader :current_filter_column, :current_detail_model
        end
      end
    end
    
    module ClassMethods
      # Set master grid, invoked from grid.apply_custom_config method
      def master_grid(master_grid_klass_name, options={}, inclusion=true)
        @current_filter_column = nil

        if master_grid_klass = master_grid_klass_name.to_s.classify.safe_constantize
          if options[:screen].present?
            detail_model = self.model
            master_grid_name = WulinMaster::Utilities.get_grid_name(master_grid_klass_name, options[:screen])

            # master_model must has_many detail_model, detail_model may belongs_to master_model OR has_many master_model
            reflection = detail_model.reflections[master_grid_klass.model.name.underscore.intern] || detail_model.reflections[master_grid_klass.model.name.underscore.pluralize.intern]

            through = options[:through] || reflection.foreign_key

            # call affiliation or reverse_affiliation behavior for detail grid
            operator = if reflection.macro == :belongs_to
              inclusion ? 'equals' : 'not_equals'
            elsif reflection.macro == :has_many
              inclusion ? 'include' : 'exclude'
            end

            # add association column to self for filtering
            unless self.columns_pool.find {|c| c.full_name == reflection.foreign_key and c.valid_in_screen(options[:screen]) }
              column reflection.name, visible: false, editable: false, formable: false, option_text_attribute: "id", detail_relation_name: @current_detail_model, only: [options[:screen].intern]
              @current_filter_column = reflection.name
            end

            behavior :add_candidate_filter, filter: reflection.foreign_key, only: [options[:screen].intern]
            behavior :affiliation, master_grid_name: master_grid_name, only: [options[:screen].intern], through: through, operator: operator
            behavior :empty_detail, master_grid_name: master_grid_name, only: [options[:screen].intern]
            behavior :clear_detail_when_multi_select, master_grid_name: master_grid_name, only: [options[:screen].intern]
          end
        else
          raise "'#{master_grid_klass_name}' is not valid grid name."
        end
      end

      # Inclusion-Exclusion relation, include of master grid
      def include_of(master_grid_klass, options={})
        self.master_grid(master_grid_klass, options, true)
        behavior :include_exclude_trivia, only: [options[:screen].intern] if options[:screen]
      end

{'fraction_non_alphanumeric': 0.08135145337863345, 'fraction_numerical': 0.0, 'mean_word_length': 3.1922468354430378, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4816953', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1421, 'n_words': 341}