repository-manager 0.0.22 → 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e37f122c9ac68a5696514ba45aa58590d70f8d2d
4
- data.tar.gz: ff2275d01570d20ea9ddcca099890a7a41d2bee0
3
+ metadata.gz: e54eb1966aef674b05d284cd6998ef0b414aa887
4
+ data.tar.gz: e6e295ed9fa6b70719c7ef318b7fc0944d253d00
5
5
  SHA512:
6
- metadata.gz: cf1c68509fb5f38b59e6493b51b903e0a556dc8e27bc079f62080801f7c10eebbdf4e7268ed8ece90ddd4182480578d50c04140ecb62a4300cf8ac4af2c11432
7
- data.tar.gz: 736abdc384ce33cb4484c249d5952753a6a7d1c2ca0477b2a3e774c2813bb9f3109d2eb862f40943ca72e0cf995d9860b3dde551da6177c1b2137b50ece164b4
6
+ metadata.gz: 6a823adf63f6394fda1d473b32ecd8c7f49133b454c30cdc78d8096d94bfb2833b174b73ca4986c086cd7e49e2571363cba0e66aa3c9dbc580b2aabcd7742228
7
+ data.tar.gz: 63474ee1c92accbf200c279391daaa3b6f537edd200dee80c2a978da08cb6690155478bf185dac871ce84a115f5be0463729a8f1073063df5719942c06a31de5
data/README.md CHANGED
@@ -408,8 +408,7 @@ the_folder.delete_zip
408
408
  ## TODO
409
409
 
410
410
  - Test the rename folder method
411
- - Donner un nom UNIQUE de dossier par default
412
- - Verifier que le nom du dossier n'existe pas dans la racine du repo
411
+ - Test if the file already exist before creating or moving it
413
412
  - Do the rename file method
414
413
  - Write the documentation for the rename method
415
414
  - Write the methods : copy, share_link.
@@ -6,10 +6,13 @@ class RepoFolder < RepoItem
6
6
  validates :name, presence: true
7
7
 
8
8
  # Add a repo_item in the folder.
9
- def add!(repo_item)
9
+ # second param destroy the repo_item if it can move it.
10
+ def add!(repo_item, destroy_if_fail = false)
10
11
  # We check if this name already exist
11
12
  #if repo_item.children.exists?(:name => repo_item.name)
12
- if RepoItem.where(name: repo_item.name).where(id: child_ids).first
13
+ if name_exist_in_children?(repo_item.name)
14
+ # we delete the repo if asked
15
+ repo_item.destroy if destroy_if_fail
13
16
  raise RepositoryManager::RepositoryManagerException.new("add failed. The repo_item '#{repo_item.name}' already exist in the folder '#{name}'")
14
17
  else
15
18
  repo_item.update_attribute :parent, self
@@ -26,13 +29,12 @@ class RepoFolder < RepoItem
26
29
 
27
30
  # Rename the item
28
31
  def rename!(new_name)
29
- # We take all siblings without itself
30
- sibling_ids_without_itself = self.sibling_ids.delete(self.id)
31
- # We check if another item has the same name
32
- if RepoItem.where(name: self.name).where(id: sibling_ids_without_itself).first
32
+ if name_exist_in_siblings?(new_name)
33
33
  raise RepositoryManager::RepositoryManagerException.new("rename failed. The repo_item '#{new_name}' already exist.'")
34
34
  else
35
35
  self.name = new_name
36
+ # TODO see if I have to save or not
37
+ save!
36
38
  end
37
39
  end
38
40
 
@@ -104,6 +106,21 @@ class RepoFolder < RepoItem
104
106
  FileUtils.rm_rf(path)
105
107
  end
106
108
 
109
+ # Returns true or false if the name exist in this folder
110
+ def name_exist_in_children?(name)
111
+ #RepoItem.where(name: name).where(id: child_ids).first ? true : false
112
+ RepoItem.where('name = ? OR file = ?', name, name).where(id: child_ids).first ? true : false
113
+ end
114
+
115
+ # Returns true or false if the name exist in siblings
116
+ def name_exist_in_siblings?(name)
117
+ # We take all siblings without itself
118
+ sibling_ids_without_itself = self.sibling_ids.delete(self.id)
119
+ # We check if another item has the same name
120
+ #RepoItem.where(name: name).where(id: sibling_ids_without_itself).first ? true : false
121
+ RepoItem.where('name = ? OR file = ?', name, name).where(id: sibling_ids_without_itself).first ? true : false
122
+ end
123
+
107
124
  private
108
125
 
109
126
  # Returns the default path of the zip file
@@ -1,13 +1,13 @@
1
1
  class RepoItem < ActiveRecord::Base
2
2
  attr_accessible :type if RepositoryManager.protected_attributes?
3
3
 
4
-
5
4
  has_ancestry
6
5
 
7
6
  # Associate with the User Class
8
7
  belongs_to :owner, :polymorphic => true
9
8
  has_many :sharings, :dependent => :destroy
10
9
  #has_many :members, through: :sharings
10
+ belongs_to :user, class_name: RepositoryManager.user_model if RepositoryManager.user_model
11
11
 
12
12
  if Rails::VERSION::MAJOR == 4
13
13
  scope :files, -> { where type: 'RepoFile' }
@@ -1,10 +1,11 @@
1
1
  class Sharing < ActiveRecord::Base
2
2
  attr_accessible :can_read, :can_create, :can_update, :can_delete, :can_share if RepositoryManager.protected_attributes?
3
3
 
4
-
5
4
  has_many :sharings_members, :dependent => :destroy
6
5
  belongs_to :owner, :polymorphic => true
7
6
  belongs_to :repo_item
7
+ belongs_to :user, class_name: RepositoryManager.user_model if RepositoryManager.user_model
8
+
8
9
 
9
10
  #scope :recipient, lambda { |recipient|
10
11
  # joins(:receipts).where('receipts.receiver_id' => recipient.id,'receipts.receiver_type' => recipient.class.base_class.to_s)
@@ -3,8 +3,9 @@ class CreateRepositoryManager < ActiveRecord::Migration
3
3
  def change
4
4
 
5
5
  create_table :sharings do |t|
6
- t.references :owner, polymorphic: true
7
- t.references :repo_item
6
+ t.references :owner, polymorphic: true, index: true
7
+ t.references :user, index: true
8
+ t.references :repo_item, index: true
8
9
  t.boolean :can_create, :default => false
9
10
  t.boolean :can_read, :default => false
10
11
  t.boolean :can_update, :default => false
@@ -20,9 +21,10 @@ class CreateRepositoryManager < ActiveRecord::Migration
20
21
  end
21
22
 
22
23
  create_table :repo_items do |t|
23
- t.references :owner, polymorphic: true
24
+ t.references :owner, polymorphic: true, index: true
25
+ t.references :user, index: true
24
26
  t.string :ancestry
25
- t.integer :ancestry_depth, :default => 0
27
+ #t.integer :ancestry_depth, :default => 0
26
28
  t.string :name
27
29
  t.float :file_size
28
30
  t.string :content_type
@@ -6,6 +6,10 @@ RepositoryManager.setup do |config|
6
6
  # Default sharing permissions that an object has when he is added in a sharing.
7
7
  config.default_sharing_permissions = { can_add: false, can_remove: false }
8
8
 
9
+ # Note here the user model. Default : 'User'
10
+ # Def false if you don't want to register the user
11
+ #config.user_model = 'User'
12
+
9
13
  # Default path for generating the zip file when a user want to download a folder
10
14
  # Default is : "download/#{member.class.to_s.underscore}/#{member.id}/#{self.class.to_s.underscore}/#{self.id}/"
11
15
  #config.default_zip_path = true
@@ -14,6 +14,10 @@ module RepositoryManager
14
14
  mattr_accessor :accept_nested_sharing
15
15
  @@accept_nested_share = false
16
16
 
17
+ mattr_accessor :user_model
18
+ #@@user_model = 'User'
19
+ @@user_model = false
20
+
17
21
  class << self
18
22
  def setup
19
23
  yield self
@@ -63,6 +63,7 @@ module RepositoryManager
63
63
 
64
64
  sharing = Sharing.new(repo_item_permissions)
65
65
  sharing.owner = self
66
+ sharing.user = current_user if RepositoryManager.user_model
66
67
 
67
68
  sharing.add_members(members, sharing_permissions)
68
69
 
@@ -99,13 +100,19 @@ module RepositoryManager
99
100
  folder.name = name
100
101
  end
101
102
  folder.owner = self
103
+ folder.user = current_user if RepositoryManager.user_model
102
104
 
103
- # Soit il a une source_folder, donc on l'ajoute et on le sauve et ça fonctionne pas => ERREUR
104
- # Soit il n'a pas de source_folder et le save ne va pas => ERREUR
105
- unless (folder.save && source_folder && source_folder.add(folder)) || (!source_folder && folder.save)
106
- folder.destroy
105
+ # If we are in root path we check if we can add this folder name
106
+ if !source_folder && repo_item_name_exist_in_root?(name)
107
+ raise RepositoryManager::RepositoryManagerException.new("create folder failed. The repo_item '#{name}' already exist in the root folder.")
108
+ end
109
+
110
+ unless folder.save
107
111
  raise RepositoryManager::RepositoryManagerException.new("create_folder failed. Can\'t save the folder '#{name}'.")
108
112
  end
113
+
114
+ # It raise an error if name already exist and destroy the folder
115
+ source_folder.add!(folder, true) if source_folder
109
116
  else
110
117
  raise RepositoryManager::AuthorisationException.new("create_folder failed. You don't have the permission to create a folder in '#{source_folder.name}'")
111
118
  end
@@ -146,31 +153,29 @@ module RepositoryManager
146
153
  if can_create?(source_folder)
147
154
  if file.class.name == 'RepoFile'
148
155
  repo_file = file
149
- repo_file.owner = self
150
- unless repo_file.save
151
- raise RepositoryManager::RepositoryManagerException.new("create_file failed. The file '#{name}' can't be save")
152
- end
153
156
  else
154
157
  repo_file = RepoFile.new
155
158
  repo_file.file = file
156
- repo_file.owner = self
157
- unless repo_file.save
158
- raise RepositoryManager::RepositoryManagerException.new("create_file failed. The file '#{name}' can't be save")
159
- end
160
159
  end
161
160
 
162
- # We have to look if it is ok to add the file here
163
- if source_folder == nil || source_folder.add(file)
164
- return repo_file
165
- else
166
- # The add didn't works, we delete the file
167
- file.destroy
168
- raise RepositoryManager::RepositoryManagerException.new("create_file failed. The file '#{name}' already exist in folder '#{source_folder.name}'")
161
+ repo_file.owner = self
162
+ repo_file.user = current_user if RepositoryManager.user_model
163
+
164
+ # If we are in root path we check if we can add this file name
165
+ if !source_folder && repo_item_name_exist_in_root?(repo_file.file.identifier)
166
+ raise RepositoryManager::RepositoryManagerException.new("create file failed. The repo_item '#{repo_file.file.identifier}' already exist in the root folder.")
169
167
  end
168
+
169
+ unless repo_file.save
170
+ raise RepositoryManager::RepositoryManagerException.new("create_file failed. The file can't be save")
171
+ end
172
+
173
+ # It raise an error if name already exist and destroy the file
174
+ source_folder.add!(repo_file, true) if source_folder
170
175
  else
171
- #raise "create_file failed. You don't have the permission to create a file in the folder '#{source_folder.name}'"
172
- raise RepositoryManager::AuthorisationException.new("create_file failed. The file '#{name}' already exist in folder '#{source_folder.name}'")
176
+ raise RepositoryManager::AuthorisationException.new("create_file failed. You don't have the permission to create a file")
173
177
  end
178
+ repo_file
174
179
  end
175
180
 
176
181
  def create_file(file, source_folder = nil)
@@ -378,6 +383,17 @@ module RepositoryManager
378
383
  "#{prefix}#{self.class.to_s.underscore}/#{self.id}/"
379
384
  end
380
385
 
386
+ # Returns true of false if the name exist in the root path of this instance
387
+ def repo_item_name_exist_in_root?(name)
388
+ # add : or file : name
389
+ #RepoItem.where(name: name).where(owner: self).where(ancestry: nil).first ? true : false
390
+ #puts RepoItem.where('name = ? OR file = ?', name, name).where(owner: self).where(ancestry: nil).first.inspect
391
+ #puts self.inspect
392
+
393
+ RepoItem.where('name = ? OR file = ?', name, name).where(owner: self).where(ancestry: nil).first ? true : false
394
+
395
+ end
396
+
381
397
  private
382
398
 
383
399
  # Return if you can do or not this action in the sharing
@@ -477,18 +493,19 @@ module RepositoryManager
477
493
  # We check if another item has the same name
478
494
 
479
495
  if source_folder
496
+ #TODO Optimiser, récupérer tout les instances contenants le nom, puis faire la boucle (pas boucle de requete)
480
497
  # We check if another item has the same name
481
- until !RepoItem.where(name: name).where(id: source_folder.child_ids).first do
498
+ until !source_folder.name_exist_in_children?(name) do
482
499
  if i == ''
483
500
  i = 1
484
501
  end
485
502
  i += 1
486
503
  name = "#{I18n.t 'repository_manager.models.repo_folder.name'}#{i}"
487
504
  end
488
-
489
505
  else
506
+ #TODO Optimiser, récupérer tout les instances contenants le nom, puis faire la boucle (pas boucle de requete)
490
507
  # Si il n'a pas de parent, racine
491
- until !RepoItem.where(name: name).where(owner: self).where(ancestry: nil).first do
508
+ until !repo_item_name_exist_in_root?(name) do
492
509
  if i == ''
493
510
  i = 1
494
511
  end
@@ -1,3 +1,3 @@
1
1
  module RepositoryManager
2
- VERSION = '0.0.22'
2
+ VERSION = '0.0.24'
3
3
  end
@@ -7,9 +7,6 @@ describe 'HasRepository' do
7
7
  @user2 = FactoryGirl.create(:user)
8
8
  @user3 = FactoryGirl.create(:user)
9
9
  @group1 = FactoryGirl.create(:group)
10
- #@group2 = FactoryGirl.create(:group)
11
- #@group3 = FactoryGirl.create(:group)
12
- #@fileAlone = FactoryGirl.create(:app_file)
13
10
  end
14
11
 
15
12
  it "should be associate with shares" do
@@ -174,4 +174,29 @@ describe 'RepoItem' do
174
174
  expect(folder4.name).to eq('translation missing: en.repository_manager.models.repo_folder.name2')
175
175
 
176
176
  end
177
+
178
+ it 'can\'t create a folder with the same name at root' do
179
+ @user1.create_folder('test')
180
+ folder2 = @user1.create_folder('test')
181
+
182
+ expect(folder2).to eq(false)
183
+ end
184
+
185
+ it "can't create too file with the same name at root" do
186
+ file = @user2.create_file(File.open("#{Rails.root}/../fixture/textfile.txt"))
187
+ expect(file.name).to eq('textfile.txt')
188
+ file2 = @user2.create_file(File.open("#{Rails.root}/../fixture/textfile.txt"))
189
+ expect(file2).to eq(false)
190
+
191
+ #@user1.create_file!(File.open("#{Rails.root}/../fixture/textfile.txt"))
192
+ end
193
+
194
+ it "can't create too file with the same name in folder" do
195
+ file = @user1.create_file(File.open("#{Rails.root}/../fixture/textfile.txt"), @user1_folder)
196
+ expect(file.name).to eq('textfile.txt')
197
+ file2 = @user1.create_file(File.open("#{Rails.root}/../fixture/textfile.txt"),@user1_folder)
198
+ expect(file2).to eq(false)
199
+
200
+ #@user1.create_file!(File.open("#{Rails.root}/../fixture/textfile.txt"), @user1_folder)
201
+ end
177
202
  end
data/spec/spec_helper.rb CHANGED
@@ -18,6 +18,10 @@ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
18
18
  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
19
19
  end
20
20
 
21
+ RSpec.configure do |config|
22
+ config.include SpecTestHelper#, :type => :controller
23
+ end
24
+
21
25
 
22
26
  #
23
27
  #class ActiveSupport::TestCase
@@ -0,0 +1,5 @@
1
+ module SpecTestHelper
2
+ def current_user
3
+ User.first
4
+ end
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: repository-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.22
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yves Baumann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-31 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -192,6 +192,7 @@ files:
192
192
  - spec/models/share_spec.rb
193
193
  - spec/repository_manager_spec.rb
194
194
  - spec/spec_helper.rb
195
+ - spec/support/spec_test_helper.rb
195
196
  homepage: https://github.com/Texicitys/repository-manager
196
197
  licenses:
197
198
  - MIT
@@ -267,3 +268,4 @@ test_files:
267
268
  - spec/models/share_spec.rb
268
269
  - spec/repository_manager_spec.rb
269
270
  - spec/spec_helper.rb
271
+ - spec/support/spec_test_helper.rb