app/models/taxon_change.rb
# For all records with a taxon association affected by this change, update the record if 
# possible / desired by its owner, or generate an update for the owner notifying them of 
# the change
For all records with a taxon association affected by this change, update the record if
possible / desired by its owner, or generate an update for the owner notifying them of
the change
For all records with a taxon association affected by this change update the record if possible / desired by its owner or generate an update for the owner notifying them of the change
def commit_records
    return if input_taxa.blank?
    Rails.logger.info "[INFO #{Time.now}] starting commit_records for #{self}"
    notified_user_ids = []
    associations_to_update = %w(observations listed_taxa taxon_links identifications)
    has_many_reflections = associations_to_update.map do |a| 
      Taxon.reflections.detect{|k,v| k.to_s == a}
    end
    has_many_reflections.each do |k, reflection|
      reflection.klass.where("#{reflection.foreign_key} IN (?)", input_taxa).find_each do |record|
        record_has_user = record.respond_to?(:user) && record.user
        if record_has_user && !notified_user_ids.include?(record.user.id)
          Update.create(
            :resource => self,
            :notifier => self,
            :subscriber => record.user, 
            :notification => "committed")
          notified_user_ids << record.user.id
        end
        if automatable? && (!record_has_user || record.user.prefers_automatic_taxonomic_changes?)
          update_records_of_class(record.class, output_taxon, :records => [record])
        end
      end
    end
    [input_taxa, output_taxa].flatten.compact.each do |taxon|
      Taxon.update_all(
        [
          "observations_count = ?, listed_taxa_count = ?", 
          Observation.of(taxon).count, 
          ListedTaxon.where(:taxon_id => taxon).count
        ],
        ["id = ?", taxon.id]
      )
    end
    Rails.logger.info "[INFO #{Time.now}] finished commit_records for #{self}"
  end
def commit_records return if input_taxa . blank? Rails . logger . info "[INFO #{Time.now}] starting commit_records for #{self}" notified_user_ids = [ ] associations_to_update = %w( observations listed_taxa taxon_links identifications ) has_many_reflections = associations_to_update . map do | a | Taxon . reflections . detect { | k , v | k . to_s == a } end has_many_reflections . each do | k , reflection | reflection . klass . where ( "#{reflection.foreign_key} IN (?)" , input_taxa ) . find_each do | record | record_has_user = record . respond_to? ( :user ) && record . user if record_has_user && ! notified_user_ids . include? ( record . user . id ) Update . create ( :resource => self , :notifier => self , :subscriber => record . user , :notification => "committed" ) notified_user_ids << record . user . id end if automatable? && ( ! record_has_user || record . user . prefers_automatic_taxonomic_changes? ) update_records_of_class ( record . class , output_taxon , :records => [ record ] ) end end end [ input_taxa , output_taxa ] . flatten . compact . each do | taxon | Taxon . update_all ( [ "observations_count = ?, listed_taxa_count = ?" , Observation . of ( taxon ) . count , ListedTaxon . where ( :taxon_id => taxon ) . count ] , [ "id = ?" , taxon . id ] ) end Rails . logger . info "[INFO #{Time.now}] finished commit_records for #{self}" end
For all records with a taxon association affected by this change, update the record if
possible / desired by its owner, or generate an update for the owner notifying them of
the change
For all records with a taxon association affected by this change update the record if possible / desired by its owner or generate an update for the owner notifying them of the change