Module: Resourceful::Default::Responses

Included in:
Base
Defined in:
lib/resourceful/default/responses.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

This method is automatically run when this module is included in Resourceful::Base. It sets up the default responses for the default actions.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/resourceful/default/responses.rb', line 53

def self.included(base)
  base.made_resourceful do
    response_for(:show, :index, :edit, :new) do |format|
      format.html
      format.js
    end

    response_for(:show_fails) do |format|
      not_found = Proc.new { render :text => I18n.t('make_resourceful.show.fails', :default => "No item found"), :status => 404 }
      format.html &not_found
      format.js &not_found
      format.xml &not_found
    end

    response_for(:create) do |format|
      format.html do
        set_default_flash :notice, I18n.t('make_resourceful.create.success', :default => "Create successful!")
        set_default_redirect object_path
      end
      format.js
    end
    
    response_for(:create_fails) do |format|
      format.html do
        set_default_flash :error, I18n.t('make_resourceful.create.fails', :default => "There was a problem!")
        render :action => :new, :status => 422
      end
      format.js
    end
  
    response_for(:update) do |format|
      format.html do
        set_default_flash :notice, I18n.t('make_resourceful.update.success', :default => "Save successful!")
        set_default_redirect object_path
      end
      format.js
    end
    
    response_for(:update_fails) do |format|
      format.html do
        set_default_flash :error, I18n.t('make_resourceful.update.fails', :default => "There was a problem saving!")
        render :action => :edit, :status => 422
      end
      format.js
    end
    
    response_for(:destroy) do |format|
      format.html do
        set_default_flash :notice, I18n.t('make_resourceful.destroy.success', :default => "Record deleted!")
        set_default_redirect objects_path
      end
      format.js
    end
    
    response_for(:destroy_fails) do |format|
      format.html do
        set_default_flash :error, I18n.t('make_resourceful.destroy.fails', :default => "There was a problem deleting!")
        set_default_redirect :back, :status => :failure
      end
      format.js
    end
  end
end

Instance Method Details

#set_default_flash(type, message) ⇒ Object

Sets the default flash message. This message can be overridden by passing in an HTTP parameter of the form "_flash" via POST or GET.

You can use this to easily have multiple forms post to the same create/edit/destroy actions but display different flash notices - without modifying the controller code at all.

By default, the flash types are notice when the database operation completes successfully and error when it fails.

-- TODO: Move this out of here ++



19
20
21
# File 'lib/resourceful/default/responses.rb', line 19

def set_default_flash(type, message)
  flash[type] ||= (params[:_flash] && params[:_flash][type]) || message
end

#set_default_redirect(to, options = {}) ⇒ Object

Sets the default redirect (the argument passed to redirect_to). This message can be overridden by passing in an HTTP parameter of the form "_redirect_on" via POST or GET.

You can use this to easily have multiple forms post to the same create/edit/destroy actions but redirect to different URLs - without modifying the controller code at all.

By default, the redirect statuses are success when the database operation completes successfully and failure when it fails. Use the :status option to specify which status to run the redirect for. For example:

set_default_redirect "/posts", :status => :failure

This will run redirect_to params[:_redirect_on][:failure] if the parameter exists, or redirect_to "/posts" otherwise.

-- TODO: Move this out of here ++



46
47
48
49
# File 'lib/resourceful/default/responses.rb', line 46

def set_default_redirect(to, options = {})
  status = options[:status] || :success
  redirect_to (params[:_redirect_on] && params[:_redirect_on][status]) || to
end