attachy 0.1.0 → 0.1.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: a405801b1f0c5c745e3723b97320273d1768ba2f
4
- data.tar.gz: 3f2174490f3404f21a3d3f4adefe663f28789aa4
3
+ metadata.gz: c10bc66de882f1201aba52a87e64c5ac48e46a34
4
+ data.tar.gz: fc2c4995faa82011745592e90dc0ab9387571a05
5
5
  SHA512:
6
- metadata.gz: 33f314c8a0bc058f7ea0f315296d8088a77ae1c256114e8a964cd70f7748c018fad658095bcc0dae82d7bbbeed789cfed0ff42b8dfa102756b4238768387344e
7
- data.tar.gz: cc7264176e05f9947037cf0fe79914738dcb391276fb1395b8f3a63417a565ae76df49ee1b84ea7ae2d593c537c58695bd7861263473d54f2054ac9fc4956d50
6
+ metadata.gz: 611a2c9b85a28674a0a026719bc743bbdc37774502ce87c3676732e651a60b87ca6463abb74c6334cf0ad3a3c03f9d0b83898b35f7ae2bbb023569cc0772bb3e
7
+ data.tar.gz: ab8e16ae400ee56ec645d18acd1cdbd52e9ce3ceffb3f85ac2e4f8f50306e86ffa19278afb04021228e8b58caf623ae1d4d4fe3904e16ba026c945f6b9dbdd3e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.1.1
2
+
3
+ - When a blank value is assigned on attachment relation, it is skipped to avoid error on JSON parser.
4
+
1
5
  # 0.1.0
2
6
 
3
7
  - Release.
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Attachy
2
2
 
3
- Attachments handler for Rails via Cloudinary
3
+ Attachments handler for Rails via http://cloudinary.com
4
4
 
5
- ## Desciption
5
+ ## Description
6
6
 
7
7
  Send files from your browser directly to Cloudinary.
8
8
  Attachy will generate a `has_one` or `has_many` files (photos) to your model
@@ -48,42 +48,34 @@ end
48
48
  Expose your Cloudinary credentials on your layout:
49
49
 
50
50
  ```html
51
- <%= cloudinary_js_config %>s
51
+ <%= cloudinary_js_config %>
52
52
  ```
53
53
 
54
- Into your form the upload field:
54
+ Into your form, add the upload field:
55
55
 
56
56
  ```html
57
57
  <%= f.attachy :avatar %>
58
58
  ```
59
59
 
60
- If you want just show your uploaded image, use:
61
-
62
- ```html
63
- <%= attachy_link :avatar, @object %>
64
- ```
65
-
66
- It will generate a link to your image with the image inside.
67
-
68
60
  ### Assets
69
61
 
70
62
  Includes the `attachy.js` on your js manifest:
71
63
 
72
64
  ```js
73
- //= require vendor/attachy
65
+ //= require attachy
74
66
  ```
75
67
 
76
68
  Includes the `attachy.sass` on your css manifest:
77
69
 
78
70
  ```js
79
71
  /*
80
- *= require vendor/attachy
72
+ *= require attachy
81
73
  */
82
74
  ```
83
75
 
84
76
  ### <a name="default-image"></a> Configurations
85
77
 
86
- On your `attachy.yml` configure a default image to show when model has no one:
78
+ On your `attachy.yml` you can configure a default image to show when model has no file attached:
87
79
 
88
80
  ```js
89
81
  format: jpg
@@ -91,6 +83,34 @@ public_id: default
91
83
  version: 42
92
84
  ```
93
85
 
86
+ ## Showing
87
+
88
+ If you want just show your uploaded image, use:
89
+
90
+ ```html
91
+ <%= attachy_link :avatar, @object %>
92
+ ```
93
+
94
+ It will generate a link to your image with the image inside.
95
+
96
+ ## Transformation
97
+
98
+ You can manipulate the image using the `t` attribute:
99
+
100
+ ```
101
+ <%= f.attachy :avatar, t: { width: 160, height: 160, crop: :fill } %>
102
+ ```
103
+
104
+ To know more about transformations, check the [Cloudinary Doc](http://cloudinary.com/documentation/image_transformations).
105
+
106
+ ## HTML
107
+
108
+ For HTML attributes, just use `html`:
109
+
110
+ ```
111
+ <%= f.attachy :avatar, html: { width: 160, height: 160, alt: 'Image' } %>
112
+ ```
113
+
94
114
  ## Test
95
115
 
96
116
  Before send pull request, check if specs is passing.
@@ -5,7 +5,7 @@
5
5
  *
6
6
  * @author : Washington Botelho
7
7
  * @doc : http://wbotelhos.com/attachy
8
- * @version : 0.1.0
8
+ * @version : 0.1.1
9
9
  *
10
10
  */
11
11
 
@@ -57,6 +57,8 @@ module Attachy
57
57
  end
58
58
 
59
59
  define_method "#{scope}=" do |data|
60
+ return if data.blank?
61
+
60
62
  attachies = attachies_for(data, scope)
61
63
 
62
64
  if attachies.present?
@@ -1,3 +1,3 @@
1
1
  module Attachy
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.1.1'.freeze
3
3
  end
@@ -17,22 +17,26 @@ RSpec.describe User, ':avatar' do
17
17
  end
18
18
 
19
19
  describe ':avatar_files=' do
20
- let!(:user) { create :user }
21
- let!(:avatar_1) { create :file, scope: :avatar }
22
- let!(:avatar_2) { create :file, scope: :avatar }
20
+ let!(:user) { create :user }
23
21
 
24
22
  context 'when given records' do
25
- before { user.avatar_files = [avatar_1, avatar_2] }
23
+ context 'already persisted' do
24
+ let!(:avatar_1) { create :file, scope: :avatar }
25
+ let!(:avatar_2) { create :file, scope: :avatar }
26
+
27
+ before { user.avatar_files = [avatar_1, avatar_2] }
26
28
 
27
- it 'is saved' do
28
- expect(user.avatar_files).to match_array [avatar_1, avatar_2]
29
+ it 'references all' do
30
+ expect(user.avatar_files).to match_array [avatar_1, avatar_2]
31
+ end
29
32
  end
30
33
  end
31
34
 
32
35
  context 'when given no records' do
33
- before { user.avatar_files = [avatar_1, avatar_2] }
36
+ let!(:avatar_1) { create :file, scope: :avatar, attachable: user }
37
+ let!(:avatar_2) { create :file, scope: :avatar, attachable: user }
34
38
 
35
- it 'clears the existents' do
39
+ it 'clears all' do
36
40
  user.avatar_files = []
37
41
 
38
42
  expect(user.avatar_files).to eq []
@@ -63,6 +67,114 @@ RSpec.describe User, ':avatar' do
63
67
  end
64
68
  end
65
69
 
70
+ describe ':avatar=' do
71
+ let!(:user) { create :user }
72
+
73
+ context 'with persisted record' do
74
+ let!(:avatar) { create :file, scope: :avatar }
75
+
76
+ context 'as a json' do
77
+ let!(:json) { avatar.to_json }
78
+
79
+ it 'does not creates a new one' do
80
+ user.avatar = json
81
+
82
+ expect { user.avatar_files }.to_not change(Attachy::File, :count)
83
+ end
84
+
85
+ it 'is referenced' do
86
+ user.avatar = json
87
+
88
+ expect(user.avatar_files).to eq [avatar]
89
+ end
90
+ end
91
+
92
+ context 'as an array of json' do
93
+ let!(:json) { [avatar].to_json }
94
+
95
+ it 'does not creates a new one' do
96
+ user.avatar = json
97
+
98
+ expect { user.avatar_files }.to_not change(Attachy::File, :count)
99
+ end
100
+
101
+ it 'is referenced' do
102
+ user.avatar = json
103
+
104
+ expect(user.avatar_files).to eq [avatar]
105
+ end
106
+ end
107
+ end
108
+
109
+ context 'with no persisted record' do
110
+ let!(:avatar) do
111
+ build(:file,
112
+ format: :jpg,
113
+ height: 600,
114
+ public_id: :public_id,
115
+ scope: :avatar,
116
+ width: 800
117
+ )
118
+ end
119
+
120
+ context 'as a json' do
121
+ let!(:json) { avatar.to_json }
122
+
123
+ it 'is saved and referenced' do
124
+ user.avatar = json
125
+
126
+ expect(user.avatar_files.size).to eq 1
127
+
128
+ file = user.avatar_files[0]
129
+
130
+ expect(file.format).to eq 'jpg'
131
+ expect(file.height).to eq 600
132
+ expect(file.public_id).to eq 'public_id'
133
+ expect(file.scope).to eq 'avatar'
134
+ expect(file.width).to eq 800
135
+ end
136
+ end
137
+
138
+ context 'as an array of json' do
139
+ let!(:json) { [avatar].to_json }
140
+
141
+ it 'is saved and referenced' do
142
+ user.avatar = json
143
+
144
+ expect(user.avatar_files.size).to eq 1
145
+
146
+ file = user.avatar_files[0]
147
+
148
+ expect(file.format).to eq 'jpg'
149
+ expect(file.height).to eq 600
150
+ expect(file.public_id).to eq 'public_id'
151
+ expect(file.scope).to eq 'avatar'
152
+ expect(file.width).to eq 800
153
+ end
154
+ end
155
+ end
156
+
157
+ context 'with an empty string' do
158
+ let!(:avatar) { create :file, scope: :avatar, attachable: user }
159
+
160
+ it 'does nothing' do
161
+ user.avatar = ''
162
+
163
+ expect(user.avatar_files).to eq [avatar]
164
+ end
165
+ end
166
+
167
+ context 'with an empty array as string' do
168
+ let!(:avatar) { create :file, scope: :avatar, attachable: user }
169
+
170
+ it 'clears the records' do
171
+ user.avatar = '[]'
172
+
173
+ expect(user.avatar_files).to eq []
174
+ end
175
+ end
176
+ end
177
+
66
178
  describe ':avatar?' do
67
179
  let!(:user) { create :user }
68
180
 
@@ -17,22 +17,26 @@ RSpec.describe User, ':photos' do
17
17
  end
18
18
 
19
19
  describe ':photos_files=' do
20
- let!(:user) { create :user }
21
- let!(:photo_1) { create :file, scope: :photo }
22
- let!(:photo_2) { create :file, scope: :photo }
20
+ let!(:user) { create :user }
23
21
 
24
22
  context 'when given records' do
25
- before { user.photos_files = [photo_1, photo_2] }
23
+ context 'already persisted' do
24
+ let!(:photo_1) { create :file, scope: :photos }
25
+ let!(:photo_2) { create :file, scope: :photos }
26
+
27
+ before { user.photos_files = [photo_1, photo_2] }
26
28
 
27
- it 'is saved' do
28
- expect(user.photos_files).to match_array [photo_1, photo_2]
29
+ it 'references all' do
30
+ expect(user.photos_files).to match_array [photo_1, photo_2]
31
+ end
29
32
  end
30
33
  end
31
34
 
32
35
  context 'when given no records' do
33
- before { user.photos_files = [photo_1, photo_2] }
36
+ let!(:photo_1) { create :file, scope: :photo, attachable: user }
37
+ let!(:photo_2) { create :file, scope: :photo, attachable: user }
34
38
 
35
- it 'clears the existents' do
39
+ it 'clears all' do
36
40
  user.photos_files = []
37
41
 
38
42
  expect(user.photos_files).to eq []
@@ -59,6 +63,114 @@ RSpec.describe User, ':photos' do
59
63
  end
60
64
  end
61
65
 
66
+ describe ':photos=' do
67
+ let!(:user) { create :user }
68
+
69
+ context 'with persisted record' do
70
+ let!(:photo) { create :file, scope: :photos }
71
+
72
+ context 'as a json' do
73
+ let!(:json) { photo.to_json }
74
+
75
+ it 'does not creates a new one' do
76
+ user.photos = json
77
+
78
+ expect { user.photos_files }.to_not change(Attachy::File, :count)
79
+ end
80
+
81
+ it 'is referenced' do
82
+ user.photos = json
83
+
84
+ expect(user.photos_files).to eq [photo]
85
+ end
86
+ end
87
+
88
+ context 'as an array of json' do
89
+ let!(:json) { [photo].to_json }
90
+
91
+ it 'does not creates a new one' do
92
+ user.photos = json
93
+
94
+ expect { user.photos_files }.to_not change(Attachy::File, :count)
95
+ end
96
+
97
+ it 'is referenced' do
98
+ user.photos = json
99
+
100
+ expect(user.photos_files).to eq [photo]
101
+ end
102
+ end
103
+ end
104
+
105
+ context 'with no persisted record' do
106
+ let!(:photo) do
107
+ build(:file,
108
+ format: :jpg,
109
+ height: 600,
110
+ public_id: :public_id,
111
+ scope: :photo,
112
+ width: 800
113
+ )
114
+ end
115
+
116
+ context 'as a json' do
117
+ let!(:json) { photo.to_json }
118
+
119
+ it 'is saved and referenced' do
120
+ user.photos = json
121
+
122
+ expect(user.photos_files.size).to eq 1
123
+
124
+ file = user.photos_files[0]
125
+
126
+ expect(file.format).to eq 'jpg'
127
+ expect(file.height).to eq 600
128
+ expect(file.public_id).to eq 'public_id'
129
+ expect(file.scope).to eq 'photos'
130
+ expect(file.width).to eq 800
131
+ end
132
+ end
133
+
134
+ context 'as an array of json' do
135
+ let!(:json) { [photo].to_json }
136
+
137
+ it 'is saved and referenced' do
138
+ user.photos = json
139
+
140
+ expect(user.photos_files.size).to eq 1
141
+
142
+ file = user.photos_files[0]
143
+
144
+ expect(file.format).to eq 'jpg'
145
+ expect(file.height).to eq 600
146
+ expect(file.public_id).to eq 'public_id'
147
+ expect(file.scope).to eq 'photos'
148
+ expect(file.width).to eq 800
149
+ end
150
+ end
151
+ end
152
+
153
+ context 'with an empty string' do
154
+ let!(:photo) { create :file, scope: :photos, attachable: user }
155
+
156
+ it 'does nothing' do
157
+ user.photos = ''
158
+
159
+ expect(user.photos_files).to eq [photo]
160
+ end
161
+ end
162
+
163
+ context 'with an empty array as string' do
164
+ let!(:photo) { create :file, scope: :photo, attachable: user }
165
+
166
+ it 'clears the records' do
167
+ user.photos = '[]'
168
+
169
+ expect(user.photos_files).to eq []
170
+ end
171
+ end
172
+ end
173
+
62
174
  describe ':photo?' do
63
175
  let!(:user) { create :user }
64
176
 
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.1.0
4
+ version: 0.1.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-03-14 00:00:00.000000000 Z
11
+ date: 2017-03-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cloudinary