Class: PopulateMe::Attachment
- Inherits:
-
Object
- Object
- PopulateMe::Attachment
show all
- Defined in:
- lib/populate_me/attachment.rb
Defined Under Namespace
Classes: Middleware
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(doc, field) ⇒ Attachment
Returns a new instance of Attachment.
42
43
44
45
|
# File 'lib/populate_me/attachment.rb', line 42
def initialize doc, field
@document = doc
@field = field
end
|
Class Attribute Details
.settings ⇒ Object
Returns the value of attribute settings.
12
13
14
|
# File 'lib/populate_me/attachment.rb', line 12
def settings
@settings
end
|
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
40
41
42
|
# File 'lib/populate_me/attachment.rb', line 40
def document
@document
end
|
#field ⇒ Object
Returns the value of attribute field.
40
41
42
|
# File 'lib/populate_me/attachment.rb', line 40
def field
@field
end
|
Class Method Details
.inherited(sub) ⇒ Object
19
20
21
22
23
|
# File 'lib/populate_me/attachment.rb', line 19
def inherited sub
super
sub.settings = settings.dup
sub::Middleware.parent = sub
end
|
.middleware ⇒ Object
25
26
27
|
# File 'lib/populate_me/attachment.rb', line 25
def middleware
Rack::Static
end
|
.middleware_options ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/populate_me/attachment.rb', line 29
def middleware_options
[
{
urls: [settings.url_prefix],
root: settings.root
}
]
end
|
.set(name, value) ⇒ Object
15
16
17
|
# File 'lib/populate_me/attachment.rb', line 15
def set name, value
self.settings[name] = value
end
|
Instance Method Details
#attachee_prefix ⇒ Object
70
71
72
|
# File 'lib/populate_me/attachment.rb', line 70
def attachee_prefix
WebUtils.dasherize_class_name self.document.class.name
end
|
#create(form_hash) ⇒ Object
98
99
100
101
102
103
|
# File 'lib/populate_me/attachment.rb', line 98
def create form_hash
self.delete
future_field_value = perform_create form_hash
create_variations form_hash.merge({future_field_value: future_field_value})
future_field_value
end
|
#create_variations(hash) ⇒ Object
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/populate_me/attachment.rb', line 105
def create_variations hash
return if self.variations.nil?
tmppath = hash[:tempfile].path
path = self.location_for_filename hash[:future_field_value]
FileUtils.mkdir_p(File.dirname(path))
variations.each do |v|
self.delete v.name
v_path = WebUtils.filename_variation path, v.name, v.ext
v.job.call tmppath, v_path
self.perform_create hash.merge({variation: v, variation_path: v_path})
end
end
|
#deletable?(variation_name = :original) ⇒ Boolean
131
132
133
|
# File 'lib/populate_me/attachment.rb', line 131
def deletable? variation_name=:original
!WebUtils.blank?(self.field_filename(variation_name)) and File.exist?(self.location(variation_name))
end
|
#delete(variation_name = :original) ⇒ Object
135
136
137
138
139
|
# File 'lib/populate_me/attachment.rb', line 135
def delete variation_name=:original
if self.deletable?(variation_name)
perform_delete variation_name
end
end
|
#delete_all ⇒ Object
141
142
143
144
145
146
|
# File 'lib/populate_me/attachment.rb', line 141
def delete_all
to_delete = [:original] + variations.map(&:name)
to_delete.each do |v_name|
self.delete v_name
end
end
|
#ensure_local_path ⇒ Object
94
95
96
|
# File 'lib/populate_me/attachment.rb', line 94
def ensure_local_path
self.location
end
|
#field_filename(variation_name = :original) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/populate_me/attachment.rb', line 63
def field_filename variation_name=:original
return nil if WebUtils.blank?(self.field_value)
return self.field_value if variation_name==:original
v = self.variations.find{|var|var.name==variation_name}
WebUtils.filename_variation(self.field_value, v.name, v.ext)
end
|
#field_options ⇒ Object
55
56
57
|
# File 'lib/populate_me/attachment.rb', line 55
def field_options
self.document.class.fields[self.field]
end
|
#field_value ⇒ Object
51
52
53
|
# File 'lib/populate_me/attachment.rb', line 51
def field_value
self.document.__send__(field)
end
|
#location(variation_name = :original) ⇒ Object
87
88
89
|
# File 'lib/populate_me/attachment.rb', line 87
def location variation_name=:original
self.location_for_filename self.field_filename(variation_name)
end
|
#location_for_filename(filename) ⇒ Object
90
91
92
|
# File 'lib/populate_me/attachment.rb', line 90
def location_for_filename filename
File.join self.location_root, filename
end
|
#location_root ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/populate_me/attachment.rb', line 79
def location_root
File.join(
settings.root,
settings.url_prefix,
self.attachee_prefix
)
end
|
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/populate_me/attachment.rb', line 118
def perform_create hash
return File.basename(hash[:variation_path]) unless WebUtils.blank?(hash[:variation_path])
FileUtils.mkdir_p self.location_root
tmppath = hash[:tempfile].path
unique_prefix = File.basename(tmppath, File.extname(tmppath))
unique_filename = "#{unique_prefix}-#{hash[:filename]}"
path = self.location_for_filename unique_filename
FileUtils.copy_entry(tmppath, path)
unique_filename
end
|
148
149
150
|
# File 'lib/populate_me/attachment.rb', line 148
def perform_delete variation_name=:original
FileUtils.rm self.location(variation_name)
end
|
#settings ⇒ Object
47
48
49
|
# File 'lib/populate_me/attachment.rb', line 47
def settings
self.class.settings
end
|
#url(variation_name = :original) ⇒ Object
74
75
76
77
|
# File 'lib/populate_me/attachment.rb', line 74
def url variation_name=:original
return nil if WebUtils.blank?(self.field_filename(variation_name))
"#{settings.url_prefix.sub(/\/$/,'')}/#{self.attachee_prefix}/#{self.field_filename(variation_name)}"
end
|
#variations ⇒ Object
59
60
61
|
# File 'lib/populate_me/attachment.rb', line 59
def variations
self.field_options[:variations] || []
end
|