base.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. class Sessions::Backend::Collections::Base
  2. class << self; attr_accessor :model, :is_role, :is_not_role end
  3. def initialize( user, client = nil, client_id = nil )
  4. @user = user
  5. @client = client
  6. @client_id = client_id
  7. @last_change = nil
  8. end
  9. def collection_key
  10. "collections::load::#{ self.class.to_s }::#{ @user.id }"
  11. end
  12. def load
  13. #puts "-LOAD--------#{self.collection_key}"
  14. # check timeout
  15. cache = Sessions::CacheIn.get( self.collection_key )
  16. return cache if @last_change && cache
  17. #puts "---REAL FETCH #{@user.id}"
  18. # update last changed
  19. last = self.class.model.constantize.select('updated_at').order('updated_at DESC').first
  20. if last
  21. @last_change = last.updated_at
  22. end
  23. # if no entry exists, remember last check
  24. if !@last_change
  25. @last_change = Time.now
  26. end
  27. # get whole collection
  28. all = self.class.model.constantize.all
  29. # set new timeout
  30. Sessions::CacheIn.set( self.collection_key, all, { :expires_in => 10.minutes } )
  31. all
  32. end
  33. def changed?
  34. # if no data has been delivered till now
  35. return true if !@last_change
  36. # check if update has been done
  37. last = self.class.model.constantize.select('updated_at').order('updated_at DESC').first
  38. return false if !last
  39. return false if last.updated_at == @last_change
  40. # delete collection cache
  41. Sessions::CacheIn.delete( self.collection_key )
  42. # collection has changed
  43. true
  44. end
  45. def client_key
  46. "collections::load::#{ self.class.to_s }::#{ @user.id }::#{ @client_id }"
  47. end
  48. def push
  49. # check role based access
  50. if self.class.is_role
  51. access = nil
  52. self.class.is_role.each {|role|
  53. if @user.is_role(role)
  54. access = true
  55. end
  56. }
  57. return if !access
  58. end
  59. if self.class.is_not_role
  60. self.class.is_not_role.each {|role|
  61. return if @user.is_role(role)
  62. }
  63. end
  64. # check timeout
  65. timeout = Sessions::CacheIn.get( self.client_key )
  66. return if timeout
  67. # set new timeout
  68. Sessions::CacheIn.set( self.client_key, true, { :expires_in => 10.seconds } )
  69. return if !self.changed?
  70. data = self.load
  71. return if !data||data.empty?
  72. # collect assets
  73. assets = {}
  74. data.each {|item|
  75. assets = item.assets(assets)
  76. }
  77. if !@client
  78. return {
  79. :collection => {
  80. data.first.class.to_app_model => data,
  81. },
  82. :assets => assets,
  83. }
  84. end
  85. @client.log 'notify', "push assets for push_collection #{ data.first.class.to_s } for user #{ @user.id }"
  86. @client.send({
  87. :data => assets,
  88. :event => [ 'loadAssets' ],
  89. })
  90. @client.log 'notify', "push push_collection #{ data.first.class.to_s } for user #{ @user.id }"
  91. @client.send({
  92. :event => 'resetCollection',
  93. :data => {
  94. data.first.class.to_app_model => data,
  95. },
  96. })
  97. end
  98. def self.model_set(model)
  99. @model = model
  100. end
  101. def self.is_role_set(role)
  102. if !@is_role
  103. @is_role = []
  104. end
  105. @is_role.push role
  106. end
  107. def self.is_not_role_set(role)
  108. if !@is_not_role
  109. @is_not_role = []
  110. end
  111. @is_not_role.push role
  112. end
  113. end