Module: Ajax::Helpers::RequestHelper

Defined in:
lib/ajax/helpers/request_helper.rb

Constant Summary collapse

DEEP_MERGE =

Recursive merge values

lambda do |key, v1, v2|
  if v1.is_a?(Hash) && v2.is_a?(Hash)
    v1.merge(v2, &DEEP_MERGE)
  elsif v1.is_a?(Array) && v2.is_a?(Array)
    v1.concat(v2)
  else
    [v1, v2].compact.first
  end
end

Instance Method Summary collapse

Instance Method Details

#exclude_path?(path) ⇒ Boolean

Return a boolean indicating whether or not to exclude a path from the AJAX redirect.

Returns:

  • (Boolean)


92
93
94
95
96
# File 'lib/ajax/helpers/request_helper.rb', line 92

def exclude_path?(path)
  !!((@exclude_paths || []).find do |excluded|
    !!excluded.match(path)
  end)
end

#exclude_paths(paths = nil, expand = true) ⇒ Object

Set one or more paths that can be accessed directly without the AJAX framework.

Useful for excluding pages with HTTPS content on them from being loaded via AJAX.

paths a list of String or Regexp instances that are matched against each REQUEST_PATH.

The string and regex paths are modified to match full URLs by prepending them with the appropriate regular expression.



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ajax/helpers/request_helper.rb', line 71

def exclude_paths(paths=nil, expand = true)
  if !instance_variable_defined?(:@exclude_paths)
    @exclude_paths = []
  end
  (paths || []).each do |path|
    if expand
      @exclude_paths << /^(\w+\:\/\/[^\/]+\/?)?#{path.to_s}$/
    else
      @exclude_paths << path
    end
  end
  @exclude_paths
end

#exclude_regex(exclude_regex = nil) ⇒ Object

Directly set regexes for one or more paths that can be accessed directly without the AJAX framework.



86
87
88
# File 'lib/ajax/helpers/request_helper.rb', line 86

def exclude_regex(exclude_regex=nil)
  exclude_paths(exclude_regex, false)
end

#get_header(object, key) ⇒ Object

Return the value at key key from the Ajax-Info header in object.

object can be a Hash or instance of ActionController::Request key Symbol or String hash key, converted to String



56
57
58
59
# File 'lib/ajax/helpers/request_helper.rb', line 56

def get_header(object, key)
  headers = object.is_a?(Hash) ? object : object.headers # ::ActionController::Request
  unserialize_hash(headers["Ajax-Info"])[key.to_s]
end

#serialize_hash(hash) {|info| ... } ⇒ Object

Return JSON given a Hash or JSON string. If a block is given, yields the Hash to the block so that the block can modify it before it is converted to JSON.

Yields:

  • (info)


101
102
103
104
105
# File 'lib/ajax/helpers/request_helper.rb', line 101

def serialize_hash(hash, &block)
  info = unserialize_hash(hash)
  yield info if block_given?
  info.to_json
end

#set_header(object, key, value) ⇒ Object

Set the value at key in the Ajax-Info header in object.

object can be a Hash or instance of ActionController::Request key Symbol or String hash key, converted to String value any value that con be converted to JSON

All Hash and Array values are deep-merged. Hash keys are converted to Strings.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ajax/helpers/request_helper.rb', line 26

def set_header(object, key, value)
  headers = object.is_a?(Hash) ? object : object.headers # ::ActionController::Response
  key = key.to_s

  headers["Ajax-Info"] = serialize_hash(headers["Ajax-Info"]) do |info|
    # Deep merge hashes
    if info.has_key?(key) &&
        value.is_a?(Hash) &&
        info[key].is_a?(Hash)
      value = value.stringify_keys!
      value = info[key].merge(value, &DEEP_MERGE)
    end

    # Concat arrays
    if info.has_key?(key) &&
        value.is_a?(Array) &&
        info[key].is_a?(Array)
      value = info[key].concat(value)
    end

    # Set the value for this key
    info[key] = value
  end
end

#unserialize_hash(hash) ⇒ Object

Return a Hash given JSON or a Hash.



108
109
110
111
112
113
114
115
116
117
# File 'lib/ajax/helpers/request_helper.rb', line 108

def unserialize_hash(hash)
  case hash
  when String
    JSON.parse(hash) rescue {}
  when Hash
    hash
  else
    {}
  end
end