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
@@ -8,6 +8,12 @@ module ErpTechSvcs
8
8
  include ErpTechSvcs::Extensions::ActiveRecord::HasRoles
9
9
  include ErpTechSvcs::Extensions::ActiveRecord::HasFileAssets
10
10
  include ErpTechSvcs::Extensions::ActiveRecord::HasCapabilities
11
+ include ErpTechSvcs::Extensions::ActiveRecord::HasRelationalDynamicAttributes
12
+ end
13
+
14
+ engine = self
15
+ config.to_prepare do
16
+ ErpBaseErpSvcs.register_compass_ae_engine(engine)
11
17
  end
12
18
 
13
19
  end
@@ -26,7 +26,11 @@ module ErpTechSvcs
26
26
  end
27
27
 
28
28
  module InstanceMethods
29
- def capabilities
29
+ def has_capabilities?
30
+ !capabilities.empty?
31
+ end
32
+
33
+ def capabilities
30
34
  capable_model.capabilities
31
35
  end
32
36
 
@@ -42,7 +46,7 @@ module ErpTechSvcs
42
46
  capability_type = convert_capability_type(capability_type)
43
47
  raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::CapabilityTypeDoesNotExist if capability_type.nil?
44
48
 
45
- capability = self.capabilities.where('capability_type_id = ? and resource = ?', capability_type.id, resource).first
49
+ capability = find_capability(capability_type, resource)
46
50
  unless capability.nil?
47
51
  capability.has_access?(user)
48
52
  else
@@ -54,7 +58,7 @@ module ErpTechSvcs
54
58
  capability_type = convert_capability_type(capability_type)
55
59
  raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::CapabilityTypeDoesNotExist if capability_type.nil?
56
60
 
57
- capability = self.capabilities.where('capability_type_id = ? and resource = ?', capability_type.id, resource).first
61
+ capability = find_capability(capability_type, resource)
58
62
  if capability.nil?
59
63
  capability = Capability.create(:capability_type => capability_type, :resource => resource)
60
64
  capability.add_roles(roles)
@@ -71,7 +75,7 @@ module ErpTechSvcs
71
75
  capability_type = convert_capability_type(capability_type)
72
76
  raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::CapabilityTypeDoesNotExist if capability_type.nil?
73
77
 
74
- capability = self.capabilities.where('capability_type_id = ? and resource = ?', capability_type.id, resource).first
78
+ capability = find_capability(capability_type, resource)
75
79
  unless capability.nil?
76
80
  capability.remove_all_roles
77
81
  capability.add_roles(roles)
@@ -82,7 +86,7 @@ module ErpTechSvcs
82
86
 
83
87
  def remove_all_capabilities
84
88
  self.capabilities.each do |capability|
85
- remove_capability(capability.type, capability.resource)
89
+ capability.destroy
86
90
  end
87
91
  end
88
92
 
@@ -90,7 +94,7 @@ module ErpTechSvcs
90
94
  capability_type = convert_capability_type(capability_type)
91
95
  raise ErpTechSvcs::Utils::CompassAccessNegotiator::Errors::CapabilityTypeDoesNotExist if capability_type.nil?
92
96
 
93
- capability = self.capabilities.where('capability_type_id = ? and resource = ?', capability_type.id, resource).first
97
+ capability = find_capability(capability_type, resource)
94
98
  unless capability.nil?
95
99
  capability.destroy
96
100
  else
@@ -132,6 +136,14 @@ module ErpTechSvcs
132
136
  CapabilityType.find_by_internal_identifier(type.to_s) if (type.is_a?(String) || type.is_a?(Symbol))
133
137
  end
134
138
 
139
+ def find_capability(capability_type, resource)
140
+ if resource.nil?
141
+ self.capabilities.where('capability_type_id = ? and resource is null', capability_type.id).first
142
+ else
143
+ self.capabilities.where('capability_type_id = ? and resource = ?', capability_type.id, resource).first
144
+ end
145
+ end
146
+
135
147
  end
136
148
  end
137
149
  end
@@ -20,9 +20,22 @@ module ErpTechSvcs
20
20
  end
21
21
 
22
22
  module InstanceMethods
23
-
24
- def add_file(data, path=nil)
25
- FileAsset.create!(:file_asset_holder => self, :base_path => path, :data => data)
23
+
24
+ # Capabilites can be passed via a hash
25
+ # {
26
+ # :download => ['admin', 'employee'],
27
+ # :edit => ['admin']
28
+ # }
29
+ #
30
+ def add_file(data, path=nil, capabilities=nil)
31
+ file_asset = FileAsset.create!(:file_asset_holder => self, :base_path => path, :data => data)
32
+
33
+ #set capabilites if they are passed
34
+ capabilities.each do |capability_type, roles|
35
+ file_asset.add_capability(capability_type, nil, roles)
36
+ end if capabilities
37
+
38
+ file_asset
26
39
  end
27
40
 
28
41
  def images
@@ -0,0 +1,99 @@
1
+ module ErpTechSvcs
2
+ module Extensions
3
+ module ActiveRecord
4
+ module HasRelationalDynamicAttributes
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+
11
+ def has_relational_dynamic_attributes
12
+ has_many :attribute_values, :as => :attributed_record, :dependent => :destroy
13
+
14
+ before_save :assign_dynamic_attribute_on_save
15
+
16
+ extend HasRelationalDynamicAttributes::SingletonMethods
17
+ include HasRelationalDynamicAttributes::InstanceMethods
18
+ end
19
+
20
+ end
21
+
22
+ module SingletonMethods
23
+ def find_by_dynamic_attribute(value, options={})
24
+ arel_query = AttributeValue.where('attributed_record_type = ?', self.name)
25
+ .where(AttributeValue.arel_table[:value].matches("%#{value}%"))
26
+
27
+ or_clauses = nil
28
+ options[:type_iids].each do |type_iid|
29
+ type = AttributeType.where('description = ? or internal_identifier = ?',type_iid,type_iid).first
30
+ raise "Attribute Type '#{type_iid}' does not exist" if type.nil?
31
+ or_clauses = or_clauses.nil? ? AttributeValue.arel_table[:attribute_type_id].eq(type.id) : or_clauses.or(AttributeValue.arel_table[:attribute_type_id].eq(type.id))
32
+ end if options[:type_iids]
33
+
34
+ arel_query = arel_query.where(or_clauses) if or_clauses
35
+ arel_query = arel_query.limit(options[:limit]) if options[:limit]
36
+ arel_query = arel_query.offset(options[:offset]) if options[:offset]
37
+ arel_query.all.collect(&:attributed_record)
38
+ end
39
+ end
40
+
41
+ module InstanceMethods
42
+ def update_or_create_dynamic_attribute(value, type, data_type)
43
+ if self.has_dynamic_attribute_of_type?(type)
44
+ update_first_dynamic_attribute_value_of_type(value, type)
45
+ else
46
+ add_dynamic_attribute(value, type, data_type)
47
+ end
48
+ end
49
+
50
+ def update_first_dynamic_attribute_value_of_type(value, type)
51
+ attribute_type = AttributeType.where('description = ? or internal_identifier = ?', type, type).first
52
+ attribute_value = self.attribute_values.where(:attribute_type_id => attribute_type.id).first
53
+ attribute_value.value = value
54
+ attribute_value.save!
55
+ end
56
+
57
+ def get_dynamic_attributes
58
+ {}.tap do |hash|
59
+ self.attribute_values.each do |value|
60
+ hash[value.attribute_type.description] = value.value
61
+ end
62
+ end
63
+ end
64
+
65
+ def get_dynamic_value_of_type(attribute_type_iid)
66
+ attribute_value = get_dynamic_attribute_of_type(attribute_type_iid)
67
+ attribute_value.nil? ? nil : attribute_value.value
68
+ end
69
+
70
+ def get_dynamic_attribute_of_type(attribute_type_iid)
71
+ attribute_value = self.attribute_values.includes(:attribute_type).where('attribute_types.internal_identifier = ? or attribute_types.description = ?', attribute_type_iid.to_s, attribute_type_iid.to_s).first
72
+ attribute_value.nil? ? nil : attribute_value
73
+ end
74
+
75
+ def assign_dynamic_attribute_on_save
76
+ #template method overridden in implementing class
77
+ end
78
+
79
+ def has_dynamic_attribute_of_type? (attribute_type_iid)
80
+ !self.attribute_values.includes(:attribute_type).where('attribute_types.internal_identifier = ? or attribute_types.description = ?', attribute_type_iid.to_s, attribute_type_iid.to_s).first.nil?
81
+ end
82
+
83
+ def destroy_dynamic_attribute_of_type (attribute_type_iid)
84
+ self.attribute_values.includes(:attribute_type).destroy_all("attribute_types.internal_identifier = #{attribute_type_iid.to_s} or attribute_types.description = #{attribute_type_iid.to_s}")
85
+ end
86
+
87
+ def add_dynamic_attribute(value, type, data_type)
88
+ attribute_type = AttributeType.where('description = ? or internal_identifier = ?', type, type).first
89
+ attribute_type = AttributeType.create(:description => type, :data_type => data_type) unless attribute_type
90
+ attribute_value = AttributeValue.create(:value => value, :attribute_type => attribute_type)
91
+ self.attribute_values << attribute_value
92
+ end
93
+
94
+ end
95
+
96
+ end#HasRelationalDynamicAttributes
97
+ end#ActiveRecord
98
+ end#Extensions
99
+ end#ErpTechSvcs
@@ -59,6 +59,7 @@ module ErpTechSvcs
59
59
  unless self.has_role?(role)
60
60
  self.secured_model.roles << role
61
61
  self.secured_model.save
62
+ self.reload
62
63
  end
63
64
  end
64
65
 
@@ -79,11 +80,13 @@ module ErpTechSvcs
79
80
  roles.flatten!
80
81
  roles.each do |role|
81
82
  self.remove_role(role)
83
+ self.reload
82
84
  end
83
85
  end
84
86
 
85
87
  def remove_all_roles
86
88
  self.roles.delete_all
89
+ self.reload
87
90
  end
88
91
 
89
92
  def has_role?(*passed_roles)
@@ -0,0 +1,13 @@
1
+ Sorcery::Model::Submodules::UserActivation::InstanceMethods.class_eval do
2
+ def deactivate!
3
+ config = sorcery_config
4
+ self.send(:"#{config.activation_state_attribute_name}=", "deactivated")
5
+ save!(:validate => false) # don't run validations
6
+ end
7
+
8
+ def reactivate!
9
+ config = sorcery_config
10
+ self.send(:"#{config.activation_state_attribute_name}=", "active")
11
+ save!(:validate => false) # don't run validations
12
+ end
13
+ end
@@ -2,4 +2,8 @@
2
2
  require 'erp_tech_svcs/extensions/active_record/has_file_assets'
3
3
  require 'erp_tech_svcs/extensions/active_record/has_roles'
4
4
  require 'erp_tech_svcs/extensions/active_record/has_capabilities'
5
- require 'erp_tech_svcs/extensions/active_record/acts_as_versioned'
5
+ require 'erp_tech_svcs/extensions/active_record/acts_as_versioned'
6
+ require 'erp_tech_svcs/extensions/active_record/has_relational_dynamic_attributes'
7
+
8
+ #sorcery
9
+ require 'erp_tech_svcs/extensions/sorcery/user_activation'
@@ -1,8 +1,6 @@
1
1
  module ErpTechSvcs
2
2
  module FileSupport
3
- DEFAULT_OPTIONS = {
4
- :storage => :filesystem
5
- }
3
+ DEFAULT_OPTIONS = {:storage => :filesystem}
6
4
 
7
5
  mattr_accessor :options
8
6
  @@options = HashWithIndifferentAccess.new(DEFAULT_OPTIONS)
@@ -4,16 +4,16 @@ module ErpTechSvcs
4
4
  REMOVE_FILES_REGEX = /^\./
5
5
 
6
6
  def root
7
- File.join(Rails.root,'public')
7
+ Rails.root.to_s
8
8
  end
9
9
 
10
10
  def update_file(path, content)
11
- File.open(path, 'w+') {|f| f.puts(content) }
11
+ File.open(path, 'w+:ASCII-8BIT') {|f| f.write(content) }
12
12
  end
13
13
 
14
14
  def create_file(path, name, contents)
15
15
  FileUtils.mkdir_p path unless File.exists? path
16
- File.open(File.join(path,name), 'w+') {|f| f.puts(contents) }
16
+ File.open(File.join(path,name), 'w+:ASCII-8BIT') {|f| f.write(contents) }
17
17
  end
18
18
 
19
19
  def create_folder(path, name)
@@ -21,14 +21,16 @@ module ErpTechSvcs
21
21
  end
22
22
 
23
23
  def save_move(path, new_parent_path)
24
- result = nil
25
- unless File.exists? path
26
- message = 'File does not exists'
24
+ old_path = File.join(path)
25
+ new_path = File.join(new_parent_path)
26
+ result = false
27
+ unless File.exists? old_path
28
+ message = FILE_DOES_NOT_EXIST
27
29
  else
28
30
  name = File.basename(path)
29
31
  #make sure path is there.
30
- FileUtils.mkdir_p new_parent_path unless File.directory? new_parent_path
31
- FileUtils.mv(path, new_parent_path + '/' + name)
32
+ FileUtils.mkdir_p new_path unless File.directory? new_path
33
+ FileUtils.mv(old_path, File.join(new_path,name))
32
34
  message = "#{name} was moved to #{new_parent_path} successfully"
33
35
  result = true
34
36
  end
@@ -41,9 +43,9 @@ module ErpTechSvcs
41
43
  end
42
44
 
43
45
  def rename_file(path, name)
44
- result = nil
46
+ result = false
45
47
  unless File.exists? path
46
- message = 'File does not exists'
48
+ message = FILE_DOES_NOT_EXIST
47
49
  else
48
50
  old_name = File.basename(path)
49
51
  path_pieces = path.split('/')
@@ -59,18 +61,18 @@ module ErpTechSvcs
59
61
  end
60
62
 
61
63
  def delete_file(path, options={})
62
- result = nil
64
+ result = false
63
65
  name = File.basename(path)
64
66
  is_directory = false
65
67
  if !File.exists? path and !File.directory? path
66
- message = 'File / Folder does not exist'
68
+ message = FILE_FOLDER_DOES_NOT_EXIST
67
69
  else
68
70
  if File.directory? path
69
71
  is_directory = true
70
72
  entries = Dir.entries(path)
71
73
  entries.delete_if{|entry| entry =~ REMOVE_FILES_REGEX}
72
74
  if entries.count > 0 && !options[:force]
73
- message = "Folder is not empty"
75
+ message = FOLDER_IS_NOT_EMPTY
74
76
  result = false;
75
77
  else
76
78
  FileUtils.rm_rf(path)
@@ -91,9 +93,9 @@ module ErpTechSvcs
91
93
  contents = nil
92
94
  message = nil
93
95
  unless File.exists? path
94
- message = 'File does not exists'
96
+ message = FILE_DOES_NOT_EXIST
95
97
  else
96
- contents = IO.read(path)
98
+ contents = IO.read(path).force_encoding('ASCII-8BIT')
97
99
  end
98
100
  return contents, message
99
101
  end
@@ -149,16 +151,16 @@ module ErpTechSvcs
149
151
  tree_data[:children] << if options[:preload]
150
152
  build_tree_for_directory(path, options) if options[:preload]
151
153
  else
152
- {:text => entry, :id => path}
154
+ {:text => entry, :id => path, :iconCls => 'icon-content'}
153
155
  end
154
156
  elsif !options[:included_file_extensions_regex].nil? && entry =~ options[:included_file_extensions_regex]
155
- tree_data[:children] << {:text => entry, :leaf => true, :downloadPath => path, :id => path}
157
+ tree_data[:children] << {:text => entry, :leaf => true, :iconCls => 'icon-document', :downloadPath => directory, :id => path}
156
158
  elsif options[:included_file_extensions_regex].nil?
157
- tree_data[:children] << {:text => entry, :leaf => true, :downloadPath => path, :id => path}
159
+ tree_data[:children] << {:text => entry, :leaf => true, :iconCls => 'icon-document', :downloadPath => directory, :id => path}
158
160
  end
159
161
  end if File.directory?(directory)
160
162
 
161
- tree_data[:children].sort_by{|item| [item[:id]]}
163
+ tree_data[:children].sort_by!{|item| [item[:text]]}
162
164
  tree_data
163
165
  end
164
166
 
@@ -1,11 +1,14 @@
1
1
  module ErpTechSvcs
2
2
  module FileSupport
3
3
  class Manager
4
+ FOLDER_IS_NOT_EMPTY = 'Folder is not empty'
5
+ FILE_DOES_NOT_EXIST = 'File does not exist'
6
+ FILE_FOLDER_DOES_NOT_EXIST = 'File / Folder does not exist'
4
7
 
5
8
  class << self
6
9
  def find_parent(item, parents)
7
10
  parents.find do |parent|
8
- path = item[:path].gsub(item[:text],'').split('/').join('/')
11
+ path = item[:path].split('/')[0..-2].join('/')
9
12
  parent[:id] == path
10
13
  end
11
14
  end
@@ -50,9 +53,11 @@ module ErpTechSvcs
50
53
  end
51
54
 
52
55
  def build_file_assets_tree_for_model(model, starting_path)
56
+ files = model.files
57
+
53
58
  node_tree = [{:text => root, :leaf => false, :id => root, :children => []}]
54
59
 
55
- paths = model.files.collect{|file| File.join(file.directory,file.name)}
60
+ paths = files.collect{|file| File.join(file.directory,file.name)}
56
61
 
57
62
  node_tree.first[:children] << {:id => starting_path, :text => starting_path.split('/').last, :children => []} if paths.select{|path| path.split('/')[1] == starting_path.split('/')[1]}.empty?
58
63
 
@@ -82,7 +87,16 @@ module ErpTechSvcs
82
87
  level.each do |item|
83
88
  parent = old_parents.count == 1 ? old_parents.first : self.class.find_parent(item, old_parents)
84
89
  path = File.join(parent[:id], item[:text]).gsub(root, '')
85
- child_hash = {:text => item[:text], :downloadPath => path, :leaf => !File.extname(item[:text]).blank?, :id => path, :children => []}
90
+ icon_cls = File.extname(item[:text]).blank? ? 'icon-content' : 'icon-document'
91
+ child_hash = {:text => item[:text], :downloadPath => parent[:id], :iconCls => icon_cls, :leaf => !File.extname(item[:text]).blank?, :id => path, :children => []}
92
+
93
+ #attempt set security if this file is a file_asset model
94
+ file = files.find{|file| file.directory == parent[:id].gsub(root,'') and file.name == item[:text]}
95
+ unless file.nil?
96
+ child_hash[:isSecured] = file.has_capabilities?
97
+ child_hash[:iconCls] = 'icon-document_lock' if child_hash[:isSecured]
98
+ end
99
+
86
100
  new_parents << child_hash
87
101
  parent[:children] << child_hash
88
102
  end
@@ -97,13 +111,15 @@ module ErpTechSvcs
97
111
  def insert_folders(file_asset_nodes)
98
112
  file_asset_nodes.each do |child_asset_node|
99
113
  node = find_node(File.join(self.root,child_asset_node[:id]))
100
- folders = node[:children].select{|item| !item[:leaf]}
101
- child_asset_node[:children] = [] if child_asset_node[:children].nil? && !folders.empty?
102
- folders.each do |folder|
103
- folder[:id].gsub!(self.root,'')
104
- child_asset_node[:children] << folder unless child_asset_node[:children].collect{|child_node| child_node[:text] }.include?(folder[:text])
114
+ unless node.nil?
115
+ folders = node[:children].select{|item| !item[:leaf]}
116
+ child_asset_node[:children] = [] if child_asset_node[:children].nil? && !folders.empty?
117
+ folders.each do |folder|
118
+ folder[:id].gsub!(self.root,'')
119
+ child_asset_node[:children] << folder unless child_asset_node[:children].collect{|child_node| child_node[:text] }.include?(folder[:text])
120
+ end
121
+ insert_folders(child_asset_node[:children])
105
122
  end
106
- insert_folders(child_asset_node[:children])
107
123
  end unless file_asset_nodes.nil?
108
124
  end
109
125
 
@@ -142,6 +158,10 @@ module ErpTechSvcs
142
158
  end
143
159
  end
144
160
 
161
+ def reload
162
+ # this is a template method
163
+ end
164
+
145
165
  end#Manager
146
166
  end#FileSupport
147
167
  end#ErpTechSvcs
@@ -5,17 +5,21 @@ module Paperclip
5
5
  @queued_for_write.each do |style, file|
6
6
  begin
7
7
  log("saving #{path(style)}")
8
- AWS::S3::S3Object.store(path(style),
9
- file,
10
- bucket_name,
11
- {:content_type => content_type,
12
- :access => (@s3_permissions[style] || @s3_permissions[:default]),
13
- }.merge(@s3_headers))
14
- rescue AWS::S3::NoSuchBucket => e
8
+ acl = @s3_permissions[style] || @s3_permissions[:default]
9
+ acl = acl.call(self, style) if acl.respond_to?(:call)
10
+ write_options = {
11
+ :content_type => content_type,
12
+ :acl => acl
13
+ }
14
+ write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
15
+ unless @s3_server_side_encryption.blank?
16
+ write_options[:server_side_encryption] = @s3_server_side_encryption
17
+ end
18
+ write_options.merge!(@s3_headers)
19
+ s3_object(style).write(file, write_options)
20
+ rescue AWS::S3::Errors::NoSuchBucket => e
15
21
  create_bucket
16
22
  retry
17
- rescue AWS::S3::ResponseError => e
18
- raise
19
23
  end
20
24
  end
21
25
 
@@ -26,10 +26,15 @@ module ActionView
26
26
  @cached[key][name][prefix][partial][locals] = decorate(yield, path_info, details, locals)
27
27
  else
28
28
  @cached[key][name][prefix][partial][locals].each do |template|
29
- last_update = mtime(template.identifier, file_support)
30
- if last_update > template.updated_at
29
+ #check if the file still exists
30
+ if file_support.exists? template.identifier
31
+ last_update = mtime(template.identifier, file_support)
32
+ if last_update > template.updated_at
33
+ @cached[key][name][prefix][partial][locals].delete_if{|item| item.identifier == template.identifier}
34
+ @cached[key][name][prefix][partial][locals] << build_template(template.identifier, template.virtual_path, (details[:formats] || [:html] if template.formats.empty?), file_support, template.locals)
35
+ end
36
+ else
31
37
  @cached[key][name][prefix][partial][locals].delete_if{|item| item.identifier == template.identifier}
32
- @cached[key][name][prefix][partial][locals] << build_template(template.identifier, template.virtual_path, (details[:formats] || [:html] if template.formats.empty?), file_support, template.locals)
33
38
  end
34
39
  end
35
40
  @cached[key][name][prefix][partial][locals]
@@ -70,9 +75,24 @@ module ActionView
70
75
 
71
76
  protected
72
77
 
78
+ def cache_template(path, file_support)
79
+ contents, message = file_support.get_contents(path)
80
+ path = path.sub(%r{^/},'')
81
+ #Rails.logger.info "creating cache with key: #{path}"
82
+ Rails.cache.write(path, contents, :expires_in => ErpTechSvcs::Config.s3_cache_expires_in_minutes.minutes)
83
+ return contents, message
84
+ end
85
+
73
86
  def build_template(p, virtual_path, formats, file_support, locals=nil)
74
87
  handler, format = extract_handler_and_format(p, formats)
75
- contents, message = file_support.get_contents(p)
88
+ contents = Rails.cache.read(p.sub(%r{^/},''))
89
+ if contents.nil?
90
+ contents, message = cache_template(p, file_support)
91
+ else
92
+ #Rails.logger.info "!!!!! USING CACHED TEMPLATE: #{contents.inspect}"
93
+ contents = contents.dup
94
+ end
95
+
76
96
  Template.new(contents, p, handler, :virtual_path => virtual_path, :format => format, :updated_at => mtime(p, file_support), :locals => locals)
77
97
  end
78
98
  end