SpeciesFileGroup/taxonworks

View on GitHub
app/models/concerns/shared/protocol_relationships.rb

Summary

Maintainability
A
0 mins
Test Coverage
# Shared code for attaching protocols to data objects.
#
module Shared::ProtocolRelationships

  extend ActiveSupport::Concern

  included do

    ::ProtocolRelationship.related_foreign_keys.push self.name.foreign_key

    has_many :protocol_relationships, as: :protocol_relationship_object, dependent: :destroy, inverse_of: :protocol_relationship_object
    has_many :protocols, through: :protocol_relationships 

    accepts_nested_attributes_for :protocol_relationships, allow_destroy: true
    accepts_nested_attributes_for :protocols, allow_destroy: true, reject_if: :reject_protocols
  end

  def protocolled?
    protocols.any?
  end

  private

  def reject_protocols(attributed)
    attributed['name'].blank? || attributed['short_name'].blank? || attributed['description'].blank?
  end

end