Class: Folder

Inherits:
ApplicationRecord show all
Extended by:
ActsAsTree::TreeWalker
Defined in:
app/models/folder.rb

Overview

The folder model

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statusObject

Returns the value of attribute status.



22
23
24
# File 'app/models/folder.rb', line 22

def status
  @status
end

Instance Method Details

#calc_metaObject

return a list of all metadatas for a given folder id
to be inserted in the folder ‘lists’ field
list will contain a table with all share ids
list will contain a table with all satisfaction ids
list will contain the id of the user being granted the folder as swarmed
to decode when reading the folder record : ActiveSupport::JSON.decode(folder.lists)



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/models/folder.rb', line 177

def calc_meta
  meta={}
  # all shares ids associated to the folder
  shares=SharedFolder.where(folder_id: self.id).select("id")
  tab=[]
  shares.each do |s|
    tab << s.id
  end
  meta["shares"]=tab
  # all satisfactions ids associated to the folder
  satisfactions=Satisfaction.where(folder_id: self.id).select("id")
  tab=[]
  satisfactions.each do |s|
    tab << s.id
  end
  meta["satis"]=tab
  #swarming legacy process
  if swid=self.legacy
    meta["swarmed_to"]=swid
  end
  ActiveSupport::JSON.encode(meta)
end

#children_give_to(i) ⇒ Object

Fix user_id to i on all folder’s children (assets, subfolders, subassets, shares, subshares)



115
116
117
118
119
120
121
122
# File 'app/models/folder.rb', line 115

def children_give_to(i)
  if self.has_sub_asset_or_share?
    self.get_subs_assets_shares.each do |c|
      c.user_id = i
      c.save
    end
  end
end

#email_customer(current_user, customer_email, share = nil, customer = nil) ⇒ Object

inform the customer by email if files/surveys are waiting for him
customer can be registered in colibri or not



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/models/folder.rb', line 303

def email_customer(current_user,customer_email,share=nil,customer=nil)
  results={}
  shared_files=self.assets
  nb_files=shared_files.length
  puts("**************#{nb_files} file(s) in the folder #{self.id}");
  unless (nb_files>0 || self.is_polled?)
    t1 = I18n.t('sb.cannot_send_mel')
    t2 = I18n.t('sb.empty_folder')
    t3 = I18n.t('sb.unpolled_folder')
    t4 = I18n.t('sb.upload_or_poll_folder')
    results["message"] = "#{t1}\n#{t2}\n#{t3}\n#{t4}"
    results["success"] = false
  else
    if InformUserJob.perform_now(current_user,customer_email,self,shared_files,customer,share)
      results["message"] = "#{I18n.t('sb.mail_sent_to')} #{customer_email}"
      results["success"] = true
    else
      results["message"] = "#{I18n.t('sb.could_not_send_mail_sent_to')} #{customer_email}"
      results["success"] = false
    end
  end
  results
end

#get_all_sub_foldersObject

returns all subfolders related to the folder, directly or indirectly



103
104
105
106
107
108
109
110
111
# File 'app/models/folder.rb', line 103

def get_all_sub_folders
  subfolders=self.children
  self.children.each do |children_folder|
    if children_folder.children
      subfolders += children_folder.get_all_sub_folders
    end
  end
  return subfolders
end

#get_metaObject

decoding the meta for an interpretation as a json object



237
238
239
240
241
242
243
244
245
# File 'app/models/folder.rb', line 237

def get_meta
  meta={}
  meta["shares"]=[]
  meta["satis"]=[]
  if (self.lists)
    meta=ActiveSupport::JSON.decode(self.lists)
  end
  meta
end

#get_subs_assets_shares(i = 0) ⇒ Object

Return all subfolders, assets and shares related to the folder, directly or indirectly
works recursively
analyzing the ‘nearest’ children first - operate layer by layer, from the nearest layer to the furthest layer



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/folder.rb', line 81

def get_subs_assets_shares(i=0)
  puts("**************************working on folder #{self.name} level #{i}")
  folders = Folder.where(parent_id: self.id)
  assets = Asset.where(folder_id: self.id)
  shared_folders = SharedFolder.where(folder_id: self.id)
  childrens = assets + folders + shared_folders
  whitespace=" * "*i
  puts("#{whitespace}BEGIN level #{i}__________________________________________________RECURSIVE SEARCH")
  folders.each do |c|
    if c.has_sub_asset_or_share?
      i+=1
      childrens += c.get_subs_assets_shares(i)
      i-=1
    end
    puts ("end of search for subfolder "+c.name.to_s+ " number "+c.id.to_s)
  end
  puts("#{whitespace}END level #{i}____________________________________________________RECURSIVE SEARCH")
  return childrens
end

#has_assets?Boolean

Return true if the folder has got assets

Returns:

  • (Boolean)


54
55
56
# File 'app/models/folder.rb', line 54

def has_assets? 
  return true if Asset.find_by_folder_id(self.id)
end

#has_satisfaction_answer?Boolean

Return true if at least a satisfaction answer has been recorded on the folder

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'app/models/folder.rb', line 44

def has_satisfaction_answer?
  if Satisfaction.find_by_folder_id(self.id)
    return true 
  else 
    return false
  end
end

#has_sub_asset_or_share?Boolean

return true if we can find at least one subfolder, asset or share directly related to the folder

Returns:

  • (Boolean)


60
61
62
63
64
65
66
# File 'app/models/folder.rb', line 60

def has_sub_asset_or_share? 
  if Asset.find_by_folder_id(self.id) || Folder.find_by_parent_id(self.id) || SharedFolder.find_by_folder_id(self.id)
    return true
  else
    return false
  end
end

#has_sub_swarmed?Boolean

is there a subfolder swarmed by another user ?

Returns:

  • (Boolean)


146
147
148
149
150
151
# File 'app/models/folder.rb', line 146

def has_sub_swarmed?
  self.get_all_sub_folders.each do |s|
    return true if s.user_id != self.user_id
  end
  return false
end

#has_sub_swarmed_to_user?(user) ⇒ Boolean

is there a subfolder swarmed by user given in argument ?

Returns:

  • (Boolean)


162
163
164
165
166
167
168
# File 'app/models/folder.rb', line 162

def has_sub_swarmed_to_user?(user)
  return false if user.has_ownership?(self)
  self.get_all_sub_folders.each do |s|
    return true if s.user_id == user.id
  end
  return false  
end

#initialize_metaObject

return an empty metadata object (for folder creation)



225
226
227
228
229
230
231
232
233
# File 'app/models/folder.rb', line 225

def initialize_meta
  meta={}
  meta["shares"]=[]
  meta["satis"]=[]
  if swid=self.legacy
    meta["swarmed_to"]=swid
  end
  ActiveSupport::JSON.encode(meta)
end

#is_polled?Boolean

Return true if the folder is polled

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
# File 'app/models/folder.rb', line 32

def is_polled?
  #return true if Poll.where(id: self.poll_id).length != 0
  result = self.poll_id
  unless result
    result=false
  end
  puts("folder model - test if folder is polled - result is #{result}")
  return result
end

#is_root?Boolean

return all assets belonging to a folder (directly - ie its own assets, not the ones in its subfolders)
inutile non - folder.assets donne le même résultat def get_assets

return Asset.where(folder_id: self.id)

end

Returns:

  • (Boolean)


73
74
75
# File 'app/models/folder.rb', line 73

def is_root?
  self.parent_id.nil?
end

#is_shared_to_user?(user) ⇒ Boolean

is the folder explicitely shared to the user ?
by a share in the shared_folders table

Returns:

  • (Boolean)


156
157
158
# File 'app/models/folder.rb', line 156

def is_shared_to_user?(user)
  return true if SharedFolder.find_by_share_user_id_and_folder_id(user.id, self.id)
end

#is_swarmed?Boolean

is the folder swarmed ?
has the folder been created or dropped in a directory belonging to another private user ?

Returns:

  • (Boolean)


127
128
129
130
131
132
# File 'app/models/folder.rb', line 127

def is_swarmed?
  self.ancestors.each do |a|
    return true if a.user_id != self.user_id
  end
  return false
end

#is_swarmed_to_user?(user) ⇒ Boolean

is the folder swarmed to the specified user ?

Returns:

  • (Boolean)


136
137
138
139
140
141
142
# File 'app/models/folder.rb', line 136

def is_swarmed_to_user?(user)
  return false if user.has_ownership?(self)
  self.ancestors.each do |a|
    return true if a.user_id == user.id
  end
  return false
end

#legacyObject

return the id of the user being granted the folder as swarmed
for this legacy process, things MUST be done in a specific order
checking if parent.user_id equals to owner.id before analysing the parent meta ‘swarmed_to’ would NOT be suitable
example : considering a folder1 belonging to user1, shared to user2 and user3
user2 creates folder2 inside folder 1 and user3 creates folder3 inside folder2
if we check the parent first, the legacy process could conclude that folder3 is swarmed to user2
this is not what is expected from the legacy process which has to conclude that folder3 is swarmed to user 1



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/folder.rb', line 208

def legacy
  owner=User.find_by_id(self.user_id)
  if self.parent_id
    parent=Folder.find_by_id(self.parent_id)
    if swid=parent.get_meta["swarmed_to"]
      puts("{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ swarmed by legacy #{swid}")
    else
      unless parent.user_id == owner.id
        swid=parent.user_id
      end
    end
  end
  swid
end

#move(destination_folder, destination_user = nil) ⇒ Object

move a folder and all related childs (subs,assets,shares,satisfactions) into another one and/or transfer to another user
quite costly but it is the ony way.…



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'app/models/folder.rb', line 330

def move(destination_folder, destination_user=nil)
  results={}
  childs={}
  unless destination_folder || destination_user
    results["success"]=false
    results["message"]="il faut fournir une donnée : un répertoire de destination, un utilisateur qui héritera du répertoire ou les 2"
  else
    if destination_folder
      case destination_folder
      when 'root'
        self.parent_id=nil
      else
        self.parent_id=destination_folder.id
      end
    end
    if destination_user
      self.user_id=destination_user.id
    end
    self.lists=self.calc_meta
    if self.save
      results["success"]=true
      results["message"]="sauvegarde du répertoire: OK\n"
      childs=self.get_subs_assets_shares
      childs.each do |c|
        if destination_user
          c.user_id = destination_user.id
        end
        #we check if the child is a folder
        if /lists/.match(c.attributes.keys.to_s)
          results["message"]="#{results["message"]} NOTA : l'enfant #{c.id} est un répertoire\n"
          c.lists=c.calc_meta
        end
        unless c.save
          results["success"]=false
          results["message"]="#{results["message"]} impossible de sauvegarder l'enfant #{c.id}\n"
        else
          results["message"]="#{results["message"]} sauvegarde de l'enfant #{c.id}: OK\n"
        end
      end
    else
      results["success"]=false
      results["message"]="impossible de sauvegarder le répertoire"
    end
  end
  results.merge!("folder": self.as_json)
  results.merge!("childs": childs.as_json)
  results
end

#process_share_emails(emails, current_user) ⇒ Object

execute all ‘folder sharing’ operations providing a text list of emails (separator ,)
update folder metadata if shares are successfully created only folder owner can add new shares
if TEAM var exists, could we authorize others users from the team who benefit from a share ?



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/models/folder.rb', line 252

def process_share_emails(emails,current_user)
    result=""
    saved_shares=false
    unless emails && current_user.id == self.user_id
      result="#{I18n.t('sb.stop')}, #{I18n.t('sb.maybe')} :\n - #{I18n.t('sb.no_mel_given')}\n - #{I18n.t('sb.folder_not_for_yu')}"
    else
      email_addresses = emails.split(",")
      email_addresses.each do |email_address|
        email_address=email_address.delete(' ')
        email_to_search=email_address
        if Rails.configuration.sharebox["downcase_email_search_autocomplete"]
          email_to_search=email_to_search.downcase
          share=current_user.shared_folders.where("LOWER(share_email) = ? and folder_id = ?", email_to_search, self.id)[0]
        else
          share=current_user.shared_folders.find_by_share_email_and_folder_id(email_to_search,self.id)
        end
        if email_to_search == current_user.email
          result = "#{result} #{I18n.t('sb.no_share_for_folder_owner')}\n"
        else
          # is the email_address already in the folder's shares ?
          if share
            result = "#{result} #{I18n.t('sb.already_shared_to')} #{email_address}\n"
          else
            shared_folder = current_user.shared_folders.new
            shared_folder.folder_id=self.id
            shared_folder.share_email = email_address
            # We search if the email exist in the user table
            # if not, we'll have to update the share_user_id field after registration
            share_user = User.find_by_email(email_to_search)
            shared_folder.share_user_id = share_user.id if share_user
            if shared_folder.save
              a = "#{I18n.t('sb.shared_to')} #{email_address}"
              result = "#{result} #{a}\n"
              saved_shares = true
            else
              flash[:notice] = "#{result} #{I18n.t('sb.unable_share_for')} #{email_address}\n"
            end
          end
        end
      end
      self.lists=self.calc_meta
      unless self.save
        result = "#{result} #{I18n.t('sb.folder_metas')} : #{I18n.t('sb.not_updated')}\n"
      end
    end
    {"message": result,"saved_shares": saved_shares}
end

#shared?Boolean

Return true is the folder is shared

Returns:

  • (Boolean)


26
27
28
# File 'app/models/folder.rb', line 26

def shared?
  !self.shared_folders.empty?
end