diff --git a/app.rb b/app.rb index e51515d..85a5174 100644 --- a/app.rb +++ b/app.rb @@ -6,6 +6,7 @@ require './app/models/user' require './app/models/area' require './app/models/project' require './app/models/task' +require './app/models/tag' require './app/helpers/authentication_helper' @@ -91,5 +92,7 @@ get '/' do end get '/inbox' do + @tasks = current_user.tasks.incomplete.where(project_id: nil).order(:name) + erb :inbox end diff --git a/app/models/project.rb b/app/models/project.rb index e79f5d7..df37298 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -2,4 +2,7 @@ class Project < ActiveRecord::Base belongs_to :user belongs_to :area, optional: true has_many :tasks, dependent: :destroy + + scope :with_incomplete_tasks, -> { joins(:tasks).where(tasks: { completed: false }).distinct } + scope :with_complete_tasks, -> { joins(:tasks).where(tasks: { completed: true }).distinct } end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..920e262 --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,4 @@ +class Tag < ActiveRecord::Base + belongs_to :user + has_and_belongs_to_many :tasks +end diff --git a/app/models/task.rb b/app/models/task.rb index efc9553..47801ee 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -1,6 +1,8 @@ class Task < ActiveRecord::Base belongs_to :user belongs_to :project, optional: true + has_and_belongs_to_many :tags - default_scope { where(completed: false) } + scope :complete, -> { where(completed: true) } + scope :incomplete, -> { where(completed: false) } end diff --git a/app/models/user.rb b/app/models/user.rb index bfb6931..d3b4b09 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,7 @@ class User < ActiveRecord::Base has_many :areas has_many :projects has_many :tasks + has_many :tags, dependent: :destroy validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }, uniqueness: true end diff --git a/app/routes/projects_routes.rb b/app/routes/projects_routes.rb index 83069f8..e686680 100644 --- a/app/routes/projects_routes.rb +++ b/app/routes/projects_routes.rb @@ -1,6 +1,7 @@ class Sinatra::Application get '/projects' do - @projects_with_tasks = current_user.projects.includes(:tasks).order(:name) + @projects_with_tasks = current_user.projects.with_incomplete_tasks.order(:name) + @projects_with_tasks_complete = current_user.projects.with_complete_tasks.order(:name) erb :'projects/index' end @@ -14,9 +15,10 @@ class Sinatra::Application post '/project/create' do project = current_user.projects.new( - name: params[:name], + name: params[:name], description: params[:description], - area_id: params[:area_id].presence) + area_id: params[:area_id].presence + ) if project.save redirect '/' diff --git a/app/routes/tasks_routes.rb b/app/routes/tasks_routes.rb index b3dd358..16f7ea5 100644 --- a/app/routes/tasks_routes.rb +++ b/app/routes/tasks_routes.rb @@ -1,26 +1,45 @@ class Sinatra::Application + def update_task_tags(task, tags_json) + return if tags_json.blank? + + begin + tag_names = JSON.parse(tags_json).map { |tag| tag['value'] }.uniq + tags = tag_names.map do |name| + current_user.tags.find_or_create_by(name: name) + end + task.tags = tags + rescue JSON::ParserError + puts "Failed to parse JSON for tags: #{tags_json}" + end + end + get '/tasks' do case params[:due_date] when 'today' today = Date.today - @tasks = current_user.tasks.where(due_date: today.beginning_of_day..today.end_of_day, project: nil) - @projects_with_tasks = current_user.projects - .joins(:tasks) - .where(tasks: { due_date: today.beginning_of_day..today.end_of_day }) + @tasks = current_user.tasks.incomplete.where('due_date <= ?', today.end_of_day).where(project: nil) + + @projects_with_tasks = current_user.projects.with_incomplete_tasks + .where('tasks.due_date <= ?', today.end_of_day) .distinct.order('projects.name ASC') when 'upcoming' one_week_from_today = Date.today + 7.days - @tasks = current_user.tasks.where(due_date: Date.today..one_week_from_today, project: nil) - @projects_with_tasks = current_user.projects.includes(:tasks).where(tasks: { due_date: Date.today..one_week_from_today }).order('projects.name ASC') + @tasks = current_user.tasks.incomplete.where(due_date: Date.today..one_week_from_today, project: nil) + @projects_with_tasks = current_user.projects.with_incomplete_tasks.includes(:tasks).where(tasks: { due_date: Date.today..one_week_from_today }).order('projects.name ASC') when 'never' - @tasks = current_user.tasks.where(due_date: nil, project: nil) - @projects_with_tasks = current_user.projects.includes(:tasks).where(tasks: { due_date: nil }).order('projects.name ASC') + @tasks = current_user.tasks.incomplete.where(due_date: nil, project: nil) + @projects_with_tasks = current_user.projects.with_incomplete_tasks.includes(:tasks).where(tasks: { due_date: nil }).order('projects.name ASC') else - @tasks = current_user.tasks.where(project: nil) - @projects_with_tasks = current_user.projects.includes(:tasks).order('projects.name ASC') + if params[:status] == 'completed' + @tasks = current_user.tasks.complete.where(project: nil) + @projects_with_tasks = current_user.projects.with_complete_tasks.includes(:tasks).order('projects.name ASC') + else + @tasks = current_user.tasks.incomplete.where(project: nil) + @projects_with_tasks = current_user.projects.with_incomplete_tasks.includes(:tasks).order('projects.name ASC') + end end @tasks ||= [] @@ -46,6 +65,7 @@ class Sinatra::Application end if task.save + update_task_tags(task, params[:tags]) redirect request.referrer || '/' else halt 400, 'There was a problem creating the task.' @@ -72,6 +92,7 @@ class Sinatra::Application end if task.update(task_attributes) + update_task_tags(task, params[:tags]) redirect request.referrer || '/' else halt 400, 'There was a problem updating the task.' @@ -80,7 +101,7 @@ class Sinatra::Application patch '/task/:id/toggle_completion' do content_type :json - task = current_user.tasks.find(params[:id]) + task = current_user.tasks.find_by(id: params[:id]) if task task.completed = !task.completed if task.save diff --git a/app/views/inbox.erb b/app/views/inbox.erb index 35b7e9e..5db6a4f 100644 --- a/app/views/inbox.erb +++ b/app/views/inbox.erb @@ -7,10 +7,9 @@ <%= partial :'tasks/_form', locals: { task: Task.new } %> -