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.



52
53
54
# File 'lib/inherited_resources/legacy/respond_to.rb', line 52

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.



38
39
40
41
42
# File 'lib/inherited_resources.rb', line 38

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


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/inherited_resources/legacy/respond_to.rb', line 36

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

  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)


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

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



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

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