Class: Project
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Project
- Includes:
- Housekeeping::Timestamps, Housekeeping::Users, Preferences
- Defined in:
- app/models/project.rb,
app/models/project/preferences.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.
Defined Under Namespace
Modules: Preferences
Constant Summary collapse
- MANIFEST =
ORDER MATTERS Used in ‘nuke` order (not available in production UI), but ultimately also for dumping records.
The intent is to use ‘delete_all` for speed. This means that callbacks are not fired (associated destroys).
%w{ Observation CitationTopic Citation Note CharacterState AlternateValue DataAttribute TaggedSectionKeyword Tag Confidence Role Label Attribution DwcOccurrence ProtocolRelationship Protocol SqedDepiction Depiction Conveyance SledImage Documentation Document CollectionObjectObservation DerivedCollectionObject PinboardItem AssertedDistribution BiocurationClassification BiologicalRelationshipType BiologicalAssociationsBiologicalAssociationsGraph BiologicalAssociation BiologicalRelationship BiologicalAssociationsGraph CollectionProfile ContainerItem Container PublicContent Content Gazetteer GazetteerImport Georeference Identifier Lead LeadItem LoanItem Loan OtuPageLayoutSection OtuPageLayout ProjectSource TaxonDetermination TypeMaterial Image Sound CommonName TaxonNameClassification TaxonNameRelationship OriginRelationship Sequence SequenceRelationship Extract GeneAttribute ObservationMatrixColumn ObservationMatrixColumnItem ObservationMatrixRow ObservationMatrixRowItem ObservationMatrix FieldOccurrence CollectionObject RangedLotCategory CollectingEvent OtuRelationship Otu TaxonName Descriptor ProjectMember Download DatasetRecordField DatasetRecord ImportDataset ControlledVocabularyTerm CachedMapItem CachedMapRegister CachedMap }.freeze
- PROJECT_DOWNLOAD_PREFERENCES_PATH =
[ 'metadata', 'dwc', 'gbif', 'institutional_collection' ].freeze
- EML_PREFERENCES_PATH =
[ *PROJECT_DOWNLOAD_PREFERENCES_PATH, 'eml' ].freeze
Constants included from Preferences
Instance Attribute Summary collapse
-
#api_access_token ⇒ String?
The token is not intended to be private.
-
#clear_api_access_token ⇒ Object
Returns the value of attribute clear_api_access_token.
-
#name ⇒ String
The name of the project.
-
#preferences ⇒ Hash
Settings for the project (for all users).
-
#set_new_api_access_token ⇒ Object
Returns the value of attribute set_new_api_access_token.
-
#without_root_taxon_name ⇒ Object
Returns the value of attribute without_root_taxon_name.
Attributes included from Housekeeping::Users
Class Method Summary collapse
Instance Method Summary collapse
-
#annotators ⇒ Hash
For each model in manifest list its annotators.
- #complete_dwc_download_default_user_id ⇒ Object
- #complete_dwc_download_extensions ⇒ Object
- #complete_dwc_download_is_public? ⇒ Boolean
- #complete_dwc_download_max_age ⇒ Object
- #complete_dwc_download_predicates ⇒ Object
- #complete_dwc_eml_preferences ⇒ Object
- #create_root_taxon_name ⇒ Object protected
- #destroy_api_access_token ⇒ Object protected
- #dwc_complete_download_preferences(user) ⇒ Object
- #generate_api_access_token ⇒ String protected
- #is_editable?(user) ⇒ Boolean
-
#nuke ⇒ Boolean
!! This is not production ready.
- #preferences_for(path) ⇒ Object protected
- #project_administrators ⇒ Object
-
#root_taxon_name ⇒ Object
TODO: boot load checks.
- #set_complete_dwc_download_default_user_id(default_user_id) ⇒ Object
-
#set_complete_dwc_download_extensions(extensions) ⇒ Object
in complete downloads: [‘media’, ‘resource_relationships’, …].
- #set_complete_dwc_download_is_public(is_public) ⇒ Object
- #set_complete_dwc_download_max_age(max_age) ⇒ Object
- #set_complete_dwc_download_predicates(predicates) ⇒ Object
- #set_complete_dwc_eml_preferences(dataset, additional_metadata) ⇒ Object
Methods included from Preferences
#fill_preferences, #layout=, #reset_preferences
Methods included from Housekeeping::Users
#set_created_by_id, #set_updated_by_id
Methods inherited from ApplicationRecord
Instance Attribute Details
#api_access_token ⇒ String?
Returns The token is not intended to be private. Generating one is akin to indicating that your project’s data are public, and they will be exposed in the general API to all. The token is primarily for tracking “anonymous” use.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'app/models/project.rb', line 21 class Project < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Project::Preferences attr_accessor :without_root_taxon_name attr_accessor :clear_api_access_token attr_accessor :set_new_api_access_token # ORDER MATTERS # Used in `nuke` order (not available in production UI), but # ultimately also for dumping records. # # The intent is to use `delete_all` for speed. This means # that callbacks are *not* fired (associated destroys). MANIFEST = %w{ Observation CitationTopic Citation Note CharacterState AlternateValue DataAttribute TaggedSectionKeyword Tag Confidence Role Label Attribution DwcOccurrence ProtocolRelationship Protocol SqedDepiction Depiction Conveyance SledImage Documentation Document CollectionObjectObservation DerivedCollectionObject PinboardItem AssertedDistribution BiocurationClassification BiologicalRelationshipType BiologicalAssociationsBiologicalAssociationsGraph BiologicalAssociation BiologicalRelationship BiologicalAssociationsGraph CollectionProfile ContainerItem Container PublicContent Content Gazetteer GazetteerImport Georeference Identifier Lead LeadItem LoanItem Loan OtuPageLayoutSection OtuPageLayout ProjectSource TaxonDetermination TypeMaterial Image Sound CommonName TaxonNameClassification TaxonNameRelationship OriginRelationship Sequence SequenceRelationship Extract GeneAttribute ObservationMatrixColumn ObservationMatrixColumnItem ObservationMatrixRow ObservationMatrixRowItem ObservationMatrix FieldOccurrence CollectionObject RangedLotCategory CollectingEvent OtuRelationship Otu TaxonName Descriptor ProjectMember Download DatasetRecordField DatasetRecord ImportDataset ControlledVocabularyTerm CachedMapItem CachedMapRegister CachedMap }.freeze PROJECT_DOWNLOAD_PREFERENCES_PATH = [ 'metadata', 'dwc', 'gbif', 'institutional_collection' ].freeze EML_PREFERENCES_PATH = [ *PROJECT_DOWNLOAD_PREFERENCES_PATH, 'eml' ].freeze has_many :project_members, dependent: :restrict_with_error has_many :users, through: :project_members has_many :project_sources, inverse_of: :projects, dependent: :restrict_with_error has_many :sources, inverse_of: :projects, through: :project_sources before_save :generate_api_access_token, if: :set_new_api_access_token before_save :destroy_api_access_token, if: -> { self.clear_api_access_token} after_create :create_root_taxon_name, unless: -> {self.without_root_taxon_name == true} validates_presence_of :name validates_uniqueness_of :name accepts_nested_attributes_for :project_members, allow_destroy: true def project_administrators users.joins(:project_members).where(project_members: {is_project_administrator: true}) end def is_editable?(user) user = User.find(user) if !user.kind_of?(User) return true if user.is_administrator? || project_administrators.include?(user) false end # @return [Hash] # for each model in manifest list its annotators def annotators known = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.safe_constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing if !MANIFEST.include?(k) raise "#{k} has not been added to annotators method ." end end h = {} begin MANIFEST.each do |o| klass = o.safe_constantize end true rescue => e raise e end 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 = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing unless MANIFEST.include?(k) raise "#{k} has not been added to #nuke order." end end begin MANIFEST.each do |o| klass = o.constantize # `unscoped` disables default_scope, which would restrict which records in a project are deleted klass.unscoped.where(project_id: id).delete_all end self.destroy true rescue => e raise e end end # TODO: boot load checks 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. # 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 {} # TODO: move to require_dependency? TaxonNameRelationship.tap {} taxon_names.root end def self.find_for_autocomplete(params) where('name LIKE ?', "#{params[:term]}%") end def dwc_complete_download_preferences(user) dataset, = complete_dwc_eml_preferences { user_is_admin: user.is_project_administrator?(self), default_user_id: complete_dwc_download_default_user_id, max_age: complete_dwc_download_max_age, is_public: complete_dwc_download_is_public?, extensions: complete_dwc_download_extensions, predicates: complete_dwc_download_predicates, eml_preferences: { dataset:, additional_metadata: }, auto_filled: { eml: ::Export::Dwca::Eml::EML_PARAMETERS, dataset: ::Export::Dwca::Eml::DATASET_PARAMETERS, additional_metadata: ::Export::Dwca::Eml::ADDITIONAL_METADATA_PARAMETERS } } end def complete_dwc_eml_preferences prefs = preferences.dig(*EML_PREFERENCES_PATH) if prefs [prefs['dataset'], prefs['additional_metadata']] else [ ::Export::Dwca::Eml.dataset_stub, ::Export::Dwca::Eml. ] end end def set_complete_dwc_eml_preferences(dataset, ) prefs = preferences_for(EML_PREFERENCES_PATH) prefs['dataset'] = dataset prefs['additional_metadata'] = save! end def set_complete_dwc_download_max_age(max_age) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) is_decimal = true if Float(max_age) rescue false if !is_decimal return false end prefs['max_age'] = Float(max_age) save! true end def complete_dwc_download_max_age prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['max_age'] else nil end end def complete_dwc_download_is_public? prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['is_public'] == true || prefs['is_public'] == 'true' else false end end def set_complete_dwc_download_is_public(is_public) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['is_public'] = is_public == true || is_public == 'true' save! end def set_complete_dwc_download_default_user_id(default_user_id) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['default_user_id'] = default_user_id save! end def complete_dwc_download_default_user_id prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['default_user_id'] else nil end end def complete_dwc_download_extensions prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['extensions'] else [] end end # @param extensions [Array[String]] list of extensions that should be included # in complete downloads: ['media', 'resource_relationships', ...] def set_complete_dwc_download_extensions(extensions) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['extensions'] = extensions save! end def complete_dwc_download_predicates prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['predicates'] else [] end end def set_complete_dwc_download_predicates(predicates) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['predicates'] = predicates save! end protected def create_root_taxon_name p = Protonym.stub_root(project_id: id, by: creator) p.save! p end # @return [String] def generate_api_access_token self.api_access_token = Utilities::RandomToken.generate end def destroy_api_access_token self.api_access_token = nil end # @param path [Array] like EML_PREFERENCES_PATH. def preferences_for(path) prefs = preferences.dig(*path) if !prefs prefs = preferences path.each do |p| if prefs[p].nil? prefs[p] = {} end prefs = prefs[p] end end prefs end end |
#clear_api_access_token ⇒ Object
Returns the value of attribute clear_api_access_token.
27 28 29 |
# File 'app/models/project.rb', line 27 def clear_api_access_token @clear_api_access_token end |
#name ⇒ String
Returns The name of the project.
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'app/models/project.rb', line 21 class Project < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Project::Preferences attr_accessor :without_root_taxon_name attr_accessor :clear_api_access_token attr_accessor :set_new_api_access_token # ORDER MATTERS # Used in `nuke` order (not available in production UI), but # ultimately also for dumping records. # # The intent is to use `delete_all` for speed. This means # that callbacks are *not* fired (associated destroys). MANIFEST = %w{ Observation CitationTopic Citation Note CharacterState AlternateValue DataAttribute TaggedSectionKeyword Tag Confidence Role Label Attribution DwcOccurrence ProtocolRelationship Protocol SqedDepiction Depiction Conveyance SledImage Documentation Document CollectionObjectObservation DerivedCollectionObject PinboardItem AssertedDistribution BiocurationClassification BiologicalRelationshipType BiologicalAssociationsBiologicalAssociationsGraph BiologicalAssociation BiologicalRelationship BiologicalAssociationsGraph CollectionProfile ContainerItem Container PublicContent Content Gazetteer GazetteerImport Georeference Identifier Lead LeadItem LoanItem Loan OtuPageLayoutSection OtuPageLayout ProjectSource TaxonDetermination TypeMaterial Image Sound CommonName TaxonNameClassification TaxonNameRelationship OriginRelationship Sequence SequenceRelationship Extract GeneAttribute ObservationMatrixColumn ObservationMatrixColumnItem ObservationMatrixRow ObservationMatrixRowItem ObservationMatrix FieldOccurrence CollectionObject RangedLotCategory CollectingEvent OtuRelationship Otu TaxonName Descriptor ProjectMember Download DatasetRecordField DatasetRecord ImportDataset ControlledVocabularyTerm CachedMapItem CachedMapRegister CachedMap }.freeze PROJECT_DOWNLOAD_PREFERENCES_PATH = [ 'metadata', 'dwc', 'gbif', 'institutional_collection' ].freeze EML_PREFERENCES_PATH = [ *PROJECT_DOWNLOAD_PREFERENCES_PATH, 'eml' ].freeze has_many :project_members, dependent: :restrict_with_error has_many :users, through: :project_members has_many :project_sources, inverse_of: :projects, dependent: :restrict_with_error has_many :sources, inverse_of: :projects, through: :project_sources before_save :generate_api_access_token, if: :set_new_api_access_token before_save :destroy_api_access_token, if: -> { self.clear_api_access_token} after_create :create_root_taxon_name, unless: -> {self.without_root_taxon_name == true} validates_presence_of :name validates_uniqueness_of :name accepts_nested_attributes_for :project_members, allow_destroy: true def project_administrators users.joins(:project_members).where(project_members: {is_project_administrator: true}) end def is_editable?(user) user = User.find(user) if !user.kind_of?(User) return true if user.is_administrator? || project_administrators.include?(user) false end # @return [Hash] # for each model in manifest list its annotators def annotators known = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.safe_constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing if !MANIFEST.include?(k) raise "#{k} has not been added to annotators method ." end end h = {} begin MANIFEST.each do |o| klass = o.safe_constantize end true rescue => e raise e end 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 = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing unless MANIFEST.include?(k) raise "#{k} has not been added to #nuke order." end end begin MANIFEST.each do |o| klass = o.constantize # `unscoped` disables default_scope, which would restrict which records in a project are deleted klass.unscoped.where(project_id: id).delete_all end self.destroy true rescue => e raise e end end # TODO: boot load checks 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. # 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 {} # TODO: move to require_dependency? TaxonNameRelationship.tap {} taxon_names.root end def self.find_for_autocomplete(params) where('name LIKE ?', "#{params[:term]}%") end def dwc_complete_download_preferences(user) dataset, = complete_dwc_eml_preferences { user_is_admin: user.is_project_administrator?(self), default_user_id: complete_dwc_download_default_user_id, max_age: complete_dwc_download_max_age, is_public: complete_dwc_download_is_public?, extensions: complete_dwc_download_extensions, predicates: complete_dwc_download_predicates, eml_preferences: { dataset:, additional_metadata: }, auto_filled: { eml: ::Export::Dwca::Eml::EML_PARAMETERS, dataset: ::Export::Dwca::Eml::DATASET_PARAMETERS, additional_metadata: ::Export::Dwca::Eml::ADDITIONAL_METADATA_PARAMETERS } } end def complete_dwc_eml_preferences prefs = preferences.dig(*EML_PREFERENCES_PATH) if prefs [prefs['dataset'], prefs['additional_metadata']] else [ ::Export::Dwca::Eml.dataset_stub, ::Export::Dwca::Eml. ] end end def set_complete_dwc_eml_preferences(dataset, ) prefs = preferences_for(EML_PREFERENCES_PATH) prefs['dataset'] = dataset prefs['additional_metadata'] = save! end def set_complete_dwc_download_max_age(max_age) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) is_decimal = true if Float(max_age) rescue false if !is_decimal return false end prefs['max_age'] = Float(max_age) save! true end def complete_dwc_download_max_age prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['max_age'] else nil end end def complete_dwc_download_is_public? prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['is_public'] == true || prefs['is_public'] == 'true' else false end end def set_complete_dwc_download_is_public(is_public) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['is_public'] = is_public == true || is_public == 'true' save! end def set_complete_dwc_download_default_user_id(default_user_id) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['default_user_id'] = default_user_id save! end def complete_dwc_download_default_user_id prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['default_user_id'] else nil end end def complete_dwc_download_extensions prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['extensions'] else [] end end # @param extensions [Array[String]] list of extensions that should be included # in complete downloads: ['media', 'resource_relationships', ...] def set_complete_dwc_download_extensions(extensions) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['extensions'] = extensions save! end def complete_dwc_download_predicates prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['predicates'] else [] end end def set_complete_dwc_download_predicates(predicates) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['predicates'] = predicates save! end protected def create_root_taxon_name p = Protonym.stub_root(project_id: id, by: creator) p.save! p end # @return [String] def generate_api_access_token self.api_access_token = Utilities::RandomToken.generate end def destroy_api_access_token self.api_access_token = nil end # @param path [Array] like EML_PREFERENCES_PATH. def preferences_for(path) prefs = preferences.dig(*path) if !prefs prefs = preferences path.each do |p| if prefs[p].nil? prefs[p] = {} end prefs = prefs[p] end end prefs end end |
#preferences ⇒ Hash
Returns Settings for the project (for all users).
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'app/models/project.rb', line 21 class Project < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Project::Preferences attr_accessor :without_root_taxon_name attr_accessor :clear_api_access_token attr_accessor :set_new_api_access_token # ORDER MATTERS # Used in `nuke` order (not available in production UI), but # ultimately also for dumping records. # # The intent is to use `delete_all` for speed. This means # that callbacks are *not* fired (associated destroys). MANIFEST = %w{ Observation CitationTopic Citation Note CharacterState AlternateValue DataAttribute TaggedSectionKeyword Tag Confidence Role Label Attribution DwcOccurrence ProtocolRelationship Protocol SqedDepiction Depiction Conveyance SledImage Documentation Document CollectionObjectObservation DerivedCollectionObject PinboardItem AssertedDistribution BiocurationClassification BiologicalRelationshipType BiologicalAssociationsBiologicalAssociationsGraph BiologicalAssociation BiologicalRelationship BiologicalAssociationsGraph CollectionProfile ContainerItem Container PublicContent Content Gazetteer GazetteerImport Georeference Identifier Lead LeadItem LoanItem Loan OtuPageLayoutSection OtuPageLayout ProjectSource TaxonDetermination TypeMaterial Image Sound CommonName TaxonNameClassification TaxonNameRelationship OriginRelationship Sequence SequenceRelationship Extract GeneAttribute ObservationMatrixColumn ObservationMatrixColumnItem ObservationMatrixRow ObservationMatrixRowItem ObservationMatrix FieldOccurrence CollectionObject RangedLotCategory CollectingEvent OtuRelationship Otu TaxonName Descriptor ProjectMember Download DatasetRecordField DatasetRecord ImportDataset ControlledVocabularyTerm CachedMapItem CachedMapRegister CachedMap }.freeze PROJECT_DOWNLOAD_PREFERENCES_PATH = [ 'metadata', 'dwc', 'gbif', 'institutional_collection' ].freeze EML_PREFERENCES_PATH = [ *PROJECT_DOWNLOAD_PREFERENCES_PATH, 'eml' ].freeze has_many :project_members, dependent: :restrict_with_error has_many :users, through: :project_members has_many :project_sources, inverse_of: :projects, dependent: :restrict_with_error has_many :sources, inverse_of: :projects, through: :project_sources before_save :generate_api_access_token, if: :set_new_api_access_token before_save :destroy_api_access_token, if: -> { self.clear_api_access_token} after_create :create_root_taxon_name, unless: -> {self.without_root_taxon_name == true} validates_presence_of :name validates_uniqueness_of :name accepts_nested_attributes_for :project_members, allow_destroy: true def project_administrators users.joins(:project_members).where(project_members: {is_project_administrator: true}) end def is_editable?(user) user = User.find(user) if !user.kind_of?(User) return true if user.is_administrator? || project_administrators.include?(user) false end # @return [Hash] # for each model in manifest list its annotators def annotators known = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.safe_constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing if !MANIFEST.include?(k) raise "#{k} has not been added to annotators method ." end end h = {} begin MANIFEST.each do |o| klass = o.safe_constantize end true rescue => e raise e end 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 = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing unless MANIFEST.include?(k) raise "#{k} has not been added to #nuke order." end end begin MANIFEST.each do |o| klass = o.constantize # `unscoped` disables default_scope, which would restrict which records in a project are deleted klass.unscoped.where(project_id: id).delete_all end self.destroy true rescue => e raise e end end # TODO: boot load checks 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. # 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 {} # TODO: move to require_dependency? TaxonNameRelationship.tap {} taxon_names.root end def self.find_for_autocomplete(params) where('name LIKE ?', "#{params[:term]}%") end def dwc_complete_download_preferences(user) dataset, = complete_dwc_eml_preferences { user_is_admin: user.is_project_administrator?(self), default_user_id: complete_dwc_download_default_user_id, max_age: complete_dwc_download_max_age, is_public: complete_dwc_download_is_public?, extensions: complete_dwc_download_extensions, predicates: complete_dwc_download_predicates, eml_preferences: { dataset:, additional_metadata: }, auto_filled: { eml: ::Export::Dwca::Eml::EML_PARAMETERS, dataset: ::Export::Dwca::Eml::DATASET_PARAMETERS, additional_metadata: ::Export::Dwca::Eml::ADDITIONAL_METADATA_PARAMETERS } } end def complete_dwc_eml_preferences prefs = preferences.dig(*EML_PREFERENCES_PATH) if prefs [prefs['dataset'], prefs['additional_metadata']] else [ ::Export::Dwca::Eml.dataset_stub, ::Export::Dwca::Eml. ] end end def set_complete_dwc_eml_preferences(dataset, ) prefs = preferences_for(EML_PREFERENCES_PATH) prefs['dataset'] = dataset prefs['additional_metadata'] = save! end def set_complete_dwc_download_max_age(max_age) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) is_decimal = true if Float(max_age) rescue false if !is_decimal return false end prefs['max_age'] = Float(max_age) save! true end def complete_dwc_download_max_age prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['max_age'] else nil end end def complete_dwc_download_is_public? prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['is_public'] == true || prefs['is_public'] == 'true' else false end end def set_complete_dwc_download_is_public(is_public) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['is_public'] = is_public == true || is_public == 'true' save! end def set_complete_dwc_download_default_user_id(default_user_id) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['default_user_id'] = default_user_id save! end def complete_dwc_download_default_user_id prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['default_user_id'] else nil end end def complete_dwc_download_extensions prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['extensions'] else [] end end # @param extensions [Array[String]] list of extensions that should be included # in complete downloads: ['media', 'resource_relationships', ...] def set_complete_dwc_download_extensions(extensions) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['extensions'] = extensions save! end def complete_dwc_download_predicates prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['predicates'] else [] end end def set_complete_dwc_download_predicates(predicates) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['predicates'] = predicates save! end protected def create_root_taxon_name p = Protonym.stub_root(project_id: id, by: creator) p.save! p end # @return [String] def generate_api_access_token self.api_access_token = Utilities::RandomToken.generate end def destroy_api_access_token self.api_access_token = nil end # @param path [Array] like EML_PREFERENCES_PATH. def preferences_for(path) prefs = preferences.dig(*path) if !prefs prefs = preferences path.each do |p| if prefs[p].nil? prefs[p] = {} end prefs = prefs[p] end end prefs end end |
#set_new_api_access_token ⇒ Object
Returns the value of attribute set_new_api_access_token.
28 29 30 |
# File 'app/models/project.rb', line 28 def set_new_api_access_token @set_new_api_access_token end |
#without_root_taxon_name ⇒ Object
Returns the value of attribute without_root_taxon_name.
26 27 28 |
# File 'app/models/project.rb', line 26 def without_root_taxon_name @without_root_taxon_name end |
Class Method Details
.find_for_autocomplete(params) ⇒ Object
222 223 224 |
# File 'app/models/project.rb', line 222 def self.find_for_autocomplete(params) where('name LIKE ?', "#{params[:term]}%") end |
Instance Method Details
#annotators ⇒ Hash
Returns for each model in manifest list its annotators.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'app/models/project.rb', line 157 def annotators known = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.safe_constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing if !MANIFEST.include?(k) raise "#{k} has not been added to annotators method ." end end h = {} begin MANIFEST.each do |o| klass = o.safe_constantize end true rescue => e raise e end end |
#complete_dwc_download_default_user_id ⇒ Object
313 314 315 316 317 318 319 320 |
# File 'app/models/project.rb', line 313 def complete_dwc_download_default_user_id prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['default_user_id'] else nil end end |
#complete_dwc_download_extensions ⇒ Object
322 323 324 325 326 327 328 329 |
# File 'app/models/project.rb', line 322 def complete_dwc_download_extensions prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['extensions'] else [] end end |
#complete_dwc_download_is_public? ⇒ Boolean
290 291 292 293 294 295 296 297 |
# File 'app/models/project.rb', line 290 def complete_dwc_download_is_public? prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['is_public'] == true || prefs['is_public'] == 'true' else false end end |
#complete_dwc_download_max_age ⇒ Object
281 282 283 284 285 286 287 288 |
# File 'app/models/project.rb', line 281 def complete_dwc_download_max_age prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['max_age'] else nil end end |
#complete_dwc_download_predicates ⇒ Object
340 341 342 343 344 345 346 347 |
# File 'app/models/project.rb', line 340 def complete_dwc_download_predicates prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) if prefs prefs['predicates'] else [] end end |
#complete_dwc_eml_preferences ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 |
# File 'app/models/project.rb', line 247 def complete_dwc_eml_preferences prefs = preferences.dig(*EML_PREFERENCES_PATH) if prefs [prefs['dataset'], prefs['additional_metadata']] else [ ::Export::Dwca::Eml.dataset_stub, ::Export::Dwca::Eml. ] end end |
#create_root_taxon_name ⇒ Object (protected)
358 359 360 361 362 |
# File 'app/models/project.rb', line 358 def create_root_taxon_name p = Protonym.stub_root(project_id: id, by: creator) p.save! p end |
#destroy_api_access_token ⇒ Object (protected)
369 370 371 |
# File 'app/models/project.rb', line 369 def destroy_api_access_token self.api_access_token = nil end |
#dwc_complete_download_preferences(user) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'app/models/project.rb', line 226 def dwc_complete_download_preferences(user) dataset, = complete_dwc_eml_preferences { user_is_admin: user.is_project_administrator?(self), default_user_id: complete_dwc_download_default_user_id, max_age: complete_dwc_download_max_age, is_public: complete_dwc_download_is_public?, extensions: complete_dwc_download_extensions, predicates: complete_dwc_download_predicates, eml_preferences: { dataset:, additional_metadata: }, auto_filled: { eml: ::Export::Dwca::Eml::EML_PARAMETERS, dataset: ::Export::Dwca::Eml::DATASET_PARAMETERS, additional_metadata: ::Export::Dwca::Eml::ADDITIONAL_METADATA_PARAMETERS } } end |
#generate_api_access_token ⇒ String (protected)
365 366 367 |
# File 'app/models/project.rb', line 365 def generate_api_access_token self.api_access_token = Utilities::RandomToken.generate end |
#is_editable?(user) ⇒ Boolean
149 150 151 152 153 |
# File 'app/models/project.rb', line 149 def is_editable?(user) user = User.find(user) if !user.kind_of?(User) return true if user.is_administrator? || project_administrators.include?(user) false end |
#nuke ⇒ Boolean
!! This is not production ready.
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'app/models/project.rb', line 184 def nuke known = ApplicationRecord.subclasses.select {|a| a.column_names.include?('project_id')}.map(&:name) known.each do |k| next if k.constantize.table_name == 'test_classes' # TODO: a kludge to ignore stubbed classes in testing unless MANIFEST.include?(k) raise "#{k} has not been added to #nuke order." end end begin MANIFEST.each do |o| klass = o.constantize # `unscoped` disables default_scope, which would restrict which records in a project are deleted klass.unscoped.where(project_id: id).delete_all end self.destroy true rescue => e raise e end end |
#preferences_for(path) ⇒ Object (protected)
374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'app/models/project.rb', line 374 def preferences_for(path) prefs = preferences.dig(*path) if !prefs prefs = preferences path.each do |p| if prefs[p].nil? prefs[p] = {} end prefs = prefs[p] end end prefs end |
#project_administrators ⇒ Object
145 146 147 |
# File 'app/models/project.rb', line 145 def project_administrators users.joins(:project_members).where(project_members: {is_project_administrator: true}) end |
#root_taxon_name ⇒ Object
TODO: boot load checks
210 211 212 213 214 215 216 217 218 219 220 |
# File 'app/models/project.rb', line 210 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. # 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 {} # TODO: move to require_dependency? TaxonNameRelationship.tap {} taxon_names.root end |
#set_complete_dwc_download_default_user_id(default_user_id) ⇒ Object
306 307 308 309 310 311 |
# File 'app/models/project.rb', line 306 def set_complete_dwc_download_default_user_id(default_user_id) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['default_user_id'] = default_user_id save! end |
#set_complete_dwc_download_extensions(extensions) ⇒ Object
in complete downloads: [‘media’, ‘resource_relationships’, …]
333 334 335 336 337 338 |
# File 'app/models/project.rb', line 333 def set_complete_dwc_download_extensions(extensions) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['extensions'] = extensions save! end |
#set_complete_dwc_download_is_public(is_public) ⇒ Object
299 300 301 302 303 304 |
# File 'app/models/project.rb', line 299 def set_complete_dwc_download_is_public(is_public) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['is_public'] = is_public == true || is_public == 'true' save! end |
#set_complete_dwc_download_max_age(max_age) ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'app/models/project.rb', line 268 def set_complete_dwc_download_max_age(max_age) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) is_decimal = true if Float(max_age) rescue false if !is_decimal return false end prefs['max_age'] = Float(max_age) save! true end |
#set_complete_dwc_download_predicates(predicates) ⇒ Object
349 350 351 352 353 354 |
# File 'app/models/project.rb', line 349 def set_complete_dwc_download_predicates(predicates) prefs = preferences_for(PROJECT_DOWNLOAD_PREFERENCES_PATH) prefs['predicates'] = predicates save! end |
#set_complete_dwc_eml_preferences(dataset, additional_metadata) ⇒ Object
259 260 261 262 263 264 265 266 |
# File 'app/models/project.rb', line 259 def set_complete_dwc_eml_preferences(dataset, ) prefs = preferences_for(EML_PREFERENCES_PATH) prefs['dataset'] = dataset prefs['additional_metadata'] = save! end |