attachy 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +15 -13
- data/lib/assets/javascripts/attachy.js +7 -1
- data/lib/attachy/models/attachy/file.rb +3 -1
- data/lib/attachy/models/attachy/viewer.rb +5 -3
- data/lib/attachy/version.rb +1 -1
- data/spec/models/attachy/extension/user/avatar_spec.rb +1 -7
- data/spec/models/attachy/extension/user/photos_spec.rb +2 -4
- data/spec/models/attachy/file/config_spec.rb +15 -4
- data/spec/models/attachy/file/default_spec.rb +8 -0
- data/spec/models/attachy/viewer/attachments_spec.rb +33 -11
- data/spec/models/attachy/viewer/image_spec.rb +7 -1
- data/spec/models/attachy/viewer/value_spec.rb +38 -14
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b17baca7030f6514b35cd20a7e1c2eb95127ebe
|
4
|
+
data.tar.gz: 7bdd58b491d44169f5060283665da063f916a449
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f14ee66ab85811c760a75e9f19662751245403bb37ed83b7d9374696c5c41c992328e4f5a789bbed897c7e331bcd7d734f376fd28cd6369f40f9bed515492c6
|
7
|
+
data.tar.gz: 164c7b2ba9bead29c25a392c249fd551487a876f7b8256b9fd507364a74c025521547e1b1c08719f2aff4022f2fc580e380ce72e65d09a2bcef1fafe55d92494
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -19,8 +19,8 @@ Add the following code on your `Gemfile` and run `bundle install`:
|
|
19
19
|
gem 'attachy'
|
20
20
|
```
|
21
21
|
|
22
|
-
So, extracts the necessary files including a migrate that create a table used
|
23
|
-
|
22
|
+
So, extracts the necessary files including a migrate that create a table used to keep your file metadata.
|
23
|
+
You can choose a [Default Image](#default-image) via params `version`, `public_id` and `format`.
|
24
24
|
|
25
25
|
```bash
|
26
26
|
rails g attachy:install
|
@@ -86,10 +86,12 @@ Includes the `attachy.sass` on your css manifest:
|
|
86
86
|
|
87
87
|
On your `attachy.yml` you can configure a default image to show when model has no file attached:
|
88
88
|
|
89
|
-
```
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
```yml
|
90
|
+
default:
|
91
|
+
image:
|
92
|
+
format: jpg
|
93
|
+
public_id: default
|
94
|
+
version: 42
|
93
95
|
```
|
94
96
|
|
95
97
|
## Transformations
|
@@ -100,7 +102,7 @@ To know more about transformations, check the [Cloudinary Doc](http://cloudinary
|
|
100
102
|
|
101
103
|
### Attachy
|
102
104
|
|
103
|
-
```
|
105
|
+
```html
|
104
106
|
<%= f.attachy :avatar,
|
105
107
|
t: { width: 160, height: 160, crop: :fill },
|
106
108
|
tl: { width: 800, height: 600, crop: :scale },
|
@@ -112,7 +114,7 @@ To know more about transformations, check the [Cloudinary Doc](http://cloudinary
|
|
112
114
|
+ `tl`: linked image transformations;
|
113
115
|
+ `button.html`: button html attributes.
|
114
116
|
|
115
|
-
```
|
117
|
+
```html
|
116
118
|
<div class="attachy">
|
117
119
|
<ul class="attachy__content">
|
118
120
|
<li class="attachy__node">
|
@@ -149,7 +151,7 @@ To know more about transformations, check the [Cloudinary Doc](http://cloudinary
|
|
149
151
|
|
150
152
|
It draws the link with the image inside:
|
151
153
|
|
152
|
-
```
|
154
|
+
```html
|
153
155
|
<%= attachy_link :avatar, @object
|
154
156
|
t: { width: 160, height: 160, crop: :fill },
|
155
157
|
tl: { width: 800, height: 600, crop: :scale },
|
@@ -161,7 +163,7 @@ It draws the link with the image inside:
|
|
161
163
|
+ `tl`: linked image transformations;
|
162
164
|
+ `html`: link html attributes.
|
163
165
|
|
164
|
-
```
|
166
|
+
```html
|
165
167
|
<a class="attachy__link" href="">
|
166
168
|
<img src="">
|
167
169
|
</a>
|
@@ -169,9 +171,9 @@ It draws the link with the image inside:
|
|
169
171
|
|
170
172
|
### Image
|
171
173
|
|
172
|
-
It draws the
|
174
|
+
It draws the image:
|
173
175
|
|
174
|
-
```
|
176
|
+
```html
|
175
177
|
<%= attachy_image :avatar, @object
|
176
178
|
t: { width: 160, height: 160, crop: :fill },
|
177
179
|
html: { alt: :me }
|
@@ -181,7 +183,7 @@ It draws the link with the image inside:
|
|
181
183
|
+ `t`: image transformations;
|
182
184
|
+ `html`: link html attributes.
|
183
185
|
|
184
|
-
```
|
186
|
+
```html
|
185
187
|
<img src="https://res.cloudinary.com/account/image/upload/secret/version/hash.format">
|
186
188
|
```
|
187
189
|
|
@@ -116,7 +116,13 @@
|
|
116
116
|
|
117
117
|
that.content.append(node);
|
118
118
|
} else {
|
119
|
-
that.content.find('li')
|
119
|
+
var item = that.content.find('li');
|
120
|
+
console.log(item.length);
|
121
|
+
if (!item.length) {
|
122
|
+
item = $('<li />', { html: link + remove, 'class': 'attachy__node' }).appendTo(that.content);
|
123
|
+
}
|
124
|
+
|
125
|
+
item.empty().append(link, remove);
|
120
126
|
}
|
121
127
|
});
|
122
128
|
},
|
@@ -37,10 +37,12 @@ module Attachy
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def self.config
|
40
|
-
::Rails.application
|
40
|
+
::Rails.application&.config_for :attachy
|
41
41
|
end
|
42
42
|
|
43
43
|
def self.default
|
44
|
+
return if config.nil?
|
45
|
+
|
44
46
|
image = config.dig('default', 'image')
|
45
47
|
|
46
48
|
return if image.nil?
|
@@ -12,7 +12,7 @@ module Attachy
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def attachments
|
15
|
-
[criteria].flatten
|
15
|
+
[criteria].flatten.compact
|
16
16
|
end
|
17
17
|
|
18
18
|
def button_label_options
|
@@ -93,6 +93,8 @@ module Attachy
|
|
93
93
|
end
|
94
94
|
|
95
95
|
def image(file = criteria, t: transform, html: htm)
|
96
|
+
return if file.nil?
|
97
|
+
|
96
98
|
url = file.url(t)
|
97
99
|
html = html.reverse_merge(height: t[:height], width: t[:width])
|
98
100
|
html[:data] = file.transform(t)
|
@@ -144,7 +146,7 @@ module Attachy
|
|
144
146
|
end
|
145
147
|
|
146
148
|
def value
|
147
|
-
|
149
|
+
default? ? '[]' : attachments.to_json
|
148
150
|
end
|
149
151
|
|
150
152
|
private
|
@@ -154,7 +156,7 @@ module Attachy
|
|
154
156
|
end
|
155
157
|
|
156
158
|
def default?
|
157
|
-
attachments.size == 1 && attachments.last.public_id == Attachy::File.default
|
159
|
+
attachments.size == 1 && attachments.last.public_id == Attachy::File.default&.public_id
|
158
160
|
end
|
159
161
|
|
160
162
|
def htm(path = [])
|
data/lib/attachy/version.rb
CHANGED
@@ -50,13 +50,7 @@ RSpec.describe User, ':avatar' do
|
|
50
50
|
let!(:user) { create :user }
|
51
51
|
|
52
52
|
context 'with no file' do
|
53
|
-
|
54
|
-
allow(Attachy::File).to receive(:default) { :default }
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'returns a default file' do
|
58
|
-
expect(user.avatar).to eq :default
|
59
|
-
end
|
53
|
+
specify { expect(user.avatar).to eq nil }
|
60
54
|
end
|
61
55
|
|
62
56
|
context 'with file' do
|
@@ -50,12 +50,10 @@ RSpec.describe User, ':photos' do
|
|
50
50
|
let!(:user) { create :user }
|
51
51
|
|
52
52
|
context 'with no file' do
|
53
|
-
|
54
|
-
expect(user.photos).to eq []
|
55
|
-
end
|
53
|
+
specify { expect(user.photos).to eq [] }
|
56
54
|
end
|
57
55
|
|
58
|
-
context 'with
|
56
|
+
context 'with files' do
|
59
57
|
let!(:photo_1) { create :file, scope: :photos, attachable: user }
|
60
58
|
let!(:photo_2) { create :file, scope: :photos, attachable: user }
|
61
59
|
|
@@ -3,11 +3,22 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Attachy::File, '#config' do
|
6
|
-
|
7
|
-
|
6
|
+
context 'when config does not exist' do
|
7
|
+
it 'returns the config data' do
|
8
|
+
expect(described_class.config).to eq nil
|
9
|
+
end
|
8
10
|
end
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
+
context 'when config exists' do
|
13
|
+
let!(:application) { double }
|
14
|
+
|
15
|
+
before do
|
16
|
+
allow(Rails).to receive(:application) { application }
|
17
|
+
allow(application).to receive(:config_for).with(:attachy) { :config }
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns the config data' do
|
21
|
+
expect(described_class.config).to eq :config
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
@@ -3,6 +3,14 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Attachy::File, '#default' do
|
6
|
+
context 'when config is missing' do
|
7
|
+
before do
|
8
|
+
allow(::Rails).to receive_message_chain(:application, :config_for) { nil }
|
9
|
+
end
|
10
|
+
|
11
|
+
specify { expect(described_class.default).to eq nil }
|
12
|
+
end
|
13
|
+
|
6
14
|
context 'when default image is present' do
|
7
15
|
before do
|
8
16
|
allow(::Rails).to receive_message_chain(:application, :config_for).with(:attachy) do
|
@@ -3,28 +3,50 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Attachy::Viewer, '.attachments' do
|
6
|
-
subject { described_class.new method, object }
|
7
|
-
|
8
6
|
let!(:object) { create :user }
|
9
7
|
|
10
8
|
before { allow(Cloudinary::Uploader).to receive(:remove_tag) }
|
11
9
|
|
12
|
-
context 'with
|
10
|
+
context 'with no files' do
|
11
|
+
subject { described_class.new :avatar, object }
|
12
|
+
|
13
|
+
specify { expect(subject.attachments).to eq [] }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when is has one' do
|
17
|
+
subject { described_class.new method, object }
|
18
|
+
|
13
19
|
let!(:method) { :avatar }
|
14
|
-
let!(:file) { create :file, attachable: object, scope: method }
|
15
20
|
|
16
|
-
|
17
|
-
expect(subject.attachments).to eq [
|
21
|
+
context 'and has no files' do
|
22
|
+
specify { expect(subject.attachments).to eq [] }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'and has file' do
|
26
|
+
let!(:file) { create :file, attachable: object, scope: method }
|
27
|
+
|
28
|
+
it 'returns an array with this file' do
|
29
|
+
expect(subject.attachments).to eq [file]
|
30
|
+
end
|
18
31
|
end
|
19
32
|
end
|
20
33
|
|
21
|
-
context '
|
34
|
+
context 'when is has many' do
|
35
|
+
subject { described_class.new method, object }
|
36
|
+
|
22
37
|
let!(:method) { :photos }
|
23
|
-
let!(:file_1) { create :file, attachable: object, scope: method }
|
24
|
-
let!(:file_2) { create :file, attachable: object, scope: method }
|
25
38
|
|
26
|
-
|
27
|
-
expect(subject.attachments).to eq [
|
39
|
+
context 'and has no files' do
|
40
|
+
specify { expect(subject.attachments).to eq [] }
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'and has files' do
|
44
|
+
let!(:file_1) { create :file, attachable: object, scope: method }
|
45
|
+
let!(:file_2) { create :file, attachable: object, scope: method }
|
46
|
+
|
47
|
+
it 'returns an array with this files' do
|
48
|
+
expect(subject.attachments).to eq [file_1, file_2]
|
49
|
+
end
|
28
50
|
end
|
29
51
|
end
|
30
52
|
end
|
@@ -161,7 +161,13 @@ RSpec.describe Attachy::Viewer, '.image' do
|
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
164
|
-
context 'when
|
164
|
+
context 'when :file is nil' do
|
165
|
+
let!(:file) { nil }
|
166
|
+
|
167
|
+
specify { expect(subject.image(file)).to eq nil }
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when :file is present' do
|
165
171
|
let!(:file) { build :file, format: :png, version: 7 }
|
166
172
|
|
167
173
|
it 'is used' do
|
@@ -7,6 +7,22 @@ RSpec.describe Attachy::Viewer, '.value' do
|
|
7
7
|
|
8
8
|
before { allow(Cloudinary::Uploader).to receive(:remove_tag) }
|
9
9
|
|
10
|
+
context 'when has no attachments' do
|
11
|
+
subject { described_class.new method, object }
|
12
|
+
|
13
|
+
context 'as attachament' do
|
14
|
+
let!(:method) { :avatar }
|
15
|
+
|
16
|
+
specify { expect(subject.value).to eq '[]' }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'as attachaments' do
|
20
|
+
let!(:method) { :photos }
|
21
|
+
|
22
|
+
specify { expect(subject.value).to eq '[]' }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
10
26
|
context 'when has one result' do
|
11
27
|
context 'as attachament' do
|
12
28
|
subject { described_class.new method, object }
|
@@ -14,27 +30,35 @@ RSpec.describe Attachy::Viewer, '.value' do
|
|
14
30
|
let!(:method) { :avatar }
|
15
31
|
let!(:file) { create :file, attachable: object, scope: method }
|
16
32
|
|
17
|
-
context '
|
18
|
-
|
19
|
-
|
20
|
-
before do
|
21
|
-
allow(Attachy::File).to receive(:default) { default }
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'is represented as an array of one json' do
|
33
|
+
context 'with no default image configured' do
|
34
|
+
it 'does not gives error trying to look :public_id and returns the file' do
|
25
35
|
expect(subject.value).to eq [file].to_json
|
26
36
|
end
|
27
37
|
end
|
28
38
|
|
29
|
-
context '
|
30
|
-
let!(:default) { Attachy::File.new public_id:
|
39
|
+
context 'with default image configured' do
|
40
|
+
let!(:default) { Attachy::File.new public_id: 0 }
|
31
41
|
|
32
|
-
|
33
|
-
|
42
|
+
context 'and it is not default' do
|
43
|
+
before do
|
44
|
+
allow(Attachy::File).to receive(:default) { default }
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'is represented as an array of one json' do
|
48
|
+
expect(subject.value).to eq [file].to_json
|
49
|
+
end
|
34
50
|
end
|
35
51
|
|
36
|
-
|
37
|
-
|
52
|
+
context 'and it is default' do
|
53
|
+
let!(:default) { Attachy::File.new public_id: file.public_id }
|
54
|
+
|
55
|
+
before do
|
56
|
+
allow(Attachy::File).to receive(:default) { default }
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'ignores the virtual default value and returns an empty data' do
|
60
|
+
expect(subject.value).to eq '[]'
|
61
|
+
end
|
38
62
|
end
|
39
63
|
end
|
40
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attachy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Washington Botelho
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-10-
|
11
|
+
date: 2017-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cloudinary
|