userstamp 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG +26 -0
  3. data/LICENSE +20 -0
  4. data/README +177 -0
  5. data/Rakefile +38 -0
  6. data/VERSION +1 -0
  7. data/init.rb +1 -0
  8. data/lib/migration_helper.rb +19 -0
  9. data/lib/stampable.rb +151 -0
  10. data/lib/stamper.rb +43 -0
  11. data/lib/userstamp.rb +48 -0
  12. data/rdoc/classes/Ddb/Controller.html +111 -0
  13. data/rdoc/classes/Ddb/Controller/Userstamp.html +125 -0
  14. data/rdoc/classes/Ddb/Controller/Userstamp/InstanceMethods.html +105 -0
  15. data/rdoc/classes/Ddb/Userstamp.html +121 -0
  16. data/rdoc/classes/Ddb/Userstamp/MigrationHelper.html +111 -0
  17. data/rdoc/classes/Ddb/Userstamp/MigrationHelper/InstanceMethods.html +142 -0
  18. data/rdoc/classes/Ddb/Userstamp/Stampable.html +128 -0
  19. data/rdoc/classes/Ddb/Userstamp/Stampable/ClassMethods.html +222 -0
  20. data/rdoc/classes/Ddb/Userstamp/Stamper.html +112 -0
  21. data/rdoc/classes/Ddb/Userstamp/Stamper/ClassMethods.html +142 -0
  22. data/rdoc/classes/Ddb/Userstamp/Stamper/InstanceMethods.html +207 -0
  23. data/rdoc/created.rid +1 -0
  24. data/rdoc/files/CHANGELOG.html +137 -0
  25. data/rdoc/files/LICENSE.html +129 -0
  26. data/rdoc/files/README.html +341 -0
  27. data/rdoc/files/lib/migration_helper_rb.html +101 -0
  28. data/rdoc/files/lib/stampable_rb.html +101 -0
  29. data/rdoc/files/lib/stamper_rb.html +101 -0
  30. data/rdoc/files/lib/userstamp_rb.html +101 -0
  31. data/rdoc/fr_class_index.html +37 -0
  32. data/rdoc/fr_file_index.html +33 -0
  33. data/rdoc/fr_method_index.html +33 -0
  34. data/rdoc/index.html +24 -0
  35. data/rdoc/rdoc-style.css +208 -0
  36. data/test/compatibility_stamping_test.rb +63 -0
  37. data/test/controllers/posts_controller.rb +26 -0
  38. data/test/controllers/users_controller.rb +12 -0
  39. data/test/controllers/userstamp_controller.rb +9 -0
  40. data/test/database.yml +4 -0
  41. data/test/fixtures/comments.yml +16 -0
  42. data/test/fixtures/people.yml +11 -0
  43. data/test/fixtures/posts.yml +9 -0
  44. data/test/fixtures/users.yml +7 -0
  45. data/test/helpers/functional_test_helper.rb +37 -0
  46. data/test/helpers/unit_test_helper.rb +29 -0
  47. data/test/models/comment.rb +4 -0
  48. data/test/models/person.rb +3 -0
  49. data/test/models/ping.rb +7 -0
  50. data/test/models/post.rb +4 -0
  51. data/test/models/user.rb +3 -0
  52. data/test/schema.rb +56 -0
  53. data/test/stamping_test.rb +110 -0
  54. data/test/userstamp_controller_test.rb +118 -0
  55. data/userstamp.gemspec +107 -0
  56. metadata +123 -0
@@ -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
data/userstamp.gemspec ADDED
@@ -0,0 +1,107 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{userstamp}
8
+ s.version = "2.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["DeLynn Berry"]
12
+ s.date = %q{2009-11-18}
13
+ s.email = %q{delynn@gmail.com}
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "CHANGELOG",
21
+ "LICENSE",
22
+ "README",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "init.rb",
26
+ "lib/migration_helper.rb",
27
+ "lib/stampable.rb",
28
+ "lib/stamper.rb",
29
+ "lib/userstamp.rb",
30
+ "rdoc/classes/Ddb/Controller.html",
31
+ "rdoc/classes/Ddb/Controller/Userstamp.html",
32
+ "rdoc/classes/Ddb/Controller/Userstamp/InstanceMethods.html",
33
+ "rdoc/classes/Ddb/Userstamp.html",
34
+ "rdoc/classes/Ddb/Userstamp/MigrationHelper.html",
35
+ "rdoc/classes/Ddb/Userstamp/MigrationHelper/InstanceMethods.html",
36
+ "rdoc/classes/Ddb/Userstamp/Stampable.html",
37
+ "rdoc/classes/Ddb/Userstamp/Stampable/ClassMethods.html",
38
+ "rdoc/classes/Ddb/Userstamp/Stamper.html",
39
+ "rdoc/classes/Ddb/Userstamp/Stamper/ClassMethods.html",
40
+ "rdoc/classes/Ddb/Userstamp/Stamper/InstanceMethods.html",
41
+ "rdoc/created.rid",
42
+ "rdoc/files/CHANGELOG.html",
43
+ "rdoc/files/LICENSE.html",
44
+ "rdoc/files/README.html",
45
+ "rdoc/files/lib/migration_helper_rb.html",
46
+ "rdoc/files/lib/stampable_rb.html",
47
+ "rdoc/files/lib/stamper_rb.html",
48
+ "rdoc/files/lib/userstamp_rb.html",
49
+ "rdoc/fr_class_index.html",
50
+ "rdoc/fr_file_index.html",
51
+ "rdoc/fr_method_index.html",
52
+ "rdoc/index.html",
53
+ "rdoc/rdoc-style.css",
54
+ "test/compatibility_stamping_test.rb",
55
+ "test/controllers/posts_controller.rb",
56
+ "test/controllers/users_controller.rb",
57
+ "test/controllers/userstamp_controller.rb",
58
+ "test/database.yml",
59
+ "test/fixtures/comments.yml",
60
+ "test/fixtures/people.yml",
61
+ "test/fixtures/posts.yml",
62
+ "test/fixtures/users.yml",
63
+ "test/helpers/functional_test_helper.rb",
64
+ "test/helpers/unit_test_helper.rb",
65
+ "test/models/comment.rb",
66
+ "test/models/person.rb",
67
+ "test/models/ping.rb",
68
+ "test/models/post.rb",
69
+ "test/models/user.rb",
70
+ "test/schema.rb",
71
+ "test/stamping_test.rb",
72
+ "test/userstamp_controller_test.rb",
73
+ "userstamp.gemspec"
74
+ ]
75
+ s.homepage = %q{http://github.com/delynn/userstamp}
76
+ s.rdoc_options = ["--charset=UTF-8"]
77
+ s.require_paths = ["lib"]
78
+ s.rubygems_version = %q{1.3.5}
79
+ s.summary = %q{This Rails plugin extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.}
80
+ s.test_files = [
81
+ "test/helpers/unit_test_helper.rb",
82
+ "test/helpers/functional_test_helper.rb",
83
+ "test/schema.rb",
84
+ "test/userstamp_controller_test.rb",
85
+ "test/compatibility_stamping_test.rb",
86
+ "test/controllers/userstamp_controller.rb",
87
+ "test/controllers/posts_controller.rb",
88
+ "test/controllers/users_controller.rb",
89
+ "test/models/ping.rb",
90
+ "test/models/person.rb",
91
+ "test/models/user.rb",
92
+ "test/models/post.rb",
93
+ "test/models/comment.rb",
94
+ "test/stamping_test.rb"
95
+ ]
96
+
97
+ if s.respond_to? :specification_version then
98
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
99
+ s.specification_version = 3
100
+
101
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
102
+ else
103
+ end
104
+ else
105
+ end
106
+ end
107
+