123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- class Sessions::Backend::Collections::Base
- class << self; attr_accessor :model, :roles, :not_roles end
- def initialize( user, client, client_id, ttl )
- @user = user
- @client = client
- @client_id = client_id
- @ttl = ttl
- @last_change = nil
- end
- def load
- # get whole collection
- self.class.model.constantize.all
- end
- def client_key
- "collections::load::#{self.class}::#{@user.id}::#{@client_id}"
- end
- def push
- # check role based access
- if self.class.roles
- access = false
- self.class.roles.each {|role|
- next if !@user.role?(role)
- access = true
- break
- }
- return if !access
- end
- if self.class.not_roles
- access = false
- self.class.not_roles.each {|role|
- next if @user.role?(role)
- access = true
- break
- }
- return if !access
- end
- # check timeout
- timeout = Sessions::CacheIn.get( client_key )
- return if timeout
- # set new timeout
- Sessions::CacheIn.set( client_key, true, { expires_in: @ttl.seconds } )
- # check if update has been done
- last_change = self.class.model.constantize.latest_change
- return if last_change == @last_change
- @last_change = last_change
- # load current data
- items = load
- return if !items || items.empty?
- # get relations of data
- all = []
- items.each {|item|
- all.push item.attributes_with_associations
- }
- # collect assets
- assets = {}
- items.each {|item|
- assets = item.assets(assets)
- }
- if !@client
- return {
- collection: {
- items.first.class.to_app_model => all,
- },
- assets: assets,
- }
- end
- @client.log "push assets for push_collection #{items.first.class} for user #{@user.id}"
- @client.send(
- data: assets,
- event: [ 'loadAssets' ],
- )
- @client.log "push push_collection #{items.first.class} for user #{@user.id}"
- @client.send(
- event: 'resetCollection',
- data: {
- items.first.class.to_app_model => all,
- },
- )
- end
- def self.model_set(model)
- @model = model
- end
- def self.add_if_role(role)
- if !@roles
- @roles = []
- end
- @roles.push role
- end
- def self.add_if_not_role(role)
- if !@not_roles
- @not_roles = []
- end
- @not_roles.push role
- end
- end
|