kithe 2.0.0.pre.alpha2 → 2.0.2

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.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +4 -4
  3. data/app/indexing/kithe/indexable/record_index_updater.rb +1 -1
  4. data/app/jobs/kithe/create_derivatives_job.rb +2 -2
  5. data/app/models/kithe/asset.rb +82 -154
  6. data/app/models/kithe/asset/derivative_creator.rb +32 -62
  7. data/app/models/kithe/asset/derivative_definition.rb +12 -13
  8. data/app/models/kithe/asset/set_shrine_uploader.rb +64 -0
  9. data/app/models/kithe/collection.rb +0 -6
  10. data/app/models/kithe/model.rb +0 -21
  11. data/app/models/kithe/work.rb +0 -5
  12. data/app/uploaders/kithe/asset_uploader.rb +15 -78
  13. data/lib/kithe.rb +22 -20
  14. data/{app/models → lib}/kithe/config_base.rb +6 -1
  15. data/lib/kithe/engine.rb +14 -3
  16. data/lib/kithe/indexable_settings.rb +1 -1
  17. data/lib/kithe/patch_fx.rb +39 -0
  18. data/lib/kithe/version.rb +4 -1
  19. data/lib/shrine/plugins/kithe_checksum_signatures.rb +41 -0
  20. data/lib/shrine/plugins/kithe_controllable_backgrounding.rb +53 -0
  21. data/lib/shrine/plugins/kithe_derivative_definitions.rb +101 -0
  22. data/lib/shrine/plugins/kithe_derivatives.rb +54 -0
  23. data/lib/shrine/plugins/kithe_determine_mime_type.rb +39 -0
  24. data/lib/shrine/plugins/kithe_persisted_derivatives.rb +161 -0
  25. data/lib/shrine/plugins/kithe_promotion_callbacks.rb +4 -0
  26. data/lib/shrine/plugins/kithe_promotion_directives.rb +33 -3
  27. data/lib/shrine/plugins/kithe_storage_location.rb +53 -4
  28. data/lib/tasks/kithe_tasks.rake +22 -15
  29. data/spec/dummy/app/models/plain_active_record.rb +3 -0
  30. data/spec/dummy/config/database.yml +6 -0
  31. data/spec/dummy/db/schema.rb +102 -0
  32. data/spec/dummy/log/development.log +3616 -0
  33. data/spec/dummy/log/test.log +86464 -0
  34. data/spec/dummy/tmp/development_secret.txt +1 -1
  35. data/spec/indexing/indexable_spec.rb +1 -1
  36. data/spec/models/kithe/asset/asset_derivatives_spec.rb +137 -0
  37. data/spec/models/kithe/asset/asset_promotion_hooks_spec.rb +26 -5
  38. data/spec/models/kithe/asset/set_shrine_uploader_spec.rb +39 -0
  39. data/spec/models/kithe/asset_spec.rb +9 -59
  40. data/spec/models/kithe/model_spec.rb +0 -32
  41. data/spec/models/kithe_spec.rb +10 -0
  42. data/spec/shrine/kithe_accept_remote_url_spec.rb +49 -0
  43. data/spec/shrine/kithe_checksum_signatures_spec.rb +63 -0
  44. data/spec/shrine/kithe_derivative_definitions_spec.rb +303 -0
  45. data/spec/shrine/kithe_persisted_derivatives_spec.rb +424 -0
  46. data/spec/shrine/kithe_storage_location_spec.rb +43 -15
  47. data/spec/spec_helper.rb +0 -19
  48. data/spec/test_support/images/3x3_pixel.jpg +0 -0
  49. data/spec/test_support/shrine_spec_support.rb +2 -1
  50. metadata +60 -36
  51. data/app/models/kithe/asset/derivative_updater.rb +0 -119
  52. data/app/models/kithe/derivative.rb +0 -15
  53. data/app/uploaders/kithe/derivative_uploader.rb +0 -48
  54. data/spec/dummy/db/structure.sql +0 -309
  55. data/spec/models/kithe/asset/asset_create_derivatives_spec.rb +0 -320
  56. data/spec/models/kithe/derivative_spec.rb +0 -168
@@ -1,15 +0,0 @@
1
- module Kithe
2
-
3
- # Derivatives by default will be stored in Shrine storage :kithe_derivatives, so
4
- # that should be registered in your app.
5
- #
6
- # Only one deriv can exist for a given asset_id/key pair, enforced by db constraint.
7
- class Derivative < ApplicationRecord
8
- # the fk is to kithe_models STI table, but we only intend for assets
9
- belongs_to :asset, class_name: "Kithe::Asset"
10
-
11
- include Kithe::DerivativeUploader::Attachment.new(:file, store: :kithe_derivatives)
12
-
13
- delegate :content_type, :size, :height, :width, :url, to: :file, allow_nil: true
14
- end
15
- end
@@ -1,48 +0,0 @@
1
- require 'mini_mime'
2
-
3
- module Kithe
4
- # The derivative uploader doesn't have to do too much, we don't even use
5
- # promotion for derivatives, just writing directly to a storage.
6
- #
7
- # But it needs activerecord integration, and limited metadata automatic extraction.
8
- class DerivativeUploader < Shrine
9
- plugin :activerecord
10
-
11
- plugin :determine_mime_type, analyzer: :marcel
12
-
13
- # ignore error, often from storing a non-image file which can't have dimensions
14
- # extracted. behavior consistent with shrine 2.x.
15
- plugin :store_dimensions, on_error: :ignore
16
-
17
- # Useful in case consumers want it, and doesn't harm anything to be available.
18
- # https://github.com/shrinerb/shrine/blob/master/doc/plugins/rack_response.md
19
- plugin :rack_response
20
-
21
- # should this be in a plugin? location in file system based on original asset
22
- # id and derivative key, as well as unique random file id from shrine.
23
- def generate_location(io, context)
24
- # assumes we're only used with Derivative model, that has an asset_id and key
25
- asset_id = context[:record].asset_id
26
- key = context[:record].key
27
- original = super
28
- [asset_id, key, original].compact.join("/")
29
- end
30
-
31
-
32
- # Override to fix "filename" metadata to be something reasonable, regardless
33
- # of what if anything was the filename of the IO being attached. shrine S3 will
34
- # insist on setting a default content-disposition with this filename.
35
- def extract_metadata(io, context = {})
36
- result = super
37
-
38
- if context[:kithe_derivative_key] &&
39
- context[:record]
40
- extension = MiniMime.lookup_by_content_type(result["mime_type"] || "")&.extension
41
- result["filename"] = "#{context[:record].asset.friendlier_id}_#{context[:kithe_derivative_key]}.#{extension}"
42
- result["kithe_derivative_key"] = context[:kithe_derivative_key]
43
- end
44
-
45
- return result
46
- end
47
- end
48
- end
@@ -1,309 +0,0 @@
1
- SET statement_timeout = 0;
2
- SET lock_timeout = 0;
3
- SET idle_in_transaction_session_timeout = 0;
4
- SET client_encoding = 'UTF8';
5
- SET standard_conforming_strings = on;
6
- SELECT pg_catalog.set_config('search_path', '', false);
7
- SET check_function_bodies = false;
8
- SET client_min_messages = warning;
9
- SET row_security = off;
10
-
11
- --
12
- -- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
13
- --
14
-
15
- CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
16
-
17
-
18
- --
19
- -- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
20
- --
21
-
22
- COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
23
-
24
-
25
- --
26
- -- Name: kithe_models_friendlier_id_gen(bigint, bigint); Type: FUNCTION; Schema: public; Owner: -
27
- --
28
-
29
- CREATE FUNCTION public.kithe_models_friendlier_id_gen(min_value bigint, max_value bigint) RETURNS text
30
- LANGUAGE plpgsql
31
- AS $$
32
- DECLARE
33
- new_id_int bigint;
34
- new_id_str character varying := '';
35
- done bool;
36
- tries integer;
37
- alphabet char[] := ARRAY['0','1','2','3','4','5','6','7','8','9',
38
- 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
39
- 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
40
- alphabet_length integer := array_length(alphabet, 1);
41
-
42
- BEGIN
43
- done := false;
44
- tries := 0;
45
- WHILE (NOT done) LOOP
46
- tries := tries + 1;
47
- IF (tries > 3) THEN
48
- RAISE 'Could not find non-conflicting friendlier_id in 3 tries';
49
- END IF;
50
-
51
- new_id_int := trunc(random() * (max_value - min_value) + min_value);
52
-
53
- -- convert bigint to a Base-36 alphanumeric string
54
- -- see https://web.archive.org/web/20130420084605/http://www.jamiebegin.com/base36-conversion-in-postgresql/
55
- -- https://gist.github.com/btbytes/7159902
56
- WHILE new_id_int != 0 LOOP
57
- new_id_str := alphabet[(new_id_int % alphabet_length)+1] || new_id_str;
58
- new_id_int := new_id_int / alphabet_length;
59
- END LOOP;
60
-
61
- done := NOT exists(SELECT 1 FROM kithe_models WHERE friendlier_id=new_id_str);
62
- END LOOP;
63
- RETURN new_id_str;
64
- END;
65
- $$;
66
-
67
-
68
- SET default_tablespace = '';
69
-
70
- SET default_with_oids = false;
71
-
72
- --
73
- -- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: -
74
- --
75
-
76
- CREATE TABLE public.ar_internal_metadata (
77
- key character varying NOT NULL,
78
- value character varying,
79
- created_at timestamp without time zone NOT NULL,
80
- updated_at timestamp without time zone NOT NULL
81
- );
82
-
83
-
84
- --
85
- -- Name: kithe_derivatives; Type: TABLE; Schema: public; Owner: -
86
- --
87
-
88
- CREATE TABLE public.kithe_derivatives (
89
- id bigint NOT NULL,
90
- key character varying NOT NULL,
91
- file_data jsonb,
92
- asset_id uuid NOT NULL,
93
- created_at timestamp without time zone NOT NULL,
94
- updated_at timestamp without time zone NOT NULL
95
- );
96
-
97
-
98
- --
99
- -- Name: kithe_derivatives_id_seq; Type: SEQUENCE; Schema: public; Owner: -
100
- --
101
-
102
- CREATE SEQUENCE public.kithe_derivatives_id_seq
103
- START WITH 1
104
- INCREMENT BY 1
105
- NO MINVALUE
106
- NO MAXVALUE
107
- CACHE 1;
108
-
109
-
110
- --
111
- -- Name: kithe_derivatives_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
112
- --
113
-
114
- ALTER SEQUENCE public.kithe_derivatives_id_seq OWNED BY public.kithe_derivatives.id;
115
-
116
-
117
- --
118
- -- Name: kithe_model_contains; Type: TABLE; Schema: public; Owner: -
119
- --
120
-
121
- CREATE TABLE public.kithe_model_contains (
122
- containee_id uuid,
123
- container_id uuid
124
- );
125
-
126
-
127
- --
128
- -- Name: kithe_models; Type: TABLE; Schema: public; Owner: -
129
- --
130
-
131
- CREATE TABLE public.kithe_models (
132
- id uuid DEFAULT public.gen_random_uuid() NOT NULL,
133
- title character varying NOT NULL,
134
- type character varying NOT NULL,
135
- "position" integer,
136
- json_attributes jsonb,
137
- created_at timestamp without time zone NOT NULL,
138
- updated_at timestamp without time zone NOT NULL,
139
- parent_id uuid,
140
- friendlier_id character varying DEFAULT public.kithe_models_friendlier_id_gen('2821109907456'::bigint, '101559956668415'::bigint) NOT NULL,
141
- file_data jsonb,
142
- representative_id uuid,
143
- leaf_representative_id uuid,
144
- kithe_model_type integer NOT NULL
145
- );
146
-
147
-
148
- --
149
- -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
150
- --
151
-
152
- CREATE TABLE public.schema_migrations (
153
- version character varying NOT NULL
154
- );
155
-
156
-
157
- --
158
- -- Name: kithe_derivatives id; Type: DEFAULT; Schema: public; Owner: -
159
- --
160
-
161
- ALTER TABLE ONLY public.kithe_derivatives ALTER COLUMN id SET DEFAULT nextval('public.kithe_derivatives_id_seq'::regclass);
162
-
163
-
164
- --
165
- -- Name: ar_internal_metadata ar_internal_metadata_pkey; Type: CONSTRAINT; Schema: public; Owner: -
166
- --
167
-
168
- ALTER TABLE ONLY public.ar_internal_metadata
169
- ADD CONSTRAINT ar_internal_metadata_pkey PRIMARY KEY (key);
170
-
171
-
172
- --
173
- -- Name: kithe_derivatives kithe_derivatives_pkey; Type: CONSTRAINT; Schema: public; Owner: -
174
- --
175
-
176
- ALTER TABLE ONLY public.kithe_derivatives
177
- ADD CONSTRAINT kithe_derivatives_pkey PRIMARY KEY (id);
178
-
179
-
180
- --
181
- -- Name: kithe_models kithe_models_pkey; Type: CONSTRAINT; Schema: public; Owner: -
182
- --
183
-
184
- ALTER TABLE ONLY public.kithe_models
185
- ADD CONSTRAINT kithe_models_pkey PRIMARY KEY (id);
186
-
187
-
188
- --
189
- -- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
190
- --
191
-
192
- ALTER TABLE ONLY public.schema_migrations
193
- ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
194
-
195
-
196
- --
197
- -- Name: index_kithe_derivatives_on_asset_id; Type: INDEX; Schema: public; Owner: -
198
- --
199
-
200
- CREATE INDEX index_kithe_derivatives_on_asset_id ON public.kithe_derivatives USING btree (asset_id);
201
-
202
-
203
- --
204
- -- Name: index_kithe_derivatives_on_asset_id_and_key; Type: INDEX; Schema: public; Owner: -
205
- --
206
-
207
- CREATE UNIQUE INDEX index_kithe_derivatives_on_asset_id_and_key ON public.kithe_derivatives USING btree (asset_id, key);
208
-
209
-
210
- --
211
- -- Name: index_kithe_model_contains_on_containee_id; Type: INDEX; Schema: public; Owner: -
212
- --
213
-
214
- CREATE INDEX index_kithe_model_contains_on_containee_id ON public.kithe_model_contains USING btree (containee_id);
215
-
216
-
217
- --
218
- -- Name: index_kithe_model_contains_on_container_id; Type: INDEX; Schema: public; Owner: -
219
- --
220
-
221
- CREATE INDEX index_kithe_model_contains_on_container_id ON public.kithe_model_contains USING btree (container_id);
222
-
223
-
224
- --
225
- -- Name: index_kithe_models_on_friendlier_id; Type: INDEX; Schema: public; Owner: -
226
- --
227
-
228
- CREATE UNIQUE INDEX index_kithe_models_on_friendlier_id ON public.kithe_models USING btree (friendlier_id);
229
-
230
-
231
- --
232
- -- Name: index_kithe_models_on_parent_id; Type: INDEX; Schema: public; Owner: -
233
- --
234
-
235
- CREATE INDEX index_kithe_models_on_parent_id ON public.kithe_models USING btree (parent_id);
236
-
237
-
238
- --
239
- -- Name: index_kithe_models_on_representative_id; Type: INDEX; Schema: public; Owner: -
240
- --
241
-
242
- CREATE INDEX index_kithe_models_on_representative_id ON public.kithe_models USING btree (representative_id);
243
-
244
-
245
- --
246
- -- Name: kithe_model_contains fk_rails_091010187b; Type: FK CONSTRAINT; Schema: public; Owner: -
247
- --
248
-
249
- ALTER TABLE ONLY public.kithe_model_contains
250
- ADD CONSTRAINT fk_rails_091010187b FOREIGN KEY (container_id) REFERENCES public.kithe_models(id);
251
-
252
-
253
- --
254
- -- Name: kithe_derivatives fk_rails_3dac8b4201; Type: FK CONSTRAINT; Schema: public; Owner: -
255
- --
256
-
257
- ALTER TABLE ONLY public.kithe_derivatives
258
- ADD CONSTRAINT fk_rails_3dac8b4201 FOREIGN KEY (asset_id) REFERENCES public.kithe_models(id);
259
-
260
-
261
- --
262
- -- Name: kithe_models fk_rails_403cce5c0d; Type: FK CONSTRAINT; Schema: public; Owner: -
263
- --
264
-
265
- ALTER TABLE ONLY public.kithe_models
266
- ADD CONSTRAINT fk_rails_403cce5c0d FOREIGN KEY (leaf_representative_id) REFERENCES public.kithe_models(id);
267
-
268
-
269
- --
270
- -- Name: kithe_model_contains fk_rails_490c1158f7; Type: FK CONSTRAINT; Schema: public; Owner: -
271
- --
272
-
273
- ALTER TABLE ONLY public.kithe_model_contains
274
- ADD CONSTRAINT fk_rails_490c1158f7 FOREIGN KEY (containee_id) REFERENCES public.kithe_models(id);
275
-
276
-
277
- --
278
- -- Name: kithe_models fk_rails_90130a9780; Type: FK CONSTRAINT; Schema: public; Owner: -
279
- --
280
-
281
- ALTER TABLE ONLY public.kithe_models
282
- ADD CONSTRAINT fk_rails_90130a9780 FOREIGN KEY (parent_id) REFERENCES public.kithe_models(id);
283
-
284
-
285
- --
286
- -- Name: kithe_models fk_rails_afa93b7b5d; Type: FK CONSTRAINT; Schema: public; Owner: -
287
- --
288
-
289
- ALTER TABLE ONLY public.kithe_models
290
- ADD CONSTRAINT fk_rails_afa93b7b5d FOREIGN KEY (representative_id) REFERENCES public.kithe_models(id);
291
-
292
-
293
- --
294
- -- PostgreSQL database dump complete
295
- --
296
-
297
- SET search_path TO "$user", public;
298
-
299
- INSERT INTO "schema_migrations" (version) VALUES
300
- ('20181015143259'),
301
- ('20181015143413'),
302
- ('20181015143737'),
303
- ('20181031190647'),
304
- ('20181128185658'),
305
- ('20190103144947'),
306
- ('20190109192252'),
307
- ('20190404144551');
308
-
309
-
@@ -1,320 +0,0 @@
1
- # Make a new test file cause it's a buncha func
2
- require 'rails_helper'
3
-
4
- # Not sure how to get our
5
- describe "Kithe::Asset derivative definitions", queue_adapter: :test do
6
- let(:a_jpg_deriv_file) { Kithe::Engine.root.join("spec/test_support/images/2x2_pixel.jpg") }
7
-
8
- temporary_class("TestAssetSubclass") do
9
- deriv_src_path = a_jpg_deriv_file
10
- Class.new(Kithe::Asset) do
11
- define_derivative(:some_data) do |original_file|
12
- StringIO.new("some one data")
13
- end
14
-
15
- define_derivative(:a_jpg) do |original_file|
16
- FileUtils.cp(deriv_src_path,
17
- Kithe::Engine.root.join("spec/test_support/images/2x2_pixel-TEMP.jpg"))
18
-
19
- File.open(Kithe::Engine.root.join("spec/test_support/images/2x2_pixel-TEMP.jpg"))
20
- end
21
- end
22
- end
23
-
24
- let(:asset) do
25
- TestAssetSubclass.create(title: "test",
26
- file: File.open(Kithe::Engine.root.join("spec/test_support/images/1x1_pixel.jpg"))
27
- ).tap do |a|
28
- # We want to promote without create_derivatives being automatically called
29
- # as usual, so we can test create_derivatives manually.
30
- a.file_attacher.set_promotion_directives(create_derivatives: false)
31
- a.promote
32
-
33
- # Precondition assumptions for our test setup to be valid
34
- expect(a.file_attacher.stored?).to be(true)
35
- expect(a.derivatives).to be_empty
36
- end
37
- end
38
-
39
- it "builds derivatives" do
40
- asset.create_derivatives
41
-
42
- one_deriv = asset.derivatives.find { |d| d.key == "some_data" }
43
- expect(one_deriv).to be_present
44
- expect(one_deriv.file.read).to eq("some one data")
45
-
46
- jpg_deriv = asset.derivatives.find {|d| d.key == "a_jpg"}
47
- expect(jpg_deriv.file.read).to eq(File.read(a_jpg_deriv_file, encoding: "BINARY"))
48
- end
49
-
50
- it "sets #derivatives_created?" do
51
- expect(asset.derivatives_created?).to be(false)
52
- asset.create_derivatives
53
- asset.reload
54
- expect(asset.derivatives_created?).to be(true)
55
- end
56
-
57
-
58
- describe "Original deleted before derivatives can be created", queue_adapter: :inline do
59
- let(:short_lived_asset) do
60
- TestAssetSubclass.create!(title: "test",
61
- file: File.open(Kithe::Engine.root.join("spec/test_support/images/1x1_pixel.jpg")))
62
- end
63
- it """catches the ActiveJob::DeserializationError
64
- if the asset is no longer in the database
65
- once the derivative creation job starts up.""" do
66
- id_to_delete = short_lived_asset.id
67
- Kithe::Derivative.where(asset_id: id_to_delete).delete_all
68
- Kithe::Model.where(id: id_to_delete).delete_all
69
-
70
- # short_lived_asset is no longer in the DB.
71
- # Let's try and create derivatives for it:
72
- expect do
73
- Kithe::CreateDerivativesJob.perform_later(short_lived_asset)
74
- end.not_to raise_error
75
- end
76
- end
77
-
78
- describe "under normal operation", queue_adapter: :inline do
79
- let(:asset) do
80
- TestAssetSubclass.create!(title: "test",
81
- file: File.open(Kithe::Engine.root.join("spec/test_support/images/1x1_pixel.jpg")))
82
- end
83
- it "automatically creates derivatives" do
84
- expect(asset.derivatives.count).to eq(2)
85
- end
86
- end
87
-
88
- it "extracts limited metadata from derivative" do
89
- asset.create_derivatives
90
-
91
- jpg_deriv = asset.derivatives.find {|d| d.key == "a_jpg"}
92
- expect(jpg_deriv.size).to eq(File.size(Kithe::Engine.root.join("spec/test_support/images/2x2_pixel.jpg")))
93
- expect(jpg_deriv.width).to eq(2)
94
- expect(jpg_deriv.height).to eq(2)
95
- expect(jpg_deriv.content_type).to eq("image/jpeg")
96
- end
97
-
98
- it "deletes derivative file returned by block" do
99
- asset.create_derivatives
100
-
101
- expect(File.exist?(Kithe::Engine.root.join("spec/test_support/images/2x2_pixel-TEMP.jpg"))).not_to be(true)
102
- end
103
-
104
- it "by default saves in :kithe_derivatives storage" do
105
- asset.create_derivatives
106
-
107
- jpg_deriv = asset.derivatives.find {|d| d.key == "a_jpg"}
108
- expect(jpg_deriv.file.storage_key).to eq(:kithe_derivatives)
109
- end
110
-
111
-
112
- describe "block arguments" do
113
- let(:monitoring_proc) do
114
- proc do |original_file, record:|
115
- expect(original_file.kind_of?(File) || original_file.kind_of?(Tempfile)).to be(true)
116
- expect(original_file.path).to be_present
117
- expect(original_file.read).to eq(asset.file.read)
118
-
119
- expect(record).to eq(asset)
120
-
121
- nil
122
- end
123
- end
124
-
125
- temporary_class("TestAssetSubclass") do
126
- our_proc = monitoring_proc
127
- Class.new(Kithe::Asset) do
128
- define_derivative(:some_data, &our_proc)
129
- end
130
- end
131
-
132
- it "as expected" do
133
- expect(monitoring_proc).to receive(:call).and_call_original
134
-
135
- asset.create_derivatives
136
- expect(asset.derivatives.length).to eq(0)
137
- end
138
- end
139
-
140
- describe "custom storage_key" do
141
- temporary_class("TestAssetSubclass") do
142
- Class.new(Kithe::Asset) do
143
- define_derivative(:some_data, storage_key: :store) do |original_file|
144
- StringIO.new("some one data")
145
- end
146
- end
147
- end
148
- it "saves appropriately" do
149
- asset.create_derivatives
150
-
151
- deriv = asset.derivatives.first
152
-
153
- expect(deriv).to be_present
154
- expect(deriv.file.storage_key).to eq(:store)
155
- end
156
- end
157
-
158
- describe "default_create false" do
159
- let(:monitoring_proc) { proc { |asset| } }
160
-
161
- temporary_class("TestAssetSubclass") do
162
- p = monitoring_proc
163
- Class.new(Kithe::Asset) do
164
- define_derivative(:some_data, default_create: false, &p)
165
- end
166
- end
167
-
168
- it "is not run automatically" do
169
- expect(monitoring_proc).not_to receive(:call)
170
- asset.create_derivatives
171
- end
172
- end
173
-
174
- describe "only/except" do
175
- let(:monitoring_proc1) { proc { |asset| StringIO.new("one") } }
176
- let(:monitoring_proc2) { proc { |asset| StringIO.new("two") } }
177
- let(:monitoring_proc3) { proc { |asset| StringIO.new("three") } }
178
-
179
- temporary_class("TestAssetSubclass") do
180
- p1, p2, p3 = monitoring_proc1, monitoring_proc2, monitoring_proc3
181
- Class.new(Kithe::Asset) do
182
- define_derivative(:one, default_create: false, &p1)
183
- define_derivative(:two, &p2)
184
- define_derivative(:three, &p3)
185
- end
186
- end
187
-
188
- it "can call with only" do
189
- expect(monitoring_proc1).to receive(:call).and_call_original
190
- expect(monitoring_proc2).to receive(:call).and_call_original
191
- expect(monitoring_proc3).not_to receive(:call)
192
-
193
- asset.create_derivatives(only: [:one, :two])
194
-
195
- expect(asset.derivatives.collect(&:key)).to eq(["one", "two"])
196
- end
197
-
198
- it "can call with except" do
199
- expect(monitoring_proc1).not_to receive(:call)
200
- expect(monitoring_proc2).to receive(:call).and_call_original
201
- expect(monitoring_proc3).not_to receive(:call)
202
-
203
- asset.create_derivatives(except: [:three])
204
-
205
- expect(asset.derivatives.collect(&:key)).to eq(["two"])
206
- end
207
-
208
- it "can call with only and except" do
209
- expect(monitoring_proc1).to receive(:call).and_call_original
210
- expect(monitoring_proc2).not_to receive(:call)
211
- expect(monitoring_proc3).not_to receive(:call)
212
-
213
- asset.create_derivatives(only: [:one, :two], except: :two)
214
-
215
- expect(asset.derivatives.collect(&:key)).to eq(["one"])
216
- end
217
- end
218
-
219
- describe "content_type filters" do
220
- temporary_class("TestAssetSubclass") do
221
- Class.new(Kithe::Asset) do
222
- define_derivative(:never_called, content_type: "nothing/nothing") { |o| StringIO.new("never") }
223
- define_derivative(:gated_positive, content_type: "image/jpeg") { |o| StringIO.new("gated positive") }
224
- define_derivative(:gated_positive_main_type, content_type: "image") { |o| StringIO.new("gated positive") }
225
- end
226
- end
227
-
228
- it "does not call if content type does not match" do
229
- asset.create_derivatives
230
- expect(asset.derivatives.collect(&:key)).not_to include("never_called")
231
- end
232
-
233
- it "calls for exact content type match" do
234
- asset.create_derivatives
235
- expect(asset.derivatives.collect(&:key)).to include("gated_positive")
236
- end
237
-
238
- it "calls for main content type match" do
239
- asset.create_derivatives
240
- expect(asset.derivatives.collect(&:key)).to include("gated_positive_main_type")
241
- end
242
-
243
- describe "as array" do
244
- temporary_class("TestAssetSubclass") do
245
- Class.new(Kithe::Asset) do
246
- define_derivative(:never_called, content_type: ["nothing/nothing", "also/nothing"]) { |o| StringIO.new("never") }
247
- define_derivative(:gated_positive, content_type: ["image/jpeg", "something/else"]) { |o| StringIO.new("gated positive") }
248
- end
249
- end
250
- it "calls for one match" do
251
- asset.create_derivatives
252
- expect(asset.derivatives.collect(&:key)).to eq(["gated_positive"])
253
- end
254
- end
255
-
256
- describe "conflicting types" do
257
- let(:unfiltered) { proc { |asset| StringIO.new("unfiltered") } }
258
- let(:image) { proc { |asset| StringIO.new("image") } }
259
- let(:image_jpeg) { proc { |asset| StringIO.new("image/jpeg") } }
260
-
261
-
262
- temporary_class("TestAssetSubclass") do
263
- u, i, ij = unfiltered, image, image_jpeg
264
- Class.new(Kithe::Asset) do
265
- define_derivative(:key, &u)
266
- define_derivative(:key, content_type: "image/jpeg", &ij)
267
- define_derivative(:key, content_type: "image", &i)
268
- end
269
- end
270
-
271
- it "takes most specific" do
272
- expect(unfiltered).not_to receive(:call)
273
- expect(image).not_to receive(:call)
274
- expect(image_jpeg).to receive(:call).and_call_original
275
-
276
- asset.create_derivatives
277
- expect(asset.derivatives.count).to eq(1)
278
-
279
- deriv = asset.derivatives.first
280
- expect(deriv.key).to eq("key")
281
- expect(deriv.file.read). to eq("image/jpeg")
282
- end
283
- end
284
- end
285
-
286
- describe "lazy creation" do
287
- before do
288
- asset.class.derivative_definitions.collect(&:key).each do |key|
289
- asset.update_derivative(key, StringIO.new("#{key} original"))
290
- end
291
- end
292
-
293
- it "does not re-create" do
294
- derivatives_pre_creation = asset.derivatives.collect(&:attributes)
295
-
296
- asset.create_derivatives(lazy: true)
297
- derivatives_post_creation = asset.derivatives.reload.collect(&:attributes)
298
-
299
- expect(derivatives_post_creation).to eq(derivatives_pre_creation)
300
- end
301
- end
302
-
303
- describe "#remove_derivative_definition!" do
304
- it "can remove by string" do
305
- original_keys = TestAssetSubclass.defined_derivative_keys
306
- TestAssetSubclass.remove_derivative_definition!(original_keys.first.to_s)
307
- expect(TestAssetSubclass.defined_derivative_keys).to eq(original_keys.slice(1..original_keys.length))
308
- end
309
- it "can remove by symbol" do
310
- original_keys = TestAssetSubclass.defined_derivative_keys
311
- TestAssetSubclass.remove_derivative_definition!(original_keys.first.to_sym)
312
- expect(TestAssetSubclass.defined_derivative_keys).to eq(original_keys.slice(1..original_keys.length))
313
- end
314
- it "can remove multiple args" do
315
- original_keys = TestAssetSubclass.defined_derivative_keys
316
- TestAssetSubclass.remove_derivative_definition!(*original_keys)
317
- expect(TestAssetSubclass.defined_derivative_keys).to eq([])
318
- end
319
- end
320
- end