Skip to content

Commit

Permalink
Allow editing of existing time entries
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesu committed Mar 12, 2010
1 parent f187474 commit abb4fe9
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/controllers/entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def create
end

def edit
respond_to do |f|
f.js {}
end
end

def update
Expand All @@ -41,6 +44,7 @@ def update
f.js {}
else
f.html{ flash.now[:info] = t('response.error'); render :action => :edit }
f.js {}
end
end
end
Expand All @@ -61,6 +65,7 @@ def restart
end

def terminate
@entry.quick_update = true
@entry.terminate

respond_to do |f|
Expand All @@ -83,6 +88,9 @@ def destroy
end

def show
respond_to do |f|
f.js { render :action => :update }
end
end

def report
Expand Down
6 changes: 6 additions & 0 deletions app/helpers/entries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ def new_entry_form(project, client, service)
:entry => Entry.new }
end

def entry_form_for(entry)
remote_form_for entry,
:id => "entry_#{entry.id}_form",
&proc
end

def entry_tag(type, value)
"<div class=\"#{type}\">#{value.escape_html}</div>"
end
Expand Down
47 changes: 41 additions & 6 deletions app/models/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ class Entry < ActiveRecord::Base
belongs_to :service

before_create :check_entry
before_update :check_entry_update

attr_accessible :content, :service_id, :project_id

attr_accessor :quick_update

def start(started_at=nil)
self.start_date = started_at || Time.now
self.seconds = nil
Expand All @@ -20,9 +23,14 @@ def start(started_at=nil)
TIMEVAL_UNIT_REGEXP = /[0-9]+[sSHhMm]/
TAG_REGEXP = /[#\@][a-zA-Z0-9\-_]*/

def format_entry
def format_entry(origin_base=nil)
found_service = nil
found_project = nil
time_now = Time.now

if origin_base
time_now = origin_base
end

gen = content.gsub(TAG_REGEXP) do |tag|
is_project = tag[0...1] == '@' and found_project.nil?
Expand Down Expand Up @@ -79,16 +87,16 @@ def format_entry
# Determine start
if delta_inc
# will be spending x time on it
found_start = Time.now
found_start = time_now
found_done = in_progress ? nil : found_start + delta_s
elsif delta_dec
# spent x time on it already
last_ended = Time.now
last_ended = time_now
found_done = in_progress ? nil : last_ended
found_start = last_ended - delta_s
else
# expecting to spend x time on it [not confirmed]
found_start = Time.now
found_start = time_now
found_done = nil
found_limit = delta_s
end
Expand All @@ -97,12 +105,17 @@ def format_entry
end

self.content_html = gen
{:service => found_service, :project => found_project, :start_date => found_start, :done_date => found_done, :limit => found_limit}
{ :service => found_service,
:project => found_project,
:start_date => found_start,
:done_date => found_done,
:limit => found_limit,
:origin_time => time_now }
end

def check_entry
built = format_entry

self.original_start = built[:origin_time]

self.service = built[:service] if built[:service]
self.project = built[:project] if built[:project]
Expand All @@ -117,6 +130,28 @@ def check_entry
self.start if self.start_date.nil?
end

def check_entry_update
unless @quick_update
# Like check_entry, except we maintain start_date
# We also maintain done_date unless it was specified
# in the content
built = format_entry(self.original_start)

self.service = built[:service] if built[:service]
self.project = built[:project] if built[:project]
self.service ||= self.user.default_service
self.project ||= self.user.default_project

unless self.original_start.nil?
self.start_date = built[:start_date]
self.done_date ||= built[:done_date]
self.seconds_limit = built[:limit]
end
end

self.seconds = current_time unless self.done_date.nil?
end

def terminate
self.done_date = Time.now
self.seconds = self.done_date - self.start_date
Expand Down
9 changes: 9 additions & 0 deletions app/views/entries/_edit.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.entry{:id => "entry_#{entry.id}"}
- entry_form_for entry do |f|
= f.text_field :content
.formActions
= f.submit t('.submit')
= t('.or')
= link_to_remote t('.remove'), :url => entry_path(entry), :method => :delete
= t('.or')
= link_to_remote t('.cancel'), :url => entry_path(entry), :method => :get
3 changes: 2 additions & 1 deletion app/views/entries/_entry.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.entry{:id => "entry_#{entry.id}"}
.actions
.actions
= link_to_remote t('actions.edit'), :url => edit_entry_path(entry), :method => :get
- unless entry.terminated?
= link_to_remote t('actions.finish'), :url => terminate_entry_path(entry), :method => :put
.entryContent{:class => entry.is_overdue? ? 'overdue' : nil}= entry.content_html
Expand Down
2 changes: 2 additions & 0 deletions app/views/entries/edit.js.rjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
page.call "Timer.remove", @entry.id
page.replace "entry_#{@entry.id}", :partial => 'edit', :locals => {:entry => @entry}
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ en:
what_status: "What are you doing? (<a href=\"/taglist.html\" target=\"_blank\">Help?</a>)"
more:
display_x_more_entries: "Display {{num}} more entries"
edit:
submit: Update
or: or
remove: Delete
cancel: Cancel
projects:
create: Create
update: Update
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
t.integer "service_id"
t.integer "project_id"
t.integer "user_id"
t.datetime "original_start",:default => nil # time which all deltas were calculated from
t.datetime "start_date", :default => nil
t.datetime "done_date", :default => nil
t.integer "seconds", :default => 0
Expand Down
4 changes: 4 additions & 0 deletions public/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,7 @@ span.time {
padding-left: 2px;
padding-right: 2px;
}

form .formActions {
text-align: right;
}
7 changes: 7 additions & 0 deletions public/taglist.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
<div class='entryContent'><span class="project">@wdom</span> fleeing to next evil lair <span class="service">#run</span> <span class="time">-5M+</span></div>
<p class="desc">You have spent 5 minutes fleeing to your next evil lair, and are still in the process of doing so. When done, use "Finish" to mark as complete.</p>
</div>
<h2>Note on editing</h2>
<div class='entry'>
<div class='entryContent'>Entries base their time calculations around the time the entry was first created. e.g. changing <span class="time">15M</span> to <span class="time">-15M+</span> will backdate the entry by 15 minutes.</div>
</div>
<div class='entry'>
<div class='entryContent'>Entries which have finished will maintain their end date. e.g. changing <span class="time">-5M</span> to <span class="time">5M</span> results in 0S.</div>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit abb4fe9

Please sign in to comment.