Module: ResourceController::ClassMethods

Defined in:
lib/resource_controller/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#actions(*opts) ⇒ Object

Use this method in your controller to specify which actions you’d like it to respond to.

class PostsController < ResourceController::Base
  actions :all, :except => :create
end


9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/resource_controller/class_methods.rb', line 9

def actions(*opts)
  config = {}
  config.merge!(opts.pop) if opts.last.is_a?(Hash)

  all_actions = (singleton? ? ResourceController::SINGLETON_ACTIONS : ResourceController::ACTIONS) - [:new_action] + [:new]
  
  actions_to_remove = []
  actions_to_remove += all_actions - opts unless opts.first == :all                
  actions_to_remove += [*config[:except]] if config[:except]
  actions_to_remove.uniq!

  actions_to_remove.each { |action| undef_method(action) if method_defined?(action) }
end

#defined_actionsObject



23
24
25
# File 'lib/resource_controller/class_methods.rb', line 23

def defined_actions
  ACTIONS.select { |a| method_defined?(a) }
end

#responds_to(*args) ⇒ Object



27
28
29
30
# File 'lib/resource_controller/class_methods.rb', line 27

def responds_to(*args)
  options = args.extract_options!      
  (defined_actions - [:edit]).each { |action| setup_action_response_for_format(action, *args) }
end

#setup_action_response_for_format(action, *formats) ⇒ Object



59
60
61
62
63
# File 'lib/resource_controller/class_methods.rb', line 59

def setup_action_response_for_format(action, *formats)
  formats.each do |format|
    send("setup_#{action}_response_for_format", format)
  end
end

#setup_create_response_for_format(format) ⇒ Object



32
33
34
35
# File 'lib/resource_controller/class_methods.rb', line 32

def setup_create_response_for_format(format)
  create.wants.send(format) { render format => object, :status => :created, :location => object_url }
  create.fails.wants.send(format) { render format => object.errors, :status => :unprocessable_entity }
end

#setup_destroy_response_for_format(format) ⇒ Object



54
55
56
57
# File 'lib/resource_controller/class_methods.rb', line 54

def setup_destroy_response_for_format(format)
  destroy.wants.send(format) { render :nothing => true, :status => :ok }
  destroy.fails.wants.send(format) { render :nothing => true, :status => :not_found }
end

#setup_index_response_for_format(format) ⇒ Object



37
38
39
# File 'lib/resource_controller/class_methods.rb', line 37

def setup_index_response_for_format(format)
  index.wants.send(format) { render format => collection, :status => :ok }
end

#setup_new_response_for_format(format) ⇒ Object



50
51
52
# File 'lib/resource_controller/class_methods.rb', line 50

def setup_new_response_for_format(format)
  new_action.wants.send(format) { render format => object, :status => :ok }
end

#setup_show_response_for_format(format) ⇒ Object



41
42
43
# File 'lib/resource_controller/class_methods.rb', line 41

def setup_show_response_for_format(format)
  show.wants.send(format) { render format => object, :status => :ok }
end

#setup_update_response_for_format(format) ⇒ Object



45
46
47
48
# File 'lib/resource_controller/class_methods.rb', line 45

def setup_update_response_for_format(format)
  update.wants.send(format) { render format => object, :status => :ok }
  update.fails.wants.send(format) { render format => object.errors, :status => :unprocessable_entity }
end