GameDevWeirdo

Transform Your Rails Code with Faat! v0.1.5: Introducing the Services Module for Better Code Organization

Faat! v0.1.5: Introducing the Services Module

Are you tired of dealing with fat models and bloated Rails code? Look no further than Faat! My open-source gem-generator is designed to help you separate the base Rails logic into resources, services, and forms. Today, I’m excited to announce the release of Faat! v0.1.5, which includes the new services module to further enhance your code organization.

In this article, I’ll show you how to work with the services module using an example.

Example:

Let’s say we have a new payment_service.rb file generated by Faat!, which looks like this:

class PaymentService < Faat::Services::Base

  def year_payment
    # ...
    if charge(year_amount)
      # ...
      send_subscribing_email
    end
  end

  # ...

  private

  def send_subscribing_email
    # sending email
  end

  def charge(amount)
    # some payment logic
  end

end

And we have a SubscribesController that creates new Subscribe objects and handles payments:

class SubscribesController < ActionController::Base
  def create
    @subscribe = Subscribe.create(subscribe_params)

    if @subscribe.premium
      PaymentService(@subscribe).year_payment
      # ...
    else
      # ...
    end
  end
  # ...
end

In this example, we also have a Subscribe model:

class Subscribe < ActiveRecord::Base
# ...
end

As you can see, all the logic related to subscription and payment has been separated and moved to a separate service. By using Faat!’s services module, we’ve completely separated the business logic from the controller logic, resulting in much better code organization.

While it’s possible to achieve this kind of organization without a gem-generator, Faat! makes it easy to quickly generate the necessary files and start writing logic in the services or resources.

So what are you waiting for? Try Faat! for yourself and see how it can transform your Rails code. Fork the project on GitHub or share this post on Twitter or Facebook to spread the word!