erp_tech_svcs 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/README.md +62 -0
  2. data/app/controllers/erp_tech_svcs/session_controller.rb +12 -7
  3. data/app/controllers/erp_tech_svcs/user_controller.rb +35 -14
  4. data/app/mailers/user_mailer.rb +8 -2
  5. data/app/models/attribute_type.rb +30 -0
  6. data/app/models/attribute_value.rb +41 -0
  7. data/app/models/file_asset.rb +58 -12
  8. data/app/models/secured_model.rb +4 -2
  9. data/app/models/user.rb +9 -7
  10. data/app/validators/password_strength_validator.rb +8 -0
  11. data/app/views/user_mailer/activation_needed_email.html.erb +1 -1
  12. data/config/initializers/erp_tech_svcs.rb +6 -1
  13. data/config/initializers/file_support.rb +9 -1
  14. data/config/initializers/sorcery.rb +3 -3
  15. data/config/routes.rb +2 -1
  16. data/db/data_migrations/20120109173616_create_download_capability_type.rb +13 -0
  17. data/db/migrate/20080805000010_base_tech_services.rb +12 -5
  18. data/db/migrate/20111117183144_create_has_attribute_tables.rb +38 -0
  19. data/db/migrate/{20111109161549_add_capabilites.rb → upgrade/20111109161549_add_capabilites.rb} +0 -0
  20. data/db/migrate/upgrade/20120329161641_add_file_asset_indexes.rb +22 -0
  21. data/lib/erp_tech_svcs/config.rb +20 -2
  22. data/lib/erp_tech_svcs/engine.rb +6 -0
  23. data/lib/erp_tech_svcs/extensions/active_record/has_capabilities.rb +18 -6
  24. data/lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb +16 -3
  25. data/lib/erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes.rb +99 -0
  26. data/lib/erp_tech_svcs/extensions/active_record/has_roles.rb +3 -0
  27. data/lib/erp_tech_svcs/extensions/sorcery/user_activation.rb +13 -0
  28. data/lib/erp_tech_svcs/extensions.rb +5 -1
  29. data/lib/erp_tech_svcs/file_support/base.rb +1 -3
  30. data/lib/erp_tech_svcs/file_support/file_system_manager.rb +21 -19
  31. data/lib/erp_tech_svcs/file_support/manager.rb +29 -9
  32. data/lib/erp_tech_svcs/file_support/paperclip_patch.rb +13 -9
  33. data/lib/erp_tech_svcs/file_support/railties/s3_resolver.rb +24 -4
  34. data/lib/erp_tech_svcs/file_support/s3_manager.rb +133 -86
  35. data/lib/erp_tech_svcs/file_support.rb +5 -3
  36. data/lib/erp_tech_svcs/sms_wrapper/clickatell.rb +25 -0
  37. data/lib/erp_tech_svcs/sms_wrapper.rb +1 -0
  38. data/lib/erp_tech_svcs/utils/compass_access_negotiator.rb +12 -6
  39. data/lib/erp_tech_svcs/utils/default_nested_set_methods.rb +14 -1
  40. data/lib/erp_tech_svcs/version.rb +7 -1
  41. data/lib/erp_tech_svcs.rb +4 -2
  42. data/lib/tasks/erp_tech_svcs_tasks.rake +1 -1
  43. data/spec/controllers/erp_tech_svcs/user_controller_spec.rb +0 -0
  44. data/spec/lib/file_support/file_system_manager_spec.rb +82 -0
  45. data/spec/lib/file_support/s3_manager_spec.rb +92 -0
  46. data/spec/models/file_asset_spec.rb +78 -0
  47. data/spec/models/secured_model_spec.rb +22 -0
  48. data/spec/spec_helper.rb +9 -7
  49. metadata +146 -133
  50. data/README.rdoc +0 -2
  51. data/lib/erp_tech_svcs/file_support/aws_s3_patch.rb +0 -3
  52. data/spec/dummy/db/schema.rb +0 -571
  53. data/spec/dummy/db/spec.sqlite3 +0 -0
  54. data/spec/dummy/log/spec.log +0 -2862
@@ -5,72 +5,76 @@ module ErpTechSvcs
5
5
  module FileSupport
6
6
  class S3Manager < Manager
7
7
  class << self
8
- cattr_accessor :node_tree
8
+ cattr_accessor :node_tree, :s3_connection
9
9
 
10
10
  def setup_connection
11
11
  @@configuration = YAML::load_file(File.join(Rails.root,'config','s3.yml'))[Rails.env]
12
-
13
- AWS::S3::Base.establish_connection!(
12
+
13
+ # S3 debug logging
14
+ # AWS.config(
15
+ # :logger => Rails.logger,
16
+ # :log_level => :info
17
+ # )
18
+
19
+ @@s3_connection = AWS::S3.new(
14
20
  :access_key_id => @@configuration['access_key_id'],
15
21
  :secret_access_key => @@configuration['secret_access_key']
16
22
  )
17
23
 
18
- @@s3_bucket = AWS::S3::Bucket.find(@@configuration['bucket'])
19
- @@node_tree = build_node_tree
24
+ @@s3_bucket = @@s3_connection.buckets[@@configuration['bucket'].to_sym]
25
+ build_node_tree(true)
20
26
  end
21
27
 
22
28
  def reload
23
- AWS::S3::Base.connected? ? (@@node_tree = build_node_tree(true)) : setup_connection
29
+ !@@s3_connection.nil? ? build_node_tree(true) : setup_connection
24
30
  end
25
31
 
26
- def build_node_tree(reload=false)
27
- tree_data = [{:text => @@s3_bucket.name, :leaf => false, :id => '', :children => []}]
28
- objects = reload ? @@s3_bucket.objects(:reload) : @@s3_bucket.objects()
29
-
30
-
31
- nesting_depth = objects.collect{|object| object.key.split('/').count}.max
32
- unless nesting_depth.nil?
33
- levels = []
34
- (1..nesting_depth).each do |i|
35
- current_items = []
36
- objects_this_depth = objects.collect{|object|
37
- text = object.key.split('/')[i - 1]
38
- path ='/' + (object.key.split('/')[0..(i-1)].join('/'))
39
- if object.key.split('/').count >= i && current_items.select{|item| item[:text] == text and item[:path] == path}.empty?
40
- item_hash = {:text => text, :path => path, :last_modified => object.last_modified}
41
- current_items << item_hash
42
- item_hash
43
- end
44
- }
45
- objects_this_depth.delete_if{|item| (item.nil? or item[:text].nil?)}
46
- levels << objects_this_depth
47
- end
32
+ def cache_key
33
+ 'node_tree'
34
+ end
48
35
 
49
- old_parents = []
50
- new_parents = [tree_data[0]]
51
- levels.each do |level|
52
- old_parents = new_parents
53
- new_parents = []
54
- level.each do |item|
55
- parent = old_parents.count == 1 ? old_parents.first : self.find_parent(item, old_parents)
56
- path = File.join(parent[:id], item[:text])
57
- child_hash = {:last_modified => item[:last_modified], :text => item[:text], :downloadPath => path, :leaf => !File.extname(item[:text]).blank?, :id => path, :children => []}
58
- new_parents << child_hash
59
- parent[:children] << child_hash
60
- end
61
- end
36
+ def cache_node_tree(node_tree)
37
+ Rails.cache.write(cache_key, node_tree, :expires_in => ErpTechSvcs::Config.s3_cache_expires_in_minutes.minutes)
38
+ node_tree
39
+ end
40
+
41
+ def add_children(parent_hash, tree)
42
+ tree.children.each do |child|
43
+ child_hash = {
44
+ :last_modified => '',
45
+ #:last_modified => (child.leaf? ? @@s3_bucket.objects[child.key].last_modified : ''),
46
+ :text => (child.leaf? ? File.basename(child.key) : File.basename(child.prefix)),
47
+ :downloadPath => (child.leaf? ? '/'+File.dirname(child.key) : "/#{child.parent.prefix}".sub(%r{/$},'')),
48
+ :leaf => child.leaf?,
49
+ :id => (child.leaf? ? '/'+child.key : "/#{child.prefix}".sub(%r{/$},'')),
50
+ :children => []
51
+ }
52
+ child_hash = add_children(child_hash, child) unless child.leaf?
53
+ parent_hash[:children] << child_hash
62
54
  end
63
55
 
64
- tree_data
56
+ parent_hash
57
+ end
58
+
59
+ def build_node_tree(reload=false)
60
+ node_tree = Rails.cache.read(cache_key)
61
+ if !reload and !node_tree.nil?
62
+ #Rails.logger.info "@@@@@@@@@@@@@@ USING CACHED node_tree: #{node_tree.inspect}"
63
+ return node_tree
64
+ end
65
+
66
+ tree_data = {:text => @@s3_bucket.name, :leaf => false, :id => '', :children => []}
67
+ tree_data = [add_children(tree_data, @@s3_bucket.as_tree)]
68
+ cache_node_tree(tree_data)
65
69
  end
66
70
  end
67
71
 
68
72
  def buckets
69
- AWS::S3::Service.buckets
73
+ @@s3_connection.buckets
70
74
  end
71
75
 
72
76
  def bucket=(name)
73
- @@s3_bucket = AWS::S3::Bucket.find(name)
77
+ @@s3_bucket = @@s3_connection.buckets[name.to_sym]
74
78
  end
75
79
 
76
80
  def bucket
@@ -81,29 +85,54 @@ module ErpTechSvcs
81
85
  ''
82
86
  end
83
87
 
88
+ def cache_key
89
+ ErpTechSvcs::FileSupport::S3Manager.cache_key
90
+ end
91
+
92
+ def clear_cache(path)
93
+ path = path.sub(%r{^/},'')
94
+ #Rails.logger.info "deleting cache with key: #{path}"
95
+ Rails.cache.delete(path) # delete template from cache
96
+ Rails.cache.delete(cache_key) # delete node tree from cache
97
+ end
98
+
84
99
  def update_file(path, content)
85
- AWS::S3::S3Object.store(path, content, bucket.name, :access => :public_read)
100
+ path = path.sub(%r{^/},'')
101
+ bucket.objects[path].write(content)
102
+ clear_cache(path)
86
103
  end
87
104
 
88
- def create_file(path, name, contents)
89
- AWS::S3::S3Object.store(File.join(path,name), contents, @@s3_bucket.name, :access => :public_read)
105
+ def create_file(path, name, content)
106
+ path = path.sub(%r{^/},'')
107
+ bucket.objects[File.join(path, name)].write(content)
108
+ clear_cache(path)
90
109
  end
91
110
 
92
111
  def create_folder(path, name)
93
- AWS::S3::S3Object.store(File.join(path,name) + "/", '', @@s3_bucket.name, :access => :public_read)
112
+ path = path.sub(%r{^/},'')
113
+ folder = File.join(path, name) + "/"
114
+ bucket.objects[folder].write('')
115
+ clear_cache(path)
94
116
  end
95
117
 
96
118
  def save_move(path, new_parent_path)
97
- result = nil
119
+ result = false
98
120
  unless self.exists? path
99
- message = 'File does not exists'
121
+ message = FILE_DOES_NOT_EXIST
100
122
  else
123
+ file = FileAsset.where(:name => ::File.basename(path)).where(:directory => ::File.dirname(path)).first
124
+ acl = (file.has_capabilities? ? :private : :public_read) unless file.nil?
125
+ options = (file.nil? ? {} : {:acl => acl})
101
126
  name = File.basename(path)
102
- if AWS::S3::S3Object.rename path, File.join(new_parent_path,name), @@s3_bucket.name, :copy_acl => true
103
- message = "#{name} was moved to #{new_parent_path} successfully"
127
+ path = path.sub(%r{^/},'')
128
+ new_path = File.join(new_parent_path,name).sub(%r{^/},'')
129
+ old_object = bucket.objects[path]
130
+ if new_object = old_object.move_to(new_path, options)
131
+ message = "#{name} was moved to #{new_path} successfully"
104
132
  result = true
133
+ clear_cache(path)
105
134
  else
106
- message = "#Error renaming {old_name}"
135
+ message = "Error moving file #{path}"
107
136
  end
108
137
  end
109
138
 
@@ -111,50 +140,67 @@ module ErpTechSvcs
111
140
  end
112
141
 
113
142
  def rename_file(path, name)
114
- result = nil
143
+ result = false
115
144
  old_name = File.basename(path)
116
145
  path_pieces = path.split('/')
117
146
  path_pieces.delete(path_pieces.last)
118
147
  path_pieces.push(name)
119
148
  new_path = path_pieces.join('/')
120
- if AWS::S3::S3Object.rename path, new_path, @@s3_bucket.name, :copy_acl => true
121
- message = "#{old_name} was renamed to #{name} successfully"
122
- result = true
123
- else
124
- message = "#Error renaming {old_name}"
149
+ begin
150
+ file = FileAsset.where(:name => ::File.basename(path)).where(:directory => ::File.dirname(path)).first
151
+ acl = (file.has_capabilities? ? :private : :public_read) unless file.nil?
152
+ options = (file.nil? ? {} : {:acl => acl})
153
+ path = path.sub(%r{^/},'')
154
+ new_path = new_path.sub(%r{^/},'')
155
+ # Rails.logger.info "renaming from #{path} to #{new_path}"
156
+ old_object = bucket.objects[path]
157
+ if new_object = old_object.move_to(new_path, options)
158
+ message = "#{old_name} was renamed to #{name} successfully"
159
+ result = true
160
+ clear_cache(path)
161
+ else
162
+ message = "Error renaming #{old_name}"
163
+ end
164
+ rescue AWS::S3::Errors::NoSuchKey=>ex
165
+ message = FILE_FOLDER_DOES_NOT_EXIST
125
166
  end
126
167
 
127
168
  return result, message
128
169
  end
129
170
 
171
+ def set_permissions(path, canned_acl=:public_read)
172
+ path = path.sub(%r{^/},'')
173
+ bucket.objects[path].acl = canned_acl
174
+ end
175
+
130
176
  def delete_file(path, options={})
131
- result = nil
177
+ path = path.sub(%r{^/},'')
178
+ result = false
132
179
  message = nil
133
180
 
134
- node = find_node(path)
135
-
136
- if node[:children].empty?
137
- is_directory = !node[:leaf]
138
- path << '/' unless node[:leaf]
139
- result = AWS::S3::S3Object.delete path, @@s3_bucket.name, :force => true
140
- message = "File was deleted successfully"
141
- result = true
142
- elsif options[:force]
143
- node[:children].each do |child|
144
- delete_file(child[:id], options)
181
+ begin
182
+ is_directory = !bucket.objects[path].exists?
183
+ if options[:force] or bucket.as_tree(:prefix => path).children.count == 0
184
+ bucket.objects.with_prefix(path).delete_all
185
+ message = "File was deleted successfully"
186
+ result = true
187
+ clear_cache(path)
188
+ else
189
+ message = FOLDER_IS_NOT_EMPTY
145
190
  end
146
- else
147
- message = "Folder is not empty"
148
- end unless node.nil?
191
+ rescue Exception => e
192
+ result = false
193
+ message = e
194
+ end
149
195
 
150
- return result, message, is_directory
196
+ return result, message, is_directory
151
197
  end
152
198
 
153
199
  def exists?(path)
154
200
  begin
155
- path = path[1..path.length] if path.first == '/'
156
- !AWS::S3::S3Object.find(path, @@s3_bucket.name).nil?
157
- rescue AWS::S3::NoSuchKey
201
+ path = path.sub(%r{^/},'')
202
+ !bucket.objects[path].nil?
203
+ rescue AWS::S3::Errors::NoSuchKey
158
204
  return false
159
205
  end
160
206
  end
@@ -163,20 +209,21 @@ module ErpTechSvcs
163
209
  contents = nil
164
210
  message = nil
165
211
 
166
- path = path[1..path.length]
212
+ path = path.sub(%r{^/},'')
167
213
  begin
168
- object = AWS::S3::S3Object.find path, @@s3_bucket.name
169
- rescue AWS::S3::NoSuchKey => error
170
- message = 'File does not exists'
214
+ object = bucket.objects[path]
215
+ rescue AWS::S3::Errors::NoSuchKey => error
216
+ message = FILE_DOES_NOT_EXIST
171
217
  end
172
218
 
173
- contents = object.value.to_s if message.nil?
219
+ contents = object.read if message.nil?
174
220
 
175
221
  return contents, message
176
222
  end
177
223
 
178
224
  def build_tree(starting_path, options={})
179
- ErpTechSvcs::FileSupport::S3Manager.reload
225
+ starting_path = "/" + starting_path unless starting_path.first == "/"
226
+ #ErpTechSvcs::FileSupport::S3Manager.reload
180
227
  node_tree = find_node(starting_path, options)
181
228
  node_tree.nil? ? [] : node_tree
182
229
  end
@@ -185,7 +232,7 @@ module ErpTechSvcs
185
232
  parent = if options[:file_asset_holder]
186
233
  super
187
234
  else
188
- parent = @@node_tree.first
235
+ parent = ErpTechSvcs::FileSupport::S3Manager.build_node_tree(false).first
189
236
  unless path.nil?
190
237
  path_pieces = path.split('/')
191
238
  path_pieces.each do |path_piece|
@@ -197,7 +244,7 @@ module ErpTechSvcs
197
244
  end
198
245
  end
199
246
  end
200
- parent = nil if parent[:id] != path
247
+ parent = nil unless parent[:id] == path
201
248
  end
202
249
 
203
250
  parent
@@ -1,10 +1,12 @@
1
1
  require 'erp_tech_svcs/file_support/base'
2
2
  require 'erp_tech_svcs/file_support/manager'
3
+ require 'erp_tech_svcs/file_support/paperclip_patch'
3
4
  require 'erp_tech_svcs/file_support/file_system_manager'
4
5
  require 'erp_tech_svcs/file_support/s3_manager'
5
- require 'erp_tech_svcs/file_support/aws_s3_patch'
6
- require 'erp_tech_svcs/file_support/paperclip_patch'
7
6
  require 'erp_tech_svcs/file_support/file_manipulator'
8
7
 
9
8
  #path resolvers
10
- require 'erp_tech_svcs/file_support/railties/s3_resolver'
9
+ require 'erp_tech_svcs/file_support/railties/s3_resolver'
10
+
11
+ #paperclip
12
+ require 'erp_tech_svcs/file_support/paperclip_patch'
@@ -0,0 +1,25 @@
1
+ module ErpTechSvcs
2
+ module SmsWrapper
3
+ class Clickatell
4
+ attr_accessor :api
5
+
6
+ def initialize()
7
+ configuration = YAML::load_file(File.join(Rails.root,'config','clickatell.yml'))[Rails.env]
8
+ @api = ::Clickatell::API.authenticate(configuration['api_id'].to_s, configuration['username'], configuration['password'])
9
+ end
10
+
11
+ def send_message(phone_number, message, options={})
12
+ phone_number = phone_number.insert(0,'1') if phone_number.length == 10
13
+
14
+ result = nil
15
+ begin
16
+ result = @api.send_message(phone_number, message, options)
17
+ rescue ::Clickatell::API::Error=>ex
18
+ Rails.logger.error("Clickatell Error:#{ex.message}")
19
+ end
20
+ result
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1 @@
1
+ require 'erp_tech_svcs/sms_wrapper/clickatell'
@@ -3,14 +3,20 @@ module ErpTechSvcs
3
3
  module CompassAccessNegotiator
4
4
 
5
5
  def has_capability?(model, capability_type, resource)
6
- model.user_has_capability?(capability_type.to_s, resource, self)
6
+ result = true
7
+ result = model.user_has_capability?(capability_type.to_s, resource, self) if model.has_capabilities?
8
+ result
7
9
  end
8
10
 
9
11
  def with_capability(model, capability_type, resource, &block)
10
- if model.user_has_capability?(capability_type.to_s, resource, self)
11
- yield
12
+ if model.has_capabilities?
13
+ if model.user_has_capability?(capability_type.to_s, resource, self)
14
+ yield
15
+ else
16
+ raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability
17
+ end
12
18
  else
13
- raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::UserDoesNotHaveCapability
19
+ yield
14
20
  end
15
21
  end
16
22
 
@@ -33,9 +39,9 @@ module ErpTechSvcs
33
39
  end
34
40
  end
35
41
 
36
- class CapabilityTypeDoesNotExist < StandardError
42
+ class CapabilityTypeDoesNotExist < StandardError
37
43
  def to_s
38
- "Capability type not exist."
44
+ "Capability type does not exist."
39
45
  end
40
46
  end
41
47
 
@@ -6,7 +6,7 @@ module ErpTechSvcs
6
6
  end
7
7
 
8
8
  def to_label
9
- "#{description}"
9
+ description
10
10
  end
11
11
 
12
12
  def leaf
@@ -18,6 +18,18 @@ module ErpTechSvcs
18
18
  end
19
19
  alias_method_chain :to_json, :leaf
20
20
 
21
+ def to_tree_hash(options={})
22
+ additional_values = options[:additional_values] || {}
23
+ options[:additional_values] = additional_values.merge({
24
+ :text => self.to_label,
25
+ :leaf => self.leaf,
26
+ :children => self.children.collect{|child| child.to_tree_hash(options)}
27
+ })
28
+ tree_hash = self.to_hash(options)
29
+ tree_hash[:iconCls] = options[:icon_cls] if options[:icon_cls]
30
+ tree_hash
31
+ end
32
+
21
33
  module ClassMethods
22
34
  def find_roots
23
35
  where("parent_id = nil")
@@ -27,6 +39,7 @@ module ErpTechSvcs
27
39
  parent_id.to_i == 0 ? self.roots : find(parent_id).children
28
40
  end
29
41
  end
42
+
30
43
  end
31
44
  end
32
45
  end
@@ -1,3 +1,9 @@
1
1
  module ErpTechSvcs
2
- VERSION = "3.0.0"
2
+ module VERSION #:nodoc:
3
+ MAJOR = 3
4
+ MINOR = 0
5
+ TINY = 1
6
+
7
+ STRING = [MAJOR, MINOR, TINY].compact.join('.')
8
+ end
3
9
  end
data/lib/erp_tech_svcs.rb CHANGED
@@ -1,17 +1,19 @@
1
- #compass libraries
1
+ #compass_ae libraries
2
2
  require 'erp_base_erp_svcs'
3
3
 
4
4
  require 'paperclip'
5
5
  require 'delayed_job'
6
6
  require 'sorcery'
7
7
  require 'pdfkit'
8
+ require "erp_tech_svcs/version"
8
9
  require "erp_tech_svcs/utils/compass_logger"
9
10
  require "erp_tech_svcs/utils/default_nested_set_methods"
10
11
  require "erp_tech_svcs/utils/compass_access_negotiator"
11
12
  require "erp_tech_svcs/extensions"
13
+ require 'aws'
12
14
  require 'erp_tech_svcs/file_support'
15
+ require 'erp_tech_svcs/sms_wrapper'
13
16
  require "erp_tech_svcs/config"
14
- #require "erp_tech_svcs/application_installer"
15
17
  require "erp_tech_svcs/engine"
16
18
  require 'erp_tech_svcs/sessions/delete_expired_sessions_job'
17
19
  require 'erp_tech_svcs/sessions/delete_expired_sessions_service'
@@ -23,7 +23,7 @@ namespace :erp_tech_svcs do
23
23
  #sync themes
24
24
  puts "Syncing Themes..."
25
25
  Theme.all.each do |theme|
26
- file_support.sync(File.join(file_support.root, "/"+theme.url), theme)
26
+ file_support.sync(File.join(file_support.root, theme.url), theme)
27
27
  end
28
28
  puts "Complete"
29
29
  end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe ErpTechSvcs::FileSupport::FileSystemManager do
4
+
5
+ before(:all) do
6
+ @file_support = ErpTechSvcs::FileSupport::Base.new
7
+ end
8
+
9
+ it "should have a root of Rails.root" do
10
+ @file_support.root.should eq Rails.root.to_s
11
+ end
12
+
13
+ it "should be able to build a hash tree from a path" do
14
+ path_tree_hash = @file_support.build_tree(File.join(Rails.root,'lib'))
15
+
16
+ path_tree_hash[:text].should eq 'lib'
17
+ path_tree_hash[:id].should eq File.join(Rails.root,'lib').to_s
18
+ path_tree_hash[:leaf].should eq false
19
+ path_tree_hash[:children].count.should eq 1
20
+
21
+ child_path_hash = path_tree_hash[:children].first
22
+ child_path_hash[:text].should eq 'assets'
23
+ child_path_hash[:id].should eq File.join(Rails.root,'lib', 'assets').to_s
24
+ end
25
+
26
+ it "should allow you to CRUD file / folder" do
27
+ @file_support.create_file(Rails.root.to_s, 'test_create_file.txt', 'test')
28
+
29
+ File.exists?(File.join(Rails.root.to_s, 'test_create_file.txt')).should eq true
30
+ contents, message = @file_support.get_contents(File.join(Rails.root,'test_create_file.txt'))
31
+ contents.should eq 'test'
32
+
33
+ @file_support.update_file(File.join(Rails.root,'test_create_file.txt'), 'test2')
34
+ contents, message = @file_support.get_contents(File.join(Rails.root,'test_create_file.txt'))
35
+ contents.should eq 'test2'
36
+
37
+ result, message, is_directory = @file_support.delete_file(File.join(Rails.root,'test_create_file.txt'))
38
+ result.should eq true
39
+ is_directory.should eq false
40
+ File.exists?(File.join(Rails.root.to_s, 'test_create_file.txt')).should eq false
41
+
42
+ @file_support.create_folder(Rails.root.to_s, 'test_folder')
43
+ File.exists?(File.join(Rails.root,'test_folder')).should eq true
44
+
45
+ result, message, is_directory = @file_support.delete_file(File.join(Rails.root,'test_folder'))
46
+ result.should eq true
47
+ is_directory.should eq true
48
+ File.exists?(File.join(Rails.root.to_s, 'test_folder')).should eq false
49
+
50
+ contents, message = @file_support.get_contents(File.join(Rails.root,'is_not_real.txt'))
51
+ message.should eq ErpTechSvcs::FileSupport::Manager::FILE_DOES_NOT_EXIST
52
+
53
+ result, message, is_directory = @file_support.delete_file(File.join(Rails.root,'not_real'))
54
+ message.should eq ErpTechSvcs::FileSupport::Manager::FILE_FOLDER_DOES_NOT_EXIST
55
+
56
+ result, message, is_directory = @file_support.delete_file(File.join(Rails.root,'db'))
57
+ message.should eq ErpTechSvcs::FileSupport::Manager::FOLDER_IS_NOT_EMPTY
58
+ end
59
+
60
+ it "should allow you to rename a file" do
61
+ @file_support.create_file(Rails.root.to_s, 'test_create_file.txt', 'test')
62
+ File.exists?(File.join(Rails.root.to_s, 'test_rename.txt')).should eq false
63
+ File.exists?(File.join(Rails.root.to_s, 'test_create_file.txt')).should eq true
64
+
65
+ result, message = @file_support.rename_file(File.join(Rails.root.to_s, 'test_create_file.txt'), 'test_rename.txt')
66
+ result.should eq true
67
+ File.exists?(File.join(Rails.root.to_s, 'test_create_file.txt')).should eq false
68
+ File.exists?(File.join(Rails.root.to_s, 'test_rename.txt')).should eq true
69
+
70
+ result, message = @file_support.rename_file(File.join(Rails.root.to_s, 'not_real.txt'), 'test_rename.txt')
71
+ result.should eq false
72
+ message.should eq ErpTechSvcs::FileSupport::Manager::FILE_DOES_NOT_EXIST
73
+ end
74
+
75
+ after(:all) do
76
+ FileUtils.rm_rf(File.join(Rails.root.to_s, 'test_create_file.txt')) if File.exists?(File.join(Rails.root.to_s, 'test_create_file.txt'))
77
+ FileUtils.rm_rf(File.join(Rails.root.to_s, 'test_rename.txt')) if File.exists?(File.join(Rails.root.to_s, 'test_rename.txt'))
78
+ FileUtils.rm_rf(File.join(Rails.root.to_s, 'test_folder')) if File.exists?(File.join(Rails.root.to_s, 'test_folder'))
79
+ end
80
+
81
+
82
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe ErpTechSvcs::FileSupport::S3Manager do
4
+
5
+ before(:all) do
6
+ ErpTechSvcs::FileSupport::S3Manager.setup_connection
7
+ @file_support = ErpTechSvcs::FileSupport::Base.new(:storage => :s3)
8
+ end
9
+
10
+ it "should have a root of nothing" do
11
+ @file_support.root.should eq ''
12
+ end
13
+
14
+ it "should allow you to CRUD file / folder" do
15
+ contents, message = @file_support.get_contents('not_real')
16
+ message.should eq ErpTechSvcs::FileSupport::Manager::FILE_DOES_NOT_EXIST
17
+
18
+ @file_support.create_file('', 'test_create_file.txt', 'test')
19
+ @file_support.exists?('test_create_file.txt').should eq true
20
+ contents, message = @file_support.get_contents('test_create_file.txt')
21
+ contents.should eq 'test'
22
+
23
+ @file_support.update_file('test_create_file.txt', 'test2')
24
+ contents, message = @file_support.get_contents('test_create_file.txt')
25
+ contents.should eq 'test2'
26
+
27
+ result, message, is_directory = @file_support.delete_file('test_create_file.txt')
28
+ result.should eq true
29
+ is_directory.should eq false
30
+ @file_support.exists?('test_create_file.txt').should eq false
31
+ end
32
+
33
+ it "should be able to build a hash tree from a path" do
34
+ @file_support.create_folder('', 'test_folder')
35
+ @file_support.create_file('test_folder', 'test_create_file.txt', 'test')
36
+
37
+ path_tree_hash = @file_support.build_tree('test_folder')
38
+
39
+ path_tree_hash[:text].should eq 'test_folder'
40
+ path_tree_hash[:id].should eq '/test_folder'
41
+ path_tree_hash[:leaf].should eq false
42
+ path_tree_hash[:children].count.should eq 1
43
+
44
+ child_path_hash = path_tree_hash[:children].first
45
+ child_path_hash[:text].should eq 'test_create_file.txt'
46
+ child_path_hash[:id].should eq File.join('/test_folder', 'test_create_file.txt').to_s
47
+
48
+ result, message, is_directory = @file_support.delete_file('test_folder')
49
+ result.should eq false
50
+ message.should eq ErpTechSvcs::FileSupport::Manager::FOLDER_IS_NOT_EMPTY
51
+ result, message, is_directory = @file_support.delete_file(File.join('test_folder','test_create_file.txt'))
52
+ result.should eq true
53
+ is_directory.should eq false
54
+ result, message, is_directory = @file_support.delete_file('test_folder')
55
+ result.should eq true
56
+ end
57
+
58
+ it "should allow you to rename a file" do
59
+ @file_support.create_file('', 'test_create_file.txt', 'test')
60
+ @file_support.exists?('test_create_file.txt').should eq true
61
+
62
+ result, message = @file_support.rename_file('test_create_file.txt', 'test_rename.txt')
63
+ result.should eq true
64
+ @file_support.exists?('test_create_file.txt').should eq false
65
+ @file_support.exists?('test_rename.txt').should eq true
66
+
67
+ result, message, is_directory = @file_support.delete_file('test_rename.txt')
68
+ @file_support.exists?('test_rename.txt').should eq false
69
+
70
+ result, message = @file_support.rename_file('not_real.txt', 'test_rename.txt')
71
+ result.should eq false
72
+ message.should eq ErpTechSvcs::FileSupport::Manager::FILE_FOLDER_DOES_NOT_EXIST
73
+ end
74
+
75
+ it "should allow you to move a file" do
76
+ @file_support.create_file('', 'test_create_file.txt', 'test')
77
+ @file_support.exists?('test_create_file.txt').should eq true
78
+
79
+ @file_support.create_folder('', 'test_folder')
80
+ result, message = @file_support.save_move('test_create_file.txt', 'test_folder')
81
+ result.should eq true
82
+ @file_support.exists?(File.join('test_folder','test_create_file.txt')).should eq true
83
+
84
+ result, message, is_directory = @file_support.delete_file(File.join('test_folder','test_create_file.txt'))
85
+ result.should eq true
86
+ is_directory.should eq false
87
+ result, message, is_directory = @file_support.delete_file('test_folder')
88
+ result.should eq true
89
+ end
90
+
91
+
92
+ end