<commit_before>module Models
  module Position
    extend ActiveSupport::Concern

    POSITION_VALUES = [:position_member, :position_deputy]

    module ClassMethods
      def position_field_symbol
        @symbol
      end
      def position_field(symbol)
        @symbol = symbol
        validates position_field_symbol, allow_nil: true, inclusion: { in:  POSITION_VALUES }
        field position_field_symbol, type: Symbol
        scope :current_members, where(position_field_symbol => :position_member, current: true)
        scope :current_deputies, where(position_field_symbol => :position_deputy, current: true)
      end
    end

    included do
      belongs_to :member, :class_name => name, :inverse_of => :deputy
      has_one :deputy, :class_name => name, :inverse_of => :member
    end

    def nil_or_find(id)
      id.blank? ? nil : self.class.find(id)
    end

    def set_position!(params)
      return to_not_selected! if params[self.class.position_field_symbol].blank?
      case params[self.class.position_field_symbol].to_sym
        when :position_member then to_member! params[:deputy]
        when :position_deputy then to_deputy! params[:member]
      end
    end

    protected

    def with_deputy_and_member_relations_cleared(id)
      position = nil_or_find(id)
      unless position.nil?
        position.deputy = nil
        position.member = nil
        position.save!
      end
      position
    end

    def to_member!(deputy_id)
      self[self.class.position_field_symbol] = :position_member
      self.member = nil
      self.deputy = with_deputy_and_member_relations_cleared(deputy_id)
      save!
    end

    def to_deputy!(member_id)<commit_msg>Filter the "internal" attributes when updating class that includes position<commit_after>module Models
  module Position
    extend ActiveSupport::Concern

    POSITION_VALUES = [:position_member, :position_deputy]

    module ClassMethods
      def position_field_symbol
        @symbol
      end
      def position_field(symbol)
        @symbol = symbol
        validates position_field_symbol, allow_nil: true, inclusion: { in:  POSITION_VALUES }
        field position_field_symbol, type: Symbol
        scope :current_members, where(position_field_symbol => :position_member, current: true)
        scope :current_deputies, where(position_field_symbol => :position_deputy, current: true)
      end
    end

    included do
      belongs_to :member, :class_name => name, :inverse_of => :deputy
      has_one :deputy, :class_name => name, :inverse_of => :member
    end

    def nil_or_find(id)
      id.blank? ? nil : self.class.find(id)
    end

    def update_attributes(attributes = {}, options = {})
      super(attributes.except(:position, :member, :deputy), options)
    end

    def set_position!(params)
      return to_not_selected! if params[self.class.position_field_symbol].blank?
      case params[self.class.position_field_symbol].to_sym
        when :position_member then to_member! params[:deputy]
        when :position_deputy then to_deputy! params[:member]
      end
    end

    protected

    def with_deputy_and_member_relations_cleared(id)
      position = nil_or_find(id)
      unless position.nil?
        position.deputy = nil
        position.member = nil
        position.save!
      end
      position
    end

    def to_member!(deputy_id)
      self[self.class.position_field_symbol] = :position_member
      self.member = nil
      self.deputy = with_deputy_and_member_relations_cleared(deputy_id)
      save!
    end

    def to_deputy!(member_id)
{'fraction_non_alphanumeric': 0.07896947633716046, 'fraction_numerical': 0.0, 'mean_word_length': 3.0869565217391304, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '1965178', 'n_tokens_mistral': 1123, 'n_tokens_neox': 1058, 'n_words': 240}