Upgrade to Pro — share decks privately, control downloads, hide ads and more …

『NaCl サマーインターン2024 成果発表』より抜粋

Junwoo Cheon
October 19, 2024
4

『NaCl サマーインターン2024 成果発表』より抜粋

See on Google Slides

Junwoo Cheon

October 19, 2024
Tweet

Transcript

  1. ちょんの担当分 • 固定ポスト #9 • フォロー中のユーザーの最新ポストを表示 #11 • プロフィールに出身地を追加 #16

    • ユーザー検索 #12 • ポストの論理削除 #14 • 「いいね」機能 #18 • 捨てられたポストの自動削除 • ハッシュタグ
  2. 固定ポスト app/controllers/microposts_controller.rb class MicropostsController < ApplicationController before_action :require_login before_action :set_micropost,

    only: %i[update destroy toggle_pinned] before_action :authorize, only: %i[update destroy toggle_pinned] def toggle_pinned @micropost.toggle(:pinned).save! redirect_to current_user, status: :see_other end end
  3. 固定ポスト app/models/micropost.rb class Micropost < ApplicationRecord default_scope -> { order(pinned:

    :desc, created_at: :desc) } after_save :ensure_single_pinned_post, if: -> { saved_change_to_pinned?(from: false, to: true) } private def ensure_single_pinned_post user.microposts.where(pinned: true).where.not(id: id).update_all(pinned: false) end end
  4. ユーザー検索 app/views/users/index.html.erb <%= form_with url: users_path, method: :get do |f|

    %> <%= f.text_field :search_query %> <%= f.select :birthplaces, BIRTHPLACES.map { |p| [p.capitalize, p] }, {}, { multiple: true, size: 10 } %> <%= f.submit 'Search' %> <% end %> <% if @users.any? %> <%= will_paginate %> <ul class='users'> <%= render @users %> </ul> <%= will_paginate %> <% else %> <p>No users found.</p> <% end %>
  5. ユーザー検索 app/models/user.rb class User < ApplicationRecord def self.search_by(query) if query.blank?

    all else q = "%#{sanitize_sql_like(query)}%" where('name LIKE ?', q).or(where('email LIKE ?', q)) end end end
  6. ユーザー検索 app/controllers/users_controller.rb class UsersController < ApplicationController def index @users =

    if (b = params[:birthplaces]&.without('')).present? User.search_by(params[:search_query]) .where(birthplace: b) .paginate(page: params[:page]) else User.search_by(params[:search_query]) .paginate(page: params[:page]) end end end
  7. ポストの論理削除 app/controllers/microposts_controller.rb class MicropostsController < ApplicationController before_action :require_login before_action :set_micropost,

    only: %i[restore destroy] before_action :authorize, only: %i[restore destroy] def restore @micropost.undiscard redirect_back_or_to root_path, flash: { success: 'Micropost restored' } end def destroy @micropost.discard || @micropost.destroy! redirect_back_or_to root_path, status: :see_other, flash: { success: 'Micropost deleted' } end end
  8. ポストの論理削除 app/models/micropost.rb class Micropost < ApplicationRecord include Discard::Model before_save ->

    { update_column(:pinned, false) }, if: -> { will_save_change_to_discarded_at?(from: nil) && pinned_in_database } end
  9. 捨てられたポストの自動削除 lib/tasks/microposts.rake namespace :microposts do desc 'Empty trash' task empty_trash:

    :environment do destroyed = Micropost.where(discarded_at: ..30.days.ago).destroy_all puts "Destroyed #{destroyed.length} microposts." end end
  10. ハッシュタグ app/models/micropost.rb class Micropost < ApplicationRecord has_and_belongs_to_many :tags after_save :tag,

    if: :saved_change_to_content? private def tag tags.clear unless tags.empty? self.tags = content.scan(/(?<=#)[^\s#]+/).uniq.map do |w| Tag.find_or_create_by!(name: w) end end end