Module: Preact::Controllers::Helpers

Included in:
ActionController::Base
Defined in:
lib/preact/rails/controllers/helpers.rb

Instance Method Summary collapse

Instance Method Details

#inject_javascriptObject



56
57
58
59
60
61
62
# File 'lib/preact/rails/controllers/helpers.rb', line 56

def inject_javascript
  if body_end = response.body.index("</body")
    script = build_script

    response.body = response.body.insert(body_end, script)
  end
end

#preact_autolog_controller_actionObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/preact/rails/controllers/helpers.rb', line 5

def preact_autolog_controller_action

  # only track logged-in users
  return true unless current_user

  # don't autolog if we logged something already on this controller execution
  # this allows you to add better preact logging in your controller and not get duplicate logging
  return true if defined?(@preact_logged_event)
  
  controller = params[:controller]
  action = params[:action]

  return true unless controller && action

  return true unless Preact.configuration.autolog_should_log?(controller, action)

  event_name = "#{controller}##{action}"

  note = self.guess_target_item_name(controller)

  event = {
    :name => event_name,
    :target_id => params[:id],
    :medium => "autolog-rails-v1.0",
    :note => note,
    :extras => {
      :_ip => request.remote_ip,
      :_url => request.url,
      :_ua => request.env['HTTP_USER_AGENT']
    }
  }

  preact_log(event)

  true
rescue => ex
  Preact.logger.warn "[Preact] Autolog failed: #{ex.message}"
  true # always returns strue no matter what...
end

#preact_log(event, account = nil) ⇒ Object

helper method on the controller to make logging events easy



46
47
48
49
50
51
52
53
54
# File 'lib/preact/rails/controllers/helpers.rb', line 46

def preact_log(event, =nil)
  user = Preact.configuration.get_current_user(self) # handle nil
   ||= Preact.configuration.(self) # handle nil

  Preact.log_event(user, event, )

  # make a note that we've logged an event on this controller
  @preact_logged_event = event
end