Class: Improves::ChangePlaybook

Inherits:
ImproveHunter show all
Defined in:
app/services/improves/change_playbook.rb

Overview

Used to apply an Improvements::ChangePlaybook to a hunter if the hunter is a valid target

Instance Method Summary collapse

Methods inherited from ImproveHunter

#check, for, #hunter_not_advanced?, #hunter_playbook_mismatch?

Constructor Details

#initialize(hunters_improvement) ⇒ ChangePlaybook

Returns a new instance of ChangePlaybook.


7
8
9
10
# File 'app/services/improves/change_playbook.rb', line 7

def initialize(hunters_improvement)
  super
  playbook
end

Instance Method Details

#improveObject


12
13
14
15
# File 'app/services/improves/change_playbook.rb', line 12

def improve
  return false unless valid?
  @hunter.update(playbook: @playbook)
end

#playbookObject


28
29
30
31
32
33
34
# File 'app/services/improves/change_playbook.rb', line 28

def playbook
  @playbook ||= Playbook.find(
    @hunters_improvement.improvable&.dig('playbook', 'id')
  )
rescue ActiveRecord::RecordNotFound => e
  @hunters_improvement.errors.add(:improvable, e.message)
end

#same_playbooks?Boolean

Returns:

  • (Boolean)

24
25
26
# File 'app/services/improves/change_playbook.rb', line 24

def same_playbooks?
  @hunter.playbook == @playbook
end

#valid?Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
# File 'app/services/improves/change_playbook.rb', line 17

def valid?
  super
  check :same_playbooks?, :hunter,
        "already is playbook #{@playbook.name}"
  @hunters_improvement.errors.none?
end