Class: Draft
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Draft
- Defined in:
- lib/kentouzu/draft.rb
Class Method Summary collapse
- .creates ⇒ Object
- .updates ⇒ Object
- .with_item_keys(item_type, item_id) ⇒ Object
- .with_source_keys(source_type, source_id) ⇒ Object
Instance Method Summary collapse
- #approve ⇒ Object
- #between(start_time, end_time) ⇒ Object
- #following(timestamp) ⇒ Object
- #preceding(obj, timestamp_arg = false) ⇒ Object
- #primary_key_is_int? ⇒ Boolean
-
#reify(options = {}) ⇒ Object
Restore the item from this draft.
- #reject ⇒ Object
- #subsequent(obj, timestamp_arg = false) ⇒ Object
- #timestamp_sort_order(direction = 'asc') ⇒ Object
Class Method Details
.creates ⇒ Object
20 21 22 |
# File 'lib/kentouzu/draft.rb', line 20 def self.creates where :event => 'create' end |
.updates ⇒ Object
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
#approve ⇒ Object
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.].gt(start_time).and(arel_table[Kentouzu.].lt(end_time))).order(self.) end |
#following(timestamp) ⇒ Object
48 49 50 |
# File 'lib/kentouzu/draft.rb', line 48 def following() where(arel_table[Kentouzu.].gt()).order(self.) 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, = false) if != 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.) if obj.is_a?(self) where(arel_table[Kentouzu.].lt(obj)).order(self.) end end |
#primary_key_is_int? ⇒ 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( = {}) return nil if object.nil? without_identity_map do [:has_one] = 3 if [:has_one] == true .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..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 [:has_one] == false reify_has_ones model, [:has_one] end model end end |
#reject ⇒ Object
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, = false) if != 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.) if obj.is_a?(self) where(arel_table[Kentouzu.].gt(obj)).order(self.) end end |
#timestamp_sort_order(direction = 'asc') ⇒ Object
56 57 58 59 60 |
# File 'lib/kentouzu/draft.rb', line 56 def (direction = 'asc') [arel_table[Kentouzu.].send(direction.downcase)].tap do |array| array << arel_table[primary_key].send(direction.downcase) if self.primary_key_is_int? end end |