Sha256: c9bf18510e11425765f7106ddde628ee383bad73968a6e567f077d29ef1fbe10

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module Aikotoba
  class AccountsController < ApplicationController
    def new
      @account = build_account({email: "", password: ""})
    end

    def create
      @account = build_account(accounts_params.to_h.symbolize_keys)
      ActiveRecord::Base.transaction do
        before_create_account_process
        save_with_callbacks!(@account)
        after_create_account_process
      end
      redirect_to after_sign_up_path, flash: {notice: successed_message}
    rescue ActiveRecord::RecordInvalid => e
      failed_create_account_process(e)
      flash[:alert] = failed_message
      render :new, status: :unprocessable_entity
    end

    private

    def accounts_params
      params.require(:account).permit(:email, :password)
    end

    def build_account(params)
      Account.build_by(attributes: params)
    end

    def save_with_callbacks!(account)
      Account::Service::Registration.call!(account: account)
    end

    def after_sign_up_path
      aikotoba.new_session_path
    end

    def successed_message
      I18n.t(".aikotoba.messages.registration.success")
    end

    def failed_message
      I18n.t(".aikotoba.messages.registration.failed")
    end

    # NOTE: Methods to override if you want to do something before account creation.
    def before_create_account_process
    end

    # NOTE: Methods to override if you want to do something after account creation.
    def after_create_account_process
    end

    # NOTE: Methods to override if you want to do something failed account creation.
    def failed_create_account_process(e)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aikotoba-0.1.1 app/controllers/aikotoba/accounts_controller.rb
aikotoba-0.1.0 app/controllers/aikotoba/accounts_controller.rb