From d648ab7cb5277ac47bf57911e4d143bab89454c1 Mon Sep 17 00:00:00 2001 From: Ancor Gonzalez Sosa Date: Mon, 8 Jul 2013 10:03:03 +0200 Subject: [PATCH] Add 'revision' attribute to Conference, managed by an observer --- app/models/revision_observer.rb | 26 +++++++++++++++++++ config/application.rb | 1 + ...130705055128_add_revision_to_conference.rb | 5 ++++ db/schema.rb | 3 ++- 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 app/models/revision_observer.rb create mode 100644 db/migrate/20130705055128_add_revision_to_conference.rb diff --git a/app/models/revision_observer.rb b/app/models/revision_observer.rb new file mode 100644 index 00000000..7247efc0 --- /dev/null +++ b/app/models/revision_observer.rb @@ -0,0 +1,26 @@ +# +# suseconferenceclient relies on a 'revision' attribute for caching and +# doing some calculations. +# +# It should be incremented after any change in the conference or in any +# associated models +# +# This observer updates the revision column in a non-intrusive way, +# preventing validations, callbacks or exceptions to be triggered +# +# Relying on paper_trail could also be an option, but a 'revision' column +# in table 'conferences' looks like a more simple and straightforward solution +# +class RevisionObserver < ActiveRecord::Observer + observe :conference, :event, :room, :social_event, :track + + def after_save(model) + begin + conference = model.kind_of?(Conference) ? model : model.conference + conference.reload.increment(:revision) + conference.update_column(:revision, conference.revision) + rescue + nil + end + end +end diff --git a/config/application.rb b/config/application.rb index 76448fbb..4c76b590 100644 --- a/config/application.rb +++ b/config/application.rb @@ -25,6 +25,7 @@ module Osem # Activate observers that should always be running. # config.active_record.observers = :cacher, :garbage_collector, :forum_observer + config.active_record.observers = :revision_observer # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. diff --git a/db/migrate/20130705055128_add_revision_to_conference.rb b/db/migrate/20130705055128_add_revision_to_conference.rb new file mode 100644 index 00000000..728b6551 --- /dev/null +++ b/db/migrate/20130705055128_add_revision_to_conference.rb @@ -0,0 +1,5 @@ +class AddRevisionToConference < ActiveRecord::Migration + def change + add_column :conferences, :revision, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 9acc0c8f..49028ecf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130626095459) do +ActiveRecord::Schema.define(:version => 20130705055128) do create_table "call_for_papers", :force => true do |t| t.date "start_date", :null => false @@ -62,6 +62,7 @@ ActiveRecord::Schema.define(:version => 20130626095459) do t.datetime "logo_updated_at" t.boolean "use_dietary_choices", :default => false t.boolean "use_supporter_levels", :default => false + t.integer "revision" end create_table "dietary_choices", :force => true do |t|