Module: Shared::IsData
- Extended by:
- ActiveSupport::Concern
- Included in:
- AlternateValue, AssertedDistribution, Attribution, BiocurationClassification, BiologicalAssociation, BiologicalAssociationsBiologicalAssociationsGraph, BiologicalAssociationsGraph, BiologicalRelationship, BiologicalRelationshipType, CharacterState, Citation, CitationTopic, CollectingEvent, CollectionObject, CollectionObjectObservation, CollectionProfile, CommonName, Confidence, Container, ContainerItem, Content, ControlledVocabularyTerm, DataAttribute, Depiction, DerivedCollectionObject, Descriptor, Document, Documentation, Extract, GeneAttribute, GeographicArea, GeographicAreaType, GeographicAreasGeographicItem, GeographicItem, Georeference, Identifier, Image, Label, Language, Loan, LoanItem, Namespace, Note, Observation, ObservationMatrix, ObservationMatrixColumn, ObservationMatrixColumnItem, ObservationMatrixRow, ObservationMatrixRowItem, Organization, OriginRelationship, Otu, OtuPageLayout, OtuPageLayoutSection, Person, PreparationType, ProjectMember, ProjectSource, Protocol, ProtocolRelationship, RangedLotCategory, Repository, Role, Sequence, SequenceRelationship, Serial, SerialChronology, SledImage, Source, Tag, TaggedSectionKeyword, TaxonDetermination, TaxonName, TaxonNameClassification, TaxonNameRelationship, TypeMaterial
- Defined in:
- app/models/concerns/shared/is_data.rb
Overview
Shared code for a classes that are “data” sensu TaxonWorks (things like Projects, users, and preferences are not data).
!! This module must in included last !!
Defined Under Namespace
Modules: Annotation, ClassMethods, Levenshtein, Navigation, Pinnable, Scopes, Stripper
Instance Method Summary
collapse
Instance Method Details
#errors_excepting(*keys) ⇒ Hash
169
170
171
172
173
174
175
|
# File 'app/models/concerns/shared/is_data.rb', line 169
def errors_excepting(*keys)
valid?
keys.each do |k|
errors.delete(k)
end
errors
end
|
#full_error_messages_excepting(*keys) ⇒ Array
205
206
207
|
# File 'app/models/concerns/shared/is_data.rb', line 205
def full_error_messages_excepting(*keys)
errors_excepting(*keys).full_messages
end
|
#identical ⇒ Scope
192
193
194
195
196
197
198
199
200
201
|
# File 'app/models/concerns/shared/is_data.rb', line 192
def identical
klass = self.class
attr = Stripper.strip_identical_attributes(klass, attributes)
if id
scope = klass.where(attr).not_self(self)
else
scope = klass.where(attr)
end
scope
end
|
155
156
157
|
# File 'app/models/concerns/shared/is_data.rb', line 155
def
self.class < Shared::SharedAcrossProjects ? true : false
end
|
#is_destroyable?(user) ⇒ Boolean
Returns whether it is permissible to try to destroy they record based on it's relationships to projects the user is in. I.e. false if it is related to data in a project in which they user is not a member. !! Does not look at :dependendant assertions
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'app/models/concerns/shared/is_data.rb', line 92
def is_destroyable?(user)
user = User.find(user) if !user.kind_of?(User)
return true if user.is_administrator?
p = user.projects.pluck(:id)
self.class.reflect_on_all_associations(:has_many).each do |r|
if r.klass.column_names.include?('project_id')
return false if send(r.name).where.not(project_id: p).count(:all) > 0
end
end
self.class.reflect_on_all_associations(:has_one).each do |r|
if if o = t.send(r.name)
return false if o.respond_to(:project_id) && !p.include?(o.project)
end
end
end
true
end
|
#is_editable?(user) ⇒ Boolean
117
118
119
120
121
122
|
# File 'app/models/concerns/shared/is_data.rb', line 117
def is_editable?(user)
user = User.find(user) if !user.kind_of?(User)
return true if user.is_administrator? ||
return false if !is_in_users_projects?(user)
true
end
|
#is_in_use?(exclude = [], only = []) ⇒ Boolean
!! provide only exclude or only
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'app/models/concerns/shared/is_data.rb', line 134
def is_in_use?(exclude = [], only = [])
if only.empty?
self.class.reflect_on_all_associations(:has_many).each do |r|
next if exclude.include?(r.name)
return true if self.send(r.name).count(:all) > 0
end
self.class.reflect_on_all_associations(:has_one).each do |r|
next if exclude.include?(r.name)
return true if self.send(r.name).count(:all) > 0
end
else
only.each do |r|
return true if self.send(r.to_s).count(:all) > 0
end
end
false
end
|
#is_in_users_projects?(user) ⇒ Boolean
124
125
126
|
# File 'app/models/concerns/shared/is_data.rb', line 124
def is_in_users_projects?(user)
user.projects.pluck(:id).include?(project_id)
end
|
Returns the same object, but namespaced to the base class used many places, might be good target for optimization.
162
163
164
165
|
# File 'app/models/concerns/shared/is_data.rb', line 162
def metamorphosize
return self if self.class.descends_from_active_record?
self.becomes(self.class.base_class)
end
|
#similar ⇒ Scope
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'app/models/concerns/shared/is_data.rb', line 178
def similar
klass = self.class
attr = Stripper.strip_similar_attributes(klass, attributes)
attr = attr.select{ |_kee, val| val.present? }
if id
scope = klass.where(attr).not_self(self)
else
scope = klass.where(attr)
end
scope
end
|