Class: Project

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping::AssociationHelpers, Housekeeping::Timestamps, Housekeeping::Users
Defined in:
app/models/project.rb

Overview

A project is a team's wrapper for a group of data. Most data is project specific, with a few exceptions. A project has many users, and one or more project administrators. With the exception of “Workers” who can only see and therefore use certain elements of the workbench all members of a project share the same privileges. A projects members are therefor well trained and trusted contributors to the project.

Constant Summary

DEFAULT_WORKBENCH_STARTING_PATH =
'/hub'
DEFAULT_WORKBENCH_SETTINGS =
{
    'workbench_starting_path' => DEFAULT_WORKBENCH_STARTING_PATH
}

Instance Attribute Summary (collapse)

Attributes included from Housekeeping::Users

#by

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Housekeeping::AssociationHelpers

#has_many_relationship_classes, #has_many_relationships

Methods included from Housekeeping::Timestamps

#data_breakdown_for_chartkick_recent

Methods included from Housekeeping::Users

#set_created_by_id, #set_updated_by_id

Instance Attribute Details

- (String) name

Returns The name of the project

Returns:

  • (String)

    The name of the project



13
14
15
16
17
18
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/project.rb', line 13

class Project < ActiveRecord::Base
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Housekeeping::AssociationHelpers

  store_accessor :workbench_settings, :worker_tasks, :workbench_starting_path

  attr_accessor :without_root_taxon_name

  DEFAULT_WORKBENCH_STARTING_PATH = '/hub'
  DEFAULT_WORKBENCH_SETTINGS = {
      'workbench_starting_path' => DEFAULT_WORKBENCH_STARTING_PATH
  }

  has_many :project_members, dependent: :restrict_with_error
  has_many :users, through: :project_members
  has_many :sources, through: :project_sources
  has_many :project_sources, dependent: :restrict_with_error

  after_initialize :set_default_workbench_settings
  after_create :create_root_taxon_name, unless: 'self.without_root_taxon_name == true'

  validates_presence_of :name
  validates_uniqueness_of :name

  def clear_workbench_settings
    self.update('workbench_settings' => DEFAULT_WORKBENCH_SETTINGS)
  end

  # !! This is not production ready.
  # @return [Boolean]
  #   based on whether the project has successfully been deleted.  Can also raise on detected problems with configuration.
  def nuke
    known = ActiveRecord::Base.subclasses.select { |a| a.column_names.include?('project_id') }.map(&:name)

    order = %w{
     DwcOccurrence
     ProtocolRelationship
     Protocol
     AlternateValue
     DataAttribute 
     CitationTopic
     Citation
     SqedDepiction 
     Depiction
     Documentation
     Document 
     CollectionObjectObservation  
     DerivedCollectionObject        
     Note
     PinboardItem
     TaggedSectionKeyword      
     Tag
     Confidence 
     Role
     AssertedDistribution
     BiocurationClassification
     BiologicalRelationshipType   
     BiologicalAssociationsBiologicalAssociationsGraph
     BiologicalAssociation
     BiologicalRelationship
     BiologicalAssociationsGraph   
     CollectionProfile   
     ContainerLabel
     ContainerItem  
     Container
     PublicContent
     Content
     Georeference
     Identifier
     LoanItem  
     Loan
     OtuPageLayoutSection   
     OtuPageLayout
     ProjectSource
     TaxonDetermination
     TypeMaterial       
     CollectionObject   
     CollectingEvent
     RangedLotCategory
     Image  
     CommonName
     Otu 
     TaxonNameClassification
     TaxonNameRelationship
     TaxonName 
     ControlledVocabularyTerm
     OriginRelationship
     ProjectMember  
    }

    known.each do |k|
      next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing
      if !order.include?(k)
        raise "#{k} has not been added to #nuke order."
      end
    end

    begin
      order.each do |o|
        klass = o.constantize
        klass.where(project_id: id).delete_all
      end

      self.destroy

      true
    rescue => e
      raise e
    end
  end

  def root_taxon_name
    # Calling TaxonName is a hack to load the required has_many into Project, 
    # "has_many :taxon_names" is invoked through TaxonName within Housekeeping::Project 
    # Within TaxonName closure_tree (appears to?) require a database connection.  Note 
    # closure_tree is apparently not robustly tested for Ruby 2.3.
    # Since we shouldn't (can't?) initiate a connection prior to a require_dependency
    # we simply load TaxonName for the first time here.
    TaxonName.tap{}  
    self.taxon_names.root
  end

  def self.find_for_autocomplete(params)
    where('name LIKE ?', "#{params[:term]}%")
  end

  protected

  def set_default_workbench_settings
    self.workbench_settings = DEFAULT_WORKBENCH_SETTINGS.merge(self.workbench_settings ||= {})
  end

  def create_root_taxon_name
    Protonym.create!(name: 'Root', rank_class: NomenclaturalRank, parent_id: nil, project: self, creator: self.creator, updater: self.updater)
  end

end

- (Object) without_root_taxon_name

Returns the value of attribute without_root_taxon_name



20
21
22
# File 'app/models/project.rb', line 20

def without_root_taxon_name
  @without_root_taxon_name
end

- (Hash) workbench_settings

Returns Settings for the project (for all users)

Returns:

  • (Hash)

    Settings for the project (for all users)



13
14
15
16
17
18
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/models/project.rb', line 13

class Project < ActiveRecord::Base
  include Housekeeping::Users
  include Housekeeping::Timestamps
  include Housekeeping::AssociationHelpers

  store_accessor :workbench_settings, :worker_tasks, :workbench_starting_path

  attr_accessor :without_root_taxon_name

  DEFAULT_WORKBENCH_STARTING_PATH = '/hub'
  DEFAULT_WORKBENCH_SETTINGS = {
      'workbench_starting_path' => DEFAULT_WORKBENCH_STARTING_PATH
  }

  has_many :project_members, dependent: :restrict_with_error
  has_many :users, through: :project_members
  has_many :sources, through: :project_sources
  has_many :project_sources, dependent: :restrict_with_error

  after_initialize :set_default_workbench_settings
  after_create :create_root_taxon_name, unless: 'self.without_root_taxon_name == true'

  validates_presence_of :name
  validates_uniqueness_of :name

  def clear_workbench_settings
    self.update('workbench_settings' => DEFAULT_WORKBENCH_SETTINGS)
  end

  # !! This is not production ready.
  # @return [Boolean]
  #   based on whether the project has successfully been deleted.  Can also raise on detected problems with configuration.
  def nuke
    known = ActiveRecord::Base.subclasses.select { |a| a.column_names.include?('project_id') }.map(&:name)

    order = %w{
     DwcOccurrence
     ProtocolRelationship
     Protocol
     AlternateValue
     DataAttribute 
     CitationTopic
     Citation
     SqedDepiction 
     Depiction
     Documentation
     Document 
     CollectionObjectObservation  
     DerivedCollectionObject        
     Note
     PinboardItem
     TaggedSectionKeyword      
     Tag
     Confidence 
     Role
     AssertedDistribution
     BiocurationClassification
     BiologicalRelationshipType   
     BiologicalAssociationsBiologicalAssociationsGraph
     BiologicalAssociation
     BiologicalRelationship
     BiologicalAssociationsGraph   
     CollectionProfile   
     ContainerLabel
     ContainerItem  
     Container
     PublicContent
     Content
     Georeference
     Identifier
     LoanItem  
     Loan
     OtuPageLayoutSection   
     OtuPageLayout
     ProjectSource
     TaxonDetermination
     TypeMaterial       
     CollectionObject   
     CollectingEvent
     RangedLotCategory
     Image  
     CommonName
     Otu 
     TaxonNameClassification
     TaxonNameRelationship
     TaxonName 
     ControlledVocabularyTerm
     OriginRelationship
     ProjectMember  
    }

    known.each do |k|
      next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing
      if !order.include?(k)
        raise "#{k} has not been added to #nuke order."
      end
    end

    begin
      order.each do |o|
        klass = o.constantize
        klass.where(project_id: id).delete_all
      end

      self.destroy

      true
    rescue => e
      raise e
    end
  end

  def root_taxon_name
    # Calling TaxonName is a hack to load the required has_many into Project, 
    # "has_many :taxon_names" is invoked through TaxonName within Housekeeping::Project 
    # Within TaxonName closure_tree (appears to?) require a database connection.  Note 
    # closure_tree is apparently not robustly tested for Ruby 2.3.
    # Since we shouldn't (can't?) initiate a connection prior to a require_dependency
    # we simply load TaxonName for the first time here.
    TaxonName.tap{}  
    self.taxon_names.root
  end

  def self.find_for_autocomplete(params)
    where('name LIKE ?', "#{params[:term]}%")
  end

  protected

  def set_default_workbench_settings
    self.workbench_settings = DEFAULT_WORKBENCH_SETTINGS.merge(self.workbench_settings ||= {})
  end

  def create_root_taxon_name
    Protonym.create!(name: 'Root', rank_class: NomenclaturalRank, parent_id: nil, project: self, creator: self.creator, updater: self.updater)
  end

end

Class Method Details

+ (Object) find_for_autocomplete(params)



136
137
138
# File 'app/models/project.rb', line 136

def self.find_for_autocomplete(params)
  where('name LIKE ?', "#{params[:term]}%")
end

Instance Method Details

- (Object) clear_workbench_settings



38
39
40
# File 'app/models/project.rb', line 38

def clear_workbench_settings
  self.update('workbench_settings' => DEFAULT_WORKBENCH_SETTINGS)
end

- (Object) create_root_taxon_name (protected)



146
147
148
# File 'app/models/project.rb', line 146

def create_root_taxon_name
  Protonym.create!(name: 'Root', rank_class: NomenclaturalRank, parent_id: nil, project: self, creator: self.creator, updater: self.updater)
end

- (Boolean) nuke

!! This is not production ready.

Returns:

  • (Boolean)

    based on whether the project has successfully been deleted. Can also raise on detected problems with configuration.



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/project.rb', line 45

def nuke
  known = ActiveRecord::Base.subclasses.select { |a| a.column_names.include?('project_id') }.map(&:name)

  order = %w{
   DwcOccurrence
   ProtocolRelationship
   Protocol
   AlternateValue
   DataAttribute 
   CitationTopic
   Citation
   SqedDepiction 
   Depiction
   Documentation
   Document 
   CollectionObjectObservation  
   DerivedCollectionObject        
   Note
   PinboardItem
   TaggedSectionKeyword      
   Tag
   Confidence 
   Role
   AssertedDistribution
   BiocurationClassification
   BiologicalRelationshipType   
   BiologicalAssociationsBiologicalAssociationsGraph
   BiologicalAssociation
   BiologicalRelationship
   BiologicalAssociationsGraph   
   CollectionProfile   
   ContainerLabel
   ContainerItem  
   Container
   PublicContent
   Content
   Georeference
   Identifier
   LoanItem  
   Loan
   OtuPageLayoutSection   
   OtuPageLayout
   ProjectSource
   TaxonDetermination
   TypeMaterial       
   CollectionObject   
   CollectingEvent
   RangedLotCategory
   Image  
   CommonName
   Otu 
   TaxonNameClassification
   TaxonNameRelationship
   TaxonName 
   ControlledVocabularyTerm
   OriginRelationship
   ProjectMember  
  }

  known.each do |k|
    next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing
    if !order.include?(k)
      raise "#{k} has not been added to #nuke order."
    end
  end

  begin
    order.each do |o|
      klass = o.constantize
      klass.where(project_id: id).delete_all
    end

    self.destroy

    true
  rescue => e
    raise e
  end
end

- (Object) root_taxon_name



125
126
127
128
129
130
131
132
133
134
# File 'app/models/project.rb', line 125

def root_taxon_name
  # Calling TaxonName is a hack to load the required has_many into Project, 
  # "has_many :taxon_names" is invoked through TaxonName within Housekeeping::Project 
  # Within TaxonName closure_tree (appears to?) require a database connection.  Note 
  # closure_tree is apparently not robustly tested for Ruby 2.3.
  # Since we shouldn't (can't?) initiate a connection prior to a require_dependency
  # we simply load TaxonName for the first time here.
  TaxonName.tap{}  
  self.taxon_names.root
end

- (Object) set_default_workbench_settings (protected)



142
143
144
# File 'app/models/project.rb', line 142

def set_default_workbench_settings
  self.workbench_settings = DEFAULT_WORKBENCH_SETTINGS.merge(self.workbench_settings ||= {})
end