Class: BiologicalRelationship

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Citations, Shared::DataAttributes, Shared::IsData, Shared::Notes, Shared::Tags
Defined in:
app/models/biological_relationship.rb

Overview

A biological relationship defines a biological relationship type between two biological entities (e.g. specimen and specimen, otu and specimen etc.)

Defined Under Namespace

Classes: Autocomplete

Instance Attribute Summary collapse

Class Method Summary collapse

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::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::Tags

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

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#inverted_nameString

Returns the name as if read in reverse (from perspective of object), for example name: `parasitoid_of` inverted_name: `host_of`. Optional.

Returns:

  • (String)

    the name as if read in reverse (from perspective of object), for example name: `parasitoid_of` inverted_name: `host_of`. Optional.


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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/biological_relationship.rb', line 23

class BiologicalRelationship < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::IsData

  validates_presence_of :name
  has_many :biological_relationship_types, inverse_of: :biological_relationship

  has_many :subject_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipSubjectType')}, class_name: 'BiologicalRelationshipType'
  has_many :object_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipObjectType')}, class_name: 'BiologicalRelationshipType'

  has_many :biological_properties, through: :biological_relationship_types
  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  has_many :biological_associations, inverse_of: :biological_relationship

  accepts_nested_attributes_for :biological_relationship_types, allow_destroy: true

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently(user_id, project_id)
      t = BiologicalAssociation.arel_table
    k = BiologicalRelationship.arel_table 

    # i is a select manager
    i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
      .where(t['created_at'].gt( 1.weeks.ago ))
      .where(t['created_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['created_at'].desc)

    # z is a table alias 
    z = i.as('recent_t')

    BiologicalRelationship.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
    ).pluck(:biological_relationship_id).uniq
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    r = used_recently(user_id, project_id)

    h = {
        quick: [],
        pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
        recent: []
    }

    if r.empty?
      h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
      h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
          BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
    end

    h
  end
end

#is_reflexiveBoolean

Returns whether the relationship is reflexive, i.e. if A is_a B and is_a is_reflexive then B is_a A.

Returns:

  • (Boolean)

    whether the relationship is reflexive, i.e. if A is_a B and is_a is_reflexive then B is_a A


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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/biological_relationship.rb', line 23

class BiologicalRelationship < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::IsData

  validates_presence_of :name
  has_many :biological_relationship_types, inverse_of: :biological_relationship

  has_many :subject_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipSubjectType')}, class_name: 'BiologicalRelationshipType'
  has_many :object_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipObjectType')}, class_name: 'BiologicalRelationshipType'

  has_many :biological_properties, through: :biological_relationship_types
  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  has_many :biological_associations, inverse_of: :biological_relationship

  accepts_nested_attributes_for :biological_relationship_types, allow_destroy: true

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently(user_id, project_id)
      t = BiologicalAssociation.arel_table
    k = BiologicalRelationship.arel_table 

    # i is a select manager
    i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
      .where(t['created_at'].gt( 1.weeks.ago ))
      .where(t['created_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['created_at'].desc)

    # z is a table alias 
    z = i.as('recent_t')

    BiologicalRelationship.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
    ).pluck(:biological_relationship_id).uniq
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    r = used_recently(user_id, project_id)

    h = {
        quick: [],
        pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
        recent: []
    }

    if r.empty?
      h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
      h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
          BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
    end

    h
  end
end

#is_transitiveBoolean

Returns whether the relationship is transitive, i.e. if A is_a B is_a C then if is_a is transitive A is_a C.

Returns:

  • (Boolean)

    whether the relationship is transitive, i.e. if A is_a B is_a C then if is_a is transitive A is_a C


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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/biological_relationship.rb', line 23

class BiologicalRelationship < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::IsData

  validates_presence_of :name
  has_many :biological_relationship_types, inverse_of: :biological_relationship

  has_many :subject_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipSubjectType')}, class_name: 'BiologicalRelationshipType'
  has_many :object_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipObjectType')}, class_name: 'BiologicalRelationshipType'

  has_many :biological_properties, through: :biological_relationship_types
  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  has_many :biological_associations, inverse_of: :biological_relationship

  accepts_nested_attributes_for :biological_relationship_types, allow_destroy: true

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently(user_id, project_id)
      t = BiologicalAssociation.arel_table
    k = BiologicalRelationship.arel_table 

    # i is a select manager
    i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
      .where(t['created_at'].gt( 1.weeks.ago ))
      .where(t['created_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['created_at'].desc)

    # z is a table alias 
    z = i.as('recent_t')

    BiologicalRelationship.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
    ).pluck(:biological_relationship_id).uniq
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    r = used_recently(user_id, project_id)

    h = {
        quick: [],
        pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
        recent: []
    }

    if r.empty?
      h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
      h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
          BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
    end

    h
  end
end

#nameString

Returns the name of the relationship.

Returns:

  • (String)

    the name of the relationship


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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/biological_relationship.rb', line 23

class BiologicalRelationship < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::IsData

  validates_presence_of :name
  has_many :biological_relationship_types, inverse_of: :biological_relationship

  has_many :subject_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipSubjectType')}, class_name: 'BiologicalRelationshipType'
  has_many :object_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipObjectType')}, class_name: 'BiologicalRelationshipType'

  has_many :biological_properties, through: :biological_relationship_types
  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  has_many :biological_associations, inverse_of: :biological_relationship

  accepts_nested_attributes_for :biological_relationship_types, allow_destroy: true

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently(user_id, project_id)
      t = BiologicalAssociation.arel_table
    k = BiologicalRelationship.arel_table 

    # i is a select manager
    i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
      .where(t['created_at'].gt( 1.weeks.ago ))
      .where(t['created_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['created_at'].desc)

    # z is a table alias 
    z = i.as('recent_t')

    BiologicalRelationship.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
    ).pluck(:biological_relationship_id).uniq
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    r = used_recently(user_id, project_id)

    h = {
        quick: [],
        pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
        recent: []
    }

    if r.empty?
      h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
      h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
          BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
    end

    h
  end
end

#project_idInteger

the project ID

Returns:

  • (Integer)

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
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/biological_relationship.rb', line 23

class BiologicalRelationship < ApplicationRecord
  include Housekeeping
  include Shared::Tags
  include Shared::Notes
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::IsData

  validates_presence_of :name
  has_many :biological_relationship_types, inverse_of: :biological_relationship

  has_many :subject_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipSubjectType')}, class_name: 'BiologicalRelationshipType'
  has_many :object_biological_relationship_types, -> () {where(type: 'BiologicalRelationshipType::BiologicalRelationshipObjectType')}, class_name: 'BiologicalRelationshipType'

  has_many :biological_properties, through: :biological_relationship_types
  has_many :subject_biological_properties, through: :subject_biological_relationship_types, source: :biological_property
  has_many :object_biological_properties, through: :object_biological_relationship_types, source: :biological_property

  has_many :biological_associations, inverse_of: :biological_relationship

  accepts_nested_attributes_for :biological_relationship_types, allow_destroy: true

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently(user_id, project_id)
      t = BiologicalAssociation.arel_table
    k = BiologicalRelationship.arel_table 

    # i is a select manager
    i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
      .where(t['created_at'].gt( 1.weeks.ago ))
      .where(t['created_by_id'].eq(user_id))
      .where(t['project_id'].eq(project_id))
      .order(t['created_at'].desc)

    # z is a table alias 
    z = i.as('recent_t')

    BiologicalRelationship.joins(
      Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
    ).pluck(:biological_relationship_id).uniq
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    r = used_recently(user_id, project_id)

    h = {
        quick: [],
        pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
        recent: []
    }

    if r.empty?
      h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
    else
      h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
      h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
          BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
    end

    h
  end
end

Class Method Details

.select_optimized(user_id, project_id) ⇒ Hash

Returns topics optimized for user selection.

Returns:

  • (Hash)

    topics optimized for user selection


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/biological_relationship.rb', line 68

def self.select_optimized(user_id, project_id)
  r = used_recently(user_id, project_id)

  h = {
      quick: [],
      pinboard: BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a,
      recent: []
  }

  if r.empty?
    h[:quick] = BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a
  else
    h[:recent] = BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(10) ).order(:name).to_a
    h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a +
        BiologicalRelationship.where('"biological_relationships"."id" IN (?)', r.first(5) ).order(:name).to_a).uniq
  end

  h
end

.used_recently(user_id, project_id) ⇒ Scope

Returns the max 10 most recently used biological relationships.

Returns:

  • (Scope)

    the max 10 most recently used biological relationships


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/biological_relationship.rb', line 47

def self.used_recently(user_id, project_id)
    t = BiologicalAssociation.arel_table
  k = BiologicalRelationship.arel_table 

  # i is a select manager
  i = t.project(t['biological_relationship_id'], t['created_at']).from(t)
    .where(t['created_at'].gt( 1.weeks.ago ))
    .where(t['created_by_id'].eq(user_id))
    .where(t['project_id'].eq(project_id))
    .order(t['created_at'].desc)

  # z is a table alias 
  z = i.as('recent_t')

  BiologicalRelationship.joins(
    Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['biological_relationship_id'].eq(k['id'])))
  ).pluck(:biological_relationship_id).uniq
end