Module: Shared::IsData

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: ClassMethods, Stripper

Instance Method Summary collapse

Instance Method Details

#errors_excepting(*keys) ⇒ Hash

Parameters:

  • keys (Symbol)

Returns:

  • (Hash)


103
104
105
106
107
108
109
# File 'app/models/concerns/shared/is_data.rb', line 103

def errors_excepting(*keys)
  valid?
  keys.each do |k|
    errors.delete(k)
  end
  errors
end

#full_error_messages_excepting(*keys) ⇒ Array

Parameters:

  • keys (Symbol)

Returns:

  • (Array)


139
140
141
# File 'app/models/concerns/shared/is_data.rb', line 139

def full_error_messages_excepting(*keys)
  errors_excepting(*keys).full_messages
end

#identicalScope

Returns:

  • (Scope)


126
127
128
129
130
131
132
133
134
135
# File 'app/models/concerns/shared/is_data.rb', line 126

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

#is_community?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/concerns/shared/is_data.rb', line 84

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

#is_in_use?Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/concerns/shared/is_data.rb', line 89

def is_in_use?
  self.class.reflect_on_all_associations(:has_many).each do |r|
    return true if self.send(r.name).count > 0
  end

  self.class.reflect_on_all_associations(:has_one).each do |r|
    return true if self.send(r.name).count > 0
  end

  false
end

#metamorphosizeObject

Returns the same object, but namespaced to the base class used many places, might be good target for optimization

Returns:

  • (Object)

    the same object, but namespaced to the base class used many places, might be good target for optimization



78
79
80
81
# File 'app/models/concerns/shared/is_data.rb', line 78

def metamorphosize
  return self if self.class.descends_from_active_record?
  self.becomes(self.class.base_class)
end

#similarScope

Returns:

  • (Scope)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/concerns/shared/is_data.rb', line 112

def similar
  klass = self.class
  attr  = Stripper.strip_similar_attributes(klass, attributes)
  # matching only those attributes from the instance which are not empty
  attr = attr.select{ |_kee, val| val.present? }
  if id
    scope = klass.where(attr).not_self(self)
  else
    scope = klass.where(attr)
  end
  scope
end