help@rskworld.in +91 93305 39277
RSK World
  • Home
  • Development
    • Web Development
    • Mobile Apps
    • Software
    • Games
    • Project
  • Technologies
    • Data Science
    • AI Development
    • Cloud Development
    • Blockchain
    • Cyber Security
    • Dev Tools
    • Testing Tools
  • About
  • Contact

Theme Settings

Color Scheme
Display Options
Font Size
100%
Back to Project
RSK World
ruby-todo-list
/
app
/
models
RSK World
ruby-todo-list
Ruby Todo List - Task Management with User Authentication + Categories + Search + File Attachments + Email Notifications + Rails MVC + Modern Web Interface + API Integration + Educational Design
models
  • category.rb1.3 KB
  • task.rb2.4 KB
  • user.rb1023 B
task.rb
app/models/task.rb
Raw Download
Find: Go to:
# Ruby To-Do List Application - Task Model
# Created by: Molla Samser (help@rskworld.in, +91 93305 39277)
# Address: Nutanhat, Mongolkote, Purba Burdwan, West Bengal, India, 713147
# Year: 2026

class Task < ApplicationRecord
  # Associations
  belongs_to :user
  belongs_to :category, optional: true

  # Active Storage
  has_many_attached :attachments

  # Validations
  validates :title, presence: true, length: { minimum: 1, maximum: 255 }
  validates :description, length: { maximum: 1000 }, allow_blank: true
  validates :priority, inclusion: { in: [1, 2, 3] }, allow_nil: true

  # Scopes
  scope :completed, -> { where(completed: true) }
  scope :pending, -> { where(completed: false) }
  scope :high_priority, -> { where(priority: 3) }
  scope :medium_priority, -> { where(priority: 2) }
  scope :low_priority, -> { where(priority: 1) }
  scope :overdue, -> { where('due_date < ? AND completed = ?', Time.current, false) }
  scope :due_today, -> { where('due_date >= ? AND due_date < ?', Time.current.beginning_of_day, Time.current.end_of_day) }

  # Callbacks
  before_update :set_completed_at, if: :completed_changed?

  # Search functionality
  include PgSearch::Model
  pg_search_scope :search_by_title_and_description,
    against: [:title, :description],
    using: {
      tsearch: { prefix: true }
    }

  # Class methods for search
  def self.search(query, user)
    if query.present?
      user.tasks.search_by_title_and_description(query)
    else
      user.tasks.all
    end
  end

  # Methods
  def priority_name
    case priority
    when 1
      'Low'
    when 2
      'Medium'
    when 3
      'High'
    else
      'Not Set'
    end
  end

  def priority_color
    case priority
    when 1
      'success' # Green for low
    when 2
      'warning' # Yellow for medium
    when 3
      'danger'  # Red for high
    else
      'secondary'
    end
  end

  def toggle_completion!
    update(completed: !completed)
  end

  def overdue?
    due_date.present? && due_date < Time.current && !completed
  end

  def due_today?
    due_date.present? && due_date >= Time.current.beginning_of_day && due_date < Time.current.end_of_day
  end

  private

  def set_completed_at
    self.completed_at = completed ? Time.current : nil
  end

  def completed_changed?
    completed_changed?
  end
end
96 lines•2.4 KB
ruby

About RSK World

Founded by Molla Samser, with Designer & Tester Rima Khatun, RSK World is your one-stop destination for free programming resources, source code, and development tools.

Founder: Molla Samser
Designer & Tester: Rima Khatun

Development

  • Game Development
  • Web Development
  • Mobile Development
  • AI Development
  • Development Tools

Legal

  • Terms & Conditions
  • Privacy Policy
  • Disclaimer

Contact Info

Nutanhat, Mongolkote
Purba Burdwan, West Bengal
India, 713147

+91 93305 39277

hello@rskworld.in
support@rskworld.in

© 2026 RSK World. All rights reserved.

Content used for educational purposes only. View Disclaimer