Module: ResourceFull::Dispatch::ClassMethods

Defined in:
lib/resource_full/dispatch.rb

Constant Summary collapse

DEFAULT_FORMATS =
[ :xml, :html, :json ]
CRUD_METHODS_TO_ACTIONS =
{
  :create => [ :create, :new ],
  :read   => [ :show, :index, :count ],
  :update => [ :update, :edit ],
  :delete => [ :destroy ]
}

Instance Method Summary collapse

Instance Method Details

#allowed_actions(format = :html) ⇒ Object

A list of symbols of all allowed controller actions (e.g. :show, :destroy) derived from the allowed CRUD actions.



78
79
80
# File 'lib/resource_full/dispatch.rb', line 78

def allowed_actions(format=:html)
  renderable_formats[format].sum {|crud_action| CRUD_METHODS_TO_ACTIONS[crud_action]}
end

#allowed_formatsObject

A list of symbols of all allowed formats (e.g. :xml, :html)



67
68
69
# File 'lib/resource_full/dispatch.rb', line 67

def allowed_formats
  renderable_formats.keys
end

#allowed_methods(format = :html) ⇒ Object

A list of symbols of all allowed CRUD methods (e.g. :create, :delete)



72
73
74
# File 'lib/resource_full/dispatch.rb', line 72

def allowed_methods(format=:html)
  renderable_formats[format] || []
end

#possible_actionsObject

A list of all possible CRUD actions that this framework understands, which is to say, the core Rails actions plus count (and perhaps others eventually).



84
85
86
# File 'lib/resource_full/dispatch.rb', line 84

def possible_actions
  CRUD_METHODS_TO_ACTIONS.values.sum([])
end

#responds_to(*formats) ⇒ Object

Indicates that the controller responds to one of the requested CRUD (create, read, update, delete) methods. These correspond to controller methods in the following manner:

  • Create: create, new

  • Read: show, index, count

  • Update: update, edit

  • Delete: destroy

By default, a format supports all of the above methods, unless you specify otherwise. Override these defaults by using the :only or :except options. For example,

responds_to :xml, :only => [:create, :delete]

A controller may be reset back to default responds (xml, html, all CRUD methods) by specifying responds_to :defaults.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/resource_full/dispatch.rb', line 39

def responds_to(*formats)
  if formats.first == :defaults
    @renderable_formats = default_responds
    @renderable_formats_overridden = false
    return
  end
  
  opts = formats.extract_options!
  
  supported_crud_methods = if opts[:only]
    [ opts[:only] ].flatten
  elsif opts[:except]
    possible_crud_methods - [ opts[:except] ].flatten
  else
    possible_crud_methods
  end
  
  unless renderable_formats_already_overridden?
    @renderable_formats = {}
    @renderable_formats_overridden = true
  end
  
  formats.each do |format|
    renderable_formats[format] = supported_crud_methods
  end
end

#responds_to_request_action?(request, action) ⇒ Boolean

Returns true if the request action is an allowed action as defined by the allowed CRUD methods.

Returns:

  • (Boolean)


94
95
96
97
98
99
# File 'lib/resource_full/dispatch.rb', line 94

def responds_to_request_action?(request, action)
  # TODO Consider using ActionController's +verify+ method in preference to this.
  # TODO We don't verify custom methods yet, so ignore them.
  return true unless possible_actions.include?(action.to_sym)
  allowed_actions(extract_request_format(request)).include? action.to_sym
end

#responds_to_request_format?(request) ⇒ Boolean

Returns true if the request format is an allowed format.

Returns:

  • (Boolean)


89
90
91
# File 'lib/resource_full/dispatch.rb', line 89

def responds_to_request_format?(request)
  allowed_formats.include? extract_request_format(request)
end