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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba799d1c72c52b5e44034d297788610c44d4f123
4
- data.tar.gz: 5797413a3e7f89ccdfb541e0852527c5f40d86aa
3
+ metadata.gz: 9b17baca7030f6514b35cd20a7e1c2eb95127ebe
4
+ data.tar.gz: 7bdd58b491d44169f5060283665da063f916a449
5
5
  SHA512:
6
- metadata.gz: 594a1d513a5f0b412d326915b990a668f4b119f3cf6bde2d038aae588190bab585285cf92cb77e6508b6f075c69ac2493b922e848b4ac7a9a7dd71afd760e276
7
- data.tar.gz: 924c8edbedcf1091852f4aa227e8b91524627d9f43580781a41481d35bfaae30a47aa342d4932deb978830487c9224d70454b57981653fe9b6b7f8436b266272
6
+ metadata.gz: 7f14ee66ab85811c760a75e9f19662751245403bb37ed83b7d9374696c5c41c992328e4f5a789bbed897c7e331bcd7d734f376fd28cd6369f40f9bed515492c6
7
+ data.tar.gz: 164c7b2ba9bead29c25a392c249fd551487a876f7b8256b9fd507364a74c025521547e1b1c08719f2aff4022f2fc580e380ce72e65d09a2bcef1fafe55d92494
@@ -1,3 +1,7 @@
1
+ # 0.4.1
2
+
3
+ - Shows all element but image when it does not exists.
4
+
1
5
  # 0.4.0
2
6
 
3
7
  - Default image is returned only if you declare it on `attacy.yml`.
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
- to keep your file metadata. You can choose a [Default Image](#default-image) via params 'version', `public_id` and 'format'.
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
- ```js
90
- format: jpg
91
- public_id: default
92
- version: 42
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 link with the image inside:
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').empty().append(link, remove);
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.config_for :attachy
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
- [(default? ? [] : criteria)].flatten.to_json
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.public_id
159
+ attachments.size == 1 && attachments.last.public_id == Attachy::File.default&.public_id
158
160
  end
159
161
 
160
162
  def htm(path = [])
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Attachy
4
- VERSION = '0.4.0'.freeze
4
+ VERSION = '0.4.1'
5
5
  end
@@ -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
- before do
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
- it 'returns empty' do
54
- expect(user.photos).to eq []
55
- end
53
+ specify { expect(user.photos).to eq [] }
56
54
  end
57
55
 
58
- context 'with file' do
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
- before do
7
- allow(Rails.application).to receive(:config_for).with(:attachy) { :config }
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
- it 'returns the config data' do
11
- expect(described_class.config).to eq :config
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 one file' do
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
- it 'returns an array' do
17
- expect(subject.attachments).to eq [file]
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 'with more than one files' do
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
- it 'returns an array' do
27
- expect(subject.attachments).to eq [file_1, file_2]
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 given a :file' do
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 'and it is not default' do
18
- let!(:default) { Attachy::File.new public_id: 0 }
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 'and it is default' do
30
- let!(:default) { Attachy::File.new public_id: file.public_id }
39
+ context 'with default image configured' do
40
+ let!(:default) { Attachy::File.new public_id: 0 }
31
41
 
32
- before do
33
- allow(Attachy::File).to receive(:default) { default }
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
- it 'ignores the virtual default value and returns an empty data' do
37
- expect(subject.value).to eq '[]'
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.0
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-19 00:00:00.000000000 Z
11
+ date: 2017-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cloudinary