Skip to content

Commit

Permalink
Merge pull request eurucamp#65 from isleofruby/feature/responders
Browse files Browse the repository at this point in the history
Responders update
  • Loading branch information
myabc authored Sep 6, 2018
2 parents 65e02e5 + 6df97f5 commit def7062
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 50 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ gem 'neat', '~> 2.1.0'
gem 'pundit'
gem 'rails_html_helpers'
gem 'redcarpet'
gem 'responders', '~> 2.4.0'
gem 'sass-rails', '~> 5.0.6'
gem 'sprockets-rails', '~> 3.2.0'
gem 'turbolinks'
Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ DEPENDENCIES
rails-controller-testing
rails_html_helpers
redcarpet
responders (~> 2.4.0)
rspec-activemodel-mocks
rspec-its
rspec-rails (~> 3.3)
Expand Down
18 changes: 7 additions & 11 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ActivitiesController < ApiController
class ActivitiesController < ApplicationController
respond_to :html

skip_before_action :authenticate_user!, only: %i[index show]
Expand Down Expand Up @@ -27,26 +27,22 @@ def edit
def create
@activity = current_event.new_activity(current_user, sanitized_params)
authorize @activity
type = @activity.save ? :notice : :error
flash[type] = I18n.t("new_activity.#{type}")
@activity.save
respond_with(@activity, location: activities_path)
end

def update
if @activity
type = @activity.update(sanitized_params) ? :notice : :error
flash[type] = I18n.t("edit_activity.#{type}")
end
@activity.update(sanitized_params)
respond_with(@activity, location: edit_activity_path(@activity))
end

def destroy
if @activity && params[:confirm_delete] && @activity.destroy
redirect_to root_path, notice: I18n.t('destroy_activity.notice')
if params[:confirm_delete]
@activity.destroy
else
flash[:error] = I18n.t('destroy_activity.error')
render :edit
@activity.errors.add(:base, :invalid)
end
respond_with(@activity, location: root_path, action: :edit)
end

private
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/api_controller.rb

This file was deleted.

5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
require 'application_responder'

class ApplicationController < ActionController::Base
self.responder = ApplicationResponder
respond_to :html

include Pundit

# Prevent CSRF attacks by raising an exception.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/participations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ParticipationsController < ApiController
class ParticipationsController < ApplicationController
respond_to :js, :html

def create
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

module Activities
class Application < Rails::Application
# Use the responders controller from the responders gem
config.app_generators.scaffold_controller :responders_controller

# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

Expand Down
20 changes: 12 additions & 8 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ en:
new: Save
edit: Update

flash:
activities:
create:
notice: Your activity has been created!
alert: Activity could not be saved. Please check your input.
edit:
notice: Your activity has been updated!
alert: Your changes could not be saved. Please check your input.
destroy:
notice: Your activity has been deleted!
alert: Your activity could not be deleted. Did you check the confirmation?

user_nav:
back_home:
label: Back to Isle of Ruby activities
Expand Down Expand Up @@ -102,17 +114,9 @@ en:

new_activity:
title: Organise an activity
notice: Your activity has been created!
error: Activity could not be saved. Please check your input.

edit_activity:
title: Edit your activity
notice: Your activity has been updated!
error: Your changes could not be saved. Please check your input.

destroy_activity:
notice: Your activity has been deleted!
error: Your activity could not be deleted. Did you check the confirmation?

activity_form:
markdown_hint: You can use Markdown here
Expand Down
12 changes: 12 additions & 0 deletions config/locales/responders.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
en:
flash:
actions:
create:
notice: '%{resource_name} was successfully created.'
# alert: '%{resource_name} could not be created.'
update:
notice: '%{resource_name} was successfully updated.'
# alert: '%{resource_name} could not be updated.'
destroy:
notice: '%{resource_name} was successfully destroyed.'
alert: '%{resource_name} could not be destroyed.'
25 changes: 0 additions & 25 deletions lib/api_responder.rb

This file was deleted.

8 changes: 8 additions & 0 deletions lib/application_responder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ApplicationResponder < ActionController::Responder
include Responders::FlashResponder
include Responders::HttpCacheResponder

# Redirects resources to the collection path (index action) instead
# of the resource path (show action) for POST/PUT/DELETE requests.
# include Responders::CollectionResponder
end

0 comments on commit def7062

Please sign in to comment.