diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6f0aa6c..1678eaa 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,25 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception attr_writer :breadcrumb + private + + def domain + @domain ||= domain_scope.find(params[:domain_id] || params[:id]) + end + + def record + @record ||= record_scope.find(params[:record_id] || params[:id]) + end + def domain_scope + @domain_scope ||= Domain.all + end + + def record_scope + @record_scope ||= domain.records + end + end diff --git a/app/controllers/domains_controller.rb b/app/controllers/domains_controller.rb index 810792d..9c1e02d 100644 --- a/app/controllers/domains_controller.rb +++ b/app/controllers/domains_controller.rb @@ -1,56 +1,53 @@ class DomainsController < ApplicationController - before_action :set_domain, only: [:show, :edit, :update, :destroy] + before_action :domain, only: [:show, :edit, :update, :destroy] # GET /domains def index - @domains = Domain.all + @domains = domain_scope.all end # GET /domains/1 def show end # GET /domains/new def new @domain = Domain.new end # GET /domains/1/edit def edit end # POST /domains def create @domain = Domain.new(domain_params) if @domain.save redirect_to @domain, notice: "#{@domain.name} was successfully created." else render :new end end # PATCH/PUT /domains/1 def update if @domain.update(domain_params) redirect_to @domain, notice: "#{@domain.name} was successfully updated." else render :edit end end # DELETE /domains/1 def destroy @domain.destroy redirect_to domains_url, notice: "#{@domain.name} was successfully destroyed." end private - def set_domain - @domain = Domain.find(params[:id]) - end def domain_params params.require(:domain).permit(:name, :type) end end diff --git a/app/controllers/records_controller.rb b/app/controllers/records_controller.rb index d90fea7..b54da1c 100644 --- a/app/controllers/records_controller.rb +++ b/app/controllers/records_controller.rb @@ -1,56 +1,49 @@ class RecordsController < ApplicationController - before_action :set_domain - before_action :set_record, only: [:edit, :update, :destroy] + before_action :domain + before_action :record, only: [:edit, :update, :destroy] # GET /records/new def new @record = @domain.records.build end # GET /records/1/edit def edit end # POST /records def create @record = @domain.records.new(new_record_params) if @record.save redirect_to @domain, notice: 'Record was successfully created.' else render :new end end # PATCH/PUT /records/1 def update if @record.update(edit_record_params) redirect_to @domain, notice: 'Record was successfully updated.' else render :edit end end # DELETE /records/1 def destroy @record.destroy redirect_to @domain, notice: 'Record was successfully destroyed.' end private - def set_record - @record = @domain.records.find(params[:id]) - end - - def set_domain - @domain = Domain.find(params[:domain_id]) - end def edit_record_params params.require(:record).permit(:name, :content, :prio, :disabled) end def new_record_params params.require(:record).permit(:name, :content, :type, :prio) end end