Class: MongoTrails::Version
- Inherits:
-
Object
- Object
- MongoTrails::Version
- Includes:
- Mongoid::Document, PaperTrail::VersionConcern
- Defined in:
- lib/mongo_trails/mongo_support/version.rb
Instance Attribute Summary collapse
-
#mongo_trails_source_item ⇒ Object
Returns the value of attribute mongo_trails_source_item.
Class Method Summary collapse
- .abstract_class? ⇒ Boolean
- .after_create(_name) ⇒ Object
- .belongs_to(_name, _options = {}, &block) ⇒ Object
- .bulk_insert_attributes(version) ⇒ Object
- .column_names ⇒ Object
- .columns_hash ⇒ Object
- .find(id) ⇒ Object
- .incrementing_fields ⇒ Object
- .next_integer_id ⇒ Object
- .next_integer_ids(count, scope: prefix_map) ⇒ Object
- .prefix_map ⇒ Object
- .table_name ⇒ Object
- .validates_presence_of(_name) ⇒ Object
Instance Method Summary collapse
- #escape_value(value) ⇒ Object
-
#initialize(data) ⇒ Version
constructor
A new instance of Version.
- #item ⇒ Object
- #object ⇒ Object
- #object=(value) ⇒ Object
- #object_changes ⇒ Object
- #object_changes=(value) ⇒ Object
- #save_version ⇒ Object
- #save_version! ⇒ Object
- #unescape_value(value) ⇒ Object
Constructor Details
#initialize(data) ⇒ Version
Returns a new instance of Version.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 148 def initialize(data) data = data.to_h.deep_symbolize_keys item = data.delete(:item) if item.present? data[:item_type] = item.class.name data[:item_id] = item.id end data[:created_at] = normalize_created_at(data[:created_at]) super end |
Instance Attribute Details
#mongo_trails_source_item ⇒ Object
Returns the value of attribute mongo_trails_source_item.
138 139 140 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 138 def mongo_trails_source_item @mongo_trails_source_item end |
Class Method Details
.abstract_class? ⇒ Boolean
74 75 76 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 74 def abstract_class? false end |
.after_create(_name) ⇒ Object
90 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 90 def after_create(_name); end |
.belongs_to(_name, _options = {}, &block) ⇒ Object
86 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 86 def belongs_to(_name, = {}, &block); end |
.bulk_insert_attributes(version) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 65 def bulk_insert_attributes(version) version = version.is_a?(self) ? version : new(version) attrs = version.attributes.dup attrs.delete('_id') if attrs['_id'].nil? attrs end |
.column_names ⇒ Object
82 83 84 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 82 def column_names fields.keys end |
.columns_hash ⇒ Object
78 79 80 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 78 def columns_hash fields end |
.find(id) ⇒ Object
28 29 30 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 28 def find(id) find_by(integer_id: id) end |
.incrementing_fields ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 32 def incrementing_fields @incrementing_fields ||= { integer_id: { scope: -> { MongoTrails::Version.prefix_map }, step: 1 } } end |
.next_integer_id ⇒ Object
45 46 47 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 45 def next_integer_id next_integer_ids(1).first end |
.next_integer_ids(count, scope: prefix_map) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 49 def next_integer_ids(count, scope: prefix_map) return [] if count.to_i <= 0 counter_id = counter_key(scope) ensure_counter_initialized(counter_id, scope) counter = AutoIncrementCounters.collection.find(_id: counter_id).find_one_and_update( { '$inc' => { sequence: count } }, upsert: true, return_document: :after ) max_integer_id = counter.fetch('sequence') ((max_integer_id - count + 1)..max_integer_id).to_a end |
.prefix_map ⇒ Object
41 42 43 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 41 def prefix_map (PaperTrail.config.mongo_prefix.is_a?(Proc) && PaperTrail.config.mongo_prefix.call) || 'paper_trail' end |
.table_name ⇒ Object
72 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 72 def table_name; end |
.validates_presence_of(_name) ⇒ Object
88 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 88 def validates_presence_of(_name); end |
Instance Method Details
#escape_value(value) ⇒ Object
184 185 186 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 184 def escape_value(value) value&.deep_transform_keys { |key| parser.escape(force_utf8(key.to_s), /[$.]/) } end |
#item ⇒ Object
160 161 162 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 160 def item item_type.constantize.find_by(id: item_id) end |
#object ⇒ Object
168 169 170 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 168 def object unescape_value(super) end |
#object=(value) ⇒ Object
164 165 166 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 164 def object=(value) super(escape_value(value)) end |
#object_changes ⇒ Object
172 173 174 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 172 def object_changes unescape_value(super) end |
#object_changes=(value) ⇒ Object
176 177 178 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 176 def object_changes=(value) super(escape_value(value)) end |
#save_version ⇒ Object
140 141 142 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 140 def save_version schedule_version_persistence(bang: false) end |
#save_version! ⇒ Object
144 145 146 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 144 def save_version! schedule_version_persistence(bang: true) end |
#unescape_value(value) ⇒ Object
180 181 182 |
# File 'lib/mongo_trails/mongo_support/version.rb', line 180 def unescape_value(value) value&.deep_transform_keys { |key| parser.unescape(force_utf8(key)) } end |