module Exploitable
  extend ActiveSupport::Concern

  FIELDS = [:applicable_medium, :territory, :term, :restriction]

  included do
    FIELDS.each do |field|
      belongs_to field, optional: true

      define_method "#{field}_value" do 
        if respond_to?(:contract_template) && contract_template.present?
          # If contract template is present, use the value from there
          contract_template.public_send("#{field}_value")
        else
          # Otherwise use the value of the label or the text
          field_assoc = public_send(field)
          if field_assoc
            field_assoc.other? ? public_send("#{field}_text") : field_assoc.label
          end
        end
      end
    end
  end
end
