From: Tomas Sedovic <tsedovic(a)redhat.com>
Previously, the session-only keys were being filtered out right when they were
assigned to the ViewState instance.
This fix does the filtering only when the data is being saved into the
database.
---
src/app/models/view_state.rb | 16 +++++++++++++---
src/spec/models/view_state_spec.rb | 10 ++++++----
2 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/src/app/models/view_state.rb b/src/app/models/view_state.rb
index 7e0d982..e677b81 100644
--- a/src/app/models/view_state.rb
+++ b/src/app/models/view_state.rb
@@ -23,10 +23,22 @@ class ViewState < ActiveRecord::Base
validates_presence_of :action
validates_presence_of :state
+ before_save :strip_session_only_keys
+
+ attr_reader :pending_delete
+
+ def mark_for_deletion
+ @pending_delete = true
+ end
+
# Things that make sense to remember for the session ViewState but not for
# the saved state -- such as position in the pagination.
SESSION_ONLY_KEYS = ['page']
+ def strip_session_only_keys
+ self.state = state.reject { |k,v| SESSION_ONLY_KEYS.include? k }
+ end
+
def state
unless read_attribute(:state).blank?
JSON.load(read_attribute :state)
@@ -34,8 +46,6 @@ class ViewState < ActiveRecord::Base
end
def state=(attrs)
- attrs = attrs || {}
- persistent_attributes = attrs.reject { |k,v| SESSION_ONLY_KEYS.include? k }
- write_attribute :state, JSON.dump(persistent_attributes)
+ write_attribute :state, JSON.dump(attrs ||= {})
end
end
diff --git a/src/spec/models/view_state_spec.rb b/src/spec/models/view_state_spec.rb
index 8c8ba06..ff4ba10 100644
--- a/src/spec/models/view_state_spec.rb
+++ b/src/spec/models/view_state_spec.rb
@@ -66,12 +66,14 @@ describe ViewState do
end
it "should not save attributes that are only relevant in a user session" do
- @view_state.state = { 'sort-column' => 'name', 'page'
=> 3 }
+ session_state = { 'sort-column' => 'name', 'page' => 3
}
+ @view_state.state = session_state
+ @view_state.state.should eql(session_state)
@view_state.save!
- expected_attributes = {'sort-column' => 'name'}
- @view_state.state.should eql(expected_attributes)
- ViewState.find((a)view_state).state.should eql(expected_attributes)
+ expected_saved_state = {'sort-column' => 'name'}
+ @view_state.state.should eql(expected_saved_state)
+ ViewState.find((a)view_state).state.should eql(expected_saved_state)
end
end
--
1.7.4.4