Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.68 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. class ConsentsController < ApplicationController
  4.  
  5.   layout :main_layout, only: %i[index show]
  6.  
  7.   before_action :set_branch, only: %i[index new create]
  8.   before_action :set_consent, except: %i[index new create]
  9.  
  10.   def index
  11.     @new_consent = Consent.new(branch: @branch)
  12.     authorize(@new_consent)
  13.     @consents = @branch.consents.page(params[:page])
  14.   end
  15.  
  16.   def show
  17.     authorize(@consent)
  18.   end
  19.  
  20.   def new
  21.     @consent = Consent.new(branch: @branch)
  22.     authorize(@consent)
  23.   end
  24.  
  25.   def create
  26.     @consent = Consent.new(create_params)
  27.     authorize(@consent)
  28.     if @consent.save
  29.       flash.notice = t('consents.create.flash.success')
  30.       redirect_to consent_path(@consent)
  31.     else
  32.       render :new
  33.     end
  34.   end
  35.  
  36.   def edit
  37.     authorize(@consent)
  38.   end
  39.  
  40.   def update
  41.     authorize(@consent)
  42.     if @consent.update(update_params)
  43.       flash.notice = t('consents.update.flash.success')
  44.       redirect_to consent_path(@consent)
  45.     else
  46.       render :edit
  47.     end
  48.   end
  49.  
  50.   def destroy
  51.     authorize(@consent)
  52.     if @consent.destroy
  53.       flash.notice = t('consents.destroy.flash.success')
  54.     else
  55.       flash.alert = t('consents.destroy.flash.alert')
  56.     end
  57.     redirect_to branch_consents_path(@branch)
  58.   end
  59.  
  60.   private
  61.  
  62.   def set_branch
  63.     @branch = policy_scope(Branch).find(params[:branch_id])
  64.   end
  65.  
  66.   def set_consent
  67.     @consent = policy_scope(Consent).find(params[:id])
  68.     @branch = @consent.branch
  69.   end
  70.  
  71.   def create_params
  72.     params.require(:consent).permit(:name, :key, :based_on).merge!(branch: @branch)
  73.   end
  74.  
  75.   def update_params
  76.     params.require(:consent).permit(:name)
  77.   end
  78.  
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement