Module: Shared::IsData::ClassMethods
- Defined in:
- app/models/concerns/shared/is_data.rb
Instance Method Summary collapse
- #auto_uuids? ⇒ Boolean
-
#batch_update_attribute(ids: [], attribute: nil, value: nil) ⇒ Boolean
Use update vs.
-
#core_attributes ⇒ Array of String
Only the non-cached and non-housekeeping column names.
- #dwc_occurrence_eligible? ⇒ Boolean
- #identical(attr) ⇒ Scope
- #is_biologically_relatable? ⇒ Boolean
- #is_community? ⇒ Boolean
- #is_containable? ⇒ Boolean
- #is_distribution_assertable? ⇒ Boolean
- #is_observable? ⇒ Boolean
-
#random_ids(total = 100) ⇒ Object
private
Array of IDs for this class.
-
#related_summary(ids) ⇒ Object
Hash { restrict: {}, destroy: {} }.
- #similar(attr) ⇒ Scope
Instance Method Details
#auto_uuids? ⇒ Boolean
49 50 51 |
# File 'app/models/concerns/shared/is_data.rb', line 49 def auto_uuids? self < Shared::AutoUuid end |
#batch_update_attribute(ids: [], attribute: nil, value: nil) ⇒ Boolean
Returns use update vs. a set of ids, but require the update to pass for all or none.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/concerns/shared/is_data.rb', line 62 def batch_update_attribute(ids: [], attribute: nil, value: nil) return false if ids.empty? || attribute.nil? || value.nil? begin self.transaction do self.where(id: ids).each do |li| li.update(attribute => value) end end rescue return false end true end |
#core_attributes ⇒ Array of String
Returns only the non-cached and non-housekeeping column names.
55 56 57 58 |
# File 'app/models/concerns/shared/is_data.rb', line 55 def core_attributes # was data_attributes column_names.reject { |c| %w{id project_id created_by_id updated_by_id created_at updated_at} .include?(c) || c =~ /^cached/ } end |
#dwc_occurrence_eligible? ⇒ Boolean
33 34 35 |
# File 'app/models/concerns/shared/is_data.rb', line 33 def dwc_occurrence_eligible? self < Shared::IsDwcOccurrence end |
#identical(attr) ⇒ Scope
103 104 105 106 107 108 109 |
# File 'app/models/concerns/shared/is_data.rb', line 103 def identical(attr) klass = self attr = Stripper.strip_identical_attributes(klass, attr) scope = klass.where(attr) #.where.not(id:) scope end |
#is_biologically_relatable? ⇒ Boolean
45 46 47 |
# File 'app/models/concerns/shared/is_data.rb', line 45 def is_biologically_relatable? self < Shared::BiologicalAssociations end |
#is_community? ⇒ Boolean
25 26 27 |
# File 'app/models/concerns/shared/is_data.rb', line 25 def is_community? self < Shared::SharedAcrossProjects ? true : false end |
#is_containable? ⇒ Boolean
29 30 31 |
# File 'app/models/concerns/shared/is_data.rb', line 29 def is_containable? self < Shared::Containable end |
#is_distribution_assertable? ⇒ Boolean
41 42 43 |
# File 'app/models/concerns/shared/is_data.rb', line 41 def is_distribution_assertable? self < Shared::AssertedDistributions end |
#is_observable? ⇒ Boolean
37 38 39 |
# File 'app/models/concerns/shared/is_data.rb', line 37 def is_observable? self < Shared::Observations end |
#random_ids(total = 100) ⇒ Object (private)
Returns Array of IDs for this class
Make cutoff smaller to reach higher ids. Useful for pseudo-benchmarking. TODO: doesn’t return total. There are some nice discussions on stack overflow. TODO: probably doesn’t belong here.
159 160 161 162 163 164 165 166 |
# File 'app/models/concerns/shared/is_data.rb', line 159 def random_ids(total = 100) return self.none if total.blank? if column_names.include?('type') self.find_by_sql( "select id, type from #{self.table_name} where type = '#{self.name}' AND random() < 0.0001 limit #{total};").pluck(:id) else self.find_by_sql( "select id from #{self.table_name} where random() < 0.0001 limit #{total};").pluck(:id) end end |
#related_summary(ids) ⇒ Object
Returns Hash { restrict: {}, destroy: {} }
Summarizes the count of records that will be destroyed if these ids are destroyed, or records that will prevent destruction.
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 |
# File 'app/models/concerns/shared/is_data.rb', line 116 def (ids) h = { restrict: {}, destroy: {} } objects = self.where(id: ids) base = self.table_name.to_sym [ self.reflect_on_all_associations(:has_many), self.reflect_on_all_associations(:has_one) ].each do |rt| rt.each do |r| d = r..dig(:dependent) next if d.nil? c = nil if r.type c = r.klass.where(r.type.to_sym => self.name, r.type.gsub('_type', '_id').to_sym => objects.map(&:id)).count else c = r.klass.where(r.foreign_key.to_sym => objects.map(&:id)).count end if c > 0 case d when :destroy h[:destroy][r.name] = c when :restrict_with_error h[:restrict][r.name] = c end end end end h end |
#similar(attr) ⇒ Scope
92 93 94 95 96 97 98 99 |
# File 'app/models/concerns/shared/is_data.rb', line 92 def similar(attr) klass = self attr = Stripper.strip_similar_attributes(klass, attr) attr = attr.select { |_kee, val| val.present? } scope = klass.where(attr) scope end |