Class: Effective::CpdCycle

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/effective/cpd_cycle.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.latest_cycleObject



64
65
66
# File 'app/models/effective/cpd_cycle.rb', line 64

def self.latest_cycle
  order(id: :desc).first
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/effective/cpd_cycle.rb', line 99

def available?
  started? && !ended?
end

#available_dateObject



111
112
113
114
115
116
117
# File 'app/models/effective/cpd_cycle.rb', line 111

def available_date
  if start_at && end_at
    "#{start_at.strftime('%F')} to #{end_at.strftime('%F')}"
  elsif start_at
    "#{start_at.strftime('%F')}"
  end
end

#build_from_cycle(cycle:) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/models/effective/cpd_cycle.rb', line 72

def build_from_cycle(cycle:)
  raise('expected a CpdCycle') unless cycle.kind_of?(CpdCycle)

  # Cycle
  attributes = cycle.dup.attributes.except('title', 'token', 'start_at', 'end_at')
  assign_attributes(attributes)

  [:all_steps_content, :start_content, :activities_content, :submit_content, :complete_content].each do |rich_text|
    self.send("#{rich_text}=", cycle.send(rich_text))
  end

  cycle.cpd_rules.each do |rule|
    attributes = rule.dup.attributes.except('cpd_cycle_id')
    self.cpd_rules.build(attributes)
  end

  self
end

#ended?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/effective/cpd_cycle.rb', line 107

def ended?
  end_at.present? && end_at < Time.zone.now
end

#rule_for(ruleable) ⇒ Object

Find or build



92
93
94
95
96
97
# File 'app/models/effective/cpd_cycle.rb', line 92

def rule_for(ruleable)
  raise('expected a CpdCategory or CpdActivity') unless ruleable.kind_of?(CpdActivity) || ruleable.kind_of?(CpdCategory)

  rule = cpd_rules.find { |rule| rule.ruleable_id == ruleable.id && rule.ruleable_type == ruleable.class.name }
  rule ||= cpd_rules.build(ruleable: ruleable)
end

#started?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/effective/cpd_cycle.rb', line 103

def started?
  start_at_was.present? && Time.zone.now >= start_at_was
end

#to_sObject



68
69
70
# File 'app/models/effective/cpd_cycle.rb', line 68

def to_s
  title.presence || 'New CPD Cycle'
end