Module: Sidekiq::WebCustom::WebAction

Defined in:
lib/sidekiq/web_custom/web_action.rb

Constant Summary collapse

OVERWRITE_VALUE =
'__sidekiq_web_custom_replacement__'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.local_erbsObject



8
9
10
# File 'lib/sidekiq/web_custom/web_action.rb', line 8

def self.local_erbs
  @local_erbs ||= "#{File.expand_path("#{File.dirname(__FILE__)}/..")}/views"
end

Instance Method Details

#erb(content, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/sidekiq/web_custom/web_action.rb', line 12

def erb(content, options = {})
  if content.is_a?(Symbol) && !Sidekiq::WebCustom.local_erb_mapping[content].nil?
    unless respond_to?(:"_erb_#{content}")
      file_name = Sidekiq::WebCustom.local_erb_mapping[content]
      src = ERB.new(src_file_replacement(content)).src
      WebAction.class_eval("def _erb_#{content}\n#{src}\n end", file_name)
    end
  end
  super(content, options)
end

#src_file_replacement(content) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sidekiq/web_custom/web_action.rb', line 23

def src_file_replacement(content)
  file_name = Sidekiq::WebCustom.local_erb_mapping[content]
  contents = File.read(file_name)
  begin
    available_actions = Sidekiq::WebCustom.config.public_send("actions_for_#{content}")
  rescue NoMethodError
    available_actions = []
  end
  actions = available_actions.map do |action, action_path|
    File.read(action_path)
  end.join(" ")
  contents.gsub(OVERWRITE_VALUE, actions)
end