dragonfly 0.9.15 → 1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of dragonfly might be problematic. Click here for more details.

Files changed (203) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +1 -8
  3. data/.travis.yml +11 -0
  4. data/Gemfile +1 -0
  5. data/History.md +52 -2
  6. data/LICENSE +1 -1
  7. data/README.md +38 -95
  8. data/dev/grid.jpg +0 -0
  9. data/dev/irbrc.rb +27 -0
  10. data/dev/rails_template.rb +38 -0
  11. data/dev/test.ru +56 -0
  12. data/dev/test_rails +19 -0
  13. data/dragonfly.gemspec +3 -21
  14. data/lib/dragonfly.rb +45 -44
  15. data/lib/dragonfly/app.rb +175 -96
  16. data/lib/dragonfly/configurable.rb +71 -170
  17. data/lib/dragonfly/content.rb +211 -0
  18. data/lib/dragonfly/core_ext/object.rb +1 -6
  19. data/lib/dragonfly/file_data_store.rb +197 -0
  20. data/lib/dragonfly/image_magick/analysers/image_properties.rb +23 -0
  21. data/lib/dragonfly/image_magick/generators/convert.rb +19 -0
  22. data/lib/dragonfly/image_magick/generators/plain.rb +26 -0
  23. data/lib/dragonfly/image_magick/generators/plasma.rb +25 -0
  24. data/lib/dragonfly/image_magick/generators/text.rb +127 -0
  25. data/lib/dragonfly/image_magick/plugin.rb +83 -0
  26. data/lib/dragonfly/image_magick/processors/convert.rb +29 -0
  27. data/lib/dragonfly/image_magick/processors/encode.rb +18 -0
  28. data/lib/dragonfly/image_magick/processors/thumb.rb +76 -0
  29. data/lib/dragonfly/job.rb +118 -134
  30. data/lib/dragonfly/job_endpoint.rb +2 -0
  31. data/lib/dragonfly/memory_data_store.rb +34 -0
  32. data/lib/dragonfly/middleware.rb +5 -3
  33. data/lib/dragonfly/{active_model_extensions.rb → model.rb} +5 -3
  34. data/lib/dragonfly/{active_model_extensions → model}/attachment.rb +40 -35
  35. data/lib/dragonfly/{active_model_extensions → model}/attachment_class_methods.rb +36 -40
  36. data/lib/dragonfly/model/class_methods.rb +109 -0
  37. data/lib/dragonfly/{active_model_extensions → model}/instance_methods.rb +2 -2
  38. data/lib/dragonfly/{active_model_extensions → model}/validations.rb +17 -12
  39. data/lib/dragonfly/railtie.rb +8 -6
  40. data/lib/dragonfly/register.rb +27 -0
  41. data/lib/dragonfly/response.rb +47 -52
  42. data/lib/dragonfly/routed_endpoint.rb +4 -0
  43. data/lib/dragonfly/serializer.rb +15 -5
  44. data/lib/dragonfly/server.rb +56 -29
  45. data/lib/dragonfly/shell.rb +12 -23
  46. data/lib/dragonfly/spec/data_store_examples.rb +64 -0
  47. data/lib/dragonfly/temp_object.rb +32 -47
  48. data/lib/dragonfly/url_attributes.rb +19 -22
  49. data/lib/dragonfly/url_mapper.rb +3 -0
  50. data/lib/dragonfly/utils.rb +12 -0
  51. data/lib/dragonfly/version.rb +1 -1
  52. data/lib/dragonfly/whitelist.rb +19 -0
  53. data/lib/rails/generators/dragonfly/USAGE +8 -0
  54. data/lib/rails/generators/dragonfly/dragonfly_generator.rb +24 -0
  55. data/lib/rails/generators/dragonfly/templates/initializer.rb.erb +27 -0
  56. data/samples/gif.gif +0 -0
  57. data/spec/dragonfly/app_spec.rb +270 -64
  58. data/spec/dragonfly/configurable_spec.rb +142 -418
  59. data/spec/dragonfly/content_spec.rb +353 -0
  60. data/spec/dragonfly/cookie_monster_spec.rb +2 -1
  61. data/spec/dragonfly/file_data_store_spec.rb +301 -0
  62. data/spec/dragonfly/image_magick/analysers/image_properties_spec.rb +20 -0
  63. data/spec/dragonfly/image_magick/generators/convert_spec.rb +19 -0
  64. data/spec/dragonfly/image_magick/generators/plain_spec.rb +50 -0
  65. data/spec/dragonfly/image_magick/generators/plasma_spec.rb +32 -0
  66. data/spec/dragonfly/image_magick/generators/text_spec.rb +77 -0
  67. data/spec/dragonfly/image_magick/plugin_spec.rb +131 -0
  68. data/spec/dragonfly/image_magick/processors/convert_spec.rb +56 -0
  69. data/spec/dragonfly/image_magick/processors/thumb_spec.rb +173 -0
  70. data/spec/dragonfly/job_endpoint_spec.rb +30 -73
  71. data/spec/dragonfly/job_spec.rb +280 -608
  72. data/spec/dragonfly/memory_data_store_spec.rb +20 -0
  73. data/spec/dragonfly/middleware_spec.rb +47 -27
  74. data/spec/dragonfly/{active_model_extensions → model}/model_spec.rb +331 -555
  75. data/spec/dragonfly/model/validations_spec.rb +196 -0
  76. data/spec/dragonfly/register_spec.rb +35 -0
  77. data/spec/dragonfly/routed_endpoint_spec.rb +6 -6
  78. data/spec/dragonfly/serializer_spec.rb +13 -15
  79. data/spec/dragonfly/server_spec.rb +122 -46
  80. data/spec/dragonfly/shell_spec.rb +43 -24
  81. data/spec/dragonfly/temp_object_spec.rb +69 -94
  82. data/spec/dragonfly/url_attributes_spec.rb +49 -0
  83. data/spec/dragonfly/utils_spec.rb +32 -0
  84. data/spec/dragonfly/whitelist_spec.rb +30 -0
  85. data/spec/dragonfly_spec.rb +43 -0
  86. data/spec/fixtures/deprecated_stored_content/eggs.bonus +1 -0
  87. data/spec/fixtures/deprecated_stored_content/eggs.bonus.meta +1 -0
  88. data/spec/functional/configuration_spec.rb +19 -0
  89. data/spec/functional/model_urls_spec.rb +43 -41
  90. data/spec/functional/remote_on_the_fly_spec.rb +14 -16
  91. data/spec/functional/shell_commands_spec.rb +24 -14
  92. data/spec/functional/to_response_spec.rb +10 -10
  93. data/spec/functional/urls_spec.rb +5 -3
  94. data/spec/spec_helper.rb +23 -28
  95. data/spec/support/argument_matchers.rb +7 -8
  96. data/spec/support/image_matchers.rb +14 -36
  97. data/spec/support/model_helpers.rb +97 -0
  98. data/spec/support/simple_matchers.rb +12 -0
  99. metadata +92 -393
  100. data/.yardopts +0 -29
  101. data/Rakefile +0 -36
  102. data/config.ru +0 -14
  103. data/docs.watchr +0 -1
  104. data/extra_docs/Analysers.md +0 -68
  105. data/extra_docs/Caching.md +0 -23
  106. data/extra_docs/Configuration.md +0 -149
  107. data/extra_docs/Couch.md +0 -49
  108. data/extra_docs/DataStorage.md +0 -226
  109. data/extra_docs/Encoding.md +0 -67
  110. data/extra_docs/ExampleUseCases.md +0 -116
  111. data/extra_docs/GeneralUsage.md +0 -105
  112. data/extra_docs/Generators.md +0 -68
  113. data/extra_docs/Heroku.md +0 -50
  114. data/extra_docs/ImageMagick.md +0 -136
  115. data/extra_docs/Index.md +0 -33
  116. data/extra_docs/MimeTypes.md +0 -40
  117. data/extra_docs/Models.md +0 -441
  118. data/extra_docs/Mongo.md +0 -42
  119. data/extra_docs/Processing.md +0 -77
  120. data/extra_docs/Rack.md +0 -52
  121. data/extra_docs/Rails2.md +0 -57
  122. data/extra_docs/Rails3.md +0 -56
  123. data/extra_docs/ServingRemotely.md +0 -104
  124. data/extra_docs/Sinatra.md +0 -25
  125. data/extra_docs/URLs.md +0 -203
  126. data/features/images.feature +0 -47
  127. data/features/no_processing.feature +0 -14
  128. data/features/rails.feature +0 -8
  129. data/features/steps/common_steps.rb +0 -8
  130. data/features/steps/dragonfly_steps.rb +0 -66
  131. data/features/steps/rails_steps.rb +0 -40
  132. data/features/support/env.rb +0 -13
  133. data/features/support/setup.rb +0 -41
  134. data/fixtures/rails/files/app/models/album.rb +0 -6
  135. data/fixtures/rails/files/app/views/albums/new.html.erb +0 -7
  136. data/fixtures/rails/files/app/views/albums/show.html.erb +0 -6
  137. data/fixtures/rails/files/config/initializers/dragonfly.rb +0 -4
  138. data/fixtures/rails/files/features/manage_album_images.feature +0 -38
  139. data/fixtures/rails/files/features/step_definitions/helper_steps.rb +0 -7
  140. data/fixtures/rails/files/features/step_definitions/image_steps.rb +0 -25
  141. data/fixtures/rails/files/features/step_definitions/web_steps.rb +0 -189
  142. data/fixtures/rails/files/features/support/paths.rb +0 -17
  143. data/fixtures/rails/files/features/text_images.feature +0 -7
  144. data/fixtures/rails/template.rb +0 -20
  145. data/irbrc.rb +0 -19
  146. data/lib/dragonfly/active_model_extensions/class_methods.rb +0 -98
  147. data/lib/dragonfly/analyser.rb +0 -58
  148. data/lib/dragonfly/analysis/file_command_analyser.rb +0 -33
  149. data/lib/dragonfly/analysis/image_magick_analyser.rb +0 -6
  150. data/lib/dragonfly/config/heroku.rb +0 -26
  151. data/lib/dragonfly/config/image_magick.rb +0 -6
  152. data/lib/dragonfly/config/rails.rb +0 -20
  153. data/lib/dragonfly/data_storage.rb +0 -11
  154. data/lib/dragonfly/data_storage/couch_data_store.rb +0 -83
  155. data/lib/dragonfly/data_storage/file_data_store.rb +0 -144
  156. data/lib/dragonfly/data_storage/mongo_data_store.rb +0 -96
  157. data/lib/dragonfly/data_storage/s3data_store.rb +0 -168
  158. data/lib/dragonfly/encoder.rb +0 -13
  159. data/lib/dragonfly/encoding/image_magick_encoder.rb +0 -6
  160. data/lib/dragonfly/function_manager.rb +0 -67
  161. data/lib/dragonfly/generation/image_magick_generator.rb +0 -6
  162. data/lib/dragonfly/generator.rb +0 -9
  163. data/lib/dragonfly/image_magick/analyser.rb +0 -53
  164. data/lib/dragonfly/image_magick/config.rb +0 -44
  165. data/lib/dragonfly/image_magick/encoder.rb +0 -57
  166. data/lib/dragonfly/image_magick/generator.rb +0 -147
  167. data/lib/dragonfly/image_magick/processor.rb +0 -114
  168. data/lib/dragonfly/image_magick/utils.rb +0 -46
  169. data/lib/dragonfly/image_magick_utils.rb +0 -4
  170. data/lib/dragonfly/job_builder.rb +0 -39
  171. data/lib/dragonfly/job_definitions.rb +0 -30
  172. data/lib/dragonfly/loggable.rb +0 -28
  173. data/lib/dragonfly/processing/image_magick_processor.rb +0 -6
  174. data/lib/dragonfly/processor.rb +0 -9
  175. data/lib/dragonfly/rails/images.rb +0 -32
  176. data/lib/dragonfly/simple_cache.rb +0 -23
  177. data/spec/dragonfly/active_model_extensions/spec_helper.rb +0 -95
  178. data/spec/dragonfly/analyser_spec.rb +0 -123
  179. data/spec/dragonfly/analysis/file_command_analyser_spec.rb +0 -49
  180. data/spec/dragonfly/data_storage/couch_data_store_spec.rb +0 -84
  181. data/spec/dragonfly/data_storage/file_data_store_spec.rb +0 -308
  182. data/spec/dragonfly/data_storage/mongo_data_store_spec.rb +0 -81
  183. data/spec/dragonfly/data_storage/s3_data_store_spec.rb +0 -277
  184. data/spec/dragonfly/data_storage/shared_data_store_examples.rb +0 -77
  185. data/spec/dragonfly/function_manager_spec.rb +0 -154
  186. data/spec/dragonfly/image_magick/analyser_spec.rb +0 -73
  187. data/spec/dragonfly/image_magick/encoder_spec.rb +0 -46
  188. data/spec/dragonfly/image_magick/generator_spec.rb +0 -178
  189. data/spec/dragonfly/image_magick/processor_spec.rb +0 -293
  190. data/spec/dragonfly/job_builder_spec.rb +0 -37
  191. data/spec/dragonfly/job_definitions_spec.rb +0 -57
  192. data/spec/dragonfly/loggable_spec.rb +0 -80
  193. data/spec/dragonfly/simple_cache_spec.rb +0 -27
  194. data/spec/dragonfly/url_attributes.rb +0 -47
  195. data/spec/functional/deprecations_spec.rb +0 -51
  196. data/spec/functional/image_magick_app_spec.rb +0 -27
  197. data/spec/test_imagemagick.ru +0 -49
  198. data/yard/handlers/configurable_attr_handler.rb +0 -38
  199. data/yard/setup.rb +0 -15
  200. data/yard/templates/default/fulldoc/html/css/common.css +0 -109
  201. data/yard/templates/default/layout/html/layout.erb +0 -93
  202. data/yard/templates/default/module/html/configuration_summary.erb +0 -31
  203. data/yard/templates/default/module/setup.rb +0 -17
@@ -1,3 +1,5 @@
1
+ require 'dragonfly/response'
2
+
1
3
  module Dragonfly
2
4
  class JobEndpoint
3
5
 
@@ -0,0 +1,34 @@
1
+ module Dragonfly
2
+ class MemoryDataStore
3
+
4
+ def initialize
5
+ @content_store = {}
6
+ end
7
+
8
+ def write(content, opts={})
9
+ uid = opts[:uid] || generate_uid
10
+ content_store[uid] = {:content => content.data, :meta => content.meta.dup}
11
+ uid
12
+ end
13
+
14
+ def read(uid)
15
+ data = content_store[uid]
16
+ [data[:content], data[:meta]] if data
17
+ end
18
+
19
+ def destroy(uid)
20
+ content_store.delete(uid)
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :content_store
26
+
27
+ def generate_uid
28
+ @uid_count ||= 0
29
+ @uid_count += 1
30
+ @uid_count.to_s
31
+ end
32
+
33
+ end
34
+ end
@@ -1,14 +1,15 @@
1
+ require 'dragonfly'
2
+
1
3
  module Dragonfly
2
4
  class Middleware
3
5
 
4
- def initialize(app, dragonfly_app_name, deprecated_arg=nil)
5
- raise ArgumentError, "mounting Dragonfly::Middleware with a mount point is deprecated - just use Dragonfly::Middleware, #{dragonfly_app_name.inspect}" if deprecated_arg
6
+ def initialize(app, dragonfly_app_name=nil)
6
7
  @app = app
7
8
  @dragonfly_app_name = dragonfly_app_name
8
9
  end
9
10
 
10
11
  def call(env)
11
- response = Dragonfly[@dragonfly_app_name].call(env)
12
+ response = Dragonfly.app(@dragonfly_app_name).call(env)
12
13
  if response[1]['X-Cascade'] == 'pass'
13
14
  @app.call(env)
14
15
  else
@@ -18,3 +19,4 @@ module Dragonfly
18
19
 
19
20
  end
20
21
  end
22
+
@@ -1,6 +1,8 @@
1
- module Dragonfly
1
+ require 'dragonfly/model/instance_methods'
2
+ require 'dragonfly/model/class_methods'
2
3
 
3
- module ActiveModelExtensions
4
+ module Dragonfly
5
+ module Model
4
6
 
5
7
  def self.extended(klass)
6
8
  unless klass.include?(InstanceMethods)
@@ -10,4 +12,4 @@ module Dragonfly
10
12
  end
11
13
 
12
14
  end
13
- end
15
+ end
@@ -1,9 +1,10 @@
1
1
  require 'forwardable'
2
- require 'dragonfly/active_model_extensions/attachment_class_methods'
2
+ require 'dragonfly/has_filename'
3
+ require 'dragonfly/job'
4
+ require 'dragonfly/model/attachment_class_methods'
3
5
 
4
6
  module Dragonfly
5
- module ActiveModelExtensions
6
-
7
+ module Model
7
8
  class Attachment
8
9
 
9
10
  # Exceptions
@@ -11,8 +12,9 @@ module Dragonfly
11
12
 
12
13
  extend Forwardable
13
14
  def_delegators :job,
14
- :data, :b64_data, :to_file, :file, :tempfile, :path,
15
- :process, :encode, :analyse,
15
+ :to_file, :file, :tempfile, :path,
16
+ :data, :b64_data, :mime_type,
17
+ :process, :analyse, :shell_eval,
16
18
  :meta, :meta=,
17
19
  :name, :size,
18
20
  :url
@@ -46,13 +48,10 @@ module Dragonfly
46
48
  reset_magic_attributes
47
49
  self.class.run_callbacks(:after_unassign, model, self) if should_run_callbacks?
48
50
  else
49
- self.job = case value
50
- when Job then value.dup
51
- when self.class then value.job.dup
52
- else app.new_job(value)
53
- end
51
+ self.job = app.new_job(value)
54
52
  set_magic_attributes
55
- job.url_attrs = all_extra_attributes
53
+ job.update_url_attributes(magic_attributes_hash)
54
+ meta.merge!(standard_meta_attributes)
56
55
  self.class.run_callbacks(:after_assign, model, self) if should_run_callbacks?
57
56
  retain! if should_retain?
58
57
  end
@@ -64,6 +63,10 @@ module Dragonfly
64
63
  !!@changed
65
64
  end
66
65
 
66
+ def stored?
67
+ uid?
68
+ end
69
+
67
70
  def destroy!
68
71
  destroy_previous!
69
72
  destroy_content(uid) if uid?
@@ -78,7 +81,7 @@ module Dragonfly
78
81
  end
79
82
 
80
83
  def to_value
81
- self if job
84
+ (self if job) || self.class.default_job
82
85
  end
83
86
 
84
87
  def name=(name)
@@ -91,11 +94,6 @@ module Dragonfly
91
94
  self
92
95
  end
93
96
 
94
- def encode!(*args)
95
- assign(encode(*args))
96
- self
97
- end
98
-
99
97
  def remote_url(opts={})
100
98
  app.remote_url_for(uid, opts) if uid?
101
99
  end
@@ -131,7 +129,7 @@ module Dragonfly
131
129
  end
132
130
 
133
131
  def destroy_retained!
134
- destroy_content(retained_attrs[:uid])
132
+ destroy_content(retained_attrs['uid'])
135
133
  end
136
134
 
137
135
  def retained_attrs
@@ -143,7 +141,7 @@ module Dragonfly
143
141
 
144
142
  def retained_attrs=(attrs)
145
143
  if changed? # if already set, ignore and destroy this retained content
146
- destroy_content(attrs[:uid])
144
+ destroy_content(attrs['uid'])
147
145
  else
148
146
  attrs.each do |key, value|
149
147
  unless attribute_keys.include?(key)
@@ -157,6 +155,11 @@ module Dragonfly
157
155
  end
158
156
  end
159
157
 
158
+ def add_meta(meta)
159
+ job.add_meta(meta)
160
+ self
161
+ end
162
+
160
163
  def inspect
161
164
  "<Dragonfly Attachment uid=#{uid.inspect}, app=#{app.name.inspect}>"
162
165
  end
@@ -170,20 +173,17 @@ module Dragonfly
170
173
  attr_writer :changed, :retained
171
174
 
172
175
  def attribute_keys
173
- [:uid] + magic_attributes
176
+ @attribute_keys ||= ['uid'] + magic_attributes.map{|attribute| attribute.to_s }
174
177
  end
175
178
 
176
179
  def store_job!
177
- meta.merge!(all_extra_attributes)
178
- opts = self.class.evaluate_storage_opts(model, self)
180
+ opts = self.class.evaluate_storage_options(model, self)
179
181
  set_uid_and_model_uid job.store(opts)
180
182
  self.job = job.to_fetched_job(uid)
181
183
  end
182
184
 
183
185
  def destroy_content(uid)
184
186
  app.datastore.destroy(uid)
185
- rescue DataStorage::DataNotFound, DataStorage::DestroyError => e
186
- app.log.warn("*** WARNING ***: tried to destroy data with uid #{uid}, but got error: #{e}")
187
187
  end
188
188
 
189
189
  def destroy_previous!
@@ -244,7 +244,15 @@ module Dragonfly
244
244
  end
245
245
 
246
246
  def set_magic_attributes
247
- magic_attributes.each{|property| set_magic_attribute(property, job.send(property)) }
247
+ magic_attributes.each do |property|
248
+ value = begin
249
+ job.send(property)
250
+ rescue RuntimeError => e
251
+ Dragonfly.warn("setting magic attribute for #{property} to nil in #{self.inspect} because got error #{e}")
252
+ nil
253
+ end
254
+ set_magic_attribute(property, value)
255
+ end
248
256
  end
249
257
 
250
258
  def reset_magic_attributes
@@ -261,27 +269,24 @@ module Dragonfly
261
269
 
262
270
  def magic_attributes_hash
263
271
  magic_attributes.inject({}) do |attrs, property|
264
- attrs[property] = model.send("#{attribute}_#{property}")
272
+ attrs[property.to_s] = model.send("#{attribute}_#{property}")
265
273
  attrs
266
274
  end
267
275
  end
268
276
 
269
- def extra_attributes
270
- @extra_attributes ||= {
271
- :model_class => model.class.name,
272
- :model_attachment => attribute
277
+ def standard_meta_attributes
278
+ @standard_meta_attributes ||= {
279
+ 'model_class' => model.class.name,
280
+ 'model_attachment' => attribute.to_s
273
281
  }
274
282
  end
275
283
 
276
- def all_extra_attributes
277
- magic_attributes_hash.merge(extra_attributes)
278
- end
279
-
280
284
  def set_job_from_uid
281
285
  self.job = app.fetch(uid)
282
- job.url_attrs = all_extra_attributes
286
+ job.update_url_attributes(magic_attributes_hash)
283
287
  end
284
288
 
285
289
  end
286
290
  end
287
291
  end
292
+
@@ -1,48 +1,42 @@
1
1
  module Dragonfly
2
- module ActiveModelExtensions
2
+ module Model
3
3
  class Attachment
4
4
  class << self
5
-
5
+
6
6
  class ConfigProxy
7
-
7
+
8
8
  def initialize(spec, block)
9
9
  @spec = spec
10
10
  instance_eval(&block)
11
11
  end
12
-
12
+
13
13
  private
14
-
14
+
15
15
  attr_reader :spec
16
-
16
+
17
17
  def after_assign(*callbacks, &block)
18
18
  add_callbacks(:after_assign, *callbacks, &block)
19
19
  end
20
-
20
+
21
21
  def after_unassign(*callbacks, &block)
22
22
  add_callbacks(:after_unassign, *callbacks, &block)
23
23
  end
24
-
24
+
25
25
  def copy_to(accessor, &block)
26
26
  after_assign do |a|
27
27
  self.send "#{accessor}=", (block_given? ? instance_exec(a, &block) : a)
28
28
  end
29
29
  after_unassign{|a| self.send("#{accessor}=", nil) }
30
30
  end
31
-
32
- def storage_opts(opts=nil, &block)
33
- spec.storage_opts_specs << (opts || block)
31
+
32
+ def default(path)
33
+ spec.default_path = path.to_s
34
34
  end
35
35
 
36
- def storage_opt(key, value, &block)
37
- if value.is_a? Symbol
38
- storage_opts{|a| {key => send(value)} }
39
- elsif block_given?
40
- storage_opts{|a| {key => instance_exec(a, &block)} }
41
- else
42
- storage_opts{|a| {key => value} }
43
- end
36
+ def storage_options(opts=nil, &block)
37
+ spec.storage_options_specs << (opts || block)
44
38
  end
45
-
39
+
46
40
  def add_callbacks(name, *callbacks, &block)
47
41
  if block_given?
48
42
  spec.callbacks[name] << block
@@ -50,38 +44,40 @@ module Dragonfly
50
44
  spec.callbacks[name].push(*callbacks)
51
45
  end
52
46
  end
53
-
47
+
54
48
  def method_missing(meth, *args, &block)
55
49
  if meth.to_s =~ /^storage_(.*)$/
56
- storage_opt($1.to_sym, args.first, &block)
50
+ raise NoMethodError, "#{meth} is deprecated - use storage_options instead"
57
51
  else
58
52
  super
59
53
  end
60
54
  end
61
-
55
+
62
56
  end
63
-
57
+
64
58
  def init(model_class, attribute, app, config_block)
65
59
  @model_class, @attribute, @app, @config_block = model_class, attribute, app, config_block
66
- include app.analyser.analysis_methods
67
- include app.job_definitions
68
- define_method :format do
69
- job.format
70
- end
71
- define_method :mime_type do
72
- job.mime_type
73
- end
60
+ include app.job_methods
74
61
  ConfigProxy.new(self, config_block) if config_block
75
62
  self
76
63
  end
77
64
 
78
- attr_reader :model_class, :attribute, :app, :config_block
65
+ attr_reader :model_class, :attribute, :app, :config_block, :default_path
66
+
67
+ def default_path=(path)
68
+ @default_path = path
69
+ app.fetch_file_whitelist.push(path)
70
+ end
71
+
72
+ def default_job
73
+ app.fetch_file(default_path) if default_path
74
+ end
79
75
 
80
76
  # Callbacks
81
77
  def callbacks
82
78
  @callbacks ||= Hash.new{|h,k| h[k] = [] }
83
79
  end
84
-
80
+
85
81
  def run_callbacks(name, model, attachment)
86
82
  attachment.should_run_callbacks = false
87
83
  callbacks[name].each do |callback|
@@ -95,7 +91,7 @@ module Dragonfly
95
91
 
96
92
  # Magic attributes
97
93
  def allowed_magic_attributes
98
- app.analyser.analysis_method_names + [:size, :name]
94
+ app.analyser_methods + [:size, :name]
99
95
  end
100
96
 
101
97
  def magic_attributes
@@ -122,12 +118,12 @@ module Dragonfly
122
118
  end
123
119
 
124
120
  # Storage options
125
- def storage_opts_specs
126
- @storage_opts_specs ||= []
121
+ def storage_options_specs
122
+ @storage_options_specs ||= []
127
123
  end
128
-
129
- def evaluate_storage_opts(model, attachment)
130
- storage_opts_specs.inject({}) do |opts, spec|
124
+
125
+ def evaluate_storage_options(model, attachment)
126
+ storage_options_specs.inject({}) do |opts, spec|
131
127
  options = case spec
132
128
  when Proc then model.instance_exec(attachment, &spec)
133
129
  when Symbol then model.send(spec)
@@ -0,0 +1,109 @@
1
+ require 'dragonfly'
2
+ require 'dragonfly/serializer'
3
+ require 'dragonfly/utils'
4
+ require 'dragonfly/model/attachment'
5
+
6
+ module Dragonfly
7
+ module Model
8
+ module ClassMethods
9
+
10
+ def dragonfly_attachment_classes
11
+ @dragonfly_attachment_classes ||= begin
12
+ parent_class = ancestors.select{|a| a.is_a?(Class) }[1]
13
+ if parent_class.respond_to?(:dragonfly_attachment_classes)
14
+ parent_class.dragonfly_attachment_classes.map do |klass|
15
+ new_dragonfly_attachment_class(klass.attribute, klass.app, klass.config_block)
16
+ end
17
+ else
18
+ []
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def dragonfly_accessor(attribute, opts={}, &config_block)
26
+ app = case opts[:app]
27
+ when Symbol, nil then Dragonfly.app(opts[:app])
28
+ else opts[:app]
29
+ end
30
+
31
+ # Add callbacks
32
+ before_save :save_dragonfly_attachments if respond_to?(:before_save)
33
+ before_destroy :destroy_dragonfly_attachments if respond_to?(:before_destroy)
34
+
35
+ # Register the new attribute
36
+ dragonfly_attachment_classes << new_dragonfly_attachment_class(attribute, app, config_block)
37
+
38
+ # Define an anonymous module for all of the attribute-specific instance
39
+ # methods.
40
+ instance_methods = Module.new do
41
+ # Define the setter for the attribute
42
+ define_method "#{attribute}=" do |value|
43
+ dragonfly_attachments[attribute].assign(value)
44
+ end
45
+
46
+ # Define the getter for the attribute
47
+ define_method attribute do
48
+ dragonfly_attachments[attribute].to_value
49
+ end
50
+
51
+ # Define the xxx_stored? method
52
+ define_method "#{attribute}_stored?" do
53
+ dragonfly_attachments[attribute].stored?
54
+ end
55
+
56
+ # Define the URL setter
57
+ define_method "#{attribute}_url=" do |url|
58
+ unless Utils.blank?(url)
59
+ dragonfly_attachments[attribute].assign(app.fetch_url(url))
60
+ end
61
+ end
62
+
63
+ # Define the URL getter
64
+ define_method "#{attribute}_url" do
65
+ nil
66
+ end
67
+
68
+ # Define the remove setter
69
+ define_method "remove_#{attribute}=" do |value|
70
+ unless [0, "0", false, "false", "", nil].include?(value)
71
+ dragonfly_attachments[attribute].assign(nil)
72
+ instance_variable_set("@remove_#{attribute}", true)
73
+ end
74
+ end
75
+
76
+ # Define the remove getter
77
+ attr_reader "remove_#{attribute}"
78
+
79
+ # Define the retained setter
80
+ define_method "retained_#{attribute}=" do |string|
81
+ unless Utils.blank?(string)
82
+ begin
83
+ dragonfly_attachments[attribute].retained_attrs = Serializer.json_b64_decode(string)
84
+ rescue Serializer::BadString => e
85
+ Dragonfly.warn("couldn't update attachment with serialized retained_#{attribute} string #{string.inspect}")
86
+ end
87
+ end
88
+ dragonfly_attachments[attribute].should_retain = true
89
+ dragonfly_attachments[attribute].retain!
90
+ string
91
+ end
92
+
93
+ # Define the retained getter
94
+ define_method "retained_#{attribute}" do
95
+ attrs = dragonfly_attachments[attribute].retained_attrs
96
+ Serializer.json_b64_encode(attrs) if attrs
97
+ end
98
+ end
99
+
100
+ include instance_methods
101
+ end
102
+
103
+ def new_dragonfly_attachment_class(attribute, app, config_block)
104
+ Class.new(Attachment).init(self, attribute, app, config_block)
105
+ end
106
+
107
+ end
108
+ end
109
+ end