module FormatRestricterRails::Includes::ClassMethods

Define the methods that can be called by host controllers.

Public Instance Methods

register_before_action(options, allowed_formats) click to toggle source

This method is turned into a private method above.

# File lib/format_restricter_rails/includes.rb, line 20
def register_before_action(options, allowed_formats)
  before_action(options) do |_controller|
    # Can't use 'return' here or we'll get a local jump error.
    if request.format.symbol.present?
      head :not_acceptable unless allowed_formats.include?(request.format.symbol)
    end
  end
end
restrict_formats_to(*args) click to toggle source

This is the main API that host controllers will call at the top the controller.

# File lib/format_restricter_rails/includes.rb, line 13
def restrict_formats_to(*args)
  options = args.extract_options!
  allowed_formats = args.collect(&:to_sym)
  register_before_action(options, allowed_formats)
end