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

.effective_cpd_cpd_cycle?Boolean

Returns:

  • (Boolean)


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

def self.effective_cpd_cpd_cycle?
  true
end

.latest_cycleObject



72
73
74
# File 'app/models/effective/cpd_cycle.rb', line 72

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

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/effective/cpd_cycle.rb', line 120

def available?
  started? && !ended?
end

#available_dateObject



132
133
134
135
136
137
138
# File 'app/models/effective/cpd_cycle.rb', line 132

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



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
# File 'app/models/effective/cpd_cycle.rb', line 80

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

Returns:

  • (Boolean)


128
129
130
# File 'app/models/effective/cpd_cycle.rb', line 128

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

#rule_for(ruleable) ⇒ Object

Find or build



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

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)


124
125
126
# File 'app/models/effective/cpd_cycle.rb', line 124

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

#to_sObject



76
77
78
# File 'app/models/effective/cpd_cycle.rb', line 76

def to_s
  title.presence || model_name.human
end