Class: HotFlash::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/hot_flash/action.rb

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Action

Returns a new instance of Action.



3
4
5
6
7
8
9
# File 'lib/hot_flash/action.rb', line 3

def initialize(controller)
  @controller = controller
  @enabled = false
  @turbo_action = HotFlash.config.turbo_action
  @turbo_target = HotFlash.config.turbo_target
  @flash_method = HotFlash.config.flash_method
end

Instance Method Details

#contentObject



49
50
51
52
53
54
55
56
57
# File 'lib/hot_flash/action.rb', line 49

def content
  if @controller.respond_to?(@flash_method)
    return @controller.send(@flash_method)
  elsif @controller.helpers.respond_to?(@flash_method)
    return @controller.helpers.send(@flash_method)
  else
    raise NoMethodError, "#{@controller.class.name} doesn't respond to #{@flash_method}"
  end
end

#disable!Object



15
16
17
# File 'lib/hot_flash/action.rb', line 15

def disable!
  @enabled = false
end

#enable!Object



11
12
13
# File 'lib/hot_flash/action.rb', line 11

def enable!
  @enabled = true
end

#enabled?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/hot_flash/action.rb', line 19

def enabled?
  @enabled
end

#renderObject



23
24
25
# File 'lib/hot_flash/action.rb', line 23

def render
  @controller.view_context.turbo_stream_action_tag(turbo_action, targets: turbo_target, template: content)
end

#render?(render_args) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/hot_flash/action.rb', line 42

def render?(render_args)
  return false unless enabled?
  return false unless render_args[0] && render_args[0][:turbo_stream]

  true
end

#render_into(render_args) ⇒ Object



35
36
37
38
39
40
# File 'lib/hot_flash/action.rb', line 35

def render_into(render_args)
  return render_args unless render?(render_args)

  render_args[0][:turbo_stream] << render
  render_args
end

#turbo_actionObject



27
28
29
# File 'lib/hot_flash/action.rb', line 27

def turbo_action
  @turbo_action
end

#turbo_targetObject



31
32
33
# File 'lib/hot_flash/action.rb', line 31

def turbo_target
  @turbo_target
end