<commit_before>class Plugin < ActiveRecord::Base
  belongs_to :district

  serialize :plugin_attributes

  before_validation :default_attributes
  before_validation :name, uniqueness: {scope: :district_id}
  after_create :hook_created
  after_update :hook_updated
  after_destroy :hook_destroyed

  def hook(trigger, origin, arg=nil)
    plugin.hook(trigger, origin, arg)
  end

  def to_param
    name
  end

  def plugin
    begin
      klass = ("Barcelona::Plugins::" + "#{name}_plugin".classify).constantize
    rescue NameError => e
      Rails.logger.error e
      return arg
    end
    klass.new(self)
  end

  private

  def default_attributes
    self.plugin_attributes ||= {}
  end

  def hook_created
    hook(:created, self)
  end

  def hook_updated
    hook(:updated, self)
  end

  def hook_destroyed
    hook(:destroyed, self)
  end
end<commit_msg>save district after a plugin is saved<commit_after>class Plugin < ActiveRecord::Base
  belongs_to :district

  serialize :plugin_attributes

  before_validation :default_attributes
  before_validation :name, uniqueness: {scope: :district_id}
  after_create :hook_created
  after_update :hook_updated
  after_save :save_district
  after_destroy :hook_destroyed

  def hook(trigger, origin, arg=nil)
    plugin.hook(trigger, origin, arg)
  end

  def to_param
    name
  end

  def plugin
    begin
      klass = ("Barcelona::Plugins::" + "#{name}_plugin".classify).constantize
    rescue NameError => e
      Rails.logger.error e
      return arg
    end
    klass.new(self)
  end

  private

  def default_attributes
    self.plugin_attributes ||= {}
  end

  def hook_created
    hook(:created, self)
  end

  def hook_updated
    hook(:updated, self)
  end

  def hook_destroyed
    hook(:destroyed, self)
  end

  def save_district
    # trigger district callbacks so that AWS resources are properly updated
    district.save!
  end
end
{'fraction_non_alphanumeric': 0.07668067226890757, 'fraction_numerical': 0.0, 'mean_word_length': 3.471830985915493, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5069553', 'n_tokens_mistral': 612, 'n_tokens_neox': 584, 'n_words': 159}