Module: HasModerated::ModerationModel

Included in:
Moderation
Defined in:
lib/has_moderated/moderation_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/has_moderated/moderation_model.rb', line 3

def self.included(base)
  base.class_eval do
    alias_method_chain :destroy, :moderation_callbacks
    cattr_accessor :moderation_disabled
    self.moderation_disabled = false

    def self.without_moderation(do_disable = true)
      already_disabled = self.moderation_disabled
      self.moderation_disabled = true if do_disable
      begin
        retval = yield(self)
      ensure
        self.moderation_disabled = false if do_disable && !already_disabled
      end
      retval
    end
  end
end

Instance Method Details

#accept(save_opts = Hash.new, preview_mode = false) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/has_moderated/moderation_model.rb', line 57

def accept(save_opts = Hash.new, preview_mode = false)
  begin
    accept!(save_opts, preview_mode)
    true
  rescue
    false
  end
end

#accept!(save_opts = Hash.new, preview_mode = false) ⇒ Object



50
51
52
53
54
55
# File 'lib/has_moderated/moderation_model.rb', line 50

def accept!(save_opts = Hash.new, preview_mode = false)
  record = apply(save_opts, preview_mode)
  accept_changes(record, save_opts)
  self.destroy(:preview_mode => preview_mode)
  record
end

#accept_changes(record, save_opts = Hash.new) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/has_moderated/moderation_model.rb', line 40

def accept_changes(record, save_opts = Hash.new)
  if record
    Moderation.without_moderation do
      # run validations (issue #12)
      record.save!(save_opts)
    end
  end
  record
end

#apply(save_opts = Hash.new, preview_mode = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/has_moderated/moderation_model.rb', line 26

def apply(save_opts = Hash.new, preview_mode = false)
  if create?
    record = HasModerated::ModeratedCreate::ApplyModeration::apply(moderatable_type.constantize, parsed_data, save_opts, preview_mode)
  else
    record = moderatable
    if record
      record = HasModerated::ModeratedAttributes::ApplyModeration::apply(record, parsed_data, preview_mode)
      record = HasModerated::Associations::Base::ApplyModeration::apply(record, parsed_data, save_opts, preview_mode)
      record = HasModerated::ModeratedDestroy::ApplyModeration::apply(record, parsed_data)
    end
  end
  record
end

#create?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/has_moderated/moderation_model.rb', line 110

def create?
  parsed_data[:create].present?
end

#destroy?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/has_moderated/moderation_model.rb', line 114

def destroy?
  parsed_data[:destroy] == true
end

#destroy_with_moderation_callbacks(*args) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/has_moderated/moderation_model.rb', line 66

def destroy_with_moderation_callbacks(*args)
  options = args.first || Hash.new
  if moderatable_type
    klass = moderatable_type.constantize
    klass.moderatable_discard(self, options) if klass.respond_to?(:moderatable_discard)
  end
  destroy_without_moderation_callbacks
end

#discardObject



75
76
77
# File 'lib/has_moderated/moderation_model.rb', line 75

def discard
  destroy_with_moderation_callbacks
end

#live_previewObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/has_moderated/moderation_model.rb', line 79

def live_preview
  # absolutely no point to preview a destroy moderation
  if destroy? && parsed_data.keys.count == 1
    yield(nil)
    return nil
  end

  self.transaction do
    record = accept!({ :perform_validation => false }, true)
    yield(record)
    raise ActiveRecord::Rollback
  end
  # self.frozen? now became true
  # since we don't actually commit to database, we don't need the freeze
  # only way I found to unfreeze is to dup attributes
  @attributes = @attributes.dup

  nil
end

#parsed_dataObject



22
23
24
# File 'lib/has_moderated/moderation_model.rb', line 22

def parsed_data
  @parsed_data ||= YAML::load(data)
end

#preview(options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/has_moderated/moderation_model.rb', line 99

def preview(options = {})
  options[:saveable] ||= false
  fake_record = nil
  live_preview do |record|
    fake_record = HasModerated::Preview::from_live(record, self, options[:saveable])
  end
  # Ruby 1.8 will unfreeze when doing ActiveRecord::Rollback
  # Only necessary to re-freeze for 1.8, associations stay frozen as normal
  fake_record.freeze
end

#update?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/has_moderated/moderation_model.rb', line 118

def update?
  !(create? || destroy?)
end