Module: Shared::IsData::ClassMethods

Defined in:
app/models/concerns/shared/is_data.rb

Instance Method Summary collapse

Instance Method Details

#auto_uuids?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'app/models/concerns/shared/is_data.rb', line 43

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.

Returns:

  • (Boolean)

    use update vs. a set of ids, but require the update to pass for all or none



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/concerns/shared/is_data.rb', line 56

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_attributesArray of String

Returns only the non-cached and non-housekeeping column names.

Returns:

  • (Array of String)

    only the non-cached and non-housekeeping column names



49
50
51
52
# File 'app/models/concerns/shared/is_data.rb', line 49

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

Returns:

  • (Boolean)


31
32
33
# File 'app/models/concerns/shared/is_data.rb', line 31

def dwc_occurrence_eligible?
  self < Shared::IsDwcOccurrence
end

#identical(attr) ⇒ Scope

Parameters:

  • attr (Hash)

    of matchable attributes

Returns:

  • (Scope)


97
98
99
100
101
102
103
# File 'app/models/concerns/shared/is_data.rb', line 97

def identical(attr)
  klass = self
  attr  = Stripper.strip_identical_attributes(klass, attr)

  scope = klass.where(attr)
  scope
end

#is_biologically_relatable?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/concerns/shared/is_data.rb', line 39

def is_biologically_relatable?
  self < Shared::BiologicalAssociations
end

#is_community?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'app/models/concerns/shared/is_data.rb', line 23

def is_community?
  self < Shared::SharedAcrossProjects ? true : false
end

#is_containable?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'app/models/concerns/shared/is_data.rb', line 27

def is_containable?
  self < Shared::Containable
end

#is_observable?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/concerns/shared/is_data.rb', line 35

def is_observable?
  self < Shared::Observations
end

Returns Hash { restrict: {}, destroy: {} }

Summarizes the count of records that will be destroyed if these ids are destroyed, or records that will prevent destruction.

Returns:

  • Hash { restrict: {}, destroy: {} }

    Summarizes the count of records that will be destroyed if these ids are destroyed, or records that will prevent destruction.



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
# File 'app/models/concerns/shared/is_data.rb', line 110

def related_summary(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.options.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

Parameters:

  • attr (Hash)

    of matchable attributes

Returns:

  • (Scope)


86
87
88
89
90
91
92
93
# File 'app/models/concerns/shared/is_data.rb', line 86

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