Class: Audit

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

Overview

This model manage the user’s medical check for a risk

Relation

Validations

  • Validate presence of User relation

  • Validate presence of Category relation

  • Validate presence of :status

  • Validate presence of :expire

Default scope

where.not(status: 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

#div

Instance Attribute Details

#category_idInteger

Returns reference id of Category, can’t be null.

Returns:

  • (Integer)

    reference id of Category, can’t be null



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

#created_atDatetime

Returns date when the record was created.

Returns:

  • (Datetime)

    date when the record was created



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

#expireDate

Returns expire date for the Audit.

Returns:

  • (Date)

    expire date for the Audit



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

#statusString

Possible statuses:

  • deleted: 0,

  • created: 1,

  • suitable: 2,

  • suitable_prescription_temporary: 3,

  • suitable_prescription_permanent: 4,

  • unsuitable_temporary: 5,

  • unsuitable_permanent: 6

Returns:

  • (String)

    Is the audit status, is an enum, can’t be null, default status is :created.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

#updated_atDatetime

Returns date when the record was updated.

Returns:

  • (Datetime)

    date when the record was updated



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

#user_idInteger

Returns reference id of User, can’t be Null.

Returns:

  • (Integer)

    reference id of User, can’t be Null



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

Class Method Details

.expire_beetween(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) ⇒ Array

select all Audit inside a period

Parameters:

  • start_at (Datetime) (defaults to: Time.zone.now.to_date)

    start date for the query

  • stop_at (Date) (defaults to: Time.zone.now.to_date)

    end date for the query

Returns:

  • (Array)

    of Audit from :start to :stop_at



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

.expired(date: Date.today.end_of_month) ⇒ Array

Select all Audit expired in a date

Parameters:

  • date (Date) (defaults to: Date.today.end_of_month)

    start date for query

Returns:

  • (Array)

    of Audit expired before :date



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

.get_calendar(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) ⇒ Array

specific query for make a calendar

Parameters:

  • start_at (Datetime) (defaults to: Time.zone.now.to_date)

    start date for the query

  • stop_at (Date) (defaults to: Time.zone.now.to_date)

    end date for the query

Returns:

  • (Array)

    of Audit from :start to :stop_at with expire and count, group and order by expire



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/audit.rb', line 54

class Audit < ApplicationRecord
  belongs_to  :user
  belongs_to  :category
  has_many    :histories, dependent: :destroy
  has_one     :history, -> { order created_at: :desc }, class_name: 'History', foreign_key: :audit_id, inverse_of: :audit
  has_many    :meetings, dependent: :destroy
  has_many    :events, through: :meetings, dependent: :destroy

  store_accessor :metadata, :author, :revision_date

  validates :user_id, presence: true
  validates :category_id, presence: true, uniqueness: { scope: %i[user_id status] }
  validates :status, presence: true
  validates :expire, presence: true

  enum status: {
    deleted: 0,
    created: 1,
    suitable: 2,
    suitable_prescription_temporary: 3,
    suitable_prescription_permanent: 4,
    unsuitable_temporary: 5,
    unsuitable_permanent: 6
  }

  default_scope { where.not(status: 0) }
  scope :ordered_by_category_title, -> { joins(:category).order('categories.title') }
  scope :expire_beetween, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { where(expire: start_at..stop_at) }
  scope :expired, ->(date: Time.zone.today.end_of_month) { where('expire <= ?', date) }
  scope :get_calendar, ->(start_at = Time.zone.now.to_date, stop_at = Time.zone.now.to_date) { expire_beetween(start_at, stop_at).select('expire as expire', 'Count(Distinct audits.user_id) as count').group('expire').order('expire') }

  accepts_nested_attributes_for :histories, allow_destroy: false, limit: 1

  # is an alias of history relation
  # @return [Object] return last {Audit}'s {History}
  def state
    history
  end

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

  # Override default destroy end return everytime false. {Audit} record cannot be destroyed
  # @return [False]
  def destroy
    false
  end

  # @return [String] based on {expired?} response 'expired' o 'active'
  def status_class
    expired? ? 'expired' : 'active'
  end

  # @return [Boolean] true if {Audit} is expired
  def expired?
    expire <= Time.zone.now
  end
end

Instance Method Details

#deleteObject

For security reason is an alias of #destroy



94
95
96
# File 'app/models/audit.rb', line 94

def delete
  destroy
end

#destroyFalse

Override default destroy end return everytime false. Audit record cannot be destroyed

Returns:

  • (False)


100
101
102
# File 'app/models/audit.rb', line 100

def destroy
  false
end

#expired?Boolean

Returns true if Audit is expired.

Returns:

  • (Boolean)

    true if Audit is expired



110
111
112
# File 'app/models/audit.rb', line 110

def expired?
  expire <= Time.zone.now
end

#stateObject

is an alias of history relation

Returns:



89
90
91
# File 'app/models/audit.rb', line 89

def state
  history
end

#status_classString

Returns based on #expired? response ‘expired’ o ‘active’.

Returns:

  • (String)

    based on #expired? response ‘expired’ o ‘active’



105
106
107
# File 'app/models/audit.rb', line 105

def status_class
  expired? ? 'expired' : 'active'
end