has_moderated 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. data/Rakefile +1 -1
  2. data/lib/has_moderated.rb +1 -1
  3. data/lib/has_moderated/associations/base.rb +7 -1
  4. data/lib/has_moderated/moderation_model.rb +3 -1
  5. data/lib/has_moderated/version.rb +1 -1
  6. data/spec/has_moderated/associations_spec.rb +44 -0
  7. data/spec/has_moderated/attributes_spec.rb +31 -0
  8. data/spec/has_moderated/common_features_spec.rb +56 -0
  9. data/spec/has_moderated/has_and_belongs_to_many_spec.rb +98 -0
  10. data/spec/has_moderated/has_many_spec.rb +108 -0
  11. data/spec/has_moderated/has_many_through_spec.rb +91 -0
  12. data/spec/has_moderated/has_one_spec.rb +119 -0
  13. data/spec/has_moderated/hooks_spec.rb +18 -0
  14. data/spec/has_moderated/moderated_create_spec.rb +41 -0
  15. data/spec/has_moderated/moderated_destroy_spec.rb +24 -0
  16. data/{test/dummy/spec/models → spec/has_moderated}/photo_spec.rb +23 -29
  17. data/spec/has_moderated/polymorphic_spec.rb +70 -0
  18. data/spec/has_moderated/preview_spec.rb +182 -0
  19. data/spec/has_moderated/without_moderation_spec.rb +38 -0
  20. metadata +195 -247
  21. data/test/dummy/Rakefile +0 -7
  22. data/test/dummy/app/assets/javascripts/application.js +0 -9
  23. data/test/dummy/app/assets/stylesheets/application.css +0 -7
  24. data/test/dummy/app/controllers/application_controller.rb +0 -3
  25. data/test/dummy/app/helpers/application_helper.rb +0 -2
  26. data/test/dummy/app/models/moderation.rb +0 -5
  27. data/test/dummy/app/uploaders/generic_uploader.rb +0 -49
  28. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  29. data/test/dummy/config.ru +0 -4
  30. data/test/dummy/config/application.rb +0 -50
  31. data/test/dummy/config/boot.rb +0 -10
  32. data/test/dummy/config/database.yml +0 -25
  33. data/test/dummy/config/environment.rb +0 -5
  34. data/test/dummy/config/environments/development.rb +0 -30
  35. data/test/dummy/config/environments/production.rb +0 -60
  36. data/test/dummy/config/environments/test.rb +0 -42
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  38. data/test/dummy/config/initializers/inflections.rb +0 -10
  39. data/test/dummy/config/initializers/mime_types.rb +0 -5
  40. data/test/dummy/config/initializers/secret_token.rb +0 -7
  41. data/test/dummy/config/initializers/session_store.rb +0 -8
  42. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  43. data/test/dummy/config/locales/en.yml +0 -5
  44. data/test/dummy/config/routes.rb +0 -58
  45. data/test/dummy/db/migrate/20120520215008_initial_schema.rb +0 -48
  46. data/test/dummy/db/migrate/20120520215224_create_photos.rb +0 -13
  47. data/test/dummy/db/schema.rb +0 -71
  48. data/test/dummy/db/test.sqlite3 +0 -0
  49. data/test/dummy/log/test.log +0 -19029
  50. data/test/dummy/public/404.html +0 -26
  51. data/test/dummy/public/422.html +0 -26
  52. data/test/dummy/public/500.html +0 -26
  53. data/test/dummy/public/favicon.ico +0 -0
  54. data/test/dummy/public/test.jpg +0 -0
  55. data/test/dummy/public/uploads/tmp/20121108-2105-18143-8298/test.jpg +0 -0
  56. data/test/dummy/public/uploads/tmp/20121108-2105-18143-8298/thumb_test.jpg +0 -0
  57. data/test/dummy/script/rails +0 -6
  58. data/test/dummy/spec/models/task_spec.rb +0 -886
  59. data/test/dummy/spec/spec_helper.rb +0 -1
  60. data/test/dummy/spec/support/crazy_models.rb +0 -85
  61. data/test/dummy/spec/support/photos.rb +0 -33
data/Rakefile CHANGED
@@ -25,7 +25,7 @@ Bundler::GemHelper.install_tasks
25
25
  require 'rspec/core/rake_task'
26
26
  RSpec::Core::RakeTask.new(:spec) do |spec|
27
27
  spec.rspec_opts = %w(--color)
28
- spec.pattern = 'test/dummy/spec{,/*/**}/*_spec.rb'
28
+ spec.pattern = 'spec{,/*/**}/*_spec.rb'
29
29
  end
30
30
 
31
31
  task :prepare_test_env do
@@ -14,4 +14,4 @@ require 'has_moderated/moderated_create'
14
14
  require 'has_moderated/moderated_destroy'
15
15
 
16
16
  require 'has_moderated/carrier_wave'
17
- require 'has_moderated/railtie' if defined?(Rails)
17
+ require 'has_moderated/railtie' if defined?(::Rails)
@@ -100,7 +100,12 @@ module HasModerated
100
100
  arec.send(key.to_s+"=", val)
101
101
  end
102
102
  # recursive, used for has_many :through
103
- apply(arec, attrs, save_opts, preview_mode) if attrs[:associations].present?
103
+ if attrs[:associations].present?
104
+ Moderation.without_moderation do
105
+ arec.save(save_opts)
106
+ end
107
+ apply(arec, attrs, save_opts, preview_mode)
108
+ end
104
109
  else
105
110
  raise "don't know how to parse #{attrs.class}"
106
111
  end
@@ -114,6 +119,7 @@ module HasModerated
114
119
  #rec.send(reflection.name.to_s) << arec unless rec.send(reflection.name.to_s).include?(arec)
115
120
  else
116
121
  rec.send(reflection.name.to_s + "=", arec)
122
+ rec.save(save_opts)
117
123
  end
118
124
  end
119
125
  end
@@ -102,7 +102,9 @@ module HasModerated
102
102
  live_preview do |record|
103
103
  fake_record = HasModerated::Preview::from_live(record, self, options[:saveable])
104
104
  end
105
- fake_record
105
+ # Ruby 1.8 will unfreeze when doing ActiveRecord::Rollback
106
+ # Only necessary to re-freeze for 1.8, associations stay frozen as normal
107
+ fake_record.freeze
106
108
  end
107
109
 
108
110
  def create?
@@ -1,3 +1,3 @@
1
1
  module HasModerated
2
- VERSION = "1.2.2"
2
+ VERSION = "1.2.3"
3
3
  end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe "associations" do
4
+ it "can handle nested associations (issue #17 and #18)" do
5
+ dynamic_models.task {
6
+ has_many :subtasks
7
+ has_moderated_create :with_associations => [:subtasks]
8
+ }.subtask {
9
+ belongs_to :task_connection, :foreign_key => "parentable_id"
10
+ belongs_to :task
11
+ has_many :photos, :as => :parentable
12
+ has_moderated :title
13
+ }.photo {
14
+ belongs_to :parentable, :polymorphic => true
15
+ }.task_connection {
16
+ has_many :things, :as => :parentable
17
+ }
18
+
19
+ tconn = TaskConnection.create! :title => "TC"
20
+ data = {
21
+ :create => {
22
+ :attributes => { "id" => nil, "title" => "Task" },
23
+ :associations => {
24
+ :subtasks =>
25
+ [
26
+ {"id" => nil, "title" => "Subtask",
27
+ :associations => {
28
+ :photos => [{"id" => nil, "title" => "Photo"}],
29
+ :task_connection => [tconn.id]
30
+ }
31
+ }
32
+ ]
33
+ }
34
+ }
35
+ }
36
+ Moderation.create! :moderatable_type => "Task", :data => data.to_yaml
37
+ Moderation.last.accept
38
+ Moderation.count.should eq(0)
39
+ (t = Task.last).title.should eq("Task")
40
+ (s = t.subtasks.last).title.should eq("Subtask")
41
+ s.photos.last.title.should eq("Photo")
42
+ s.task_connection.title.should eq("TC")
43
+ end
44
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe "moderates attributes:" do
4
+ it "moderates an attribute" do
5
+ dynamic_models.task {
6
+ has_moderated :title
7
+ }
8
+ Task.create! :title => "Task 1", :desc => "Description"
9
+ Task.first.title.should be_blank
10
+ Task.first.desc.should eq("Description")
11
+ Moderation.last.accept
12
+ Task.first.title.should eq("Task 1")
13
+ Task.first.update_attribute(:title, "Task 2")
14
+ Task.first.title.should eq("Task 1")
15
+ Moderation.last.accept
16
+ Task.first.title.should eq("Task 2")
17
+ end
18
+
19
+ it "validates uniqueness properly" do
20
+ dynamic_models.task {
21
+ has_moderated :title
22
+ validates_uniqueness_of :title
23
+ }
24
+ Task.create! :title => "Task 1"
25
+ Task.create! :title => "Task 1"
26
+ Moderation.first.accept
27
+ expect { Task.create! :title => "Task 1" }.to raise_error
28
+ Moderation.first.accept.should be_false
29
+ expect { Moderation.first.accept! }.to raise_error
30
+ end
31
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe "common features:" do
4
+ it "get_moderation_attributes can be overriden in model" do
5
+ dynamic_models.task {
6
+ has_moderated_create
7
+ self.class_eval do
8
+ def get_moderation_attributes
9
+ { :test => "ok" }
10
+ end
11
+ end
12
+ }
13
+ Task.create! :title => "Task 1"
14
+ data = YAML::load(Moderation.last.data)[:create][:attributes]
15
+ data.should_not be_blank
16
+ data[:test].should_not be_blank
17
+ data[:test].should eq("ok")
18
+ data.keys.count.should eq(1)
19
+ end
20
+
21
+ it "knows if it's a create moderation" do
22
+ dynamic_models.task {
23
+ has_moderated_create
24
+ }
25
+
26
+ Task.create! :title => "Task 1"
27
+
28
+ Moderation.last.create?.should be_true
29
+ Moderation.last.destroy?.should be_false
30
+ Moderation.last.update?.should be_false
31
+ end
32
+
33
+ it "knows if it's a destroy moderation" do
34
+ dynamic_models.task {
35
+ has_moderated_destroy
36
+ }
37
+
38
+ Task.create! :title => "Task 1"
39
+ Task.last.destroy
40
+
41
+ Moderation.last.destroy?.should be_true
42
+ Moderation.last.create?.should be_false
43
+ Moderation.last.update?.should be_false
44
+ end
45
+
46
+ it "calls moderation callbacks on destroy" do
47
+ dynamic_models.task {
48
+ has_moderated_create
49
+ def self.moderatable_discard(m, opts)
50
+ raise "moderatable_discard"
51
+ end
52
+ }
53
+ Task.create!
54
+ expect { Moderation.last.destroy }.to raise_error("moderatable_discard")
55
+ end
56
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+
3
+ describe "has_and_belongs_to_many" do
4
+ context "has_and_belongs_to_many association:" do
5
+ before do
6
+ dynamic_models.task {
7
+ has_and_belongs_to_many :renamed_subtasks, :class_name => "Subtask", :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
8
+ has_moderated_association :renamed_subtasks
9
+ }.subtask {
10
+ has_and_belongs_to_many :renamed_tasks, :class_name => "Task", :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
11
+ }
12
+ end
13
+
14
+ it "creates and associates a new subtask" do
15
+ task = Task.create! :title => "Task 1"
16
+ Moderation.count.should eq(0)
17
+ task.renamed_subtasks.create! :title => "Subtask 1"
18
+
19
+ Subtask.count.should eq(0)
20
+ task = Task.first
21
+ task.renamed_subtasks.count.should eq(0)
22
+
23
+ Moderation.count.should eq(1)
24
+ Moderation.last.accept
25
+ Moderation.count.should eq(0)
26
+
27
+ subtask = Task.first.renamed_subtasks.first
28
+ subtask.title.should eq("Subtask 1")
29
+ end
30
+
31
+ it "associates an existing subtask" do
32
+ task = Task.create! :title => "Task 1"
33
+ Subtask.create! :title => "Subtask 1"
34
+ Task.first.renamed_subtasks.count.should eq(0)
35
+ Subtask.count.should eq(1)
36
+ Moderation.count.should eq(0)
37
+ task.renamed_subtasks << Subtask.first
38
+
39
+ Moderation.count.should eq(1)
40
+ Moderation.last.accept
41
+ Moderation.count.should eq(0)
42
+
43
+ Task.first.renamed_subtasks.count.should eq(1)
44
+ subtask = Task.first.renamed_subtasks.first
45
+ subtask.title.should eq("Subtask 1")
46
+ end
47
+ end
48
+
49
+ context "has_and_belongs_to_many association (create moderation):" do
50
+ before :each do # important that we do this before EACH
51
+ dynamic_models.task {
52
+ has_moderated_create :with_associations => [:renamed_subtasks]
53
+ has_and_belongs_to_many :renamed_subtasks, :class_name => "Subtask", :join_table => "tasks_jointable", :foreign_key => "m1_id", :association_foreign_key => "m2_id"
54
+ }.subtask {
55
+ has_and_belongs_to_many :renamed_tasks, :class_name => "Task", :join_table => "tasks_jointable", :foreign_key => "m2_id", :association_foreign_key => "m1_id"
56
+ }
57
+ end
58
+
59
+ it "associates an existing subtask on create 1" do
60
+ Task.has_moderated_association :renamed_subtasks # important difference (had different behavior based
61
+ # on presence of this line, need to test with and without)
62
+ Subtask.create! :title => "Subtask 1"
63
+ Subtask.count.should eq(1)
64
+ Moderation.count.should eq(0)
65
+
66
+ task = Task.new :title => "Task 1"
67
+ task.renamed_subtasks << Subtask.first
68
+ task.save
69
+
70
+ Moderation.count.should eq(1)
71
+ Moderation.last.accept
72
+ Moderation.count.should eq(0)
73
+
74
+ Task.first.renamed_subtasks.count.should eq(1)
75
+ subtask = Task.first.renamed_subtasks.first
76
+ subtask.title.should eq("Subtask 1")
77
+ end
78
+
79
+ it "associates an existing subtask on create 2" do
80
+ Subtask.create! :title => "Subtask 1"
81
+ Subtask.count.should eq(1)
82
+ Moderation.count.should eq(0)
83
+
84
+ task = Task.new :title => "Task 1"
85
+ task.renamed_subtasks << Subtask.first
86
+ task.save
87
+
88
+ Task.count.should eq(0)
89
+ Moderation.count.should eq(1)
90
+ Moderation.last.accept
91
+ Moderation.count.should eq(0)
92
+
93
+ Task.last.renamed_subtasks.count.should eq(1)
94
+ subtask = Task.first.renamed_subtasks.first
95
+ subtask.title.should eq("Subtask 1")
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe "has_many association:" do
4
+ before do
5
+ dynamic_models.task {
6
+ has_many :renamed_subtasks, :class_name => "Subtask", :foreign_key => "task_id"
7
+ has_moderated_association :renamed_subtasks
8
+ }.subtask {
9
+ belongs_to :task, :foreign_key => "task_id"
10
+ }
11
+ end
12
+
13
+ it "creates and associates subtask (create)" do
14
+ task = Task.create! :title => "Task 1"
15
+ Moderation.count.should eq(0)
16
+ task.renamed_subtasks.create! :title => "Subtask 1"
17
+
18
+ task = Task.first
19
+ task.renamed_subtasks.count.should eq(0)
20
+
21
+ Moderation.count.should eq(1)
22
+ Moderation.last.accept
23
+ Moderation.count.should eq(0)
24
+
25
+ subtask = Task.first.renamed_subtasks.first
26
+ subtask.title.should eq("Subtask 1")
27
+ end
28
+
29
+ it "creates and associates subtask (build, save task)" do
30
+ task = Task.create! :title => "Task 1"
31
+ Moderation.count.should eq(0)
32
+ task.renamed_subtasks.build :title => "Subtask 1"
33
+ task.save
34
+
35
+ task = Task.first
36
+ task.renamed_subtasks.count.should eq(0)
37
+
38
+ Moderation.count.should eq(1)
39
+ Moderation.last.accept
40
+ Moderation.count.should eq(0)
41
+
42
+ subtask = Task.first.renamed_subtasks.first
43
+ subtask.title.should eq("Subtask 1")
44
+ end
45
+
46
+ it "creates and associates subtask (build, save subtask)", :broken => true do
47
+ task = Task.create! :title => "Task 1"
48
+ Moderation.count.should eq(0)
49
+ subtask = task.renamed_subtasks.build :title => "Subtask 1"
50
+ subtask.save
51
+
52
+ task = Task.first
53
+ task.renamed_subtasks.count.should eq(0)
54
+
55
+ Moderation.count.should eq(1)
56
+ Moderation.last.accept
57
+ Moderation.count.should eq(0)
58
+
59
+ subtask = Task.first.renamed_subtasks.first
60
+ subtask.title.should eq("Subtask 1")
61
+ end
62
+
63
+ it "associates (<<) existing subtask" do
64
+ task = Task.create! :title => "Task 1"
65
+ Subtask.create! :title => "Subtask 1"
66
+ Moderation.count.should eq(0)
67
+ task.renamed_subtasks << Subtask.first
68
+
69
+ task = Task.first
70
+ task.renamed_subtasks.count.should eq(0)
71
+
72
+ Moderation.count.should eq(1)
73
+ Moderation.last.accept
74
+ Moderation.count.should eq(0)
75
+
76
+ subtask = Task.first.renamed_subtasks.first
77
+ subtask.title.should eq("Subtask 1")
78
+ end
79
+
80
+ it "associates (push) existing subtask" do
81
+ task = Task.create! :title => "Task 1"
82
+ Subtask.create! :title => "Subtask 1"
83
+ Moderation.count.should eq(0)
84
+ task.renamed_subtasks.push Subtask.first
85
+
86
+ task = Task.first
87
+ task.renamed_subtasks.count.should eq(0)
88
+
89
+ Moderation.count.should eq(1)
90
+ Moderation.last.accept
91
+ Moderation.count.should eq(0)
92
+
93
+ subtask = Task.first.renamed_subtasks.first
94
+ subtask.title.should eq("Subtask 1")
95
+ end
96
+
97
+ it "moderates deleting association" do
98
+ task = Task.create! :title => "Task 1"
99
+ task.renamed_subtasks.create! :title => "Subtask 1"
100
+ Moderation.last.accept
101
+
102
+ Task.last.renamed_subtasks.delete(Task.last.renamed_subtasks.last)
103
+ Task.last.renamed_subtasks.count.should eq(1)
104
+
105
+ Moderation.last.accept
106
+ Task.last.renamed_subtasks.count.should eq(0)
107
+ end
108
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+ describe "has_many :through association:" do
4
+ before do
5
+ dynamic_models.task {
6
+ has_many :renamed_connections, :class_name => "TaskConnection", :foreign_key => "m1_id"
7
+ has_many :renamed_subtasks, :class_name => "Subtask", :through => :renamed_connections, :source => :renamed_subtask
8
+ has_moderated_association :renamed_subtasks
9
+ has_moderated_association :renamed_connections
10
+ }.subtask {
11
+ has_many :renamed_connections, :class_name => "TaskConnection", :foreign_key => "m2_id"
12
+ has_many :renamed_tasks, :through => :renamed_connections, :source => :renamed_task
13
+ }.task_connection {
14
+ belongs_to :renamed_task, :class_name => "Task", :foreign_key => "m1_id"
15
+ belongs_to :renamed_subtask, :class_name => "Subtask", :foreign_key => "m2_id"
16
+ }
17
+ end
18
+
19
+ it "associates subtask 1 (update)" do
20
+ task = Task.create! :title => "Task 1"
21
+ Moderation.count.should eq(0)
22
+
23
+ conn = TaskConnection.new :title => "Connection 1"
24
+ conn.renamed_subtask = Subtask.new :title => "Subtask 1"
25
+ task.renamed_connections << conn
26
+ task.save
27
+
28
+ TaskConnection.count.should eq(0)
29
+ Subtask.count.should eq(0)
30
+
31
+ Moderation.last.accept
32
+ Moderation.count.should eq(0)
33
+
34
+ subtask = Task.first.renamed_subtasks.first
35
+ subtask.title.should eq("Subtask 1")
36
+ conn = Subtask.first.renamed_connections.first
37
+ conn.title.should eq("Connection 1")
38
+ conn.renamed_subtask.title.should eq("Subtask 1")
39
+ conn.renamed_task.title.should eq("Task 1")
40
+ end
41
+
42
+ it "associates subtask 2 (update)" do
43
+ task = Task.create! :title => "Task 1"
44
+ Moderation.count.should eq(0)
45
+
46
+ task.renamed_subtasks.build :title => "Subtask 1"
47
+ task.save
48
+
49
+ TaskConnection.count.should eq(0)
50
+ Subtask.count.should eq(0)
51
+ Moderation.last.accept
52
+ Moderation.count.should eq(0)
53
+ Subtask.count.should eq(1)
54
+
55
+ subtask = Task.first.renamed_subtasks.first
56
+ subtask.title.should eq("Subtask 1")
57
+ end
58
+
59
+ it "associates subtask 3 (update)" do
60
+ task = Task.create! :title => "Task 1"
61
+ Moderation.count.should eq(0)
62
+
63
+ task.renamed_subtasks.create! :title => "Subtask 1"
64
+
65
+ TaskConnection.count.should eq(0)
66
+ Subtask.count.should eq(0)
67
+
68
+ Moderation.last.accept
69
+ Moderation.count.should eq(0)
70
+ Subtask.count.should eq(1)
71
+
72
+ subtask = Task.first.renamed_subtasks.first
73
+ subtask.title.should eq("Subtask 1")
74
+ end
75
+
76
+ it "associates subtask 1 (create)" do
77
+ t = Task.new :title => "Task 1"
78
+ t.renamed_subtasks.build :title => "Subtask 1"
79
+ t.save
80
+
81
+ Subtask.count.should eq(0)
82
+ Task.first.title.should eq("Task 1")
83
+ Task.first.renamed_subtasks.count.should eq(0)
84
+ Task.count.should eq(1)
85
+
86
+ Moderation.last.accept
87
+
88
+ Task.first.renamed_subtasks.count.should eq(1)
89
+ Task.first.renamed_subtasks.first.title.should eq("Subtask 1")
90
+ end
91
+ end