This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from omu/develop
Merge develop into master
- Loading branch information
Showing
22 changed files
with
129 additions
and
612 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
module ReferenceResource | ||
extend ActiveSupport::Concern | ||
include Pagy::Backend | ||
|
||
# rubocop:disable Metrics/BlockLength | ||
# rubocop:disable Rails/LexicallyScopedActionFilter | ||
included do | ||
before_action :set_variables | ||
before_action :set_resource, only: %i[edit update destroy] | ||
|
||
def index | ||
@pagy, value = pagy(@model_name.all) | ||
instance_variable_set("@#{controller_name}", value) | ||
end | ||
|
||
def new | ||
instance_variable_set(@singular_variable.to_sym, @model_name.new) | ||
end | ||
|
||
def create | ||
instance_variable_set(@singular_variable.to_sym, @model_name.new(secure_params)) | ||
instance_variable_get(@singular_variable).save ? redirect_with('success') : render(:new) | ||
end | ||
|
||
def update | ||
if instance_variable_get(@singular_variable).update(secure_params) | ||
flash[:notice] = t('.success') | ||
redirect_to action: :index | ||
else | ||
render(:edit) | ||
end | ||
end | ||
|
||
def destroy | ||
instance_variable_get(@singular_variable).destroy ? redirect_with('success') : redirect_with('warning') | ||
end | ||
|
||
private | ||
|
||
def set_variables | ||
@singular_variable = "@#{controller_name.singularize}" | ||
@model_name = controller_name.classify.constantize | ||
end | ||
|
||
def set_resource | ||
instance_variable_set(@singular_variable.to_sym, @model_name.find(params[:id])) | ||
end | ||
|
||
def redirect_with(message) | ||
redirect_to(send("#{controller_name}_path"), notice: t(".#{message}")) | ||
end | ||
end | ||
# rubocop:enable Metrics/BlockLength | ||
# rubocop:enable Rails/LexicallyScopedActionFilter | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.