Class: InternalAttribute
- Inherits:
-
DataAttribute
- Object
- ActiveRecord::Base
- ApplicationRecord
- DataAttribute
- InternalAttribute
- Includes:
- Shared::DwcOccurrenceHooks
- Defined in:
- app/models/internal_attribute.rb
Constant Summary
Constants included from Shared::DualAnnotator
Shared::DualAnnotator::ALWAYS_COMMUNITY
Instance Attribute Summary collapse
-
#controlled_vocabulary_term_id ⇒ id
The the id of the ControlledVocabularyTerm::Predicate.
Attributes inherited from DataAttribute
#attribute_subject_id, #attribute_subject_type, #project_id, #type, #value
Class Method Summary collapse
-
.add_value(attribute_scope, controlled_vocabulary_term_id, value_to) ⇒ Object
Add attributes to the objects in the filter that do not have them.
- .batch_create(params) ⇒ Object
-
.batch_update_or_create(params) ⇒ Object
<some_object>_query={}&value_from=123&value_to=456&predicate_id=890.
- .update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to) ⇒ Object
Instance Method Summary collapse
Methods inherited from DataAttribute
#editable?, find_for_autocomplete, #predicate_name
Methods included from Shared::PolymorphicAnnotator
#annotated_object_is_persisted?
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::Citations
#cited?, #mark_citations_for_destruction, #nomenclature_date, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#controlled_vocabulary_term_id ⇒ id
The the id of the ControlledVocabularyTerm::Predicate. Term is referenced as #predicate.
10 11 12 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 |
# File 'app/models/internal_attribute.rb', line 10 class InternalAttribute < DataAttribute include Shared::DwcOccurrenceHooks validates_presence_of :predicate validates_uniqueness_of :value, scope: [:attribute_subject_id, :attribute_subject_type, :type, :controlled_vocabulary_term_id, :project_id] def dwc_occurrences if DWC_ATTRIBUTE_URIS.values.flatten.include?(predicate&.uri) # TODO: probably use some generic interface here case attribute_subject_type when 'CollectingEvent' ::Queries::DwcOccurrence::Filter.new( collecting_event_query: { collecting_event_id: attribute_subject_id } ).all when 'CollectionObject' ::DwcOccurrence.where( dwc_occurrence_object_id: attribute_subject_id, dwc_occurrence_object_type: 'CollectionObject') else ::DwcOccurrence.none end else ::DwcOccurrence.none end end # @params attribute_scope a Query::X::Filter instance # that is NOT Query::DataAttribute::Filter def self.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to) return false if controlled_vocabulary_term_id.nil? || value_from.nil? || value_to.nil? b = ::Queries::DataAttribute::Filter.new( attribute_scope.query_name.to_sym => attribute_scope.params, controlled_vocabulary_term_id:, value: value_from, ) b.all.update_all(value: value_to) end # Add attributes to the objects in the filter that do not have them # # @return attribute_scope a Filter instance def self.add_value(attribute_scope, controlled_vocabulary_term_id, value_to) return false if controlled_vocabulary_term_id.nil? || value_to.nil? # with (not these) a = attribute_scope.all.joins(:internal_attributes).where( data_attributes: { value: value_to, controlled_vocabulary_term_id: }) # If the join is empty, we need to add to all if a.size == 0 b = attribute_scope.all else s = 'WITH query_with_ia AS (' + a.to_sql + ') ' + attribute_scope.referenced_klass .joins("LEFT JOIN query_with_ia as query_with_ia1 ON query_with_ia1.id = #{attribute_scope.table.name}.id") .where("query_with_ia1.id is null") .to_sql b = attribute_scope.referenced_klass.from('(' + s + ") as #{attribute_scope.table.name}").distinct end # stop-gap, we shouldn't hit this return false if b.size > 1000 InternalAttribute.transaction do b.each do |o| begin ::InternalAttribute.create!( controlled_vocabulary_term_id:, value: value_to, attribute_subject: o) rescue ActiveRecord::RecordInvalid => e # TODO: check necessity of this # This should not be necessary, but proceed end end end end # <some_object>_query={}&value_from=123&value_to=456&predicate_id=890 def self.batch_update_or_create(params) a = Queries::Query::Filter.base_filter(params) q = params.keys.select{|z| z =~ /_query/}.first return false if q.nil? b = a.new(params[q]) s = b.all.count return false if b.params.empty? || s > 1000 || s == 0 transaction do update_value(b, params[:predicate_id], params[:value_from], params[:value_to]) add_value(b, params[:predicate_id], params[:value_to]) end true end def self.batch_create(params) ids = params[:attribute_subject_id] params.delete(:attribute_subject_id) internal_attributes = [] InternalAttribute.transaction do begin ids.each do |id| internal_attributes.push InternalAttribute.create!( params.merge( attribute_subject_id: id ) ) end rescue ActiveRecord::RecordInvalid return false end end internal_attributes end end |
Class Method Details
.add_value(attribute_scope, controlled_vocabulary_term_id, value_to) ⇒ Object
Add attributes to the objects in the filter that do not have them
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 |
# File 'app/models/internal_attribute.rb', line 54 def self.add_value(attribute_scope, controlled_vocabulary_term_id, value_to) return false if controlled_vocabulary_term_id.nil? || value_to.nil? # with (not these) a = attribute_scope.all.joins(:internal_attributes).where( data_attributes: { value: value_to, controlled_vocabulary_term_id: }) # If the join is empty, we need to add to all if a.size == 0 b = attribute_scope.all else s = 'WITH query_with_ia AS (' + a.to_sql + ') ' + attribute_scope.referenced_klass .joins("LEFT JOIN query_with_ia as query_with_ia1 ON query_with_ia1.id = #{attribute_scope.table.name}.id") .where("query_with_ia1.id is null") .to_sql b = attribute_scope.referenced_klass.from('(' + s + ") as #{attribute_scope.table.name}").distinct end # stop-gap, we shouldn't hit this return false if b.size > 1000 InternalAttribute.transaction do b.each do |o| begin ::InternalAttribute.create!( controlled_vocabulary_term_id:, value: value_to, attribute_subject: o) rescue ActiveRecord::RecordInvalid => e # TODO: check necessity of this # This should not be necessary, but proceed end end end end |
.batch_create(params) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/models/internal_attribute.rb', line 117 def self.batch_create(params) ids = params[:attribute_subject_id] params.delete(:attribute_subject_id) internal_attributes = [] InternalAttribute.transaction do begin ids.each do |id| internal_attributes.push InternalAttribute.create!( params.merge( attribute_subject_id: id ) ) end rescue ActiveRecord::RecordInvalid return false end end internal_attributes end |
.batch_update_or_create(params) ⇒ Object
<some_object>_query={}&value_from=123&value_to=456&predicate_id=890
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/internal_attribute.rb', line 96 def self.batch_update_or_create(params) a = Queries::Query::Filter.base_filter(params) q = params.keys.select{|z| z =~ /_query/}.first return false if q.nil? b = a.new(params[q]) s = b.all.count return false if b.params.empty? || s > 1000 || s == 0 transaction do update_value(b, params[:predicate_id], params[:value_from], params[:value_to]) add_value(b, params[:predicate_id], params[:value_to]) end true end |
.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/internal_attribute.rb', line 40 def self.update_value(attribute_scope, controlled_vocabulary_term_id, value_from, value_to) return false if controlled_vocabulary_term_id.nil? || value_from.nil? || value_to.nil? b = ::Queries::DataAttribute::Filter.new( attribute_scope.query_name.to_sym => attribute_scope.params, controlled_vocabulary_term_id:, value: value_from, ) b.all.update_all(value: value_to) end |
Instance Method Details
#dwc_occurrences ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/internal_attribute.rb', line 17 def dwc_occurrences if DWC_ATTRIBUTE_URIS.values.flatten.include?(predicate&.uri) # TODO: probably use some generic interface here case attribute_subject_type when 'CollectingEvent' ::Queries::DwcOccurrence::Filter.new( collecting_event_query: { collecting_event_id: attribute_subject_id } ).all when 'CollectionObject' ::DwcOccurrence.where( dwc_occurrence_object_id: attribute_subject_id, dwc_occurrence_object_type: 'CollectionObject') else ::DwcOccurrence.none end else ::DwcOccurrence.none end end |