Class: HocusPocus::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/hocus_pocus/filter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Filter

Returns a new instance of Filter.



23
24
25
26
27
# File 'lib/hocus_pocus/filter.rb', line 23

def initialize(controller)
  @controller = controller
  @template = controller.instance_variable_get(:@template)
  @body = controller.response.body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



3
4
5
# File 'lib/hocus_pocus/filter.rb', line 3

def body
  @body
end

Class Method Details

.after(controller) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hocus_pocus/filter.rb', line 10

def self.after(controller)
  if controller.request.format.html?
    filter = self.new(controller)
    filter.add_steak_recorder if HocusPocus.config.enable_scenario_recorder
#         filter.add_js
    unless controller.is_a?(HocusPocus::EditorController) || controller.is_a?(HocusPocus::GeneratorController)
      filter.add_buttons
    end
    filter.add_command_line if HocusPocus.config.enable_command_line
    controller.response.body = filter.body
  end
end

.before(controller) ⇒ Object



5
6
7
8
# File 'lib/hocus_pocus/filter.rb', line 5

def self.before(controller)
  controller.flash[HocusPocus::SPEC] = controller.params[HocusPocus::SPEC] if controller.params[HocusPocus::SPEC]
  controller.flash.keep(HocusPocus::SPEC)
end

Instance Method Details

#add_buttonsObject



55
56
57
58
# File 'lib/hocus_pocus/filter.rb', line 55

def add_buttons
  insert_text :before, /<\/body>/i, %Q[<div id="#{HocusPocus::CONTAINER}" style="position:absolute; top:0; right: 0; font-size: small;">#{[edit_link, spec_link].compact.join(' | ')}<br>#{partials}<br>#{spec}</div>]
  Thread.current[HocusPocus::VIEW_FILENAMES] = nil
end

#add_command_lineObject



60
61
62
# File 'lib/hocus_pocus/filter.rb', line 60

def add_command_line
  insert_text :before, /<\/body>/i, %Q[<div style="position:absolute; bottom:0;"><form method="post" action="/generator/execute" data-remote="true"><input type="text" name="command" placeholder="Command?" style="width: 512px;" /><input type="submit" name="run" /></form></div>]
end

#add_jsObject



51
52
53
# File 'lib/hocus_pocus/filter.rb', line 51

def add_js
#       insert_text :before, /<\/body>/i, %Q[<script type="text/javascript">alert('hoge!');</script>]
end

#add_steak_recorderObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/hocus_pocus/filter.rb', line 29

def add_steak_recorder
  #TODO exclude EDITOR and GENERATOR
  insert_text :before, /<\/body>/i, <<-TEPPAN
<script type="text/javascript">
  $('form').live('submit', function() {
  var scenario = ["  scenario '#{@controller.action_name.humanize} #{@controller.controller_name.humanize}' do"];
  scenario.push("    visit '" + window.location.href + "'");
  $(this).find('input[type=text],input[type=email],input[type=url],input[type=number],input[type=search],textarea').each(function() {
scenario.push("    fill_in '" + $(this).attr('id') + "', :with => '" + $(this).val() + "'");
  });
  $(this).find('select').each(function() {
scenario.push("    select '" + $(this).val() + "', :from => '" + $(this).attr('id') + "'");
  });
  scenario.push("    click_button '" + $(this).find('input[type=submit]').val() + "'");
  $(this).append('<textarea id="#{HocusPocus::SPEC}" name="#{HocusPocus::SPEC}" style="height: 0px; width: 0px;" />');
  $('##{HocusPocus::SPEC}').val(scenario.join('\\n'));
})
</script>
TEPPAN
end