Class: Draft

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/kentouzu/draft.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.createsObject



14
15
16
# File 'lib/kentouzu/draft.rb', line 14

def self.creates
  where :event => 'create'
end

.updatesObject



18
19
20
# File 'lib/kentouzu/draft.rb', line 18

def self.updates
  where :event => 'update'
end

.with_item_keys(item_type, item_id) ⇒ Object



10
11
12
# File 'lib/kentouzu/draft.rb', line 10

def self.with_item_keys item_type, item_id
  scoped :conditions => { :item_type => item_type, :item_id => item_id }
end

Instance Method Details

#approveObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/kentouzu/draft.rb', line 72

def approve
  model = self.reify

  if model
    model.without_drafts :save

    if event == 'update'
      previous_drafts = Draft.where(["item_type = ? AND item_id = ? AND created_at <= ? AND id <= ?", model.class.to_s, model.id, self.created_at.strftime('%Y-%m-%d %H:%M:%S'), self.id])

      previous_drafts.each do |previous_draft|
        previous_draft.delete
      end
    end
  end

  self.destroy

  model
end

#reify(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kentouzu/draft.rb', line 30

def reify options = {}
  without_identity_map do
    options[:has_one] = 3 if options[:has_one] == true
    options.reverse_merge! :has_one => false

    unless object.nil?
      #This appears to be necessary if for some reason the draft's model hasn't been loaded (such as when done in the console).
      require self.item_type.downcase

      loaded_object = YAML::load object

      if item
        model = item
      else
        inheritance_column_name = item_type.constantize.inheritance_column

        class_name = loaded_object.respond_to?(inheritance_column_name.to_sym) ? inheritance_column_name : item_type

        klass = class_name.constantize

        model = klass.new
      end

      loaded_object.attributes.each do |key, value|
        if model.respond_to?("#{key}=")
          model.send :write_attribute, key.to_sym, value
        else
          logger.warn "Attribute #{key} does not exist on #{item_type} (Draft ID: #{id})."
        end
      end

      model.send "#{model.class.draft_association_name}=", self

      unless options[:has_one] == false
        reify_has_ones model, options[:has_one]
      end

      model
    end
  end
end

#rejectObject



92
93
94
# File 'lib/kentouzu/draft.rb', line 92

def reject
  self.destroy
end