netzke-persistence 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/README.rdoc +41 -0
  2. data/Rakefile +58 -0
  3. data/app/models/netzke_component_state.rb +103 -0
  4. data/config/database.yml +2 -0
  5. data/features/component_with_persistence.feature +20 -0
  6. data/features/step_definitions/custom_css_steps.rb +7 -0
  7. data/features/step_definitions/generic_steps.rb +15 -0
  8. data/features/step_definitions/web_steps.rb +219 -0
  9. data/features/support/env.rb +59 -0
  10. data/features/support/paths.rb +45 -0
  11. data/generators/USAGE +1 -0
  12. data/generators/netzke/templates/create_netzke_component_states.rb +20 -0
  13. data/lib/generators/USAGE +8 -0
  14. data/lib/generators/netzke/persistence_generator.rb +21 -0
  15. data/lib/generators/netzke/templates/create_netzke_component_states.rb +20 -0
  16. data/lib/netzke-persistence.rb +6 -0
  17. data/lib/netzke/persistence.rb +5 -0
  18. data/lib/netzke/persistence/version.rb +5 -0
  19. data/netzke-persistence.gemspec +162 -0
  20. data/spec/factories.rb +10 -0
  21. data/spec/netzke_component_state_spec.rb +75 -0
  22. data/spec/spec_helper.rb +35 -0
  23. data/test/rails_app/.gitignore +4 -0
  24. data/test/rails_app/Gemfile +32 -0
  25. data/test/rails_app/Gemfile.lock +164 -0
  26. data/test/rails_app/app/components/component_with_persistence.rb +31 -0
  27. data/test/rails_app/app/controllers/application_controller.rb +3 -0
  28. data/test/rails_app/app/controllers/components_controller.rb +6 -0
  29. data/test/rails_app/app/controllers/sessions_controller.rb +2 -0
  30. data/test/rails_app/app/controllers/welcome_controller.rb +5 -0
  31. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  32. data/test/rails_app/app/helpers/sessions_helper.rb +2 -0
  33. data/test/rails_app/app/helpers/welcome_helper.rb +2 -0
  34. data/test/rails_app/app/models/role.rb +2 -0
  35. data/test/rails_app/app/models/user.rb +11 -0
  36. data/test/rails_app/app/views/devise/confirmations/new.html.erb +12 -0
  37. data/test/rails_app/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  38. data/test/rails_app/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  39. data/test/rails_app/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  40. data/test/rails_app/app/views/devise/passwords/edit.html.erb +16 -0
  41. data/test/rails_app/app/views/devise/passwords/new.html.erb +12 -0
  42. data/test/rails_app/app/views/devise/registrations/edit.html.erb +25 -0
  43. data/test/rails_app/app/views/devise/registrations/new.html.erb +18 -0
  44. data/test/rails_app/app/views/devise/sessions/new.html.erb +17 -0
  45. data/test/rails_app/app/views/devise/shared/_links.erb +19 -0
  46. data/test/rails_app/app/views/devise/unlocks/new.html.erb +12 -0
  47. data/test/rails_app/app/views/layouts/application.html.erb +13 -0
  48. data/test/rails_app/app/views/welcome/index.html.erb +1 -0
  49. data/test/rails_app/config.ru +4 -0
  50. data/test/rails_app/config/application.rb +46 -0
  51. data/test/rails_app/config/boot.rb +13 -0
  52. data/test/rails_app/config/database.yml +22 -0
  53. data/test/rails_app/config/environment.rb +5 -0
  54. data/test/rails_app/config/environments/development.rb +26 -0
  55. data/test/rails_app/config/environments/production.rb +49 -0
  56. data/test/rails_app/config/environments/test.rb +35 -0
  57. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  58. data/test/rails_app/config/initializers/devise.rb +142 -0
  59. data/test/rails_app/config/initializers/inflections.rb +10 -0
  60. data/test/rails_app/config/initializers/mime_types.rb +5 -0
  61. data/test/rails_app/config/initializers/secret_token.rb +7 -0
  62. data/test/rails_app/config/initializers/session_store.rb +8 -0
  63. data/test/rails_app/config/locales/devise.en.yml +39 -0
  64. data/test/rails_app/config/locales/en.yml +5 -0
  65. data/test/rails_app/config/routes.rb +68 -0
  66. data/test/rails_app/db/migrate/20101102185654_create_netzke_component_states.rb +16 -0
  67. data/test/rails_app/db/migrate/20101102195851_devise_create_users.rb +26 -0
  68. data/test/rails_app/db/migrate/20101103195859_create_roles.rb +13 -0
  69. data/test/rails_app/db/schema.rb +50 -0
  70. data/test/rails_app/db/seeds.rb +7 -0
  71. data/test/rails_app/lib/generators/netzke_persistence/USAGE +8 -0
  72. data/test/rails_app/lib/generators/netzke_persistence/netzke_persistence_generator.rb +3 -0
  73. data/test/rails_app/lib/tasks/.gitkeep +0 -0
  74. data/test/rails_app/public/404.html +26 -0
  75. data/test/rails_app/public/422.html +26 -0
  76. data/test/rails_app/public/500.html +26 -0
  77. data/test/rails_app/public/favicon.ico +0 -0
  78. data/test/rails_app/public/images/rails.png +0 -0
  79. data/test/rails_app/public/robots.txt +5 -0
  80. metadata +175 -0
@@ -0,0 +1,41 @@
1
+ = netzke-persistence
2
+
3
+ A drop-in gem to enable persistence in Netzke components.
4
+
5
+ == Installation
6
+
7
+ In your Gemfile:
8
+
9
+ gem 'netzke-persistence'
10
+
11
+ From your app directory run:
12
+
13
+ rails generate netzke:persistence && rake db:migrate
14
+
15
+ == Usage
16
+
17
+ Refer to http://api.netzke.org/core/Netzke/State.html
18
+
19
+ For an example, see ComponentWithPersistence in test/rails_app/app/components.
20
+
21
+ == Running tests
22
+
23
+ Sym-link Ext JS library into the test app, e.g. (depends on where your Ext JS is located):
24
+
25
+ ln -s ~/code/sencha/ext-3.3.0 test/rails_app/public/extjs
26
+
27
+ Run bundle install from inside the test app:
28
+
29
+ cd test/rails_app && bundle install && cd -
30
+
31
+ To run cucumber tests:
32
+
33
+ cucumber features
34
+
35
+ To run RSpec tests:
36
+
37
+ rspec spec
38
+
39
+ ---
40
+
41
+ Copyright (c) 2008-2010 Sergei Kozlov, released under the MIT license
@@ -0,0 +1,58 @@
1
+ begin
2
+ require 'jeweler'
3
+ require './lib/netzke/persistence/version'
4
+ Jeweler::Tasks.new do |gem|
5
+ gem.version = Netzke::Persistence::VERSION
6
+ gem.name = "netzke-persistence"
7
+ gem.summary = "Persistence subsystem for the Netzke framework"
8
+ gem.description = "A drop-in gem to enable persistence in Netzke components"
9
+ gem.email = "sergei@playcode.nl"
10
+ gem.homepage = "http://netzke.org"
11
+ gem.authors = ["Sergei Kozlov"]
12
+ gem.post_install_message = <<-MESSAGE
13
+
14
+ ========================================================================
15
+
16
+ Thanks for installing netzke-persistence!
17
+
18
+ Don't forget to run "rails generate netzke:persistence"
19
+
20
+ Netzke home page: http://netzke.org
21
+ Netzke Google Groups: http://groups.google.com/group/netzke
22
+ Netzke tutorials: http://blog.writelesscode.com
23
+
24
+ ========================================================================
25
+
26
+ MESSAGE
27
+
28
+ end
29
+ Jeweler::GemcutterTasks.new
30
+ rescue LoadError
31
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
32
+ end
33
+
34
+ require 'rake/testtask'
35
+ Rake::TestTask.new(:test) do |test|
36
+ test.libs << 'lib' << 'test'
37
+ test.pattern = 'test/**/*_test.rb'
38
+ test.verbose = true
39
+ end
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ require './lib/netzke/persistence/version'
44
+ version = Netzke::Persistence::VERSION
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "netzke-persistence #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('CHANGELOG*')
50
+ rdoc.rdoc_files.include('lib/**/*.rb')
51
+ end
52
+
53
+ namespace :rdoc do
54
+ desc "Publish rdocs"
55
+ task :publish => :rdoc do
56
+ `scp -r rdoc/* fl:www/api.netzke.org/persistence`
57
+ end
58
+ end
@@ -0,0 +1,103 @@
1
+ # An emplementation of a state manager with a support for world/role/user-level masquerading.
2
+ # You can simply replace it with your own (e.g. you may not like ActiveRecord, or have different needs for masquerading),
3
+ # as long as it implements the following class-methods:
4
+ # * +init+ - gets called before anything else, and accepts a hash with the following keys:
5
+ # * +component+ should be set to component's persistence key (e.g. Netzke::Base#global_id)
6
+ # * +current_user+ should be set to the current user object
7
+ # * +session+ should be set to the controller's session
8
+ # * +state+ - returns the persistent state (a hash) for the component specified with the +init+ call
9
+ # * +update_state+ - accepts a hash, that should get merged with the current +state+
10
+ #
11
+ # == Assumptions
12
+ # This particular implementation assumes the following:
13
+ # * ActiveRecord as ORM
14
+ # * Current user belong_to Role
15
+ # * User#id is user's primary key
16
+ # * session[:masquerade_as] is a hash:
17
+ # {:world => true} # to masquerade as World
18
+ # {:role_id => some_role_id} # to masquerade as a role
19
+ # {:user_id => some_user_id} # to masquerade as a user
20
+ #
21
+ # == Class-level configuration:
22
+ # * +masquerade_as_session_key+ - defaults to :masquerade_as
23
+ #
24
+ class NetzkeComponentState < ActiveRecord::Base
25
+ serialize :value
26
+
27
+ class_attribute :masquerade_as_session_key
28
+ self.masquerade_as_session_key = :masquerade_as
29
+
30
+ belongs_to :user
31
+ belongs_to :role
32
+
33
+ class << self
34
+ def init(config)
35
+ @@config ||= {}
36
+ @@config.merge!(config)
37
+ self
38
+ end
39
+
40
+ def session
41
+ @@config[:session]
42
+ end
43
+
44
+ def component
45
+ @@config[:component]
46
+ end
47
+
48
+ def current_user
49
+ @@config[:current_user]
50
+ end
51
+
52
+ def masquerade_as
53
+ session[masquerade_as_session_key]
54
+ end
55
+
56
+ def state
57
+ find_state.try(:value)
58
+ end
59
+
60
+ def update_state!(hsh)
61
+ state_record = find_state(true)
62
+ new_value = (state_record.value || {}).merge(hsh)
63
+ new_value.delete_if{ |k,v| v.nil? } # setting values to nil means deleting them
64
+ state_record.value = new_value
65
+ state_record.save!
66
+
67
+ propagate(hsh)
68
+ end
69
+
70
+ private
71
+
72
+ def propagate(hsh)
73
+ if masquerade_as.present? && !masquerade_as[:user_id]
74
+ relation = self.where({:component => component})
75
+ if masquerade_as[:role_id]
76
+ relation = relation.includes(:user).where(:users => {:role_id => masquerade_as[:role_id]})
77
+ end
78
+ relation.all.each do |r|
79
+ r.value = r.value.merge(hsh)
80
+ r.save!
81
+ end
82
+ end
83
+ end
84
+
85
+ def find_state(or_create_new = false)
86
+ hsh = {:component => component}
87
+ if masquerade_as.present?
88
+ if masquerade_as[:world]
89
+ hsh.merge!(:role_id => 0)
90
+ elsif masquerade_as[:role_id]
91
+ hsh.merge!(:role_id => masquerade_as[:role_id])
92
+ elsif masquerade_as[:user_id]
93
+ hsh.merge!(:user_id => masquerade_as[:user_id])
94
+ end
95
+ elsif current_user
96
+ hsh.merge!(:user_id => current_user.id)
97
+ end
98
+
99
+ self.where(hsh).first || (or_create_new ? self.new(hsh) : nil)
100
+ end
101
+
102
+ end
103
+ end
@@ -0,0 +1,2 @@
1
+ # This dummy file is only here to please cucumber environment
2
+ test:
@@ -0,0 +1,20 @@
1
+ Feature: Component with persistence
2
+ In order to value
3
+ As a role
4
+ I want feature
5
+
6
+ @javascript
7
+ Scenario: ComponentWithPersistence should behave
8
+ When I go to the ComponentWithPersistence test page
9
+ Then I should see "Default title"
10
+
11
+ When I press "Change title"
12
+ And I go to the ComponentWithPersistence test page
13
+ Then I should see "New title"
14
+ But I should not see "Default title"
15
+
16
+ When I press "Set original title"
17
+ And I go to the ComponentWithPersistence test page
18
+ Then I should see "Default title"
19
+ But I should not see "New title"
20
+
@@ -0,0 +1,7 @@
1
+ Then /^the body of (.+) component should not be invisible$/ do |component|
2
+ component_id = component.split("/").map{ |klass| klass.underscore }.join("__")
3
+ page.wait_until{ page.evaluate_script("!Ext.Ajax.isLoading()") }
4
+ page.execute_script(<<-JS).should == false
5
+ return Ext.getCmp('#{component_id}').body.isVisible();
6
+ JS
7
+ end
@@ -0,0 +1,15 @@
1
+ Then /^Netzke should be initialized$/ do
2
+ Netzke::Base.should be
3
+ end
4
+
5
+ When /^I execute "([^\"]*)"$/ do |script|
6
+ page.driver.browser.execute_script(script)
7
+ end
8
+
9
+ Then /^button "([^\"]*)" should be disabled$/ do |arg1|
10
+ pending
11
+ end
12
+
13
+ When /^I sleep 1 second$/ do
14
+ sleep 1
15
+ end
@@ -0,0 +1,219 @@
1
+ # IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
2
+ # It is recommended to regenerate this file in the future when you upgrade to a
3
+ # newer version of cucumber-rails. Consider adding your own code to a new file
4
+ # instead of editing this one. Cucumber will automatically load all features/**/*.rb
5
+ # files.
6
+
7
+
8
+ require 'uri'
9
+ require 'cgi'
10
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
11
+
12
+ module WithinHelpers
13
+ def with_scope(locator)
14
+ locator ? within(locator) { yield } : yield
15
+ end
16
+ end
17
+ World(WithinHelpers)
18
+
19
+ Given /^(?:|I )am on (.+)$/ do |page_name|
20
+ visit path_to(page_name)
21
+ end
22
+
23
+ When /^(?:|I )go to (.+)$/ do |page_name|
24
+ visit path_to(page_name)
25
+ end
26
+
27
+ When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector|
28
+ with_scope(selector) do
29
+ click_button(button)
30
+ end
31
+ end
32
+
33
+ When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector|
34
+ with_scope(selector) do
35
+ click_link(link)
36
+ end
37
+ end
38
+
39
+ When /^(?:|I )fill in "([^"]*)" with "([^"]*)"(?: within "([^"]*)")?$/ do |field, value, selector|
40
+ with_scope(selector) do
41
+ fill_in(field, :with => value)
42
+ end
43
+ end
44
+
45
+ When /^(?:|I )fill in "([^"]*)" for "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
46
+ with_scope(selector) do
47
+ fill_in(field, :with => value)
48
+ end
49
+ end
50
+
51
+ # Use this to fill in an entire form with data from a table. Example:
52
+ #
53
+ # When I fill in the following:
54
+ # | Account Number | 5002 |
55
+ # | Expiry date | 2009-11-01 |
56
+ # | Note | Nice guy |
57
+ # | Wants Email? | |
58
+ #
59
+ # TODO: Add support for checkbox, select og option
60
+ # based on naming conventions.
61
+ #
62
+ When /^(?:|I )fill in the following(?: within "([^"]*)")?:$/ do |selector, fields|
63
+ with_scope(selector) do
64
+ fields.rows_hash.each do |name, value|
65
+ When %{I fill in "#{name}" with "#{value}"}
66
+ end
67
+ end
68
+ end
69
+
70
+ When /^(?:|I )select "([^"]*)" from "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
71
+ with_scope(selector) do
72
+ select(value, :from => field)
73
+ end
74
+ end
75
+
76
+ When /^(?:|I )check "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
77
+ with_scope(selector) do
78
+ check(field)
79
+ end
80
+ end
81
+
82
+ When /^(?:|I )uncheck "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
83
+ with_scope(selector) do
84
+ uncheck(field)
85
+ end
86
+ end
87
+
88
+ When /^(?:|I )choose "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
89
+ with_scope(selector) do
90
+ choose(field)
91
+ end
92
+ end
93
+
94
+ When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"(?: within "([^"]*)")?$/ do |path, field, selector|
95
+ with_scope(selector) do
96
+ attach_file(field, path)
97
+ end
98
+ end
99
+
100
+ Then /^(?:|I )should see JSON:$/ do |expected_json|
101
+ require 'json'
102
+ expected = JSON.pretty_generate(JSON.parse(expected_json))
103
+ actual = JSON.pretty_generate(JSON.parse(response.body))
104
+ expected.should == actual
105
+ end
106
+
107
+ Then /^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
108
+ with_scope(selector) do
109
+ if page.respond_to? :should
110
+ page.should have_content(text)
111
+ else
112
+ assert page.has_content?(text)
113
+ end
114
+ end
115
+ end
116
+
117
+ Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
118
+ regexp = Regexp.new(regexp)
119
+ with_scope(selector) do
120
+ if page.respond_to? :should
121
+ page.should have_xpath('//*', :text => regexp)
122
+ else
123
+ assert page.has_xpath?('//*', :text => regexp)
124
+ end
125
+ end
126
+ end
127
+
128
+ Then /^(?:|I )should not see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
129
+ with_scope(selector) do
130
+ if page.respond_to? :should
131
+ page.should have_no_content(text)
132
+ else
133
+ assert page.has_no_content?(text)
134
+ end
135
+ end
136
+ end
137
+
138
+ Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
139
+ regexp = Regexp.new(regexp)
140
+ with_scope(selector) do
141
+ if page.respond_to? :should
142
+ page.should have_no_xpath('//*', :text => regexp)
143
+ else
144
+ assert page.has_no_xpath?('//*', :text => regexp)
145
+ end
146
+ end
147
+ end
148
+
149
+ Then /^the "([^"]*)" field(?: within "([^"]*)")? should contain "([^"]*)"$/ do |field, selector, value|
150
+ with_scope(selector) do
151
+ field = find_field(field)
152
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
153
+ if field_value.respond_to? :should
154
+ field_value.should =~ /#{value}/
155
+ else
156
+ assert_match(/#{value}/, field_value)
157
+ end
158
+ end
159
+ end
160
+
161
+ Then /^the "([^"]*)" field(?: within "([^"]*)")? should not contain "([^"]*)"$/ do |field, selector, value|
162
+ with_scope(selector) do
163
+ field = find_field(field)
164
+ field_value = (field.tag_name == 'textarea') ? field.text : field.value
165
+ if field_value.respond_to? :should_not
166
+ field_value.should_not =~ /#{value}/
167
+ else
168
+ assert_no_match(/#{value}/, field_value)
169
+ end
170
+ end
171
+ end
172
+
173
+ Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should be checked$/ do |label, selector|
174
+ with_scope(selector) do
175
+ field_checked = find_field(label)['checked']
176
+ if field_checked.respond_to? :should
177
+ field_checked.should be_true
178
+ else
179
+ assert field_checked
180
+ end
181
+ end
182
+ end
183
+
184
+ Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should not be checked$/ do |label, selector|
185
+ with_scope(selector) do
186
+ field_checked = find_field(label)['checked']
187
+ if field_checked.respond_to? :should
188
+ field_checked.should be_false
189
+ else
190
+ assert !field_checked
191
+ end
192
+ end
193
+ end
194
+
195
+ Then /^(?:|I )should be on (.+)$/ do |page_name|
196
+ current_path = URI.parse(current_url).path
197
+ if current_path.respond_to? :should
198
+ current_path.should == path_to(page_name)
199
+ else
200
+ assert_equal path_to(page_name), current_path
201
+ end
202
+ end
203
+
204
+ Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
205
+ query = URI.parse(current_url).query
206
+ actual_params = query ? CGI.parse(query) : {}
207
+ expected_params = {}
208
+ expected_pairs.rows_hash.each_pair{|k,v| expected_params[k] = v.split(',')}
209
+
210
+ if actual_params.respond_to? :should
211
+ actual_params.should == expected_params
212
+ else
213
+ assert_equal expected_params, actual_params
214
+ end
215
+ end
216
+
217
+ Then /^show me the page$/ do
218
+ save_and_open_page
219
+ end