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 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
- rails g migration AddUserIdToModerations user_id:integer
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.respond_to?("[]")
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)
@@ -1,3 +1,3 @@
1
1
  module HasModerated
2
- VERSION = "0.0.26"
2
+ VERSION = "0.0.27"
3
3
  end
@@ -6454,3 +6454,85 @@ Migrating to CreateHabtmNameTests (20111018180207)
6454
6454
   (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009205545')
6455
6455
   (1.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
6456
6456
   (2.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018174319')
6457
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
6458
+  (0.1ms) select sqlite_version(*)
6459
+  (1.8ms) CREATE TABLE "habtm_name_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6460
+  (1.5ms) CREATE TABLE "habtm_name_tests_tasks" ("task_id" integer, "habtm_name_test_id" integer)
6461
+  (1.7ms) CREATE TABLE "hjoin_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6462
+  (1.2ms) CREATE TABLE "hjoin_tests_tasks" ("task_id" integer, "hjoin_test_id" integer)
6463
+  (1.5ms) CREATE TABLE "hmany_fk_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "something_id" integer, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6464
+  (1.7ms) 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
+  (2.0ms) CREATE TABLE "hmanythrough_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6466
+  (1.6ms) 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
+  (2.4ms) CREATE TABLE "hone_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "created_at" datetime, "updated_at" datetime, "title" varchar(255)) 
6468
+  (1.4ms) CREATE TABLE "hook_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "foo" varchar(255), "created_at" datetime, "updated_at" datetime)
6469
+  (1.8ms) CREATE 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) 
6470
+  (1.8ms) CREATE TABLE "photo_holders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime)
6471
+  (2.2ms) CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "created_at" datetime, "updated_at" datetime, "photo_holder_id" integer) 
6472
+  (1.7ms) 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
+  (1.8ms) CREATE TABLE "task_alls" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "value" varchar(255), "created_at" datetime, "updated_at" datetime) 
6474
+  (2.0ms) CREATE TABLE "task_photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "task_id" integer, "created_at" datetime, "updated_at" datetime)
6475
+  (1.7ms) CREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime) 
6476
+  (1.6ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
6477
+  (0.0ms) PRAGMA index_list("schema_migrations")
6478
+  (2.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
6479
+  (0.2ms) SELECT version FROM "schema_migrations"
6480
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018180207')
6481
+  (2.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013205')
6482
+  (1.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013228')
6483
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013618')
6484
+  (1.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20110908025410')
6485
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20110908025606')
6486
+  (2.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111003205633')
6487
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20111003234101')
6488
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20111004153147')
6489
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20111004164509')
6490
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20111008195728')
6491
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111008195809')
6492
+  (2.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009193145')
6493
+  (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009201729')
6494
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009205517')
6495
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009205545')
6496
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
6497
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018174319')
6498
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
6499
+  (0.1ms) select sqlite_version(*)
6500
+  (2.7ms) CREATE TABLE "habtm_name_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6501
+  (1.4ms) CREATE TABLE "habtm_name_tests_tasks" ("task_id" integer, "habtm_name_test_id" integer)
6502
+  (1.7ms) CREATE TABLE "hjoin_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6503
+  (1.4ms) CREATE TABLE "hjoin_tests_tasks" ("task_id" integer, "hjoin_test_id" integer)
6504
+  (238.6ms) CREATE TABLE "hmany_fk_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "something_id" integer, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6505
+  (1.6ms) 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
+  (1.7ms) CREATE TABLE "hmanythrough_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime) 
6507
+  (2.0ms) 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
+  (1.7ms) CREATE TABLE "hone_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "task_id" integer, "created_at" datetime, "updated_at" datetime, "title" varchar(255)) 
6509
+  (228.6ms) CREATE TABLE "hook_tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "foo" varchar(255), "created_at" datetime, "updated_at" datetime)
6510
+  (2.0ms) CREATE 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) 
6511
+  (1.9ms) CREATE TABLE "photo_holders" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime)
6512
+  (2.1ms) CREATE TABLE "photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "created_at" datetime, "updated_at" datetime, "photo_holder_id" integer) 
6513
+  (231.8ms) 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
+  (2.0ms) CREATE TABLE "task_alls" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "value" varchar(255), "created_at" datetime, "updated_at" datetime) 
6515
+  (1.9ms) CREATE TABLE "task_photos" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "photo" varchar(255), "task_id" integer, "created_at" datetime, "updated_at" datetime)
6516
+  (1.6ms) CREATE TABLE "tasks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "desc" varchar(255), "created_at" datetime, "updated_at" datetime) 
6517
+  (232.9ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
6518
+  (0.2ms) PRAGMA index_list("schema_migrations")
6519
+  (2.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
6520
+  (0.1ms) SELECT version FROM "schema_migrations"
6521
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018180207')
6522
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013205')
6523
+  (1.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013228')
6524
+  (233.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20110901013618')
6525
+  (1.7ms) INSERT INTO "schema_migrations" (version) VALUES ('20110908025410')
6526
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20110908025606')
6527
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111003205633')
6528
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111003234101')
6529
+  (245.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20111004153147')
6530
+  (2.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20111004164509')
6531
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111008195728')
6532
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111008195809')
6533
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009193145')
6534
+  (1.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009201729')
6535
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009205517')
6536
+  (1.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20111009205545')
6537
+  (1.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018172409')
6538
+  (1.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20111018174319')