paperdragon 0.0.7 → 0.0.8

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: 00a917cff82896af01efe02f6217157cea6eb144
4
- data.tar.gz: cd5588ff1dcb1dc713086d57b37b2ce5a12ffe8b
3
+ metadata.gz: c572a392762a6e502908b2669acf14b0584f3865
4
+ data.tar.gz: 134a7784f0170d5c9aea5343a4217a85458aff1b
5
5
  SHA512:
6
- metadata.gz: e2944453772c81e767aa30ead6e175ca76beb8febf43c985d6446a1ab5ae4672d1647a0e1dde5c6c020fd7857b2dccbb0a1d2ceae9bef02177726937d7a30596
7
- data.tar.gz: a5b2009a183835567b9d5e9217416287d9659ad86f02912a5592b0b073bd53c651ef09a993236fa5d6f85d9294d93152eaa7faa85046957bdfc749921d2d774f
6
+ metadata.gz: c1a60a4f77ad4a151d44f0c36fb578f3b620242e3681874d51ab2e83c787d416b6e659c341c23159e849f048807dd462738f21e99cbbe02102a287400f121331
7
+ data.tar.gz: e099ad54ab6ca8657d61695a25793431b669930caaf324baf2d96c13eeebc7793598910a780c6046c6aa39edec1a0e2341d96a814496ac106c94701a7a6ad7b6
data/README.md CHANGED
@@ -319,6 +319,43 @@ Documentation how to use Sidekiq and paperdragon in Traiblazer will be added sho
319
319
 
320
320
  ## Validations
321
321
 
322
+ Validating uploads are discussed in the _Callbacks_ chapter of the [Trailblazer
323
+ book](https://leanpub.com/trailblazer). We use [file_validators](https://github.com/musaffa/file_validations).
324
+
325
+ ## Model: Reader and Writer
326
+
327
+ If you don't like `Paperdragon::Model#image`'s fuzzy API you can use `Reader` and `Writer`.
328
+
329
+ The `Writer` will usually be mixed into a form.
330
+
331
+ ```ruby
332
+ class AlbumForm < Reform::Form
333
+ extend Paperdragon::Model::Writer
334
+ processable_writer :image
335
+ ```
336
+
337
+ This provides the `image!` writer for processing a file.
338
+
339
+ ```ruby
340
+ form.image!(file) { |v| v.thumb!("64x64") }
341
+ ```
342
+
343
+ Likewise, `Reader` will usually be used in cells or decorators.
344
+
345
+ ```ruby
346
+ class AlbumCell < Cell::ViewModel
347
+ extend Paperdragon::Model::Reader
348
+ processable_reader :image
349
+ property :image_meta_data
350
+ ```
351
+
352
+ You can now access the `Attachment` via `image`.
353
+
354
+ ```ruby
355
+ cell.image[:thumb].url
356
+ ```
357
+
358
+
322
359
  ## Paperclip compatibility
323
360
 
324
361
  I wrote paperdragon as an explicit alternative to paperclip. In the process of doing so, I step-wise replaced upload code, but left the rendering code unchanged. Paperclip has a slightly different API for rendering.
@@ -1,3 +1,7 @@
1
+ # 0.0.8
2
+
3
+ * Introduce `Model::Writer` and `Model::Reader` in case you don't like `Model#image`'s fuzzy API.
4
+
1
5
  # 0.0.7
2
6
 
3
7
  * `Task#process!` (and the delegated `File#process!`) now delete the original version of an attachment if `process!` is used to replace the latter.
@@ -1,4 +1,5 @@
1
1
  module Paperdragon
2
+ # Fuzzy API: gives you #image that can do both upload and rendering.
2
3
  module Model
3
4
  def self.included(base)
4
5
  base.extend ClassMethods
@@ -14,7 +15,7 @@ module Paperdragon
14
15
  def attachment_accessor_for(name, attachment_class)
15
16
  mod = Module.new do # TODO: abstract that into Uber, we use it everywhere.
16
17
  define_method name do |file=nil, options={}, &block|
17
- attachment = attachment_class.new(self.image_meta_data, options.merge({:model => self}))
18
+ attachment = attachment_class.new(image_meta_data, options.merge(model: self))
18
19
 
19
20
  return attachment unless file or block
20
21
 
@@ -24,5 +25,39 @@ module Paperdragon
24
25
  end
25
26
  end
26
27
  end
28
+
29
+
30
+ # class Album
31
+ # extend Paperdragon::Model::Writer
32
+ # processable_writer :image
33
+ #
34
+ # Provides Album#image!(file) { |v| v.thumb!("64x64") }
35
+ module Writer
36
+ def processable_writer(name, attachment_class=Attachment)
37
+ mod = Module.new do # TODO: abstract that into Uber, we use it everywhere.
38
+ define_method "#{name}!" do |file=nil, options={}, &block|
39
+ attachment = attachment_class.new(image_meta_data, options.merge(model: self))
40
+
41
+ # run the task block and save the returned new metadata in the model.
42
+ self.image_meta_data = attachment.task(*[file], &block)
43
+ end
44
+ end
45
+ include mod
46
+ end
47
+ end # Writer.
48
+
49
+
50
+ # class Album
51
+ # extend Paperdragon::Model::Reader
52
+ # processable_reader :image
53
+ #
54
+ # Provides Album#image #=> Attachment.
55
+ module Reader
56
+ def processable_reader(name, attachment_class=Attachment)
57
+ define_method name do
58
+ attachment_class.new(image_meta_data, model: self)
59
+ end
60
+ end
61
+ end
27
62
  end
28
63
  end
@@ -1,3 +1,3 @@
1
1
  module Paperdragon
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/test/model_test.rb CHANGED
@@ -18,7 +18,10 @@ class PaperdragonModelTest < MiniTest::Spec
18
18
  end
19
19
  end
20
20
 
21
+ # model has image_meta_data hash.
21
22
  it { Avatar.new.image[:thumb].url.must_equal "/paperdragon/Avatar-thumb" }
23
+ # model doesn't have upload, yet. returns empty attachment.
24
+ it { Image.new.image.metadata.must_equal({}) }
22
25
 
23
26
 
24
27
  # minimum setup
@@ -51,7 +54,6 @@ class PaperdragonModelTest < MiniTest::Spec
51
54
  model.image_meta_data.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>8, :height=>2, :uid=>"thumb-apotomo-1.png"}})
52
55
  end
53
56
 
54
-
55
57
  # passing options from image(file, {..}) to the Attachment.
56
58
  class ImageWithAttachment < OpenStruct
57
59
  include Paperdragon::Model
@@ -84,4 +86,44 @@ class PaperdragonModelTest < MiniTest::Spec
84
86
 
85
87
  model.image_meta_data.must_equal({:original=>{:width=>8, :height=>2, :uid=>"{:new=>true, :model=>#<PaperdragonModelTest::ImageWithAttachment image_meta_data={:original=>{:width=>216, :height=>63, :uid=>\"{:path=>\\\"/\\\", :model=>_<PaperdragonModelTest::ImageWithAttachment>}\"}}>}"}})
86
88
  end
89
+ end
90
+
91
+
92
+ class ModelWriterTest < MiniTest::Spec
93
+ class Image < OpenStruct
94
+ extend Paperdragon::Model::Writer
95
+ processable_writer :image
96
+ end
97
+
98
+ # process with #image{}
99
+ let (:logo) { Pathname("test/fixtures/apotomo.png") }
100
+
101
+ it do
102
+ model = Image.new
103
+ model.image!(logo) do |v|
104
+ v.process!(:original)
105
+ v.process!(:thumb) { |j| j.thumb!("16x16") }
106
+ end
107
+
108
+ model.image_meta_data.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>16, :height=>5, :uid=>"thumb-apotomo.png"}})
109
+
110
+
111
+ model.image! do |v|
112
+ v.reprocess!(:thumb, "1") { |j| j.thumb!("8x8") }
113
+ end
114
+
115
+ model.image_meta_data.class.must_equal Hash
116
+ model.image_meta_data.must_equal({:original=>{:width=>216, :height=>63, :uid=>"original-apotomo.png"}, :thumb=>{:width=>8, :height=>2, :uid=>"thumb-apotomo-1.png"}})
117
+ end
118
+
119
+ end
120
+
121
+
122
+ class ModelReaderTest < MiniTest::Spec
123
+ class Image < OpenStruct
124
+ extend Paperdragon::Model::Reader
125
+ processable_reader :image
126
+ end
127
+
128
+ it { Image.new(:image_meta_data => {:thumb => {:uid => "Avatar-thumb"}}).image[:thumb].url.must_equal "/paperdragon/Avatar-thumb" }
87
129
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paperdragon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-09 00:00:00.000000000 Z
11
+ date: 2015-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dragonfly