Skip to content
This repository has been archived by the owner on Sep 17, 2019. It is now read-only.

Commit

Permalink
Save external_updated_at as metadata for versions
Browse files Browse the repository at this point in the history
This obviates the need reify all versions just to list them.
  • Loading branch information
joegatt committed Sep 3, 2013
1 parent da67232 commit e7ffed0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
5 changes: 3 additions & 2 deletions app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class Note < ActiveRecord::Base
if: proc { |note| (note.external_updated_at - Note.find(note.id).external_updated_at) > Settings.notes.version_gap_minutes.minutes || (Note.find(note.id).word_count - note.word_count).abs > Settings.notes.version_gap_word_count },
unless: proc { |note| note.has_instruction?('reset') || note.has_instruction?('unversion') },
meta: {
word_count: proc { |note| Note.find(note.id).word_count },
external_updated_at: proc { |note| Note.find(note.id).external_updated_at },
instruction_list: proc { |note| Note.find(note.id).instruction_list },
sequence: proc { |note| note.versions.length + 1 }, # To retrieve by version number
tag_list: proc { |note| Note.find(note.id).tag_list }, # Note.tag_list would store incoming tags
instruction_list: proc { |note| Note.find(note.id).instruction_list }
word_count: proc { |note| Note.find(note.id).word_count }
}

default_scope { order('external_updated_at DESC') }
Expand Down
2 changes: 1 addition & 1 deletion app/views/notes/_versions_list.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ nav.versions

ol
- if note.versions.length == 0
li = "v#{ note.versions.length + 1 } (#{ timeago_tag note.external_updated_at }, #{ t('notes.word_count', word_count: number_with_delimiter(note.word_count)) })"
li = "v#{ note.versions.length + 1 } (#{ timeago_tag note.external_updated_at }, #{ t('notes.word_count', word_count: number_with_delimiter(note.word_count)) })".html_safe
- else
li = link_to_unless_current_or_wrap "#{ t('notes.version.title', sequence: note.versions.length + 1) } (#{ timeago_tag note.external_updated_at }), #{ t('notes.word_count', word_count: number_with_delimiter(note.word_count)) })".html_safe, note_version_path(note, note.versions.length + 1)
- note.versions.reverse.each do |version|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddExternalUpdatedAtToVersions < ActiveRecord::Migration
def change
add_column :versions, :external_updated_at, :datetime
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class SeedVersionsWithExternalUpdatedAt < ActiveRecord::Migration
def up
Note.all.each do |n|
n.versions.each do |v|
v.external_updated_at = v.reify.external_updated_at
v.save!
end
end
end

def down
# Nothing to reverse
end
end
11 changes: 6 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20130831114747) do
ActiveRecord::Schema.define(version: 20130903210802) do

create_table "books", force: true do |t|
t.string "title"
Expand Down Expand Up @@ -193,16 +193,17 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

create_table "versions", force: true do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.integer "sequence"
t.text "tag_list"
t.text "instruction_list", limit: 255
t.text "instruction_list", limit: 255
t.integer "word_count"
t.datetime "external_updated_at"
end

add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id"
Expand Down
5 changes: 3 additions & 2 deletions spec/models/note_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@
note.save
end
it 'saves metadata' do
note.versions.last.external_updated_at.to_i.should == note.versions.last.reify.external_updated_at.to_i
note.versions.last.instruction_list = %w(__FIRST_INSTRUCTION)
note.versions.last.sequence.should == note.versions.size
note.versions.last.word_count.should == 2
note.versions.last.tag_list = %w(first_tag)
note.versions.last.instruction_list = %w(__FIRST_INSTRUCTION)
note.versions.last.word_count.should == 2
end
end
end
Expand Down

0 comments on commit e7ffed0

Please sign in to comment.