Sha256: 53feb65a02fdd60ce1b71146e77ee75bb2627bc1b2bb1af1e1561d85cb52c49d

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true

require 'active_record'
require 'ids_of_all_dependencies'
require 'nullify_dependencies'

class AssociationWithValue
  delegate_missing_to :@association

  def initialize(association, value)
    @association = association
    @value = value
  end
end

class Model < ActiveRecord::Base
  include IdsOfAllDependencies
  include NullifyDependencies

  self.abstract_class = true

  def attributes_without_id
    self.attributes.reject{|k, v| k == "id"}
  end

  def associations_hash
    self.associations.map do |association|
      [association.name, association]
    end.to_h
  end

  def associations
    self.class.reflect_on_all_associations.map do |association|
      value = self.send(association.name)
      AssociationWithValue.new(association, value)
    end
  end

  def associations_without_belongs_to
    self.associations.select { |a| a.macro != :belongs_to }
  end

  def self.get_model(name)
    self.subclasses.find{ |m| m.name == name.to_s.camelcase }
  end

  def self.get_model_by_table_name(name)
    self.subclasses.find{ |m| m.table_name == name.to_s }
  end

  def self.ids_of_subclasses_entries
    self.subclasses.map do |subclass|
      [subclass.name, subclass.all.map(&:id)] if subclass.any?
    end.compact.to_h
  end

  def self.sum_of_subclasses_rows(except: [])
    self.subclasses.map do |subclass|
      next 0 if except.include?(subclass.table_name) || except.include?(subclass.name)

      subclass.all.size
    end.reduce(:+)
  end

  def removed?
    begin
      reload
      false
    rescue ActiveRecord::RecordNotFound => e
      true
    end
  end
end

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'abuse', 'abuses'
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
travis-backup-0.3.0 lib/model.rb