module Api
module V1
class PostsController < BaseController
def index
# Eager load author and recent comments with their authors
posts = Post.published
class User < ApplicationRecord
has_many :posts, foreign_key: :author_id
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
production:
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>
timeout: 5000
checkout_timeout: 5
module ApiRequestLogger
extend ActiveSupport::Concern
included do
around_action :log_api_request
end
class AddCommentsCountToPosts < ActiveRecord::Migration[6.1]
def up
add_column :posts, :comments_count, :integer, default: 0, null: false
# Populate existing counts
Post.find_each do |post|
class AddLockVersionToPosts < ActiveRecord::Migration[6.1]
def change
add_column :posts, :lock_version, :integer, default: 0, null: false
end
end
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :excerpt, :body, :published_at, :views, :likes_count, :comments_count
attribute :can_edit, if: :current_user_can_edit?
belongs_to :author, serializer: UserSummarySerializer
class PopularPostsQuery
def initialize(relation = Post.all)
@relation = relation
end
def call(timeframe: 7.days, min_views: 100, limit: 20)
class User < ApplicationRecord
has_many :posts, foreign_key: :author_id, dependent: :destroy
before_validation :normalize_email
before_create :generate_auth_token
after_create :send_welcome_email
module Api
module V1
class PostsController < BaseController
before_action :authenticate_user!
def create
class TransferFundsService
def initialize(from_account:, to_account:, amount:)
@from_account = from_account
@to_account = to_account
@amount = amount
end
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins_list = if Rails.env.production?
ENV['CORS_ALLOWED_ORIGINS']&.split(',') || []
else
'localhost:3000', 'localhost:5173', /127\.0\.0\.1:\d+/