Class: RamenRails::RamenAfterFilter

Inherits:
Object
  • Object
show all
Includes:
ScriptTagHelper
Defined in:
lib/ramen-rails/ramen_after_filter.rb

Defined Under Namespace

Classes: InvalidUserObject

Constant Summary collapse

CLOSING_BODY_TAG =
%r{</body>}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kontroller) ⇒ RamenAfterFilter

Returns a new instance of RamenAfterFilter.



32
33
34
# File 'lib/ramen-rails/ramen_after_filter.rb', line 32

def initialize(kontroller)
  @controller = kontroller 
end

Instance Attribute Details

#controllerObject (readonly)

Returns the value of attribute controller.



30
31
32
# File 'lib/ramen-rails/ramen_after_filter.rb', line 30

def controller
  @controller
end

Class Method Details

.filter(controller) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/ramen-rails/ramen_after_filter.rb', line 20

def self.filter(controller)
  auto_include_filter = new(controller)
  
  if auto_include_filter.include_javascript?
    auto_include_filter.include_javascript!
  elsif auto_include_filter.include_disabled_comment?
    auto_include_filter.include_disabled_comment!
  end
end

Instance Method Details

#disabled_environment?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ramen-rails/ramen_after_filter.rb', line 54

def disabled_environment?
  !enabled_environment?
end

#enabled_environment?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/ramen-rails/ramen_after_filter.rb', line 48

def enabled_environment?
  return true unless RamenRails.config.enabled_environments

  return RamenRails.config.enabled_environments.map(&:to_s).include?(Rails.env.to_s)
end

#include_disabled_comment!Object



40
41
42
# File 'lib/ramen-rails/ramen_after_filter.rb', line 40

def include_disabled_comment! 
  response.body = response.body.gsub(CLOSING_BODY_TAG, "<!-- Ramen not enabled for environment `#{Rails.env}`. Enabled for: #{RamenRails.config.enabled_environments.inspect} -->" + '\\0')
end

#include_disabled_comment?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/ramen-rails/ramen_after_filter.rb', line 58

def include_disabled_comment?
  disabled_environment? &&
    html_content_type? &&
    response_has_closing_body_tag?
end

#include_javascript!Object



44
45
46
# File 'lib/ramen-rails/ramen_after_filter.rb', line 44

def include_javascript! 
  response.body = response.body.gsub(CLOSING_BODY_TAG, ramen_script_tag + '\\0')
end

#include_javascript?Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
# File 'lib/ramen-rails/ramen_after_filter.rb', line 64

def include_javascript?
  enabled_environment? &&
    ramen_user_object.present? &&
    ramen_org_id.present? &&
    html_content_type? &&
    !ramen_script_tag_called_manually? &&
    response_has_closing_body_tag?
end

#ramen_script_tag_optionsObject



36
37
38
# File 'lib/ramen-rails/ramen_after_filter.rb', line 36

def ramen_script_tag_options
  controller.ramen_script_tag_options
end