Skip to content

Instantly share code, notes, and snippets.

# frozen_string_literal: true
class Templates::Templates::CreateInput < BaseInput
REQUIRED_KEYS = %i[title file_name file_base64 user_id]
attributes(*REQUIRED_KEYS)
def validate!
validate_required_keys!
validate_user!
end
class Templates::Template < ApplicationRecord
self.table_name = 'templates_templates'
belongs_to :user
has_many :components, class_name: 'Templates::Component'
end
# == Schema Information
#
# Table name: templates_templates
class Templates::Component < ApplicationRecord
self.table_name = 'templates_components'
KEY_TYPE_APPLICATORS = {
invoices: %i[
issue_date
due_date
unit_price
total_hours
sub_total
# frozen_string_literal: true
class UsersController < ApplicationController
def register
context = ::Users::Contexts::Registration.call(input: users_registration_input)
return error_response(context.message) unless context.success?
render json: ::Tokens::TokenBlueprint.render(context.payload), status: :ok
end
class UsersController < ApplicationController
def show
context = ::Users::Contexts::Fetch.call(params: query_users_params, type: :single)
return error_response(context.message) unless context.success?
render json: UserBlueprint.render(context.payload), status: :ok
end
private
# frozen_string_literal: true
# The Fetch class is responsible for retrieving user records from the database.
# It extends the BaseService and includes the Queries::Engine module to leverage
# query execution functionality.
class Users::Contexts::Fetch < BaseService
include Queries::Engine
private
# frozen_string_literal: true
module Queries
# The Engine module provides a structure for executing queries against
# a given model with a defined set of parameters and conditions.
module Engine
def self.call(**kwargs)
new(**kwargs).call
end
# frozen_string_literal: true
# The ErrorHandling module provides mechanisms for managing errors
# within service classes. It includes methods for safely executing
# blocks of code and handling known error classes.
module ErrorHandling
module Core
# A list of error classes that can be rescued during safe execution.
ERROR_CLASSES = [
ActiveRecord::RecordNotFound,
class Users::RegistrationInput < BaseInput
REQUIRED_KEYS = %i[email password].freeze
attributes(*REQUIRED_KEYS, :first_name, :last_name)
def validate!
validate_required_keys!
end
end
############
# frozen_string_literal: true
class UsersController < ApplicationController
def register
context = ::Users::Contexts::Registration.call(input: users_registration_input)
return error_response(context.message) unless context.success?
render json: ::Tokens::TokenBlueprint.render(context.payload), status: :ok
end