<commit_before>class ContactsController < MyplaceonlineController
  def model
    Contact
  end
  
  def before_destroy
    if @obj.identity_id == current_user.primary_identity.id
      raise "Cannot delete own identity"
    end
  end
  
  def may_upload
    true
  end
  
  def index
    @contact_type = params[:contact_type]
    if !@contact_type.blank?
      @contact_type = @contact_type.to_i
    end
    super
  end

  def self.param_names
    [
      :contact_type,
      identity_attributes: [
        :id,
        :name,
        :nickname,
        :birthday,
        :notes,
        :likes,
        :gift_ideas,
        :ktn,
        identity_phones_attributes: [
          :id,
          :number,
          :phone_type,
          :_destroy
        ],
        identity_emails_attributes: [:id, :email, :_destroy],
        identity_locations_attributes: [
          :id,
          :_destroy,
          {
            location_attributes: LocationsController.param_names + [:id]
          }
        ],
        identity_drivers_licenses_attributes: [
          :id,
          :identifier,
          :expires,
          :region,
          :sub_region1,
          :_destroy,
          {
            identity_file_attributes: [
              :id,
              :file,
              :_destroy
            ]
          }
        ],
        identity_relationships_attributes: [
          :id,
          :relationship_type,
          :_destroy,
          {
            contact_attributes: [
              :id,
              :_destroy
            ]
          }
        ],
        identity_pictures_attributes: [
          :id,
          :_destroy,
          identity_file_attributes: [
            :id,
            :file,
            :notes
          ]
        ]
      ]
    ]
  end

  def self.reject_if_blank(attributes)
    attributes.all?{|key, value|
      if key == "identity_attributes"
        value.all?{|key2, value2| value2.blank?}
      else
        value.blank?
      end
    }
  end

  protected

    def sorts
      ["lower(identities.name) ASC"]
    end

    def obj_params
      params.require(:contact).permit(
        ContactsController.param_names,
        conversations_attributes: [
          :id,
          :conversation,
          :conversation_date,
          :_destroy
        ]
      )
    end

    def all
      if @contact_type.blank?
        model.joins(:identity).where(
          owner_id: current_user.primary_identity.id
        )
      else
        model.joins(:identity).where(
          owner_id: current_user.primary_identity.id,
          contact_type: @contact_type
        )
      end
    end
    
    def before_all_actions
      # Create a Contact for the current user identity if it doesn't exist
      current_user.primary_identity.ensure_contact!
    end
    
    def presave
      if !@obj.identity.nil?
        @obj.identity.identity_pictures.each do |pic|<commit_msg>Allow conversations when creating nested contact<commit_after>class ContactsController < MyplaceonlineController
  def model
    Contact
  end
  
  def before_destroy
    if @obj.identity_id == current_user.primary_identity.id
      raise "Cannot delete own identity"
    end
  end
  
  def may_upload
    true
  end
  
  def index
    @contact_type = params[:contact_type]
    if !@contact_type.blank?
      @contact_type = @contact_type.to_i
    end
    super
  end

  def self.param_names
    [
      :contact_type,
      conversations_attributes: [
        :id,
        :conversation,
        :conversation_date,
        :_destroy
      ],
      identity_attributes: [
        :id,
        :name,
        :nickname,
        :birthday,
        :notes,
        :likes,
        :gift_ideas,
        :ktn,
        identity_phones_attributes: [
          :id,
          :number,
          :phone_type,
          :_destroy
        ],
        identity_emails_attributes: [:id, :email, :_destroy],
        identity_locations_attributes: [
          :id,
          :_destroy,
          {
            location_attributes: LocationsController.param_names + [:id]
          }
        ],
        identity_drivers_licenses_attributes: [
          :id,
          :identifier,
          :expires,
          :region,
          :sub_region1,
          :_destroy,
          {
            identity_file_attributes: [
              :id,
              :file,
              :_destroy
            ]
          }
        ],
        identity_relationships_attributes: [
          :id,
          :relationship_type,
          :_destroy,
          {
            contact_attributes: [
              :id,
              :_destroy
            ]
          }
        ],
        identity_pictures_attributes: [
          :id,
          :_destroy,
          identity_file_attributes: [
            :id,
            :file,
            :notes
          ]
        ]
      ]
    ]
  end

  def self.reject_if_blank(attributes)
    attributes.all?{|key, value|
      if key == "identity_attributes"
        value.all?{|key2, value2| value2.blank?}
      else
        value.blank?
      end
    }
  end

  protected

    def sorts
      ["lower(identities.name) ASC"]
    end

    def obj_params
      params.require(:contact).permit(
        ContactsController.param_names
      )
    end

    def all
      if @contact_type.blank?
        model.joins(:identity).where(
          owner_id: current_user.primary_identity.id
        )
      else
        model.joins(:identity).where(
          owner_id: current_user.primary_identity.id,
          contact_type: @contact_type
        )
      end
    end
    
    def before_all_actions
      # Create a Contact for the current user identity if it doesn't exist
      current_user.primary_identity.ensure_contact!
    end
    
    def presave
      if !@obj.identity.nil?
        @obj.identity.identity_pictures.each do |pic|
{'fraction_non_alphanumeric': 0.08109495295124038, 'fraction_numerical': 0.0013686911890504706, 'mean_word_length': 1.4834324553950722, 'pattern_counts': {'":': 0, '<': 5, '<?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': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5435059', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1671, 'n_words': 316}