has_moderated 1.0.rc3 → 1.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,107 @@
1
+ # everything that does with activerecord should be here.
2
+ # use a wrapper that calls stuff from here, so that impl. can be switched eg with mongomapper
3
+ # also write test that if one attr is moderated and one is not, the unmoderated one must still save immediately!
4
+
5
+ module HasModerated
6
+ module ActiveRecordHelpers
7
+ # AR specific
8
+ def self.foreign_key(reflection)
9
+ if reflection.respond_to?(:foreign_key)
10
+ reflection.foreign_key
11
+ else # Rails < v3.1
12
+ reflection.primary_key_name
13
+ end
14
+ end
15
+
16
+ # General
17
+ def self.add_moderations_association(klass)
18
+ klass.class_eval do
19
+ has_many :moderations, :as => :moderatable, :dependent => :destroy
20
+ end
21
+ end
22
+
23
+ def self.get_default_moderation_attributes(record)
24
+ record.attributes
25
+ end
26
+
27
+ def self.hashize_association(from_record, assoc_name, m)
28
+ # todo
29
+ through_assocs = {}
30
+ from_record.class.reflections.keys.each do |assoc|
31
+ join_model = from_record.class.reflections[assoc.to_sym].options[:through]
32
+ if join_model
33
+ join_model = join_model.to_sym
34
+ through_assocs[join_model] ||= []
35
+ through_assocs[join_model].push(assoc)
36
+ end
37
+ end
38
+
39
+ assoc = nil
40
+ if m.class == Fixnum
41
+ assoc = m
42
+ elsif m.kind_of? Hash # already a hash (for has_many :through association)
43
+ assoc = m
44
+ elsif m.respond_to?(:get_moderation_attributes) && m.new_record?
45
+ assoc = m.get_moderation_attributes
46
+ elsif m.respond_to?(:get_moderation_attributes)
47
+ assoc = m.id
48
+ else
49
+ raise "don't know how to convert #{m.class} to hash"
50
+ end
51
+
52
+ if through_assocs[assoc_name.to_sym] # TODO !
53
+ assoc[:associations] = get_assocs_for_moderation(:all, m)
54
+ end
55
+ assoc
56
+ end
57
+
58
+ def self.get_assocs_for_moderation assocs, from_record = nil
59
+ from_record ||= self
60
+ return if assocs.blank?
61
+
62
+ if assocs == :all
63
+ assocs = from_record.class.reflections.keys.reject do |r|
64
+ r == :moderations
65
+ end
66
+ end
67
+
68
+ assocs = [assocs] unless assocs.respond_to?("[]")
69
+
70
+ # check for through assocs
71
+ assocs = assocs.dup
72
+ through_assocs = {}
73
+ assocs.each do |assoc|
74
+ join_model = from_record.class.reflections[assoc.to_sym].options[:through]
75
+ if join_model
76
+ join_model = join_model.to_sym
77
+ through_assocs[join_model] ||= []
78
+ through_assocs[join_model].push(assoc)
79
+ assocs.push(join_model) unless assocs.include?(join_model)
80
+ #assocs.delete(assoc)
81
+ end
82
+ end
83
+
84
+ assoc_attrs = {}
85
+ assocs.each do |assoc|
86
+ one_assoc = []
87
+ assoc_value = from_record.send(assoc)
88
+ # if it's has_one it won't be an array
89
+ assoc_value = [assoc_value] if assoc_value && assoc_value.class != Array
90
+ assoc_value ||= []
91
+ assoc_value.each do |m|
92
+ if m.new_record?
93
+ one_assoc.push(m.get_moderation_attributes)
94
+ else
95
+ one_assoc.push(m.id)
96
+ end
97
+ if through_assocs[assoc.to_sym]
98
+ one_assoc.last[:associations] = get_assocs_for_moderation(:all, m)
99
+ end
100
+ end
101
+ assoc_attrs[assoc] = one_assoc unless one_assoc.empty?
102
+ end
103
+
104
+ assoc_attrs
105
+ end
106
+ end
107
+ end
@@ -71,7 +71,7 @@ module HasModerated
71
71
 
72
72
  def self.apply_add_association(to, reflection, attrs)
73
73
  klass = reflection.class_name.constantize
74
- fk = HasModerated::Adapters::ActiveRecord::foreign_key(reflection)
74
+ fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection)
75
75
 
76
76
  attrs = HashWithIndifferentAccess.new(attrs) if attrs.kind_of? Hash
77
77
 
@@ -107,7 +107,8 @@ module HasModerated
107
107
  # TODO: validations? sup?
108
108
  end
109
109
  if reflection.collection?
110
- rec.send(reflection.name.to_s) << arec unless rec.send(reflection.name.to_s).include?(arec)
110
+ rec = rec.reload
111
+ #rec.send(reflection.name.to_s) << arec unless rec.send(reflection.name.to_s).include?(arec)
111
112
  else
112
113
  rec.send(reflection.name.to_s + "=", arec)
113
114
  end
@@ -118,7 +119,7 @@ module HasModerated
118
119
  def self.apply_delete_association(to, reflection, attrs)
119
120
  m = reflection.class_name.constantize
120
121
 
121
- fk = HasModerated::Adapters::ActiveRecord::foreign_key(reflection)
122
+ fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection)
122
123
 
123
124
  return if attrs.blank?
124
125
  return if attrs.class != Fixnum
@@ -85,14 +85,14 @@ module HasModerated
85
85
  end
86
86
 
87
87
  def self.add_assoc_to_record_hm(to, record, reflection)
88
- fk = HasModerated::Adapters::ActiveRecord::foreign_key(reflection).try(:to_s)
88
+ fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection).try(:to_s)
89
89
  field = if !reflection.options[:as].blank?
90
90
  reflection.options[:as].to_s
91
91
  elsif !fk.blank?
92
92
  all_keys = []
93
93
  results = record.class.reflections.reject do |assoc_name, assoc|
94
- all_keys.push(HasModerated::Adapters::ActiveRecord::foreign_key(assoc).try(:to_s))
95
- !(HasModerated::Adapters::ActiveRecord::foreign_key(assoc).try(:to_s) == fk)
94
+ all_keys.push(HasModerated::ActiveRecordHelpers::foreign_key(assoc).try(:to_s))
95
+ !(HasModerated::ActiveRecordHelpers::foreign_key(assoc).try(:to_s) == fk)
96
96
  end
97
97
  if results.blank?
98
98
  raise "Please set foreign_key for both belongs_to and has_one/has_many! fk: #{fk}, keys: #{all_keys.to_yaml}"
@@ -10,7 +10,7 @@ module HasModerated
10
10
  attr_accessor :moderation_disabled
11
11
  @@moderation_disabled = false
12
12
  end
13
- HasModerated::Adapters::Proxy::add_moderations_association klass
13
+ HasModerated::ActiveRecordHelpers::add_moderations_association klass
14
14
  end
15
15
  end
16
16
 
@@ -42,7 +42,7 @@ module HasModerated
42
42
  end
43
43
 
44
44
  def get_moderation_attributes
45
- HasModerated::Adapters::Proxy::get_default_moderation_attributes(self)
45
+ HasModerated::ActiveRecordHelpers::get_default_moderation_attributes(self)
46
46
  end
47
47
 
48
48
  def create_moderation_with_hooks!(*args)
@@ -68,7 +68,7 @@ module HasModerated
68
68
  assocs_processed[assoc_name] = []
69
69
  assoc_data_array.each do |assoc_data|
70
70
  # convert assoc data to either ID or, if it's a new record, hash of its attributes
71
- pdata = HasModerated::Adapters::Proxy::hashize_association(self, assoc_name, assoc_data)
71
+ pdata = HasModerated::ActiveRecordHelpers::hashize_association(self, assoc_name, assoc_data)
72
72
  assocs_processed[assoc_name].push(pdata)
73
73
  end
74
74
  end
@@ -44,7 +44,7 @@ module HasModerated
44
44
 
45
45
  def to_moderation_created
46
46
  options = self.class.moderated_create_options
47
- assoc_attrs = HasModerated::Adapters::ActiveRecord::get_assocs_for_moderation(options[:with_associations], self)
47
+ assoc_attrs = HasModerated::ActiveRecordHelpers::get_assocs_for_moderation(options[:with_associations], self)
48
48
 
49
49
  create_moderation_with_hooks!(:create => {
50
50
  :attributes => get_moderation_attributes,
@@ -1,3 +1,3 @@
1
1
  module HasModerated
2
- VERSION = "1.0.rc3"
2
+ VERSION = "1.0.rc4"
3
3
  end
data/lib/has_moderated.rb CHANGED
@@ -2,8 +2,7 @@ require 'has_moderated/common'
2
2
  require 'has_moderated/user_hooks'
3
3
  require 'has_moderated/moderation_model'
4
4
 
5
- require 'has_moderated/adapters/proxy'
6
- require 'has_moderated/adapters/active_record'
5
+ require 'has_moderated/active_record/active_record_helpers'
7
6
  require 'has_moderated/associations/base'
8
7
  require 'has_moderated/associations/has_one'
9
8
  require 'has_moderated/associations/collection'
@@ -1,3 +1,4 @@
1
1
  class Subtask < ActiveRecord::Base
2
+ attr_accessible :title, :desc
2
3
  belongs_to :task
3
4
  end
@@ -1,3 +1,3 @@
1
1
  class Task < ActiveRecord::Base
2
- attr_accessible :title
2
+ attr_accessible :title, :desc
3
3
  end
@@ -5,7 +5,7 @@ Dummy::Application.configure do
5
5
  # test suite. You never need to work with it otherwise. Remember that
6
6
  # your test database is "scratch space" for the test suite and is wiped
7
7
  # and recreated between test runs. Don't rely on the data there!
8
- config.cache_classes = true
8
+ config.cache_classes = false
9
9
 
10
10
  # Configure static asset server for tests with Cache-Control for performance
11
11
  config.serve_static_assets = true