From 2374ae8f44b4124ffde981d9d07de3722e0e2196 Mon Sep 17 00:00:00 2001 From: jattoabdul Date: Thu, 18 Apr 2019 00:24:07 +0100 Subject: [PATCH] persist entries by passing last updated hour and mileage on creation of new record and add model test for scope added --- app/controllers/projects_controller.rb | 2 ++ app/models/hour.rb | 1 + app/models/mileage.rb | 1 + app/views/application/_hours_entry_form.html.haml | 8 ++++---- app/views/application/_mileages_entry_form.html.haml | 4 ++-- spec/models/hour_spec.rb | 10 ++++++++++ spec/models/mileage_spec.rb | 10 ++++++++++ 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 22cd8577..5394c3be 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -6,6 +6,8 @@ def index @hours_entry = Hour.new @mileages_entry = Mileage.new @activities = Hour.by_last_created_at.limit(30) + @last_hour_logged = current_user.hours.by_last_updated_at.first + @last_mileage_logged = current_user.mileages.by_last_updated_at.first end def show diff --git a/app/models/hour.rb b/app/models/hour.rb index 5c123639..bf3f6681 100644 --- a/app/models/hour.rb +++ b/app/models/hour.rb @@ -27,6 +27,7 @@ class Hour < Entry accepts_nested_attributes_for :taggings scope :by_last_created_at, -> { order("created_at DESC") } + scope :by_last_updated_at, -> { order("updated_at DESC") } scope :by_date, -> { order("date DESC") } scope :billable, -> { where("billable").joins(:project) } scope :with_clients, -> { diff --git a/app/models/mileage.rb b/app/models/mileage.rb index 0ea6b4a1..4cf17ebd 100644 --- a/app/models/mileage.rb +++ b/app/models/mileage.rb @@ -14,6 +14,7 @@ class Mileage < Entry scope :by_last_created_at, -> { order("created_at DESC") } + scope :by_last_updated_at, -> { order("updated_at DESC") } scope :by_date, -> { order("date DESC") } scope :billable, -> { where("billable").joins(:project) } scope :with_clients, -> { diff --git a/app/views/application/_hours_entry_form.html.haml b/app/views/application/_hours_entry_form.html.haml index ace2929d..176b5c51 100644 --- a/app/views/application/_hours_entry_form.html.haml +++ b/app/views/application/_hours_entry_form.html.haml @@ -1,10 +1,10 @@ = simple_form_for @hours_entry do |f| = f.error_notification - = f.association :project, required: true, collection: Project.unarchived.by_name, label: false, placeholder: t("entries.index.project") - = f.association :category, required: true, collection: Category.by_name, label: false, placeholder: t("entries.index.category") - = f.input :value, required: true, label: false, placeholder: t("entries.index.hours") + = f.association :project, required: true, collection: Project.unarchived.by_name, label: false, placeholder: t("entries.index.project"), selected: @last_hour_logged&.project_id || @hours_entry&.project_id + = f.association :category, required: true, collection: Category.by_name, label: false, placeholder: t("entries.index.category"), selected: @last_hour_logged&.category_id || @hours_entry&.category_id + = f.input :value, required: true, label: false, placeholder: t("entries.index.hours"), input_html: { value: @last_hour_logged&.value || @hours_entry&.value } = f.input :date, required: true, as: :string, input_html: { value: (@hours_entry.date || DateTime.current).strftime("%d/%m/%Y"), class: "datepicker"}, label: false .taggable - = f.input :description, input_html: { data: { data: Tag.list }, autocomplete: :off }, label: false, autocomplete: "off", placeholder: t("entries.index.description") + = f.input :description, input_html: { data: { data: Tag.list }, autocomplete: :off }, label: false, autocomplete: "off", placeholder: t("entries.index.description"), input_html: { value: @last_hour_logged&.description || @hours_entry&.description } %span.background-highlighter = f.button :submit, data: { disable_with: t("loader.saving") } diff --git a/app/views/application/_mileages_entry_form.html.haml b/app/views/application/_mileages_entry_form.html.haml index a1d9edf2..b9d64db5 100644 --- a/app/views/application/_mileages_entry_form.html.haml +++ b/app/views/application/_mileages_entry_form.html.haml @@ -1,6 +1,6 @@ = simple_form_for @mileages_entry do |f| = f.error_notification - = f.association :project, required: true, collection: Project.unarchived.by_name, label: false, placeholder: t("entries.index.project") - = f.input :value, required: true, as: :integer, label:false, placeholder: t("entries.index.mileages") + = f.association :project, required: true, collection: Project.unarchived.by_name, label: false, placeholder: t("entries.index.project"), selected: @last_mileage_logged&.project_id || @mileages_entry&.project_id + = f.input :value, required: true, as: :integer, label:false, placeholder: t("entries.index.mileages"), input_html: { value: @last_mileage_logged&.value || @mileages_entry&.value } = f.input :date, required: true, as: :string, input_html: { value: (@mileages_entry.date || DateTime.current).strftime("%d/%m/%Y"), class: "datepicker"}, label: false = f.button :submit, data: { disable_with: t("loader.saving") } diff --git a/spec/models/hour_spec.rb b/spec/models/hour_spec.rb index b0ba21fb..9b784c9d 100644 --- a/spec/models/hour_spec.rb +++ b/spec/models/hour_spec.rb @@ -90,6 +90,16 @@ end end + describe "#by_last_updated_at" do + it "orders the entries by updated_at" do + initial_hour = create(:hour) + Timecop.scale(600) + create(:hour) + initial_hour.update_attribute(:value, 2) + expect(Hour.by_last_updated_at.first).to eq(initial_hour) + end + end + describe "#by_date" do it "orders the entries by date (latest first)" do create(:hour, date: Date.new(2014, 01, 01)) diff --git a/spec/models/mileage_spec.rb b/spec/models/mileage_spec.rb index 680341d7..9b3ddcba 100644 --- a/spec/models/mileage_spec.rb +++ b/spec/models/mileage_spec.rb @@ -46,6 +46,16 @@ end end + describe "#by_last_updated_at" do + it "orders the entries by updated_at" do + initial_hour = create(:mileage) + Timecop.scale(600) + create(:mileage) + initial_hour.update_attribute(:value, 2) + expect(Mileage.by_last_updated_at.first).to eq(initial_hour) + end + end + describe "by_date" do it "orders the entries by date (latest first)" do create(:mileage, date: Date.new(2014, 01, 01))