Class: Version
- Inherits:
-
PaperTrail::Version
- Object
- PaperTrail::Version
- Version
- Defined in:
- app/models/polymorphic/version.rb
Constant Summary collapse
- ASSETS =
%w[all tasks campaigns leads accounts contacts opportunities comments emails]
- EVENTS =
%w[all_events create view update destroy]
- DURATION =
%w[one_hour one_day two_days one_week two_weeks one_month]
Class Method Summary collapse
- .history(object) ⇒ Object
- .latest(options = {}) ⇒ Object
- .recent_for_user(user, limit = 10) ⇒ Object
- .related_to(object) ⇒ Object
- .visible_to(user) ⇒ Object
Class Method Details
.history(object) ⇒ Object
61 62 63 |
# File 'app/models/polymorphic/version.rb', line 61 def history(object) (object).exclude_events(:view).default_order end |
.latest(options = {}) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'app/models/polymorphic/version.rb', line 46 def latest( = {}) includes(:item, :related, :user) .where(({ item_type: [:asset] } if [:asset])) .where(({ event: [:event] } if [:event])) .where(({ whodunnit: [:user].to_s } if [:user])) .where('versions.created_at >= ?', Time.zone.now - ([:duration] || 2.days)) .limit([:max]) .default_order end |
.recent_for_user(user, limit = 10) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/polymorphic/version.rb', line 24 def recent_for_user(user, limit = 10) # Hybrid SQL/Ruby to build a unique list of the most recent entities that the # user has interacted with versions = [] offset = 0 while versions.size < limit query = includes(:item) .where(whodunnit: user.id.to_s) .where(item_type: ENTITIES) .limit(limit * 2) .offset(offset) .default_order break if query.empty? versions += query.select { |v| v.item.present? } versions.uniq! { |v| [v.item_id, v.item_type] } offset += limit * 2 end versions[0...10] end |
.related_to(object) ⇒ Object
56 57 58 59 |
# File 'app/models/polymorphic/version.rb', line 56 def (object) where('(item_id = :id AND item_type = :type) OR (related_id = :id AND related_type = :type)', id: object.id, type: object.class.name) end |
.visible_to(user) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'app/models/polymorphic/version.rb', line 65 def visible_to(user) all.to_a.delete_if do |version| if item = version.item || version.reify if item.respond_to?(:access) # NOTE: Tasks don't have :access as of yet. # Delete from scope if it shouldn't be visible next item.user_id != user.id && item.assigned_to != user.id && (item.access == "Private" || (item.access == "Shared" && !item..map(&:user_id).include?(user.id))) end # Don't delete any objects that don't have :access method (e.g. tasks) next false end # Delete from scope if no object can be found or reified (e.g. from 'show' events) true end end |