Menu

Post image 1
Post image 2
1 / 2
0

SOLID Principles in Ruby on Rails

DEV Community: ruby·Shamila TP·3 days ago
#Fg44FGU1
#dev#process#class#fullscreen#ticket#enter
Reading 0:00
15s threshold

SOLID Principles in Ruby on Rails SOLID is not a Rails or Ruby concept. It's a set of five object-oriented design principles that apply to any language/framework. But Rails makes it surprisingly easy to violate all five of them. Let's start with Single Responsibility Principle It states a class should have only one reason to change. A classic example is a User model that handles authentication, sends emails, and formats reports. That's three responsibilities. Instead, you'd extract email sending into a Mailer class and reporting into a service object. class User def authenticate end def send_welcome_email end def generate_report end end Enter fullscreen mode Exit fullscreen mode The Fix class User < ApplicationRecord has_many :orders validates :email , presence: true end class UserAuthenticationService def authenticate end end class UserMailer < ApplicationMailer def send_welcome_email ( user ) end end class UserReportService def generate_report end end Enter fullscreen mode Exit fullscreen mode In…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More