<commit_before>module ProposesEdits

  def self.included(base)
    base.extend ClassMethods
  end

  def instance_to_edit
    @instance_to_edit ||=
      self.send(self.class.klass_to_edit)
  end

  def accept params
    instance_to_edit.update(params)
    update!(:accepted => true, :archived => true)
  end

  def non_published_generally_editable_fields
    self.class.fields_to_edit.select{|f| !editable?(f)}
  end

  def editable? field, opts = {}
    usr = opts[:by]
    publish_field = self.class.publish_proc.call(field)
    if instance_to_edit.respond_to? publish_field
      (instance_to_edit.send(publish_field) || usr.try(self.class.user_type_who_edits_non_public_fields)) && self.class.fields_to_edit.include?(field)
    else
      self.class.fields_to_edit.include?(field)
    end
  end

  def has_proposed_edit? field
    instance_to_edit.send(field) != self.send(field)
  end

  module ClassMethods
    attr_reader :klass_to_edit
    attr_reader :fields_to_edit
    attr_reader :publish_proc
    attr_reader :user_type_who_edits_non_public_fields<commit_msg>Refactor editable? method in proposes edits module<commit_after>module ProposesEdits

  def self.included(base)
    base.extend ClassMethods
  end

  def instance_to_edit
    @instance_to_edit ||=
      self.send(self.class.klass_to_edit)
  end

  def accept params
    instance_to_edit.update(params)
    update!(:accepted => true, :archived => true)
  end

  def non_published_generally_editable_fields
    self.class.fields_to_edit.select{|f| !editable?(f)}
  end

  def editable? field, opts = {}
    editable = self.class.fields_to_edit.include?(field)
    editable &= can_user_edit_this_field_if_private? self.class.publish_proc.call(field), opts[:by]
    editable
  end

  def has_proposed_edit? field
    instance_to_edit.send(field) != self.send(field)
  end
  private 
  def can_user_edit_this_field_if_private? publish_field, usr
    return true unless instance_to_edit.respond_to? publish_field
    is_public = instance_to_edit.send(publish_field)
    user_can_edit_private_fields = usr.try(self.class.user_type_who_edits_non_public_fields)
    is_public || user_can_edit_private_fields  
  end

  module ClassMethods
    attr_reader :klass_to_edit
    attr_reader :fields_to_edit
    attr_reader :publish_proc
    attr_reader :user_type_who_edits_non_public_fields
{'fraction_non_alphanumeric': 0.08085106382978724, 'fraction_numerical': 0.0, 'mean_word_length': 4.454756380510441, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '2810108', 'n_tokens_mistral': 820, 'n_tokens_neox': 805, 'n_words': 136}