Class: ActionController::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/inherited_resources/legacy/respond_to.rb,
lib/inherited_resources.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formatsObject

Returns the value of attribute formats.



3
4
5
# File 'lib/inherited_resources/legacy/respond_to.rb', line 3

def formats
  @formats
end

Class Method Details

.clear_respond_toObject

Clear all mimes in respond_to.



42
43
44
# File 'lib/inherited_resources/legacy/respond_to.rb', line 42

def self.clear_respond_to
  write_inheritable_attribute(:mimes_for_respond_to, ActiveSupport::OrderedHash.new)
end

.inherit_resourcesObject

If you cannot inherit from InheritedResources::Base you can call inherit_resource in your controller to have all the required modules and funcionality included.



18
19
20
21
22
# File 'lib/inherited_resources.rb', line 18

def self.inherit_resources
  InheritedResources::Base.inherit_resources(self)
  initialize_resources_class_accessors!
  create_resources_url_helpers!
end

.respond_to(*mimes) ⇒ Object

Defines mimes that are rendered by default when invoking respond_with.

Examples:

respond_to :html, :xml, :json

All actions on your controller will respond to :html, :xml and :json.

But if you want to specify it based on your actions, you can use only and except:

respond_to :html
respond_to :xml, :json, :except => [ :edit ]

The definition above explicits that all actions respond to :html. And all actions except :edit respond to :xml and :json.

You can specify also only parameters:

respond_to :rjs, :only => :create


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/inherited_resources/legacy/respond_to.rb', line 26

def self.respond_to(*mimes)
  options = mimes.extract_options!

  only_actions   = Array(options.delete(:only))
  except_actions = Array(options.delete(:except))

  mimes.each do |mime|
    mime = mime.to_sym
    mimes_for_respond_to[mime]          = {}
    mimes_for_respond_to[mime][:only]   = only_actions   unless only_actions.empty?
    mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty?
  end
end

Instance Method Details

#respond_to(*mimes, &block) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
# File 'lib/inherited_resources/legacy/respond_to.rb', line 54

def respond_to(*mimes, &block)
  raise ArgumentError, "respond_to takes either types or a block, never both" if mimes.any? && block_given?
  if response = retrieve_response_from_mimes(mimes, &block)
    response.call
  end
end

#respond_with(*resources, &block) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/inherited_resources/legacy/respond_to.rb', line 61

def respond_with(*resources, &block)
  if response = retrieve_response_from_mimes([], &block)
    options = resources.extract_options!
    options.merge!(:default_response => response)
    (options.delete(:responder) || responder).call(self, resources, options)
  end
end

#responderObject



69
70
71
# File 'lib/inherited_resources/legacy/respond_to.rb', line 69

def responder
  ActionController::Responder
end