grosser-userstamp 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/CHANGELOG +26 -0
  2. data/LICENSE +20 -0
  3. data/README +179 -0
  4. data/Rakefile +36 -0
  5. data/init.rb +1 -0
  6. data/lib/ddb/userstamp.rb +44 -0
  7. data/lib/ddb/userstamp/migration_helper.rb +19 -0
  8. data/lib/ddb/userstamp/stampable.rb +151 -0
  9. data/lib/ddb/userstamp/stamper.rb +43 -0
  10. data/lib/userstamp.rb +8 -0
  11. data/rdoc/classes/Ddb/Controller.html +111 -0
  12. data/rdoc/classes/Ddb/Controller/Userstamp.html +125 -0
  13. data/rdoc/classes/Ddb/Controller/Userstamp/InstanceMethods.html +105 -0
  14. data/rdoc/classes/Ddb/Userstamp.html +121 -0
  15. data/rdoc/classes/Ddb/Userstamp/MigrationHelper.html +111 -0
  16. data/rdoc/classes/Ddb/Userstamp/MigrationHelper/InstanceMethods.html +142 -0
  17. data/rdoc/classes/Ddb/Userstamp/Stampable.html +128 -0
  18. data/rdoc/classes/Ddb/Userstamp/Stampable/ClassMethods.html +222 -0
  19. data/rdoc/classes/Ddb/Userstamp/Stamper.html +112 -0
  20. data/rdoc/classes/Ddb/Userstamp/Stamper/ClassMethods.html +142 -0
  21. data/rdoc/classes/Ddb/Userstamp/Stamper/InstanceMethods.html +207 -0
  22. data/rdoc/files/CHANGELOG.html +137 -0
  23. data/rdoc/files/LICENSE.html +129 -0
  24. data/rdoc/files/README.html +341 -0
  25. data/rdoc/files/lib/ddb/userstamp/migration_helper_rb.html +101 -0
  26. data/rdoc/files/lib/ddb/userstamp/stampable_rb.html +101 -0
  27. data/rdoc/files/lib/ddb/userstamp/stamper_rb.html +101 -0
  28. data/rdoc/files/lib/ddb/userstamp_rb.html +101 -0
  29. data/rdoc/files/lib/userstamp_rb.html +114 -0
  30. data/test/compatibility_stamping_test.rb +63 -0
  31. data/test/controllers/posts_controller.rb +26 -0
  32. data/test/controllers/users_controller.rb +12 -0
  33. data/test/controllers/userstamp_controller.rb +9 -0
  34. data/test/database.yml +4 -0
  35. data/test/fixtures/comments.yml +16 -0
  36. data/test/fixtures/people.yml +11 -0
  37. data/test/fixtures/posts.yml +9 -0
  38. data/test/fixtures/users.yml +7 -0
  39. data/test/helpers/functional_test_helper.rb +37 -0
  40. data/test/helpers/unit_test_helper.rb +29 -0
  41. data/test/models/comment.rb +4 -0
  42. data/test/models/person.rb +3 -0
  43. data/test/models/ping.rb +7 -0
  44. data/test/models/post.rb +4 -0
  45. data/test/models/user.rb +3 -0
  46. data/test/schema.rb +56 -0
  47. data/test/stamping_test.rb +110 -0
  48. data/test/userstamp_controller_test.rb +118 -0
  49. data/userstamp.gemspec +53 -0
  50. metadata +180 -0
@@ -0,0 +1,63 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'helpers/unit_test_helper'
4
+ Ddb::Userstamp.compatibility_mode = true
5
+ require 'models/user'
6
+ require 'models/person'
7
+ require 'models/post'
8
+ require 'models/comment'
9
+
10
+ class CompatibilityStampingTests< Test::Unit::TestCase # :nodoc:
11
+ fixtures :people, :comments
12
+
13
+ def setup
14
+ Person.stamper = @delynn
15
+ end
16
+
17
+ def test_comment_creation_with_stamped_object
18
+ assert_equal @delynn.id, Person.stamper
19
+
20
+ comment = Comment.create(:comment => "Test Comment")
21
+ assert_equal @delynn.id, comment.created_by
22
+ assert_equal @delynn.id, comment.updated_by
23
+ assert_equal @delynn, comment.creator
24
+ assert_equal @delynn, comment.updater
25
+ end
26
+
27
+ def test_comment_creation_with_stamped_integer
28
+ Person.stamper = 2
29
+ assert_equal 2, Person.stamper
30
+
31
+ comment = Comment.create(:comment => "Test Comment - 2")
32
+ assert_equal @nicole.id, comment.created_by
33
+ assert_equal @nicole.id, comment.updated_by
34
+ assert_equal @nicole, comment.creator
35
+ assert_equal @nicole, comment.updater
36
+ end
37
+
38
+ def test_comment_updating_with_stamped_object
39
+ Person.stamper = @nicole
40
+ assert_equal @nicole.id, Person.stamper
41
+
42
+ @first_comment.comment << " - Updated"
43
+ @first_comment.save
44
+ @first_comment.reload
45
+ assert_equal @delynn.id, @first_comment.created_by
46
+ assert_equal @nicole.id, @first_comment.updated_by
47
+ assert_equal @delynn, @first_comment.creator
48
+ assert_equal @nicole, @first_comment.updater
49
+ end
50
+
51
+ def test_comment_updating_with_stamped_integer
52
+ Person.stamper = 2
53
+ assert_equal 2, Person.stamper
54
+
55
+ @first_comment.comment << " - Updated"
56
+ @first_comment.save
57
+ @first_comment.reload
58
+ assert_equal @delynn.id, @first_comment.created_by
59
+ assert_equal @nicole.id, @first_comment.updated_by
60
+ assert_equal @delynn, @first_comment.creator
61
+ assert_equal @nicole, @first_comment.updater
62
+ end
63
+ end
@@ -0,0 +1,26 @@
1
+ class PostsController < UserstampController
2
+ def edit
3
+ @post = Post.find(params[:id])
4
+ render(:inline => "<%= @post.title %>")
5
+ end
6
+
7
+ def update
8
+ @post = Post.find(params[:id])
9
+ @post.update_attributes(params[:post])
10
+ render(:inline => "<%= @post.title %>")
11
+ end
12
+
13
+ protected
14
+ def current_user
15
+ Person.find(session[:person_id])
16
+ end
17
+
18
+ def set_stamper
19
+ Person.stamper = self.current_user
20
+ end
21
+
22
+ def reset_stamper
23
+ Person.reset_stamper
24
+ end
25
+ #end
26
+ end
@@ -0,0 +1,12 @@
1
+ class UsersController < UserstampController
2
+ def edit
3
+ @user = User.find(params[:id])
4
+ render(:inline => "<%= @user.name %>")
5
+ end
6
+
7
+ def update
8
+ @user = User.find(params[:id])
9
+ @user.update_attributes(params[:user])
10
+ render(:inline => "<%= @user.name %>")
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class UserstampController < ActionController::Base
2
+ include Ddb::Controller::Userstamp
3
+
4
+ protected
5
+ def current_user
6
+ User.find(session[:user_id])
7
+ end
8
+ #end
9
+ end
data/test/database.yml ADDED
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: userstamp_plugin_test.sqlite3
4
+ timeout: 5000
@@ -0,0 +1,16 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ first_comment:
3
+ id: 1
4
+ post_id: 1
5
+ comment: First Comment
6
+ created_by: 1
7
+ second_comment:
8
+ id: 2
9
+ post_id: 1
10
+ comment: Second Comment
11
+ created_by: 2
12
+ third_comment:
13
+ id: 3
14
+ post_id: 2
15
+ comment: Third Comment
16
+ created_by: 1
@@ -0,0 +1,11 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ delynn:
3
+ id: 1
4
+ name: DeLynn
5
+ creator_id: 1
6
+ updater_id: 1
7
+ nicole:
8
+ id: 2
9
+ name: Nicole
10
+ creator_id: 2
11
+ updater_id: 2
@@ -0,0 +1,9 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ first_post:
3
+ id: 1
4
+ title: First Post
5
+ creator_id: 1
6
+ second_post:
7
+ id: 2
8
+ title: Second Post
9
+ creator_id: 1
@@ -0,0 +1,7 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ zeus:
3
+ id: 1
4
+ name: Zeus
5
+ hera:
6
+ id: 2
7
+ name: Hera
@@ -0,0 +1,37 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../..')
2
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
3
+ schema_file = File.join(File.dirname(__FILE__), '..', 'schema.rb')
4
+ ENV["RAILS_ENV"] = "test"
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'active_support'
8
+ require 'active_support/test_case'
9
+ require 'active_record'
10
+ require 'active_record/fixtures'
11
+ require 'action_controller'
12
+ require 'action_controller/test_case'
13
+ require 'action_controller/test_process'
14
+ require 'action_controller/integration'
15
+ require 'init'
16
+
17
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), '..', 'database.yml')))[ENV['DB'] || 'test']
18
+ ActiveRecord::Base.configurations = config
19
+ ActiveRecord::Base.establish_connection(config)
20
+
21
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/models.log")
22
+ ActionController::Base.logger = Logger.new(File.dirname(__FILE__) + "/controllers.log")
23
+
24
+ load(schema_file) if File.exist?(schema_file)
25
+
26
+ Test::Unit::TestCase.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
27
+ $:.unshift(Test::Unit::TestCase.fixture_path)
28
+
29
+ class Test::Unit::TestCase
30
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
31
+ self.use_transactional_fixtures = true
32
+
33
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
34
+ self.use_instantiated_fixtures = true
35
+
36
+ # Add more helper methods to be used by all tests here...
37
+ end
@@ -0,0 +1,29 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../..')
2
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
3
+ schema_file = File.join(File.dirname(__FILE__), '..', 'schema.rb')
4
+
5
+ require 'rubygems'
6
+ require 'test/unit'
7
+ require 'active_record'
8
+ require 'active_record/fixtures'
9
+ require 'active_support'
10
+ require 'init'
11
+
12
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), '..', 'database.yml')))[ENV['DB'] || 'test']
13
+ ActiveRecord::Base.configurations = config
14
+ ActiveRecord::Base.establish_connection(config)
15
+
16
+ load(schema_file) if File.exist?(schema_file)
17
+
18
+ Test::Unit::TestCase.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
19
+ $:.unshift(Test::Unit::TestCase.fixture_path)
20
+
21
+ class Test::Unit::TestCase #:nodoc:
22
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
23
+ self.use_transactional_fixtures = true
24
+
25
+ # Instantiated fixtures are slow, but give you @david where you otherwise would need people(:david)
26
+ self.use_instantiated_fixtures = true
27
+
28
+ # Add more helper methods to be used by all tests here...
29
+ end
@@ -0,0 +1,4 @@
1
+ class Comment < ActiveRecord::Base
2
+ stampable :stamper_class_name => :person
3
+ belongs_to :post
4
+ end
@@ -0,0 +1,3 @@
1
+ class Person < ActiveRecord::Base
2
+ model_stamper
3
+ end
@@ -0,0 +1,7 @@
1
+ class Ping < ActiveRecord::Base
2
+ stampable :stamper_class_name => :person,
3
+ :creator_attribute => :creator_name,
4
+ :updater_attribute => :updater_name,
5
+ :deleter_attribute => :deleter_name
6
+ belongs_to :post
7
+ end
@@ -0,0 +1,4 @@
1
+ class Post < ActiveRecord::Base
2
+ stampable :stamper_class_name => :person
3
+ has_many :comments
4
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ model_stamper
3
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,56 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ # Users are created and updated by other Users
3
+ create_table :users, :force => true do |t|
4
+ t.column :name, :string
5
+ t.column :creator_id, :integer
6
+ t.column :created_on, :datetime
7
+ t.column :updater_id, :integer
8
+ t.column :updated_at, :datetime
9
+ end
10
+
11
+ # People are created and updated by Users
12
+ create_table :people, :force => true do |t|
13
+ t.column :name, :string
14
+ t.column :creator_id, :integer
15
+ t.column :created_on, :datetime
16
+ t.column :updater_id, :integer
17
+ t.column :updated_at, :datetime
18
+ end
19
+
20
+ # Posts are created and updated by People
21
+ create_table :posts, :force => true do |t|
22
+ t.column :title, :string
23
+ t.column :creator_id, :integer
24
+ t.column :created_on, :datetime
25
+ t.column :updater_id, :integer
26
+ t.column :updated_at, :datetime
27
+ t.column :deleter_id, :integer
28
+ t.column :deleted_at, :datetime
29
+ end
30
+
31
+ # Comments are created and updated by People
32
+ # and also use non-standard foreign keys.
33
+ create_table :comments, :force => true do |t|
34
+ t.column :post_id, :integer
35
+ t.column :comment, :string
36
+ t.column :created_by, :integer
37
+ t.column :created_at, :datetime
38
+ t.column :updated_by, :integer
39
+ t.column :updated_at, :datetime
40
+ t.column :deleted_by, :integer
41
+ t.column :deleted_at, :datetime
42
+ end
43
+
44
+ # Pings are created and updated by People,
45
+ # but they store their foreign keys as strings.
46
+ create_table :pings, :force => true do |t|
47
+ t.column :post_id, :integer
48
+ t.column :ping, :string
49
+ t.column :creator_name, :string
50
+ t.column :created_at, :datetime
51
+ t.column :updater_name, :string
52
+ t.column :updated_at, :datetime
53
+ t.column :deleter_name, :string
54
+ t.column :deleted_at, :datetime
55
+ end
56
+ end
@@ -0,0 +1,110 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'helpers/unit_test_helper'
4
+ require 'models/user'
5
+ require 'models/person'
6
+ require 'models/post'
7
+ require 'models/comment'
8
+
9
+ class StampingTests < Test::Unit::TestCase # :nodoc:
10
+ fixtures :users, :people, :posts, :comments
11
+
12
+ def setup
13
+ User.stamper = @zeus
14
+ Person.stamper = @delynn
15
+ end
16
+
17
+ def test_person_creation_with_stamped_object
18
+ assert_equal @zeus.id, User.stamper
19
+
20
+ person = Person.create(:name => "David")
21
+ assert_equal @zeus.id, person.creator_id
22
+ assert_equal @zeus.id, person.updater_id
23
+ assert_equal @zeus, person.creator
24
+ assert_equal @zeus, person.updater
25
+ end
26
+
27
+ def test_person_creation_with_stamped_integer
28
+ User.stamper = 2
29
+ assert_equal 2, User.stamper
30
+
31
+ person = Person.create(:name => "Daniel")
32
+ assert_equal @hera.id, person.creator_id
33
+ assert_equal @hera.id, person.updater_id
34
+ assert_equal @hera, person.creator
35
+ assert_equal @hera, person.updater
36
+ end
37
+
38
+ def test_post_creation_with_stamped_object
39
+ assert_equal @delynn.id, Person.stamper
40
+
41
+ post = Post.create(:title => "Test Post - 1")
42
+ assert_equal @delynn.id, post.creator_id
43
+ assert_equal @delynn.id, post.updater_id
44
+ assert_equal @delynn, post.creator
45
+ assert_equal @delynn, post.updater
46
+ end
47
+
48
+ def test_post_creation_with_stamped_integer
49
+ Person.stamper = 2
50
+ assert_equal 2, Person.stamper
51
+
52
+ post = Post.create(:title => "Test Post - 2")
53
+ assert_equal @nicole.id, post.creator_id
54
+ assert_equal @nicole.id, post.updater_id
55
+ assert_equal @nicole, post.creator
56
+ assert_equal @nicole, post.updater
57
+ end
58
+
59
+ def test_person_updating_with_stamped_object
60
+ User.stamper = @hera
61
+ assert_equal @hera.id, User.stamper
62
+
63
+ @delynn.name << " Berry"
64
+ @delynn.save
65
+ @delynn.reload
66
+ assert_equal @zeus, @delynn.creator
67
+ assert_equal @hera, @delynn.updater
68
+ assert_equal @zeus.id, @delynn.creator_id
69
+ assert_equal @hera.id, @delynn.updater_id
70
+ end
71
+
72
+ def test_person_updating_with_stamped_integer
73
+ User.stamper = 2
74
+ assert_equal 2, User.stamper
75
+
76
+ @delynn.name << " Berry"
77
+ @delynn.save
78
+ @delynn.reload
79
+ assert_equal @zeus.id, @delynn.creator_id
80
+ assert_equal @hera.id, @delynn.updater_id
81
+ assert_equal @zeus, @delynn.creator
82
+ assert_equal @hera, @delynn.updater
83
+ end
84
+
85
+ def test_post_updating_with_stamped_object
86
+ Person.stamper = @nicole
87
+ assert_equal @nicole.id, Person.stamper
88
+
89
+ @first_post.title << " - Updated"
90
+ @first_post.save
91
+ @first_post.reload
92
+ assert_equal @delynn.id, @first_post.creator_id
93
+ assert_equal @nicole.id, @first_post.updater_id
94
+ assert_equal @delynn, @first_post.creator
95
+ assert_equal @nicole, @first_post.updater
96
+ end
97
+
98
+ def test_post_updating_with_stamped_integer
99
+ Person.stamper = 2
100
+ assert_equal 2, Person.stamper
101
+
102
+ @first_post.title << " - Updated"
103
+ @first_post.save
104
+ @first_post.reload
105
+ assert_equal @delynn.id, @first_post.creator_id
106
+ assert_equal @nicole.id, @first_post.updater_id
107
+ assert_equal @delynn, @first_post.creator
108
+ assert_equal @nicole, @first_post.updater
109
+ end
110
+ end
@@ -0,0 +1,118 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'helpers/functional_test_helper'
4
+ require 'controllers/userstamp_controller'
5
+ require 'controllers/users_controller'
6
+ require 'controllers/posts_controller'
7
+ require 'models/user'
8
+ require 'models/person'
9
+ require 'models/post'
10
+ require 'models/comment'
11
+
12
+ ActionController::Routing::Routes.draw do |map|
13
+ map.connect ':controller/:action/:id'
14
+ end
15
+
16
+ class PostsControllerTest < Test::Unit::TestCase
17
+ fixtures :users, :people, :posts, :comments
18
+
19
+ def setup
20
+ @controller = PostsController.new
21
+ @request = ActionController::TestRequest.new
22
+ @response = ActionController::TestResponse.new
23
+ end
24
+
25
+ def test_update_post
26
+ @request.session = {:person_id => 1}
27
+ post :update, :id => 1, :post => {:title => 'Different'}
28
+ assert_response :success
29
+ assert_equal 'Different', assigns["post"].title
30
+ assert_equal @delynn, assigns["post"].updater
31
+ end
32
+
33
+ def test_update_with_multiple_requests
34
+ @request.session = {:person_id => 1}
35
+ get :edit, :id => 2
36
+ assert_response :success
37
+
38
+ simulate_second_request
39
+
40
+ post :update, :id => 2, :post => {:title => 'Different'}
41
+ assert_response :success
42
+ assert_equal 'Different', assigns["post"].title
43
+ assert_equal @delynn, assigns["post"].updater
44
+ end
45
+
46
+ def simulate_second_request
47
+ @second_controller = PostsController.new
48
+ @second_request = ActionController::TestRequest.new
49
+ @second_response = ActionController::TestResponse.new
50
+ @second_response.session = {:person_id => 2}
51
+
52
+ @second_request.env['REQUEST_METHOD'] = "POST"
53
+ @second_request.action = 'update'
54
+
55
+ parameters = {:id => 1, :post => {:title => 'Different Second'}}
56
+ @second_request.assign_parameters(@second_controller.class.controller_path, 'update', parameters)
57
+ @second_request.session = ActionController::TestSession.new(@second_response.session)
58
+
59
+ options = @second_controller.send!(:rewrite_options, parameters)
60
+ options.update(:only_path => true, :action => 'update')
61
+
62
+ url = ActionController::UrlRewriter.new(@second_request, parameters)
63
+ @second_request.set_REQUEST_URI(url.rewrite(options))
64
+ @second_controller.process(@second_request, @second_response)
65
+
66
+ assert_equal @nicole, @second_response.template.instance_variable_get("@post").updater
67
+ end
68
+ end
69
+
70
+ class UsersControllerTest < Test::Unit::TestCase
71
+ fixtures :users, :people, :posts, :comments
72
+
73
+ def setup
74
+ @controller = UsersController.new
75
+ @request = ActionController::TestRequest.new
76
+ @response = ActionController::TestResponse.new
77
+ end
78
+
79
+ def test_update_user
80
+ @request.session = {:user_id => 2}
81
+ post :update, :id => 2, :user => {:name => 'Different'}
82
+ assert_response :success
83
+ assert_equal 'Different', assigns["user"].name
84
+ assert_equal @hera, assigns["user"].updater
85
+ end
86
+
87
+ def test_update_with_multiple_requests
88
+ @request.session = {:user_id => 2}
89
+ get :edit, :id => 2
90
+ assert_response :success
91
+
92
+ simulate_second_request
93
+ end
94
+
95
+ def simulate_second_request
96
+ @second_controller = UsersController.new
97
+ @second_request = ActionController::TestRequest.new
98
+ @second_response = ActionController::TestResponse.new
99
+ @second_response.session = {:user_id => 1}
100
+
101
+ @second_request.env['REQUEST_METHOD'] = "POST"
102
+ @second_request.action = 'update'
103
+
104
+ parameters = {:id => 2, :user => {:name => 'Different Second'}}
105
+ @second_request.assign_parameters(@second_controller.class.controller_path, 'update', parameters)
106
+
107
+ @second_request.session = ActionController::TestSession.new(@second_response.session)
108
+
109
+ options = @second_controller.send!(:rewrite_options, parameters)
110
+ options.update(:only_path => true, :action => 'update')
111
+
112
+ url = ActionController::UrlRewriter.new(@second_request, parameters)
113
+ @second_request.set_REQUEST_URI(url.rewrite(options))
114
+ @second_controller.process(@second_request, @second_response)
115
+
116
+ assert_equal @zeus, @second_response.template.instance_variable_get("@user").updater
117
+ end
118
+ end