module ActiveConformity::ConformableExtensions

Public Instance Methods

add_conformity_set!(conformity_set = {}, conformist_type) click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 62
def add_conformity_set!(conformity_set = {}, conformist_type)
  conformable_attrs = {conformable_id: self.id, conformable_type: self.class.name, conformist_type: conformist_type}
  @conformable = Conformable.where(conformable_attrs).first_or_create
  @conformable.add_conformity_set(conformity_set)
  @conformable.save!
end
add_self_to_conformable_references() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 77
def add_self_to_conformable_references
  self if Conformable.where(conformable_id: self.id,
  conformable_type: self.class.name).any?
end
aggregate_conformity_set() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 32
def aggregate_conformity_set
  acs = {}
  return acs if !conformable_references.any? #need to think about this a little more
  conformable_references.each do |c|
    # This could be more efficient with some advanced sql techniques
    # Also need indexes on these
    c = Conformable.find_by!(conformable_id: c.id, conformable_type: c.class.name).conformity_set
    c = JSON.parse(c) if c.is_a?(String)
    acs.merge!(c)
  end
  acs
end
conformable() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 57
def conformable
  @conformable ||= Conformable.find_by(conformable_id: self.id, conformable_type: self.class.name)
  @conformable
end
conformable_references() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 69
def conformable_references
  return @conformable_references if defined? @conformable_references
  @conformable_references = Set.new
  @conformable_references.merge conformable_references_from_associations
  @conformable_references.add add_self_to_conformable_references
  @conformable_references = @conformable_references.to_a.compact
end
conforming_dependents_conform?() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 45
def conforming_dependents_conform?
  return true if dependents.blank?
  !dependents.flat_map { |da| self.send(da)}
  .any?{ |da| !da.conforms? }
end
conformity_errors() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 28
def conformity_errors
  validator.errors.messages
end
conformity_sets_by_reference() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 51
def conformity_sets_by_reference
  conformable_references.flat_map do |cr|
    {"#{cr.class.name} id: #{cr.id}"=> cr.conformable.conformity_set}
  end
end
conforms?() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 24
def conforms?
  validator.conforms? && conforming_dependents_conform?
end
remove_conformity_rule!(attr) click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 82
def remove_conformity_rule!(attr)
  conformable.remove_coformity_rule!(attr)
end
remove_rules() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 86
def remove_rules
  conformable.remove_rules!
end

Private Instance Methods

conformable_references_from_associations() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 108
def conformable_references_from_associations
  self.class.reflect_on_all_associations.flat_map do |assoc|
    self.send(assoc.name) if conformable_types.include?(assoc.klass.name) rescue nil
  end
end
conformable_types() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 114
def conformable_types
  return @conformable_types if defined?(@conformable_types)
  @conformable_types = conformables_for_class.pluck(:conformable_type)
  @conformable_types
end
conformables_for_class() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 120
def conformables_for_class
  return @conformables_for_class if defined?(@conformables_for_class)
  @conformables_for_class = Conformable.where(conformist_type: self.class.name)
  @conformables_for_class
end
dependents() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 92
def dependents
  self.class.dependents
end
method_missing(m, *args, &block) click to toggle source
Calls superclass method
# File lib/active_conformity/conformable_extensions.rb, line 96
def method_missing(m, *args, &block)
  if m.to_sym == :conformity_set && !self.class.column_names.include?(m.to_s)
    conformable.try(:conformity_set)
  else
    super
  end
end
validator() click to toggle source
# File lib/active_conformity/conformable_extensions.rb, line 104
def validator
  ActiveConformity::ObjectValidator.new(self, aggregate_conformity_set)
end