Saturday, May 24, 2025
HomeRuby On RailsBoolean vs Datetime | Drifting Ruby

Boolean vs Datetime | Drifting Ruby


# Terminal
rails g migration change_published_to_published_on_posts
# db/migrate/20250406012935_change_published_to_published_on_posts.rb
class ChangePublishedToPublishedOnPosts < ActiveRecord::Migration[8.0]
  def up
    add_column :posts, :published_at, :datetime
    execute <<-SQL
      UPDATE posts
      SET published_at = updated_at
      WHERE printed = TRUE
    SQL
    remove_column :posts, :printed
    add_index :posts, :published_at
  finish

  def down
    add_column :posts, :printed, :boolean, default: false
    execute <<-SQL
      UPDATE posts
      SET printed = (published_at IS NOT NULL AND published_at <= CURRENT_TIMESTAMP)
    SQL
    remove_column :posts, :published_at
  finish
finish
# app/controllers/posts_controller.rb
def index
  @posts = Submit.printed.order(published_at: :desc)
finish

def post_params
  params.anticipate(publish: [ :title, :published_at, :content ])
finish
# app/fashions/publish.rb
class Submit < ApplicationRecord
  has_rich_text :content material

  scope :printed, -> { the place(published_at: ..Time.present) }
  scope :scheduled, -> { the place(published_at: Time.present..) }
  # scope :scheduled, -> { the place("published_at > ?", Time.present) }
  scope :unpublished, -> { the place(published_at: nil) }

  def printed?
    published_at.current? && published_at.previous?
  finish

  def scheduled?
    published_at.current? && published_at.future?
  finish
finish
# app/views/posts/_form.html.erb
<div class="my-5 flex items-center gap-2">
  <%= kind.label :published_at %>
  <%= kind.datetime_field :published_at %>
</div>
# app/views/posts/_post.html.erb
<div>
  <%= "Printed" if publish.printed? %>
  <%= "Scheduled" if publish.scheduled? %>
  <%= "Unpublished" if !publish.scheduled? && !publish.printed? %>
</div>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments