Module: Specstar::Controllers::Matchers

Defined in:
lib/specstar/controllers.rb

Instance Method Summary collapse

Instance Method Details

#callback_matches?(callback, condition, actions) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
# File 'lib/specstar/controllers.rb', line 46

def callback_matches?(callback, condition, actions)
  field = "@#{condition}"

  if actions.size > 0
    actions.all? { |a|
      callback.instance_variable_get(field).any? { |item| item.include? "action_name == '#{a}'" }
    }
  else
    callback.instance_variable_get(field).empty?
  end
end

#has_before_action?(controller, action, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/specstar/controllers.rb', line 82

def has_before_action?(controller, action, options={})
    has_before_filter? controller, action, options
end

#has_before_filter?(controller, filter, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/specstar/controllers.rb', line 58

def has_before_filter?(controller, filter, options={})
  result = true

  if options.include?(:only)
    result = controller._process_action_callbacks.any? { |callback|
      callback.kind == :before && callback.filter.to_s == filter.to_s && callback_matches?(callback, :if, options[:only])
    }
  end

  if result && options.include?(:except)
    result = controller._process_action_callbacks.any? { |callback|
      callback.kind == :before && callback.filter.to_s == filter.to_s && callback_matches?(callback, :unless, options[:except])
    }
  end

  if result && options.blank?
    result = controller._process_action_callbacks.any? { |callback|
      callback.kind == :before && callback.filter.to_s == filter.to_s
    }
  end

  result
end

#has_skip_before_filter?(controller, filter, actions = []) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/specstar/controllers.rb', line 38

def has_skip_before_filter?(controller, filter, actions=[])
  if actions.present?
    actions.all? { |action| has_skip_before_filter_for_action?(controller, filter, action) }
  else
    !has_before_filter?(controller, filter)
  end
end

#has_skip_before_filter_for_action?(controller, filter, action) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/specstar/controllers.rb', line 32

def has_skip_before_filter_for_action?(controller, filter, action)
  controller._process_action_callbacks.any? { |callback|
    callback.kind == :before && callback.filter.to_s == filter.to_s && callback.per_key[:unless].any? { |item| item.include? "action_name == '#{action}'" }
  }
end