Class: Category

Inherits:
ApplicationRecord show all
Defined in:
app/models/category.rb

Overview

This model manage the categories of work’s risks

Relations

has many Audit has many CategoryRisk has many Risk through CategoryRisk has many User through Audit

Validations

Default scope

where active is true ordered by title

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#div

Instance Attribute Details

#activeBoolean

Returns if category is active or not, can’t be Null, default True.

Returns:

  • (Boolean)

    if category is active or not, can’t be Null, default True



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#codeString

Returns Category code.

Returns:

  • (String)

    Category code



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#created_atDatetime

Returns date when the record was created.

Returns:

  • (Datetime)

    date when the record was created



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#idInteger

Returns is the unique identifier for Category.

Returns:

  • (Integer)

    is the unique identifier for Category



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#monthsInteger

Returns periodicity of checks expressed in months, can’t be Null, default 0.

Returns:

  • (Integer)

    periodicity of checks expressed in months, can’t be Null, default 0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#protocolString

Returns description of check required.

Returns:

  • (String)

    description of check required



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#titleString

Returns Category Title, can’t be Null, default is an empty string.

Returns:

  • (String)

    Category Title, can’t be Null, default is an empty string



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#updated_atDatetime

Returns date when the record was updated.

Returns:

  • (Datetime)

    date when the record was updated



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

Instance Method Details

#auditArray

Returns of Audit referenced from Audit#category_id.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#category_risksArray

Returns of CategoryRisk referenced from CategoryRisk#category_id.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end

#check_destroyBoolean

Returns if category is unused and can be destroyed run Super, otherwise update for set active as false.

Returns:

  • (Boolean)

    if category is unused and can be destroyed run Super, otherwise update for set active as false.



63
64
65
66
# File 'app/models/category.rb', line 63

def check_destroy
  update(active: false) if users.present? || risks.present?
  false
end

#deleteObject

for security reason is an alias of destroy



58
59
60
# File 'app/models/category.rb', line 58

def delete
  destroy
end

#disabledArray

Returns of disablec Category.

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/category.rb', line 43

class Category < ApplicationRecord
  has_many :audits, dependent: :restrict_with_exception
  has_many :category_risks, dependent: :restrict_with_exception
  has_many :risks, through: :category_risks
  has_many :users, through: :audits

  validates :title, presence: true, uniqueness: true
  validates :months, presence: true, numericality: { only_integer: true }

  default_scope { order('title asc').where(active: true) }
  scope :disabled, -> { unscoped.where(active: false) }

  before_destroy :check_destroy

  # for security reason is an alias of {destroy}
  def delete
    destroy
  end

  # @return [Boolean] if category is unused and can be destroyed run Super, otherwise update for set active as false.
  def check_destroy
    update(active: false) if users.present? || risks.present?
    false
  end
end