carrierwave-meta 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  module CarrierWave
2
2
  module ModelDelegateAttribute
3
3
  extend ::ActiveSupport::Concern
4
-
4
+
5
5
  module ClassMethods
6
6
  def model_delegate_attribute(attribute, default = nil)
7
7
  attr_accessor attribute
@@ -10,29 +10,39 @@ module CarrierWave
10
10
 
11
11
  var_name = :"@#{attribute}"
12
12
 
13
+ define_getter(attribute, var_name, default)
14
+ define_setter(attribute, var_name, default)
15
+ define_reset(attribute, default)
16
+ end
17
+
18
+ private
19
+ def define_getter(attribute, var_name, default)
13
20
  define_method :"#{attribute}" do
14
21
  model_accessor = model_getter_name(attribute)
15
22
  value = instance_variable_get(var_name)
16
- value ||= self.model.send(model_accessor) if self.model.present? && self.model.respond_to?(model_accessor)
23
+ value ||= model.send(model_accessor) if model.present? && model.respond_to?(model_accessor)
17
24
  value ||= default
18
25
  instance_variable_set(var_name, value)
19
26
  end
27
+ end
20
28
 
29
+ def define_setter(attribute, var_name, default)
21
30
  define_method :"#{attribute}=" do |value|
22
31
  model_accessor = model_getter_name(attribute)
23
32
  instance_variable_set(var_name, value)
24
- if self.model.present? && self.model.respond_to?(:"#{model_accessor}=") && !self.model.destroyed?
25
- self.model.send(:"#{model_accessor}=", value)
33
+ if model.present? && model.respond_to?(:"#{model_accessor}=") && !model.destroyed?
34
+ model.send(:"#{model_accessor}=", value)
26
35
  end
27
36
  end
37
+ end
28
38
 
39
+ def define_reset(attribute, default)
29
40
  define_method :"reset_#{attribute}" do
30
- self.send(:"#{attribute}=", default)
31
41
  send(:"#{attribute}=", default)
32
42
  end
33
43
  end
34
44
  end
35
-
45
+
36
46
  private
37
47
  def model_getter_name(attribute)
38
48
  name = []
@@ -1,5 +1,5 @@
1
1
  module Carrierwave
2
2
  module Meta
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
File without changes
File without changes
Binary file
Binary file
Binary file
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe CarrierWave::Meta::ActiveRecord do
4
+ let(:model) { TestComposedModel.new }
5
+ let(:model_with_image) do
6
+ model.tap do |m|
7
+ m.image = File.open('spec/fixtures/big.jpg')
8
+ m.save!
9
+ end
10
+ end
11
+
12
+ it "model's virtual meta attributes must exists" do
13
+ subject::ALLOWED.each do |name|
14
+ model.should respond_to(:"image_#{name}")
15
+ end
16
+ end
17
+
18
+ it "must assign model's virtual attributes and save meta column" do
19
+ subject::ALLOWED.each do |name|
20
+ model_with_image.send(:"image_#{name}").should_not be_blank
21
+ end
22
+
23
+ model_with_image.image_version_width.should_not be_blank
24
+ model_with_image.image_version_height.should_not be_blank
25
+
26
+ model_with_image.reload
27
+
28
+ model_with_image.image_version_width.should_not be_blank
29
+ model_with_image.image_version_height.should_not be_blank
30
+ end
31
+ end
@@ -2,118 +2,150 @@ require 'spec_helper'
2
2
  require 'mime/types'
3
3
 
4
4
  describe TestUploader do
5
- ORIGINAL_SIZE = [600, 277]
6
- VERSION_SIZE = [200, 200]
7
-
8
- let(:file) { File.open("spec/fixtures/big.jpg") }
9
- let(:corrupted_file) { File.open("spec/fixtures/corrupted.bmp") }
10
- let(:file_name) { File.basename(file.path) }
11
5
  let(:obj) { TestModel.new }
12
6
 
13
- def uploader_values_are_correct(uploader)
14
- uploader.width.should eq(ORIGINAL_SIZE[0])
15
- uploader.height.should eq(ORIGINAL_SIZE[1])
16
- uploader.image_size.should eq(ORIGINAL_SIZE)
17
- uploader.content_type.should eq(MIME::Types.type_for(file_name).first.to_s)
18
- uploader.file_size.should_not be_blank
19
- uploader.image_size_s.should eq(ORIGINAL_SIZE.join('x'))
20
-
21
- uploader.version.width.should eq(VERSION_SIZE[0])
22
- uploader.version.height.should eq(VERSION_SIZE[1])
23
- uploader.version.image_size.should eq(VERSION_SIZE)
24
- uploader.version.content_type.should eq(MIME::Types.type_for(file_name).first.to_s)
25
- uploader.version.file_size.should_not be_blank
26
- uploader.version.image_size_s.should eq(VERSION_SIZE.join('x'))
7
+ IMAGE_FILE_NAME = "spec/fixtures/big.jpg"
8
+ EPS_FILE_NAME = "spec/fixtures/sample.eps"
9
+ PDF_FILE_NAME = "spec/fixtures/sample.pdf"
10
+
11
+ FORMATS = {
12
+ image: {
13
+ original_size: [600, 277],
14
+ version_size: [200, 200],
15
+ file: File.open(IMAGE_FILE_NAME),
16
+ file_name: File.basename(IMAGE_FILE_NAME),
17
+ corrupted_file: File.open("spec/fixtures/corrupted.bmp"),
18
+ mime_type: MIME::Types.type_for(IMAGE_FILE_NAME).first.to_s
19
+ },
20
+ eps: {
21
+ original_size: [464, 205],
22
+ version_size: [200, 200],
23
+ file: File.open(EPS_FILE_NAME),
24
+ corrupted_file: File.open("spec/fixtures/corrupted.eps"),
25
+ file_name: File.basename(EPS_FILE_NAME),
26
+ mime_type: MIME::Types.type_for(EPS_FILE_NAME).first.to_s
27
+ },
28
+ pdf: {
29
+ original_size: [360, 360],
30
+ version_size: [200, 200],
31
+ file: File.open(PDF_FILE_NAME),
32
+ corrupted_file: File.open("spec/fixtures/corrupted.pdf"),
33
+ file_name: File.basename(PDF_FILE_NAME),
34
+ mime_type: MIME::Types.type_for(PDF_FILE_NAME).first.to_s
35
+ }
36
+ }
37
+
38
+ def obj_values_eq(obj, size_arg, args, prefix = nil)
39
+ size = args[size_arg]
40
+
41
+ obj.send(obj_value_name(:width, prefix)).should eq(size.first)
42
+ obj.send(obj_value_name(:height, prefix)).should eq(size.last)
43
+ obj.send(obj_value_name(:image_size, prefix)).should eq(size)
44
+ obj.send(obj_value_name(:content_type, prefix)).should eq(args[:mime_type])
45
+ obj.send(obj_value_name(:file_size, prefix)).should_not be_blank
46
+
47
+ if obj.is_a?(CarrierWave::Uploader::Base)
48
+ obj.send(obj_value_name(:image_size_s, prefix)).should eq(size.join('x'))
49
+ end
27
50
  end
28
51
 
29
- def obj_values_are_correct(obj)
30
- obj.image_width.should eq(ORIGINAL_SIZE[0])
31
- obj.image_height.should eq(ORIGINAL_SIZE[1])
32
- obj.image_image_size.should eq(ORIGINAL_SIZE)
33
- obj.image_content_type.should eq(MIME::Types.type_for(file_name).first.to_s)
34
- obj.image_file_size.should_not be_blank
35
-
36
- obj.image_version_width.should eq(VERSION_SIZE[0])
37
- obj.image_version_height.should eq(VERSION_SIZE[1])
38
- obj.image_version_image_size.should eq(VERSION_SIZE)
39
- obj.image_version_content_type.should eq(MIME::Types.type_for(file_name).first.to_s)
40
- obj.image_version_file_size.should_not be_blank
52
+ def obj_value_name(name, prefix)
53
+ [prefix, name].compact.join('_')
41
54
  end
42
55
 
43
- context "detached uploader" do
44
- it "stores metadata after cache!" do
45
- uploader = TestUploader.new
46
- uploader.cache!(file)
47
- uploader_values_are_correct(uploader)
48
- end
49
-
50
- it "stores metadata after store!" do
51
- uploader = TestUploader.new
52
- uploader.store!(file)
53
- uploader_values_are_correct(uploader)
54
- end
55
-
56
- it "has metadata after cache!/retrieve_from_cache!" do
57
- uploader = TestUploader.new
58
- uploader.cache!(file)
59
- cache_name = uploader.cache_name
60
-
61
- uploader = TestUploader.new
62
- uploader.retrieve_from_cache!(cache_name)
63
- uploader_values_are_correct(uploader)
64
- end
65
-
66
- it "has metadata after store!/retrieve_from_store!" do
67
- uploader = TestUploader.new
68
- uploader.store!(file)
69
- uploader_values_are_correct(uploader)
70
-
71
- uploader = TestUploader.new
72
- uploader.retrieve_from_store!(File.basename(file.path))
73
- uploader_values_are_correct(uploader)
74
- end
56
+ def uploader_values_eq(uploader, args)
57
+ obj_values_eq(uploader, :original_size, args)
58
+ obj_values_eq(uploader.version, :version_size, args)
59
+ uploader.md5sum.should_not be_blank
60
+ uploader.version.md5sum.should be_blank
75
61
  end
76
62
 
77
- context "attached uploader" do
78
- it "stores metadata after cache!" do
79
- uploader = TestUploader.new(obj, :image)
80
- uploader.cache!(file)
81
- uploader_values_are_correct(uploader)
82
- obj_values_are_correct(obj)
83
- end
84
-
85
- it "stores metadata after store!" do
86
- uploader = TestUploader.new(obj, :image)
87
- uploader.store!(file)
88
- uploader_values_are_correct(uploader)
89
- obj_values_are_correct(obj)
90
- end
91
-
92
- it "has metadata after cache!/retrieve_from_cache!" do
93
- uploader = TestUploader.new(obj, :image)
94
- uploader.cache!(file)
95
- cache_name = uploader.cache_name
63
+ def model_values_eq(model, args)
64
+ obj_values_eq(model, :original_size, args, 'image')
65
+ obj_values_eq(model, :version_size, args, 'image_version')
66
+ model.image_md5sum.should_not be_blank
67
+ end
96
68
 
97
- uploader = TestUploader.new(obj, :image)
98
- uploader.retrieve_from_cache!(cache_name)
99
- uploader_values_are_correct(uploader)
69
+ FORMATS.each do |format, args|
70
+ next if not(CarrierWave::Meta.ghostscript_enabled) and [:pdf, :eps].include?(format)
71
+ context "detached uploader for #{format}" do
72
+ it "stores metadata after cache!" do
73
+ uploader = TestUploader.new
74
+ uploader.cache!(args[:file])
75
+ uploader_values_eq(uploader, args)
76
+ end
77
+
78
+ it "stores metadata after store!" do
79
+ uploader = TestUploader.new
80
+ uploader.store!(args[:file])
81
+ uploader_values_eq(uploader, args)
82
+ end
83
+
84
+ it "has metadata after cache!/retrieve_from_cache!" do
85
+ uploader = TestUploader.new
86
+ uploader.cache!(args[:file])
87
+ cache_name = uploader.cache_name
88
+ uploader = TestUploader.new
89
+ uploader.retrieve_from_cache!(cache_name)
90
+
91
+ uploader_values_eq(uploader, args)
92
+ end
93
+
94
+ it "has metadata after store!/retrieve_from_store!" do
95
+ uploader = TestUploader.new
96
+ uploader.store!(args[:file])
97
+ uploader_values_eq(uploader, args)
98
+
99
+ uploader = TestUploader.new
100
+ uploader.retrieve_from_store!(args[:file_name])
101
+ uploader_values_eq(uploader, args)
102
+ end
100
103
  end
101
104
 
102
- it "has metadata after store!/retrieve_from_store!" do
103
- uploader = TestUploader.new(obj, :image)
104
- uploader.store!(file)
105
- uploader_values_are_correct(uploader)
106
-
107
- uploader = TestUploader.new(obj, :image)
108
- uploader.retrieve_from_store!(File.basename(file.path))
109
- uploader_values_are_correct(uploader)
105
+ context "attached uploader for #{format}" do
106
+ it "stores metadata after cache!" do
107
+ uploader = TestUploader.new(obj, :image)
108
+ uploader.cache!(args[:file])
109
+ uploader_values_eq(uploader, args)
110
+ model_values_eq(obj, args)
111
+ end
112
+
113
+ it "stores metadata after store!" do
114
+ uploader = TestUploader.new(obj, :image)
115
+ uploader.store!(args[:file])
116
+ uploader_values_eq(uploader, args)
117
+ model_values_eq(obj, args)
118
+ end
119
+
120
+ it "has metadata after cache!/retrieve_from_cache!" do
121
+ uploader = TestUploader.new(obj, :image)
122
+ uploader.cache!(args[:file])
123
+ cache_name = uploader.cache_name
124
+
125
+ uploader = TestUploader.new(obj, :image)
126
+ uploader.retrieve_from_cache!(cache_name)
127
+ uploader_values_eq(uploader, args)
128
+ model_values_eq(obj, args)
129
+ end
130
+
131
+ it "has metadata after store!/retrieve_from_store!" do
132
+ uploader = TestUploader.new(obj, :image)
133
+ uploader.store!(args[:file])
134
+ uploader_values_eq(uploader, args)
135
+ model_values_eq(obj, args)
136
+
137
+ uploader = TestUploader.new(obj, :image)
138
+ uploader.retrieve_from_store!(args[:file_name])
139
+ uploader_values_eq(uploader, args)
140
+ model_values_eq(obj, args)
141
+ end
110
142
  end
111
- end
112
143
 
113
- context "when file is corrupted/can not be processed" do
114
- it "passes without exceptions" do
115
- uploader = TestBlankUploader.new(obj, :image)
116
- proc { uploader.store!(corrupted_file) }.should_not raise_error
144
+ context "when file is corrupted/can not be processed" do
145
+ it "passes without exceptions" do
146
+ uploader = TestBlankUploader.new(obj, :image)
147
+ proc { uploader.store!(args[:corrupted_file]) }.should_not raise_error
148
+ end
117
149
  end
118
150
  end
119
151
  end
@@ -2,35 +2,35 @@ require 'spec_helper'
2
2
 
3
3
  describe TestDelegateUploader do
4
4
  let(:values) { subject.class::VALUES }
5
- let(:default_values) { subject.class::DEFAULT_VALUES }
5
+ let(:default_values) { subject.class::DEFAULT_VALUES }
6
6
  let(:file) { File.open("spec/fixtures/big.jpg") }
7
7
  let(:obj) { TestModel.new }
8
-
8
+
9
9
  before do
10
10
  obj.destroyed = false
11
11
  end
12
12
 
13
13
  def uploader_values_are_correct(uploader)
14
14
  uploader.x.should eq(values[:x])
15
- uploader.dimensions.should eq(values[:dimensions])
15
+ uploader.dimensions.should eq(values[:dimensions])
16
16
  uploader.version.x.should eq(values[:version_x])
17
- uploader.version.dimensions.should eq(values[:version_dimensions])
17
+ uploader.version.dimensions.should eq(values[:version_dimensions])
18
18
  end
19
19
 
20
20
  def obj_values_are_correct(obj)
21
21
  obj.image_x.should eq(values[:x])
22
- obj.image_dimensions.should eq(values[:dimensions])
22
+ obj.image_dimensions.should eq(values[:dimensions])
23
23
  obj.image_version_x.should eq(values[:version_x])
24
- obj.image_version_dimensions.should eq(values[:version_dimensions])
24
+ obj.image_version_dimensions.should eq(values[:version_dimensions])
25
25
  end
26
26
 
27
27
  def uploader_values_are_default(obj)
28
28
  obj.x.should eq(default_values[:x])
29
- obj.dimensions.should eq(default_values[:dimensions])
29
+ obj.dimensions.should eq(default_values[:dimensions])
30
30
  obj.version.x.should eq(default_values[:x])
31
- obj.version.dimensions.should eq(default_values[:dimensions])
31
+ obj.version.dimensions.should eq(default_values[:dimensions])
32
32
  end
33
-
33
+
34
34
  it "model receives values after process done" do
35
35
  uploader = TestDelegateUploader.new(obj, :image)
36
36
  uploader.cache!(file)
@@ -38,15 +38,15 @@ describe TestDelegateUploader do
38
38
  uploader_values_are_correct(uploader)
39
39
  obj_values_are_correct(obj)
40
40
  end
41
-
41
+
42
42
  it "resets values to default after file is removed" do
43
43
  uploader = TestDelegateUploader.new(obj, :image)
44
44
  uploader.store!(file)
45
45
 
46
46
  uploader_values_are_correct(uploader)
47
47
 
48
- # CarrierWave's file is removed in after_destroy callback. Model attributes are freezed after destroy.
49
- # Let mock this case.
48
+ # CarrierWave's file is removed in after_destroy callback.
49
+ # Model attributes are freezed after destroy. Let mock this case.
50
50
  obj.image_x.freeze
51
51
  obj.destroyed = true
52
52
  uploader.remove!
@@ -54,7 +54,7 @@ describe TestDelegateUploader do
54
54
  uploader_values_are_default(uploader)
55
55
  obj_values_are_correct(obj)
56
56
  end
57
-
57
+
58
58
  it "tries to receive value from a model when file was not processed at this time" do
59
59
  uploader = TestDelegateUploader.new(obj, :image)
60
60
  uploader.store!(file)
@@ -1,23 +1,34 @@
1
1
  $LOAD_PATH << "." unless $LOAD_PATH.include?(".")
2
2
 
3
- begin
4
- require "bundler"
5
- Bundler.setup
6
- Bundler.require
7
- rescue Bundler::GemNotFound
8
- raise RuntimeError, "Bundler couldn't find some gems." +
9
- "Did you run `bundle install`?"
10
- end
11
-
12
- PROCESSOR = (ENV["PROCESSOR"] || :rmagick).to_sym
13
-
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ require 'simplecov'
14
6
  require 'mime/types'
15
7
  require 'carrierwave'
8
+ require 'carrierwave/orm/activerecord'
9
+ require 'support/remote'
10
+ require 'support/current_processor'
11
+ require 'active_record'
12
+ require 'support/schema'
13
+
14
+ SimpleCov.start
15
+
16
16
  require 'carrierwave-meta'
17
+
18
+ PROCESSOR = (ENV["PROCESSOR"] || :rmagick).to_sym
19
+ puts "Using #{PROCESSOR} processor"
20
+
21
+ PDF_EPS = ENV['PDF_EPS']
22
+ unless PDF_EPS == 'false' or [:vips, :image_sorcery].include?(PROCESSOR)
23
+ CarrierWave::Meta.ghostscript_enabled = true
24
+ end
25
+
17
26
  require 'support/test_delegate_uploader'
18
27
  require 'support/test_blank_uploader'
19
28
  require 'support/test_uploader'
20
29
  require 'support/test_model'
30
+ require 'support/test_composed_model'
31
+ require 'fog'
21
32
 
22
33
  RSpec.configure do |config|
23
34
  config.before do