has_moderated 0.0.26 → 0.0.27
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +17 -31
- data/lib/has_moderated/moderated_attributes.rb +1 -1
- data/lib/has_moderated/version.rb +1 -1
- data/test/dummy/log/development.log +82 -0
- data/test/dummy/log/test.log +2173 -0
- data/test/dummy/spec/models/hjoin_test_spec.rb +18 -0
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -26,6 +26,17 @@ When upgrading, rerun the generator
|
|
26
26
|
rails generate has_moderated:install
|
27
27
|
|
28
28
|
If there is a new migration file and you have filename conflicts, remove the old one and apply the new one, in case the schema changed.
|
29
|
+
|
30
|
+
== Why use this instead of something like Papertrail?
|
31
|
+
|
32
|
+
Papertrail and vestal_versions for example, are designed not for moderation, but for tracking changes on models. If you use Papertrail for moderating, you will always have the "newest" version in the database, while previous changes will be recorded in a special table. The problem with this for moderation is that when you are showing records, you have to read from both the model table and the "changes" table in the database, and do additional processing to combine them. This will impact performance quite a bit.
|
33
|
+
|
34
|
+
In contrast, with this gem the database will always represent the last accepted/confirmed version of the model, and not necessarily the newest as with Paperclip. A "changes" table like in Paperclip is used here too, but it is used to store "suggested" (to-be-accepted) changes. Therefore you have no performance hit when querying for the last accepted version of the model, like you would have with Paperclip. You only have a performance hit when accepting a moderation, but this is irrelevant. Also this way all changes submitted are relative to the currently last accepted version and not to the newest version like in Paperclip.
|
35
|
+
|
36
|
+
So Paperclip and vestal_versions are designed for model versioning and changes tracking, while this gem is designed for moderation. The database will not be changed until a moderation is actually accepted.
|
37
|
+
|
38
|
+
Note that because of this, handling associations is quite a bit harder, so this gem is a little bit more complex. Therefore is recommended you test moderated associations before deploying to production, to avoid any possible bugs.
|
39
|
+
|
29
40
|
== Usage
|
30
41
|
To moderate one or more attributes, add
|
31
42
|
|
@@ -98,7 +109,7 @@ For example you have a Comment model, and it is moderated. But your visitors are
|
|
98
109
|
|
99
110
|
* first create a migration to add user_id into Moderation
|
100
111
|
|
101
|
-
|
112
|
+
rails g migration AddUserIdToModerations user_id:integer
|
102
113
|
|
103
114
|
* run rake db:migrate
|
104
115
|
* in models/comment.rb, add something like this:
|
@@ -139,48 +150,23 @@ You can run the tests by running
|
|
139
150
|
|
140
151
|
rake spec
|
141
152
|
|
142
|
-
in the root directory.
|
153
|
+
in the root directory of this gem (so you have to clone it first).
|
143
154
|
|
144
155
|
== TODO
|
145
156
|
|
146
|
-
DONT save association when calling .build... only when calling save!
|
147
|
-
|
148
157
|
This is just for my personal todo list...
|
149
158
|
Amend moderations... Eg if you create a new record and save it, then change something additionally and save again.
|
150
159
|
Preview method which gives changed object but doesnt save it.
|
151
160
|
|
152
|
-
For automagic associations, look at https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_association.rb method concat, concat_records, add_to_target
|
153
|
-
u get the instance of this class from with .association("name")
|
154
|
-
assoc = t.association(:subtasks)
|
155
|
-
def assoc.concat *args ; whatever ; end
|
156
|
-
or if we need block (capture outer scope)
|
157
|
-
metaclass = class << assoc; self; end
|
158
|
-
metaclass.send(:define_method, :concat) do
|
159
|
-
puts "concat 2"
|
160
|
-
end
|
161
|
-
|
162
|
-
in model use
|
163
|
-
after_initialize do...end
|
164
|
-
to overwrite these methods
|
165
|
-
ruby-1.8.7-p334 :013 > def assoc.concat_with_mo *args
|
166
|
-
ruby-1.8.7-p334 :014?> puts "in concat mo"
|
167
|
-
ruby-1.8.7-p334 :015?> concat_without_mo *args
|
168
|
-
ruby-1.8.7-p334 :016?> end
|
169
|
-
=> nil
|
170
|
-
ruby-1.8.7-p334 :017 > assoc.class_eval do
|
171
|
-
ruby-1.8.7-p334 :018 > alias_method_chain :concat, :mo
|
172
|
-
ruby-1.8.7-p334 :019?> end
|
173
|
-
use add_to_target!!! lowest lvl
|
174
|
-
add something like has_moderated_association
|
175
|
-
don't do anything on create (parent.new_record?) since we already handle that
|
176
|
-
test also for one-to-one and join table and has_many through assoc
|
177
|
-
|
178
|
-
fix tests for offline working
|
179
161
|
== Problems
|
180
162
|
|
163
|
+
If you have problems open an issue here on Github.
|
164
|
+
|
181
165
|
You may encounter problems with models that have some sort of non-serializable attributes. This might be something like file attachments, you'll have to try it to see.
|
182
166
|
If you have a problem like that you can extract the problematic attributes into a seperate has_one association. If you moderate create, save that model without the foreign key first, and then use has_moderated_create :with_associations => [:association_name] and add the association (to the existing associated model) before saving the moderated model. If you have questions about this or don't understand what I mean, open an issue here at GitHub and I will explain it further.
|
183
167
|
|
168
|
+
It is also possible you will encounter problems with some heavily customized associations, or with has_many :through associations (although I have tested this for a simple, ordinary case and it works). If that happens open an issue here on Github.
|
169
|
+
|
184
170
|
== License
|
185
171
|
|
186
172
|
This project rocks and uses MIT-LICENSE.
|
@@ -15,7 +15,7 @@ module HasModerated
|
|
15
15
|
self.moderated_options ||= {}
|
16
16
|
|
17
17
|
args.each do |arg|
|
18
|
-
if arg.
|
18
|
+
if arg.class == Hash || arg.class == HashWithIndifferentAccess
|
19
19
|
self.moderated_options = self.moderated_options.merge(arg)
|
20
20
|
else
|
21
21
|
self.moderated_attributes.push(arg.to_s)
|
@@ -6454,3 +6454,85 @@ Migrating to CreateHabtmNameTests (20111018180207)
|
|
6454
6454
|
[1m[36m (1.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111009205545')[0m
|
6455
6455
|
[1m[35m (1.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
|
6456
6456
|
[1m[36m (2.1ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111018174319')[0m
|
6457
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
6458
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
6459
|
+
[1m[36m (1.8ms)[0m [1mCREATE TABLE "habtm_name_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6460
|
+
[1m[35m (1.5ms)[0m CREATE TABLE "habtm_name_tests_tasks" ("task_id" integer, "habtm_name_test_id" integer)
|
6461
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "hjoin_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6462
|
+
[1m[35m (1.2ms)[0m CREATE TABLE "hjoin_tests_tasks" ("task_id" integer, "hjoin_test_id" integer)
|
6463
|
+
[1m[36m (1.5ms)[0m [1mCREATE TABLE "hmany_fk_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "something_id" integer, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6464
|
+
[1m[35m (1.7ms)[0m CREATE TABLE "hmanythrough_joins" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "hmanythrough_test_id" integer, "task_id" integer, "exdata" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6465
|
+
[1m[36m (2.0ms)[0m [1mCREATE TABLE "hmanythrough_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6466
|
+
[1m[35m (1.6ms)[0m CREATE TABLE "hone_as_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "testable_id" integer, "testable_type" varchar(255), "title" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6467
|
+
[1m[36m (2.4ms)[0m [1mCREATE TABLE "hone_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "created_at" datetime, "updated_at" datetime, "title" varchar(255)) [0m
|
6468
|
+
[1m[35m (1.4ms)[0m CREATE TABLE "hook_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "foo" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6469
|
+
[1m[36m (1.8ms)[0m [1mCREATE TABLE "moderations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "moderatable_id" integer, "moderatable_type" varchar(255) NOT NULL, "attr_name" varchar(60) NOT NULL, "attr_value" text NOT NULL, "created_at" datetime, "updated_at" datetime) [0m
|
6470
|
+
[1m[35m (1.8ms)[0m CREATE TABLE "photo_holders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6471
|
+
[1m[36m (2.2ms)[0m [1mCREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "created_at" datetime, "updated_at" datetime, "photo_holder_id" integer) [0m
|
6472
|
+
[1m[35m (1.7ms)[0m CREATE TABLE "subtasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime, "task_all_id" integer)
|
6473
|
+
[1m[36m (1.8ms)[0m [1mCREATE TABLE "task_alls" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "value" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6474
|
+
[1m[35m (2.0ms)[0m CREATE TABLE "task_photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "task_id" integer, "created_at" datetime, "updated_at" datetime)
|
6475
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6476
|
+
[1m[35m (1.6ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
6477
|
+
[1m[36m (0.0ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
6478
|
+
[1m[35m (2.0ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
6479
|
+
[1m[36m (0.2ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
6480
|
+
[1m[35m (1.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111018180207')
|
6481
|
+
[1m[36m (2.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110901013205')[0m
|
6482
|
+
[1m[35m (1.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20110901013228')
|
6483
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110901013618')[0m
|
6484
|
+
[1m[35m (1.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20110908025410')
|
6485
|
+
[1m[36m (1.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110908025606')[0m
|
6486
|
+
[1m[35m (2.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111003205633')
|
6487
|
+
[1m[36m (1.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111003234101')[0m
|
6488
|
+
[1m[35m (1.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111004153147')
|
6489
|
+
[1m[36m (1.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111004164509')[0m
|
6490
|
+
[1m[35m (1.4ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111008195728')
|
6491
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111008195809')[0m
|
6492
|
+
[1m[35m (2.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111009193145')
|
6493
|
+
[1m[36m (1.3ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111009201729')[0m
|
6494
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111009205517')
|
6495
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111009205545')[0m
|
6496
|
+
[1m[35m (1.6ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
|
6497
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111018174319')[0m
|
6498
|
+
[1m[36m (0.1ms)[0m [1mSELECT "schema_migrations"."version" FROM "schema_migrations" [0m
|
6499
|
+
[1m[35m (0.1ms)[0m select sqlite_version(*)
|
6500
|
+
[1m[36m (2.7ms)[0m [1mCREATE TABLE "habtm_name_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6501
|
+
[1m[35m (1.4ms)[0m CREATE TABLE "habtm_name_tests_tasks" ("task_id" integer, "habtm_name_test_id" integer)
|
6502
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "hjoin_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6503
|
+
[1m[35m (1.4ms)[0m CREATE TABLE "hjoin_tests_tasks" ("task_id" integer, "hjoin_test_id" integer)
|
6504
|
+
[1m[36m (238.6ms)[0m [1mCREATE TABLE "hmany_fk_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "something_id" integer, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6505
|
+
[1m[35m (1.6ms)[0m CREATE TABLE "hmanythrough_joins" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "hmanythrough_test_id" integer, "task_id" integer, "exdata" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6506
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "hmanythrough_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6507
|
+
[1m[35m (2.0ms)[0m CREATE TABLE "hone_as_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "testable_id" integer, "testable_type" varchar(255), "title" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6508
|
+
[1m[36m (1.7ms)[0m [1mCREATE TABLE "hone_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "created_at" datetime, "updated_at" datetime, "title" varchar(255)) [0m
|
6509
|
+
[1m[35m (228.6ms)[0m CREATE TABLE "hook_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "foo" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6510
|
+
[1m[36m (2.0ms)[0m [1mCREATE TABLE "moderations" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "moderatable_id" integer, "moderatable_type" varchar(255) NOT NULL, "attr_name" varchar(60) NOT NULL, "attr_value" text NOT NULL, "created_at" datetime, "updated_at" datetime) [0m
|
6511
|
+
[1m[35m (1.9ms)[0m CREATE TABLE "photo_holders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime)
|
6512
|
+
[1m[36m (2.1ms)[0m [1mCREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "created_at" datetime, "updated_at" datetime, "photo_holder_id" integer) [0m
|
6513
|
+
[1m[35m (231.8ms)[0m CREATE TABLE "subtasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime, "task_all_id" integer)
|
6514
|
+
[1m[36m (2.0ms)[0m [1mCREATE TABLE "task_alls" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "value" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6515
|
+
[1m[35m (1.9ms)[0m CREATE TABLE "task_photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "task_id" integer, "created_at" datetime, "updated_at" datetime)
|
6516
|
+
[1m[36m (1.6ms)[0m [1mCREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime) [0m
|
6517
|
+
[1m[35m (232.9ms)[0m CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
|
6518
|
+
[1m[36m (0.2ms)[0m [1mPRAGMA index_list("schema_migrations")[0m
|
6519
|
+
[1m[35m (2.1ms)[0m CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
|
6520
|
+
[1m[36m (0.1ms)[0m [1mSELECT version FROM "schema_migrations"[0m
|
6521
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111018180207')
|
6522
|
+
[1m[36m (1.7ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110901013205')[0m
|
6523
|
+
[1m[35m (1.9ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20110901013228')
|
6524
|
+
[1m[36m (233.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110901013618')[0m
|
6525
|
+
[1m[35m (1.7ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20110908025410')
|
6526
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20110908025606')[0m
|
6527
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111003205633')
|
6528
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111003234101')[0m
|
6529
|
+
[1m[35m (245.8ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111004153147')
|
6530
|
+
[1m[36m (2.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111004164509')[0m
|
6531
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111008195728')
|
6532
|
+
[1m[36m (1.5ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111008195809')[0m
|
6533
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111009193145')
|
6534
|
+
[1m[36m (1.6ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111009201729')[0m
|
6535
|
+
[1m[35m (1.4ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111009205517')
|
6536
|
+
[1m[36m (1.3ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111009205545')[0m
|
6537
|
+
[1m[35m (1.5ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
|
6538
|
+
[1m[36m (1.4ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20111018174319')[0m
|