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.)

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Attribute Details

- (Boolean) is_reflexive

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



19
20
21
22
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
# File 'app/models/biological_relationship.rb', line 19

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 :biological_associations, inverse_of: :biological_relationship
  has_many :biological_properties, through: :biological_relationship_types
 
  # TODO: move to /lib/queries
  def self.find_for_autocomplete(params)
    t = params[:term]
    t2 = t + '%'
    t3 = '%' + t2
    BiologicalRelationship.where('(name ILIKE ?) OR (name ILIKE ?) OR (name ILIKE ?)', t,t2,t3).where(project_id: params[:project_id])
  end

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently
    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 ))
      .order(t['created_at'])

    # 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'])))
    ).distinct.limit(10)
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    h = {
      recent: BiologicalRelationship.where(project_id: project_id).used_recently.limit(10).distinct.to_a,
      pinboard:  BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a
    }

    h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a  + h[:recent][0..3]).uniq
    h
  end
end

- (Boolean) is_transitive

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



19
20
21
22
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
# File 'app/models/biological_relationship.rb', line 19

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 :biological_associations, inverse_of: :biological_relationship
  has_many :biological_properties, through: :biological_relationship_types
 
  # TODO: move to /lib/queries
  def self.find_for_autocomplete(params)
    t = params[:term]
    t2 = t + '%'
    t3 = '%' + t2
    BiologicalRelationship.where('(name ILIKE ?) OR (name ILIKE ?) OR (name ILIKE ?)', t,t2,t3).where(project_id: params[:project_id])
  end

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently
    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 ))
      .order(t['created_at'])

    # 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'])))
    ).distinct.limit(10)
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    h = {
      recent: BiologicalRelationship.where(project_id: project_id).used_recently.limit(10).distinct.to_a,
      pinboard:  BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a
    }

    h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a  + h[:recent][0..3]).uniq
    h
  end
end

- (String) name

Returns the name of the relationship

Returns:

  • (String)

    the name of the relationship



19
20
21
22
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
# File 'app/models/biological_relationship.rb', line 19

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 :biological_associations, inverse_of: :biological_relationship
  has_many :biological_properties, through: :biological_relationship_types
 
  # TODO: move to /lib/queries
  def self.find_for_autocomplete(params)
    t = params[:term]
    t2 = t + '%'
    t3 = '%' + t2
    BiologicalRelationship.where('(name ILIKE ?) OR (name ILIKE ?) OR (name ILIKE ?)', t,t2,t3).where(project_id: params[:project_id])
  end

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently
    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 ))
      .order(t['created_at'])

    # 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'])))
    ).distinct.limit(10)
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    h = {
      recent: BiologicalRelationship.where(project_id: project_id).used_recently.limit(10).distinct.to_a,
      pinboard:  BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a
    }

    h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a  + h[:recent][0..3]).uniq
    h
  end
end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


19
20
21
22
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
# File 'app/models/biological_relationship.rb', line 19

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 :biological_associations, inverse_of: :biological_relationship
  has_many :biological_properties, through: :biological_relationship_types
 
  # TODO: move to /lib/queries
  def self.find_for_autocomplete(params)
    t = params[:term]
    t2 = t + '%'
    t3 = '%' + t2
    BiologicalRelationship.where('(name ILIKE ?) OR (name ILIKE ?) OR (name ILIKE ?)', t,t2,t3).where(project_id: params[:project_id])
  end

  # @return [Scope]
  #    the max 10 most recently used biological relationships 
  def self.used_recently
    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 ))
      .order(t['created_at'])

    # 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'])))
    ).distinct.limit(10)
  end

  # @params target [String] one of `Citation` or `Content`
  # @return [Hash] topics optimized for user selection
  def self.select_optimized(user_id, project_id)
    h = {
      recent: BiologicalRelationship.where(project_id: project_id).used_recently.limit(10).distinct.to_a,
      pinboard:  BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a
    }

    h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a  + h[:recent][0..3]).uniq
    h
  end
end

Class Method Details

+ (Object) find_for_autocomplete(params)

TODO: move to /lib/queries



33
34
35
36
37
38
# File 'app/models/biological_relationship.rb', line 33

def self.find_for_autocomplete(params)
  t = params[:term]
  t2 = t + '%'
  t3 = '%' + t2
  BiologicalRelationship.where('(name ILIKE ?) OR (name ILIKE ?) OR (name ILIKE ?)', t,t2,t3).where(project_id: params[:project_id])
end

+ (Hash) select_optimized(user_id, project_id)

Returns topics optimized for user selection

Returns:

  • (Hash)

    topics optimized for user selection



61
62
63
64
65
66
67
68
69
# File 'app/models/biological_relationship.rb', line 61

def self.select_optimized(user_id, project_id)
  h = {
    recent: BiologicalRelationship.where(project_id: project_id).used_recently.limit(10).distinct.to_a,
    pinboard:  BiologicalRelationship.pinned_by(user_id).where(project_id: project_id).to_a
  }

  h[:quick] = (BiologicalRelationship.pinned_by(user_id).pinboard_inserted.where(project_id: project_id).to_a  + h[:recent][0..3]).uniq
  h
end

+ (Scope) used_recently

Returns the max 10 most recently used biological relationships

Returns:

  • (Scope)

    the max 10 most recently used biological relationships



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/biological_relationship.rb', line 42

def self.used_recently
  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 ))
    .order(t['created_at'])

  # 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'])))
  ).distinct.limit(10)
end