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)


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

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)


149
150
151
# File 'app/models/concerns/shared/is_data.rb', line 149

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

#identicalScope

Returns:

  • (Scope)


136
137
138
139
140
141
142
143
144
145
# File 'app/models/concerns/shared/is_data.rb', line 136

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)


91
92
93
# File 'app/models/concerns/shared/is_data.rb', line 91

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

#is_in_use?(exclude = []) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_in_use?(exclude = [])
  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|
    return true if self.send(r.name).count(:all) > 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



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

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

#similarScope

Returns:

  • (Scope)


122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/concerns/shared/is_data.rb', line 122

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