Module: ControllerAccess::ClassMethods

Includes:
ForceFormat::Errors
Defined in:
lib/force_format/controller_access.rb

Constant Summary collapse

FORCE_FORMAT_TYPES =
[:html, :js, :json, :pdf, :csv, :zip, :xml]
FORCE_FORMAT_DEFAULT_TYPES =
[:html]

Instance Method Summary collapse

Instance Method Details

#force_format_filter(opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/force_format/controller_access.rb', line 11

def force_format_filter(opts={})
  forced_formats = extract_options(opts)

  unsupported = forced_formats - FORCE_FORMAT_TYPES
  raise UnsupportedFormatsError.new("There is no support for #{unsupported} format") if unsupported.any?

  self.send(:before_filter, opts.slice(:only, :except, :if, :unless)) do |controller|
    return if controller.instance_variable_get("@_skip_force_format_filter") == true
    format = controller.request.format
    unless forced_formats.include?(format.try(:to_sym))
      raise ActionController::RoutingError, "Format '#{format}' not supported for #{request.path.inspect}"
    end
  end
end

#skip_force_format_filter(opts = {}) ⇒ Object



26
27
28
29
30
# File 'lib/force_format/controller_access.rb', line 26

def skip_force_format_filter(opts={})
  self.send(:prepend_before_filter, opts.slice(:only, :except, :if, :unless)) do |controller|
    controller.instance_variable_set("@_skip_force_format_filter", true)
  end
end