Create a Rails service object with Gem Light Service

Daftar Isi

Service object with Ruby on Rails

This service object tutorial will separate business logic and application logic so that they are easy to maintain and scalable.

What are service objects?

a pattern that allows you to separate business logic and other elements from controllers and models, allowing the model to be the data layer and the controller to be the entry point to the API.

Service object, here we will create a class which will be responsible only for business logic and there will be only one public method for a task being executed.

When should we use service objects?

1. There is a complex data process or it must first be connected to an external API, with which you can encapsulate the complex process so that it can be managed

2. Complex business rules

3. need integration with third-party integrations

Measures:

We will first create a folder in application/services

TO DO app/services/user_registration.rb

class UserRegistration

  extend LightService::Organizer

  def self.call(params)

    with(params: params).reduce(

      ValidateUserInput,

      CreateUser

 )

  end

end

This code will handle creating a public method to call calss User registration and will execute the ValidateUserInput, CreateUser actions

then we create a user model

rails g model User

Then create a CreateUser.rb class in app/services/user_registration/create_user.rb like this

class UserRegistration::CreateUser

  extend LightService::Action

  expects :validated_params

  promises :user

  executed do |context|

    user = User.new(context.validated_params)

   

    if user.save

    context.user = user

    else

    context.fail!(user.errors.full_messages.to_sentence)

    end

  end

end

UserRegistration class will perform user creation

then create app/services/user_registration/validate_user_input.rb like below:

class UserRegistration::ValidateUserInput

  extend LightService::Action

  expects :params

  promises :validated_params

  executed do |context|

    if context.params[:email].blank?

      context.fail("Please Fill Email !! ")

    end

  context.validated_params = context.params

  end

end

the ValidateUserInput class, will validate the input if the email is empty

The last step we can run the service object like this in Rails console


result = UserRegistration.call(email: 'jaka@testdoterb.com', name: 'Jaka Pratama')

PakarPBN

A Private Blog Network (PBN) is a collection of websites that are controlled by a single individual or organization and used primarily to build backlinks to a “money site” in order to influence its ranking in search engines such as Google. The core idea behind a PBN is based on the importance of backlinks in Google’s ranking algorithm. Since Google views backlinks as signals of authority and trust, some website owners attempt to artificially create these signals through a controlled network of sites.

In a typical PBN setup, the owner acquires expired or aged domains that already have existing authority, backlinks, and history. These domains are rebuilt with new content and hosted separately, often using different IP addresses, hosting providers, themes, and ownership details to make them appear unrelated. Within the content published on these sites, links are strategically placed that point to the main website the owner wants to rank higher. By doing this, the owner attempts to pass link equity (also known as “link juice”) from the PBN sites to the target website.

The purpose of a PBN is to give the impression that the target website is naturally earning links from multiple independent sources. If done effectively, this can temporarily improve keyword rankings, increase organic visibility, and drive more traffic from search results.

Jasa Backlink

Download Anime Batch