Skip to content

Commit

Permalink
Cleaner session handling (empty hashes do not get created for each co…
Browse files Browse the repository at this point in the history
…mponent on component_session call)
  • Loading branch information
Sergei Kozlov committed Dec 14, 2010
1 parent 1a3fb95 commit d7eb05c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 27 deletions.
10 changes: 6 additions & 4 deletions features/persistence.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ Feature: Persistence
@javascript
Scenario: The component with persistence should be able to store and retrieve a persistence setting
When I go to the ComponentWithSessionPersistence test page
Then I should see "No Title (yet!)"
Then I should see "Default Title"
And I should see "Default HTML"
But I should not see "Title From Session"
And I should not see "HTML from session"

When I press "Tell server to store new title"
And I go to the ComponentWithSessionPersistence test page
Then I should see "New Title!"


Then I should see "Title From Session"
And I should see "HTML from session"

21 changes: 18 additions & 3 deletions lib/netzke/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,30 @@ module Netzke
# This modules provides (component-specific) session manupulation.
# The :session_persistence config option should be set to true in order for the component to make use of this.
module Session
class ComponentSessionProxy < Hash #:nodoc:
def initialize(component_id)
@component_id = component_id
super
end

def [](key)
(Netzke::Core.session[@component_id] || {})[key]
end

def []=(key, value)
(Netzke::Core.session[@component_id] ||= {})[key] = value
# super
end
end

# Top-level session (straight from the controller).
def session
::Netzke::Core.session
end

# Component-specific session.
def component_session
session[global_id] ||= {}
@component_session_proxy ||= ComponentSessionProxy.new(global_id)
end

# Returns this component's configuration options stored in the session. Those get merged into the component's configuration at instantiation.
Expand All @@ -20,8 +36,7 @@ def session_options
# Updates the session options
def update_session_options(hash)
if session_persistence_enabled?
component_session[:options] ||= {}
component_session[:options].merge!(hash)
component_session.deep_merge!(:options => hash)
else
logger.debug "Netzke warning: No session persistence enabled for component '#{global_id}'"
end
Expand Down
2 changes: 1 addition & 1 deletion netzke-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Gem::Specification.new do |s|

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Sergei Kozlov"]
s.date = %q{2010-12-13}
s.date = %q{2010-12-14}
s.description = %q{Allows building ExtJS/Rails reusable code in a DRY way}
s.email = %q{[email protected]}
s.extra_rdoc_files = [
Expand Down
32 changes: 16 additions & 16 deletions test/rails_app/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ GEM
rack-test (>= 0.5.4)
selenium-webdriver (>= 0.0.27)
xpath (~> 0.1.2)
celerity (0.8.4)
celerity (0.8.5)
childprocess (0.1.4)
ffi (~> 0.6.3)
configuration (1.2.0)
cucumber (0.9.4)
builder (~> 2.1.2)
cucumber (0.10.0)
builder (>= 2.1.2)
diff-lcs (~> 1.1.2)
gherkin (~> 2.2.9)
gherkin (~> 2.3.2)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
cucumber-rails (0.3.2)
Expand All @@ -58,7 +58,7 @@ GEM
abstract (>= 1.0.0)
ffi (0.6.3)
rake (>= 0.8.7)
gherkin (2.2.9)
gherkin (2.3.2)
json (~> 1.4.6)
term-ansicolor (~> 1.0.5)
i18n (0.5.0)
Expand All @@ -67,9 +67,9 @@ GEM
launchy (0.3.7)
configuration (>= 0.0.5)
rake (>= 0.8.1)
mail (2.2.11)
mail (2.2.12)
activesupport (>= 2.3.6)
i18n (~> 0.5.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
Expand All @@ -94,19 +94,19 @@ GEM
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rspec (2.2.0)
rspec-core (~> 2.2)
rspec-expectations (~> 2.2)
rspec-mocks (~> 2.2)
rspec-core (2.2.1)
rspec-expectations (2.2.0)
rspec (2.3.0)
rspec-core (~> 2.3.0)
rspec-expectations (~> 2.3.0)
rspec-mocks (~> 2.3.0)
rspec-core (2.3.0)
rspec-expectations (2.3.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.2.0)
rspec-rails (2.2.1)
rspec-mocks (2.3.0)
rspec-rails (2.3.0)
actionpack (~> 3.0)
activesupport (~> 3.0)
railties (~> 3.0)
rspec (~> 2.2.0)
rspec (~> 2.3.0)
rubyzip (0.9.4)
selenium-webdriver (0.1.1)
childprocess (= 0.1.4)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class ComponentWithSessionPersistence < Netzke::Base
js_property :title, "No Title (yet!)"
js_property :title, "Default Title"
js_property :bbar, [{:text => "Tell server to store new title", :ref => "../button"}]

config :default, :session_persistence => true
def default_config
super.merge(:session_persistence => true)
end

def configuration
super.merge(:html => component_session[:html_content] || "Default HTML")
end

js_method :bug_server, <<-JS
function(){
Expand All @@ -18,7 +24,8 @@ class ComponentWithSessionPersistence < Netzke::Base
JS

endpoint :whats_up do |params|
update_session_options(:title => "New Title!")
update_session_options(:title => "Title From Session") # setting a value in session_options, which will get auto-merged into +config+
component_session[:html_content] = "HTML from session" # setting some custom session key/value, which we use manually in +configuration+
{}
end

Expand Down

0 comments on commit d7eb05c

Please sign in to comment.