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



20
21
22
# File 'lib/kentouzu/draft.rb', line 20

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

.updatesObject



24
25
26
# File 'lib/kentouzu/draft.rb', line 24

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

.with_item_keys(item_type, item_id) ⇒ Object



12
13
14
# File 'lib/kentouzu/draft.rb', line 12

def self.with_item_keys(item_type, item_id)
  where :item_type => item_type, :item_id => item_id
end

.with_source_keys(source_type, source_id) ⇒ Object



16
17
18
# File 'lib/kentouzu/draft.rb', line 16

def self.with_source_keys(source_type, source_id)
  where :source_type => source_type, :source_id => source_id
end

Instance Method Details

#approveObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/kentouzu/draft.rb', line 120

def approve
  warn 'DEPRECATED: `approve` should be handled by your application, not Kentouzu. Will be removed in Kentouzu 0.3.0.'

  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

#between(start_time, end_time) ⇒ Object



52
53
54
# File 'lib/kentouzu/draft.rb', line 52

def between(start_time, end_time)
  where(arel_tabl[Kentouzu.timestamp_field].gt(start_time).and(arel_table[Kentouzu.timestamp_field].lt(end_time))).order(self.timestamp_sort_order)
end

#following(timestamp) ⇒ Object



48
49
50
# File 'lib/kentouzu/draft.rb', line 48

def following(timestamp)
  where(arel_table[Kentouzu.timestamp_field].gt(timestamp)).order(self.timestamp_sort_order)
end

#preceding(obj, timestamp_arg = false) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/kentouzu/draft.rb', line 38

def preceding(obj, timestamp_arg = false)
  if timestamp_arg != true && self.primary_key_is_int?
    where(arel_table[primary_key].lt(obj.id)).order(arel_table[primary_key].asc)
  else
    obj = obj.send(Kentouzu.timestamp_field) if obj.is_a?(self)

    where(arel_table[Kentouzu.timestamp_field].lt(obj)).order(self.timestamp_sort_order)
  end
end

#primary_key_is_int?Boolean

Returns:

  • (Boolean)


148
149
150
151
152
# File 'lib/kentouzu/draft.rb', line 148

def primary_key_is_int?
  @primary_key_is_int ||= columns_hash[primary_key].type == :integer
rescue
  true
end

#reify(options = {}) ⇒ Object

Restore the item from this draft.

Options: :has_one Set to ‘false` to disable has_one reification.

Set to a float to change the lookback time.


67
68
69
70
71
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
# File 'lib/kentouzu/draft.rb', line 67

def reify(options = {})
  return nil if object.nil?

  without_identity_map do
    options[:has_one] = 3 if options[:has_one] == true

    options.reverse_merge! :has_one => false

    #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.underscore

    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) && loaded_object.send(inheritance_column_name.to_sym).present? ? loaded_object.send(inheritance_column_name.to_sym) : item_type

      klass = class_name.constantize

      model = klass.new
    end

    has_many_associations = model.class.reflect_on_all_associations(:has_many).reject { |association| association.name == :drafts || association.options.keys.include?(:through) }.map { |association| association.name }

    has_and_belongs_to_many_associations = model.class.reflect_on_all_associations(:has_and_belongs_to_many).map { |association| association.plural_name }

    loaded_object.each do |key, value|
      if model.respond_to?("#{key}=")
        if has_many_associations.include?(key.to_sym)
          model.send "#{key}=".to_sym, value.map { |v| model.send(key.to_sym).proxy_association.klass.new(v) }
        elsif has_and_belongs_to_many_associations.include?(key.gsub('_ids', '').pluralize)
          model.send "#{key.gsub('_ids', '').pluralize}=".to_sym, model.class.reflect_on_all_associations(:has_and_belongs_to_many).select { |association| association.plural_name == key.gsub('_ids', '').pluralize }.first.class_name.constantize.where(id: value)
        else
          model.send :write_attribute, key.to_sym, value
        end
      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

#rejectObject



142
143
144
145
146
# File 'lib/kentouzu/draft.rb', line 142

def reject
  warn 'DEPRECATED: `reject` should be handled by your application, not Kentouzu. Will be removed in Kentouzu 0.3.0.'

  self.destroy
end

#subsequent(obj, timestamp_arg = false) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/kentouzu/draft.rb', line 28

def subsequent(obj, timestamp_arg = false)
  if timestamp_arg != true && self.primary_key_is_int?
    where(arel_table[primary_key].gt(obj.id)).order(arel_table[primary_key].asc)
  else
    obj = obj.send(Kentouzu.timestamp_field) if obj.is_a?(self)

    where(arel_table[Kentouzu.timestamp_field].gt(obj)).order(self.timestamp_sort_order)
  end
end

#timestamp_sort_order(direction = 'asc') ⇒ Object



56
57
58
59
60
# File 'lib/kentouzu/draft.rb', line 56

def timestamp_sort_order(direction = 'asc')
  [arel_table[Kentouzu.timestamp_field].send(direction.downcase)].tap do |array|
    array << arel_table[primary_key].send(direction.downcase) if self.primary_key_is_int?
  end
end