Created
March 13, 2019 09:00
-
-
Save itssomething/23dc68d791a2c0ea296f048eb370ed95 to your computer and use it in GitHub Desktop.
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
class PlansController < ApplicationController | |
before_action :init_plan_service | |
before_action :find_plan, only: %i(edit update) | |
# after_action :check_plan, only: %i(find_plan) | |
def index | |
@plans = plan_service.filter_plan_list | |
.page(params[:page]) | |
.per Settings.pagination.list | |
end | |
def new | |
@plan = plan_service.new_plan | |
plan.attachments.build | |
end | |
def create | |
@plan = plan_service.create_resource | |
plan.errors.blank? ? plan_create_success : plan_create_fail | |
end | |
def show | |
@plan = Plan.without_deleted | |
.plan_viewable(current_user) | |
.find_by id: params[:id] | |
check_plan | |
end | |
def edit | |
plan.attachments.build unless get_attachments.any? | |
end | |
def update | |
if plan_service.update_resource | |
flash[:success] = t "successful_update" | |
redirect_to plan | |
else | |
flash[:danger] = t "failed_update" | |
plan.attachments.build unless get_attachments.any? | |
render :edit | |
end | |
end | |
private | |
attr_reader :plan, :plan_service | |
def init_plan_service | |
@plan_service = PlanService.new params, current_user | |
end | |
def plan_params | |
params.require(:plan).permit Plan::PARAMS | |
end | |
def find_plan | |
@plan = if current_user.can_view_all_plan? | |
Plan.find_by id: param_id | |
else | |
current_user.plans.find_by id: params[:id] | |
end | |
check_plan | |
end | |
def param_id | |
params[:id] | |
end | |
def check_plan | |
return if plan | |
flash[:warning] = t "not_found" | |
redirect_to plans_path | |
end | |
def plan_create_success | |
flash[:success] = t "successful_create" | |
redirect_to plan | |
end | |
def plan_create_fail | |
plan.attachments.build unless params[:plan][:attachments_attributes] | |
flash[:danger] = t "failed_create" | |
render :new | |
end | |
def get_attachments | |
plan.attachments | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment