class CreateAuditLogs < ActiveRecord::Migration[7.1]
def change
create_table :audit_logs do |t|
t.references :auditable, polymorphic: true, null: false
t.string :actor
t.string :action, null: false
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 DailySignupRollup < ApplicationRecord
validates :day, presence: true, uniqueness: true
validates :total, :verified, numericality: { greater_than_or_equal_to: 0 }
scope :for_range, ->(from, to) do
where(day: from.to_date..to.to_date).order(:day)
class ReportsController < ApplicationController
def monthly_sales
year = params.fetch(:year, Date.current.year).to_i
rows = Order.monthly_sales(year: year)
@report = MonthlySalesReport.new(rows, year: year)
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
class User < ApplicationRecord
has_many :posts, foreign_key: :author_id
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
module KeysetPaginatable
extend ActiveSupport::Concern
class_methods do
def keyset_page(cursor: nil, limit: 20, direction: :desc)
limit = limit.to_i.clamp(1, 100)
class PopularPostsQuery
def initialize(relation = Post.all)
@relation = relation
end
def call(timeframe: 7.days, min_views: 100, limit: 20)
class Product < ApplicationRecord
scope :by_status, ->(status) {
status.present? ? where(status: status) : all
}
scope :in_category, ->(category_id) {
class ImportResult
RowError = Struct.new(:line, :messages)
attr_reader :imported, :row_errors
def initialize
class PriceBreakdown < Struct.new(:subtotal_cents, :discount_cents, :tax_cents, keyword_init: true)
def initialize(*)
super
freeze
end
class LastSeenTracker
THROTTLE = 5.minutes
PENDING_KEY = "pending:last_seen".freeze
class << self
def touch(user_id, at: Time.current)