Module: HasModerated::Associations::Base::ApplyModeration
- Defined in:
- lib/has_moderated/associations/base.rb
Overview
module
Class Method Summary collapse
- .add_assoc_to_record(to, assoc_id, reflection) ⇒ Object
-
.apply(record, data, save_opts = Hash.new, preview_mode = false) ⇒ Object
add/delete associations to a record.
- .apply_add_association(to, reflection, attrs, save_opts = Hash.new) ⇒ Object
- .apply_delete_association(to, reflection, attrs) ⇒ Object
- .delete_assoc_from_record(from, assoc_id, reflection) ⇒ Object
-
.try_without_moderation(*args, &block) ⇒ Object
just a helper.
Class Method Details
.add_assoc_to_record(to, assoc_id, reflection) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/has_moderated/associations/base.rb', line 53 def self.add_assoc_to_record(to, assoc_id, reflection) return unless to && assoc_id # TODO has_one weirdness? if reflection.macro == :has_many || reflection.macro == :has_and_belongs_to_many HasModerated::Associations::Collection::AssociationHelpers::add_assoc_to_record(to, assoc_id, reflection) end end |
.apply(record, data, save_opts = Hash.new, preview_mode = false) ⇒ Object
add/delete associations to a record
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/has_moderated/associations/base.rb', line 142 def self.apply(record, data, save_opts = Hash.new, preview_mode = false) record.instance_variable_set(:@has_moderated_preview, true) if preview_mode associations = data[:associations] delete_associations = data[:delete_associations] associations && associations.each_pair do |assoc_name, assoc_records| reflection = record.class.reflections[assoc_name.to_sym] assoc_records.each do |attrs| apply_add_association(record, reflection, attrs, save_opts) if attrs.present? end end delete_associations && delete_associations.each_pair do |assoc_name, assoc_records| reflection = record.class.reflections[assoc_name.to_sym] assoc_records.each do |attrs| apply_delete_association(record, reflection, attrs) if attrs.present? end end record end |
.apply_add_association(to, reflection, attrs, save_opts = Hash.new) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/has_moderated/associations/base.rb', line 72 def self.apply_add_association(to, reflection, attrs, save_opts = Hash.new) preview_mode = to.instance_variable_get(:@has_moderated_preview) klass = reflection.class_name.constantize fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection) attrs = HashWithIndifferentAccess.new(attrs) if attrs.kind_of? Hash # TODO: perhaps allow to change existing associated object if attrs.class != Fixnum && !attrs[:id].blank? attrs = attrs[:id].to_i end # parse new associations try_without_moderation(to) do |rec| arec = nil # PARAM = ID if attrs.class == Fixnum arec = klass.find_by_id(attrs) arec.instance_variable_set(:@has_moderated_preview, preview_mode) add_assoc_to_record(rec, arec, reflection) # PARAM = Hash (create) elsif attrs.kind_of? Hash arec = klass.new arec.instance_variable_set(:@has_moderated_preview, preview_mode) # set foreign key first, may be required sometimes add_assoc_to_record(rec, arec, reflection) attrs.each_pair do |key, val| next if key.to_s == 'associations' || key.to_s == 'id' || key.to_s == fk arec.send(key.to_s+"=", val) end # recursive, used for has_many :through if attrs[:associations].present? Moderation.without_moderation do arec.save(save_opts) end apply(arec, attrs, save_opts, preview_mode) end else raise "don't know how to parse #{attrs.class}" end if arec try_without_moderation(arec) do arec.save(save_opts) end if reflection.collection? rec = rec.reload rec.instance_variable_set(:@has_moderated_preview, true) if preview_mode #rec.send(reflection.name.to_s) << arec unless rec.send(reflection.name.to_s).include?(arec) else rec.send(reflection.name.to_s + "=", arec) rec.save(save_opts) end end end end |
.apply_delete_association(to, reflection, attrs) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/has_moderated/associations/base.rb', line 128 def self.apply_delete_association(to, reflection, attrs) m = reflection.class_name.constantize fk = HasModerated::ActiveRecordHelpers::foreign_key(reflection) return if attrs.blank? return if attrs.class != Fixnum try_without_moderation(to) do delete_assoc_from_record(to, attrs, reflection) end end |
.delete_assoc_from_record(from, assoc_id, reflection) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/has_moderated/associations/base.rb', line 62 def self.delete_assoc_from_record(from, assoc_id, reflection) return unless from && assoc_id if reflection.macro == :has_one HasModerated::Associations::HasOne::AssociationHelpers::delete_assoc_from_record(from, assoc_id, reflection) else HasModerated::Associations::Collection::AssociationHelpers::delete_assoc_from_record(from, assoc_id, reflection) end end |
.try_without_moderation(*args, &block) ⇒ Object
just a helper
49 50 51 |
# File 'lib/has_moderated/associations/base.rb', line 49 def self.try_without_moderation(*args, &block) HasModerated::Common::try_without_moderation(*args, &block) end |