class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.new(comment_params.merge(author: current_member))
if @comment.save
# config/initializers/turbo_stream_actions.rb
Turbo::Streams::TagBuilder.class_eval do
def flash(message:, level: :notice)
update('flash', partial: 'shared/flash', locals: { message: message, level: level })
end
end
class ButtonComponent < ViewComponent::Base
VARIANTS = {
primary: "bg-blue-600 hover:bg-blue-700 text-white",
secondary: "bg-gray-200 hover:bg-gray-300 text-gray-900",
danger: "bg-red-600 hover:bg-red-700 text-white"
}.freeze
# Gemfile
gem 'view_component'
# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
VARIANTS = %w[primary secondary danger].freeze
class Webhooks::Verifier
def initialize(secret: Rails.application.credentials.webhooks_secret)
@secret = secret
end
def valid?(request)
class CreateComments < ActiveRecord::Migration[6.1]
def change
create_table :comments do |t|
t.references :commentable, polymorphic: true, null: false
t.references :author, null: false, foreign_key: { to_table: :users }
t.text :body, null: false
<% if @project.errors.any? %>
<%= turbo_stream.replace dom_id(@project, :form), partial: "projects/form", locals: { project: @project } %>
<% else %>
<%= turbo_stream.replace dom_id(@project), partial: "projects/project", locals: { project: @project } %>
<%= turbo_stream.update "flash", partial: "shared/flash" %>
<% end %>
class DashboardPresenter
def initialize(member)
@member = member
end
def unread_notifications_count
UPDATE outbox_events
SET status = 1, locked_at = NOW()
WHERE id = (
SELECT id
FROM outbox_events
WHERE status = 0
class AddUniqueSlugToArticles < ActiveRecord::Migration[6.1]
def change
add_column :articles, :slug, :string, null: false
add_index :articles, :slug, unique: true
end
end
module Posts
class CreateService
def initialize(author:, params:)
@author = author
@params = params
end
# Map - transform elements
[1, 2, 3, 4].map { |n| n * 2 } # => [2, 4, 6, 8]
['alice', 'bob'].map(&:upcase) # => ['ALICE', 'BOB']
users.map(&:email) # => ['alice@example.com', ...]
# Select/Reject - filter elements