Class: Schedule

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

Overview

== Schema Information

Table name: schedules
Database name: primary

id :integer not null, primary key
count :integer
date :date
day :text
day_of_week :text
interval :string
rule :string
schedulable_type :string
time :time
until :datetime
created_at :datetime
updated_at :datetime
schedulable_id :integer

A recurrence rule attached to a schedulable record (see Models::Schedulable).

Vendored from the schedulable gem's Schedulable::Model::Schedule base
class. rule selects the recurrence kind (singular, daily, weekly,
monthly); the matching IceCube::Schedule is rebuilt on every load and
save, and unknown method calls delegate to it.

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#dateObject (readonly)

Validates a date is present for one-off schedules.

Validations (if => -> { rule == 'singular' } ):



44
# File 'app/models/schedule.rb', line 44

validates :date, presence: true, if: -> { rule == 'singular' }

#ruleObject (readonly)

Validates a recurrence rule is present.

Validations:



40
# File 'app/models/schedule.rb', line 40

validates :rule, presence: true

#timeTime? (readonly)

Normalizes the AR time value to a core Time.

ActiveRecord maps time columns to ActiveSupport::TimeWithZone;
callers (and the recurrence builder) expect a plain Time.

Returns:

  • (Time, nil)


42
# File 'app/models/schedule.rb', line 42

validates :time, presence: true

Class Method Details

.param_namesArray

Strong-parameter shape for a nested schedule form.

Returns:

  • (Array)


86
87
88
89
90
91
92
93
94
# File 'app/models/schedule.rb', line 86

def self.param_names
  [
    :id, :date, :time, :time_end, :rule, :until, :count, :interval,
    { day: [], day_of_week: {
      monday: [], tuesday: [], wednesday: [],
      thursday: [], friday: [], saturday: [], sunday: []
    } }
  ]
end

Instance Method Details

#schedulableSchedulable

Record this schedule belongs to (employee, store, etc.).

Returns:

  • (Schedulable)

See Also:



34
# File 'app/models/schedule.rb', line 34

belongs_to :schedulable, polymorphic: true

#to_icecubeIceCube::Schedule?

Returns the underlying recurrence schedule.

Returns:

  • (IceCube::Schedule, nil)

    the underlying recurrence schedule



69
70
71
# File 'app/models/schedule.rb', line 69

def to_icecube
  @schedule
end

#to_sString

Returns a localized, human-readable description of the schedule.

Returns:

  • (String)

    a localized, human-readable description of the schedule



74
75
76
77
78
79
80
81
# File 'app/models/schedule.rb', line 74

def to_s
  if rule == 'singular'
    datetime = date.to_datetime + time.seconds_since_midnight.seconds
    I18n.localize(datetime, format: :long)
  else
    to_icecube.to_s
  end
end