Module: TypicalSituation::Responses
- Included in:
- TypicalSituation
- Defined in:
- lib/typical_situation/responses.rb
Overview
Rails MIME responses.
Instance Method Summary collapse
- #after_resource_created_path(resource) ⇒ Object
- #after_resource_destroyed_path(_resource) ⇒ Object
- #after_resource_updated_path(resource) ⇒ Object
-
#changed_so_redirect ⇒ Object
HTML response when @resource saved or updated.
-
#gone_so_redirect ⇒ Object
HTML response when @resource deleted.
- #respond_as_changed ⇒ Object
- #respond_as_created ⇒ Object
- #respond_as_error ⇒ Object
- #respond_as_forbidden ⇒ Object
- #respond_as_gone ⇒ Object
- #respond_as_not_found ⇒ Object
-
#respond_with_resource ⇒ Object
Return the resource as HTML or JSON.
-
#respond_with_resources ⇒ Object
Return the collection as HTML or JSON.
Instance Method Details
#after_resource_created_path(resource) ⇒ Object
135 136 137 |
# File 'lib/typical_situation/responses.rb', line 135 def after_resource_created_path(resource) {action: :show, id: resource.id} end |
#after_resource_destroyed_path(_resource) ⇒ Object
143 144 145 |
# File 'lib/typical_situation/responses.rb', line 143 def after_resource_destroyed_path(_resource) {action: :index} end |
#after_resource_updated_path(resource) ⇒ Object
139 140 141 |
# File 'lib/typical_situation/responses.rb', line 139 def after_resource_updated_path(resource) {action: :show, id: resource.id} end |
#changed_so_redirect ⇒ Object
HTML response when @resource saved or updated.
148 149 150 151 |
# File 'lib/typical_situation/responses.rb', line 148 def changed_so_redirect redirect_to after_resource_updated_path(@resource) true end |
#gone_so_redirect ⇒ Object
HTML response when @resource deleted.
154 155 156 157 158 159 |
# File 'lib/typical_situation/responses.rb', line 154 def gone_so_redirect set_error_flash if has_errors? set_success_flash(:destroy) unless has_errors? redirect_to after_resource_destroyed_path(@resource) true end |
#respond_as_changed ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/typical_situation/responses.rb', line 40 def respond_as_changed if has_errors? respond_as_error else respond_to do |format| yield(format) if block_given? format.html do set_success_flash(:update) set_single_instance changed_so_redirect || render end format.json do render json: serialize_resource(@resource) end end end end |
#respond_as_created ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/typical_situation/responses.rb', line 59 def respond_as_created if has_errors? respond_as_error else respond_to do |format| yield(format) if block_given? format.html do set_success_flash(:create) set_single_instance changed_so_redirect || render end format.json do render json: serialize_resource(@resource), location: location_url, status: :created end end end end |
#respond_as_error ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/typical_situation/responses.rb', line 80 def respond_as_error respond_to do |format| yield(format) if block_given? format.html do set_single_instance render action: (@resource.new_record? ? :new : :edit), status: :unprocessable_entity end format.json do render json: serialize_resource(@resource, methods: [:errors]), status: :unprocessable_entity end end end |
#respond_as_forbidden ⇒ Object
128 129 130 131 132 133 |
# File 'lib/typical_situation/responses.rb', line 128 def respond_as_forbidden respond_to do |format| format.html { render plain: "Forbidden", status: :forbidden } format.json { head :forbidden } end end |
#respond_as_gone ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/typical_situation/responses.rb', line 96 def respond_as_gone if has_errors? set_error_flash respond_as_error else respond_to do |format| yield(format) if block_given? format.html do set_single_instance gone_so_redirect || render end format.json do head :no_content end end end end |
#respond_as_not_found ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/typical_situation/responses.rb', line 115 def respond_as_not_found respond_to do |format| yield(format) if block_given? format.html do raise ActionController::RoutingError, "Not Found" end format.json do head :not_found end end end |
#respond_with_resource ⇒ Object
Return the resource as HTML or JSON
A provided block is passed the #respond_to format to further define responses.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/typical_situation/responses.rb', line 26 def respond_with_resource respond_to do |format| yield(format) if block_given? format.html do set_single_instance render end format.json do render json: serialize_resource(@resource) end end end |
#respond_with_resources ⇒ Object
Return the collection as HTML or JSON
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/typical_situation/responses.rb', line 8 def respond_with_resources respond_to do |format| yield(format) if block_given? format.html do set_collection_instance render end format.json do render json: serialize_resources(@resources) end end end |