Ruby on Rails

Motivation: The core framework used for GitLab’s web application. Learn about its structure, conventions, and common libraries used.

Structure:

  • app/: Contains the core application logic, controllers, models, views, and helpers.
  • config/: Holds configuration files like database.yml, routes.rb, and environments/*.rb.
  • db/: Stores database schemas, migrations, and seeds.
  • lib/: Provides reusable code modules and libraries.
  • public/: Contains static files such as images, CSS, and JavaScript.
  • test/: Houses test suites for the application’s functionality.
  • vendor/: Includes external dependencies and gems.

Conventions:

  • Model-View-Controller (MVC): A common architectural pattern separating concerns for data, presentation, and user interactions.
  • RESTful Routing: A convention for defining routes and actions based on HTTP verbs (GET, POST, PUT, DELETE).
  • Active Record: An Object-Relational Mapper (ORM) simplifying database interactions.
  • ActiveRecord Associations: Defines relationships between models such as has_many, belongs_to, has_one.
  • Gemfile: A file managing application dependencies and gems.

Common Libraries and Tools:

  • Action Pack: Provides the foundation for controllers, views, and routing.
  • Active Support: Offers utility classes and extensions to Ruby core functionality.
  • Active Record: Manages database interactions.
  • Action Mailer: Facilitates email sending within the application.
  • Action View: Renders HTML templates and assists with view logic.
  • Action Cable: Enables real-time communication features using WebSockets.
  • Rake: A build and task management tool.
  • Capistrano: Deploys Rails applications to servers.
  • Sidekiq: A background worker queue.

Code Examples:

  • app/controllers/projects_controller.rb: Contains actions for managing projects, such as creating, updating, and deleting.
class ProjectsController < ApplicationController
            def index
              @projects = Project.all
            end
          
            def show
              @project = Project.find(params[:id])
            end
          
            # ...
          end
          
  • app/models/project.rb: Defines the Project model, including its attributes and relationships.
class Project < ApplicationRecord
            has_many :issues
            has_many :users, through: :members
          
            # ...
          end
          
  • config/routes.rb: Defines application routes and their corresponding actions.
Rails.application.routes.draw do
            resources :projects do
              resources :issues
            end
          end
          
  • db/migrate/20231207123456_create_projects.rb: A database migration for creating the Project table.
class CreateProjects < ActiveRecord::Migration[7.0]
            def change
              create_table :projects do |t|
                t.string :name
                t.timestamps
              end
            end
          end
          
  • Gemfile: Lists the application’s dependencies and gems.
source 'https://rubygems.org'
          
          gem 'rails', '~> 7.0'
          gem 'sqlite3'
          gem 'rspec-rails'
          gem 'factory_bot_rails'
          gem 'faker'
          
          # ...
          

References: