Class: Effective::CpdCycle
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::CpdCycle
- Defined in:
- app/models/effective/cpd_cycle.rb
Class Method Summary collapse
Instance Method Summary collapse
- #available? ⇒ Boolean
- #available_date ⇒ Object
- #build_from_cycle(cycle:) ⇒ Object
- #ended? ⇒ Boolean
-
#rule_for(ruleable) ⇒ Object
Find or build.
- #started? ⇒ Boolean
-
#targeted_score ⇒ Object
Todo, implement a cpd cycle targeted score entirely.
- #to_s ⇒ Object
Class Method Details
.effective_cpd_cpd_cycle? ⇒ Boolean
69 70 71 |
# File 'app/models/effective/cpd_cycle.rb', line 69 def self.effective_cpd_cpd_cycle? true end |
.latest_cycle ⇒ Object
73 74 75 |
# File 'app/models/effective/cpd_cycle.rb', line 73 def self.latest_cycle order(id: :desc).first end |
Instance Method Details
#available? ⇒ Boolean
126 127 128 |
# File 'app/models/effective/cpd_cycle.rb', line 126 def available? started? && !ended? end |
#available_date ⇒ Object
138 139 140 141 142 143 144 |
# File 'app/models/effective/cpd_cycle.rb', line 138 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
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 114 115 116 |
# File 'app/models/effective/cpd_cycle.rb', line 86 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, :sidebar_content, :start_content, :activities_content, :agreements_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') cpd_rule = self.cpd_rules.build(attributes) if rule.category? cpd_rule.category_credit_description = rule.category_credit_description end end cycle.cpd_special_rules.each do |special_rule| attributes = special_rule.dup.attributes.except('cpd_cycle_id') cpd_special_rule = self.cpd_special_rules.build(attributes) special_rule.ruleables.each do |ruleable| cpd_special_rule.cpd_special_rule_mates.build(cpd_rule: self.rule_for(ruleable)) end end self end |
#ended? ⇒ Boolean
134 135 136 |
# File 'app/models/effective/cpd_cycle.rb', line 134 def ended? end_at.present? && end_at < Time.zone.now end |
#rule_for(ruleable) ⇒ Object
Find or build
119 120 121 122 123 124 |
# File 'app/models/effective/cpd_cycle.rb', line 119 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
130 131 132 |
# File 'app/models/effective/cpd_cycle.rb', line 130 def started? start_at_was.present? && Time.zone.now >= start_at_was end |
#targeted_score ⇒ Object
Todo, implement a cpd cycle targeted score entirely
82 83 84 |
# File 'app/models/effective/cpd_cycle.rb', line 82 def targeted_score nil end |
#to_s ⇒ Object
77 78 79 |
# File 'app/models/effective/cpd_cycle.rb', line 77 def to_s title.presence || model_name.human end |