Class: Documentation

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData, Shared::Notes, Shared::PolymorphicAnnotator, Shared::Tags, SoftValidation
Defined in:
app/models/documentation.rb

Overview

A Documentation instance describes the relationship between a digital document (e.g pdf, txt file) and some data instance. Data types that can be document use Shared::Documentation.

Documentation is to Documents as Depictions are to Images. !!‘Documentation` is both singular and plural!!

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#document_idString

Returns the id of the Document.

Returns:

  • (String)

    the id of the Document



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/documentation.rb', line 23

class Documentation < ApplicationRecord

  acts_as_list scope: [:project_id, :documentation_object_id, :documentation_object_type]

  include Housekeeping

  include Shared::Notes
  include Shared::Tags
  include Shared::IsData
  include SoftValidation
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:documentation_object)

  # These are all handled on the database side as not-null constraints
  # They can't be validated because we use accepts_nested_attributes
  # validates_presence_of :documentation_object_type, :documentation_object_id, :document_id
  # TODO: above is probably bs
  #
  # We catch invalid statements with this around:
  around_save :catch_statement_invalid

  after_save :check_for_projects_source, if: Proc.new {|n| n.documentation_object_type == 'Source'}

  belongs_to :documentation_object, polymorphic: true
  belongs_to :document, inverse_of: :documentation

  validates_presence_of :document

  accepts_nested_attributes_for :document
  accepts_nested_attributes_for :documentation_object

  protected

  def check_for_projects_source
    ProjectSource.find_or_create_by(
      project_id:,
      source_id: documentation_object_id)
  end

  def catch_statement_invalid
    begin
      yield # calls :after_save callback
    rescue ActiveRecord::StatementInvalid => e
      if e.cause.class.name == 'PG::NotNullViolation'
        errors.add(:base, 'a required field was not provided')
      else
        raise
      end
    end
  end

end

#documentation_object_idString

Returns the id of the documented object.

Returns:

  • (String)

    the id of the documented object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/documentation.rb', line 23

class Documentation < ApplicationRecord

  acts_as_list scope: [:project_id, :documentation_object_id, :documentation_object_type]

  include Housekeeping

  include Shared::Notes
  include Shared::Tags
  include Shared::IsData
  include SoftValidation
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:documentation_object)

  # These are all handled on the database side as not-null constraints
  # They can't be validated because we use accepts_nested_attributes
  # validates_presence_of :documentation_object_type, :documentation_object_id, :document_id
  # TODO: above is probably bs
  #
  # We catch invalid statements with this around:
  around_save :catch_statement_invalid

  after_save :check_for_projects_source, if: Proc.new {|n| n.documentation_object_type == 'Source'}

  belongs_to :documentation_object, polymorphic: true
  belongs_to :document, inverse_of: :documentation

  validates_presence_of :document

  accepts_nested_attributes_for :document
  accepts_nested_attributes_for :documentation_object

  protected

  def check_for_projects_source
    ProjectSource.find_or_create_by(
      project_id:,
      source_id: documentation_object_id)
  end

  def catch_statement_invalid
    begin
      yield # calls :after_save callback
    rescue ActiveRecord::StatementInvalid => e
      if e.cause.class.name == 'PG::NotNullViolation'
        errors.add(:base, 'a required field was not provided')
      else
        raise
      end
    end
  end

end

#documentation_object_typeString

Returns the type of the documented object.

Returns:

  • (String)

    the type of the documented object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/documentation.rb', line 23

class Documentation < ApplicationRecord

  acts_as_list scope: [:project_id, :documentation_object_id, :documentation_object_type]

  include Housekeeping

  include Shared::Notes
  include Shared::Tags
  include Shared::IsData
  include SoftValidation
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:documentation_object)

  # These are all handled on the database side as not-null constraints
  # They can't be validated because we use accepts_nested_attributes
  # validates_presence_of :documentation_object_type, :documentation_object_id, :document_id
  # TODO: above is probably bs
  #
  # We catch invalid statements with this around:
  around_save :catch_statement_invalid

  after_save :check_for_projects_source, if: Proc.new {|n| n.documentation_object_type == 'Source'}

  belongs_to :documentation_object, polymorphic: true
  belongs_to :document, inverse_of: :documentation

  validates_presence_of :document

  accepts_nested_attributes_for :document
  accepts_nested_attributes_for :documentation_object

  protected

  def check_for_projects_source
    ProjectSource.find_or_create_by(
      project_id:,
      source_id: documentation_object_id)
  end

  def catch_statement_invalid
    begin
      yield # calls :after_save callback
    rescue ActiveRecord::StatementInvalid => e
      if e.cause.class.name == 'PG::NotNullViolation'
        errors.add(:base, 'a required field was not provided')
      else
        raise
      end
    end
  end

end

#positionInteger

Returns for acts as list, scopes to document.

Returns:

  • (Integer)

    for acts as list, scopes to document



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/documentation.rb', line 23

class Documentation < ApplicationRecord

  acts_as_list scope: [:project_id, :documentation_object_id, :documentation_object_type]

  include Housekeeping

  include Shared::Notes
  include Shared::Tags
  include Shared::IsData
  include SoftValidation
  include Shared::PolymorphicAnnotator
  polymorphic_annotates(:documentation_object)

  # These are all handled on the database side as not-null constraints
  # They can't be validated because we use accepts_nested_attributes
  # validates_presence_of :documentation_object_type, :documentation_object_id, :document_id
  # TODO: above is probably bs
  #
  # We catch invalid statements with this around:
  around_save :catch_statement_invalid

  after_save :check_for_projects_source, if: Proc.new {|n| n.documentation_object_type == 'Source'}

  belongs_to :documentation_object, polymorphic: true
  belongs_to :document, inverse_of: :documentation

  validates_presence_of :document

  accepts_nested_attributes_for :document
  accepts_nested_attributes_for :documentation_object

  protected

  def check_for_projects_source
    ProjectSource.find_or_create_by(
      project_id:,
      source_id: documentation_object_id)
  end

  def catch_statement_invalid
    begin
      yield # calls :after_save callback
    rescue ActiveRecord::StatementInvalid => e
      if e.cause.class.name == 'PG::NotNullViolation'
        errors.add(:base, 'a required field was not provided')
      else
        raise
      end
    end
  end

end

Instance Method Details

#catch_statement_invalidObject (protected)



62
63
64
65
66
67
68
69
70
71
72
# File 'app/models/documentation.rb', line 62

def catch_statement_invalid
  begin
    yield # calls :after_save callback
  rescue ActiveRecord::StatementInvalid => e
    if e.cause.class.name == 'PG::NotNullViolation'
      errors.add(:base, 'a required field was not provided')
    else
      raise
    end
  end
end

#check_for_projects_sourceObject (protected)



56
57
58
59
60
# File 'app/models/documentation.rb', line 56

def check_for_projects_source
  ProjectSource.find_or_create_by(
    project_id:,
    source_id: documentation_object_id)
end