Module: ActionInterceptor::Controller

Defined in:
lib/action_interceptor/controller.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
# File 'lib/action_interceptor/controller.rb', line 7

def self.included(base)
  base.class_attribute :is_interceptor, :interceptor_filters
  base.is_interceptor = false
  base.interceptor_filters = {}
  base.extend(ClassMethods)
end

Instance Method Details

#current_page?(url) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/action_interceptor/controller.rb', line 18

def current_page?(url)
  # Blank is the current page
  url.blank? || URI(url).path == request.path
end

#current_urlObject



14
15
16
# File 'lib/action_interceptor/controller.rb', line 14

def current_url
  "#{request.protocol}#{request.host_with_port}#{request.fullpath}"
end

#current_url_hashObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/action_interceptor/controller.rb', line 23

def current_url_hash
  return @current_url_hash if @current_url_hash

  key = ActionInterceptor.intercepted_url_key

  # Can't redirect back to non-get
  # Also, can't call root_url here, so use '/' instead
  url = Encryptor.encrypt_and_sign(request.get? ? current_url : '/')
  @current_url_hash = {key => url}
end

#with_interceptor(&block) ⇒ Object

Executes the given block as if it was an interceptor



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_interceptor/controller.rb', line 35

def with_interceptor(&block)
  @previous_default_url_options ||= default_url_options

  begin
    # Send the referer with intercepted requests
    # So we don't rely on the user's browser to do it for us
    self.default_url_options = @previous_default_url_options
                                 .merge(current_url_hash)

    # Execute the block as if it was defined in this controller
    instance_exec &block
  rescue LocalJumpError => e
    # Silently ignore `return` errors in the block
    # and return the given value
    e.exit_value
  ensure
    self.default_url_options = @previous_default_url_options
  end
end