simple_form_extension 1.4.15 → 1.4.16

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: 82af1dba00fbb08a47aee5b4349f302fcbac1913
4
- data.tar.gz: e81162516676385c045eee7112ce549d49e82452
3
+ metadata.gz: b0923a6c64e3a373fe4cd5112d4f45c5e7ea3c2c
4
+ data.tar.gz: b4545d0d4570bc4a9674a605d52989d504eaea4e
5
5
  SHA512:
6
- metadata.gz: eb6b95c398462365a174815ceb82e344da1131401be8f0fd80b625a99ccc0409537c34ad17426cc1e5dcffdada246712645fd3a56b73ea80e12a6dac42998d5e
7
- data.tar.gz: 97cd330f20ba85b9aa97dee5bcaaa27ca46f58b2486e8b8f0c4d812e4ed9e945f2063ec685b5c3921528e19d019ba8cd1417ff0dde5de074323d3dff349e116a
6
+ metadata.gz: 7d90c6780273260f670ea82c4936a5dac17f30ee2c39c27bec505f0fc61d628b700c0b810ef2f9c09e3e51d9e12afe2725af167caa89f641b3f014d3a3ef26ff
7
+ data.tar.gz: 946edfa5efa417d9d78242eba9e3a4b449fabccfaba9287bb5ee4176675393f7980528e6cc281152243521f445a6ad516f619d9e65e08d296219ac8f1a4e4b61
@@ -0,0 +1,62 @@
1
+ module SimpleFormExtension
2
+ module FileConcern
3
+ def existing_file_tag
4
+ return '' unless file_exists?
5
+
6
+ content_tag(:div, class: 'input-group help-block existing-file', data: { provides: 'existing-file'}) do
7
+ content_tag(:span, class: 'input-group-addon') do
8
+ "#{ _translate('file.existing_file') } : ".html_safe
9
+ end +
10
+
11
+ file_preview_and_remove_button
12
+ end
13
+ end
14
+
15
+ def file_preview_and_remove_button
16
+ content_tag(:div, class: 'btn-group') do
17
+ content_tag(:a, class: 'btn btn-default ', href: file_url, target: '_blank', data: { toggle: 'existing-file' }) do
18
+ content_tag(:i, '', class: 'fa fa-file') +
19
+ " ".html_safe +
20
+
21
+ existing_file_name
22
+ end +
23
+
24
+ remove_file_button
25
+ end
26
+ end
27
+
28
+ def existing_file_name
29
+ if object.try(:"#{ attribute_name }?")
30
+ object.send(:"#{ attribute_name }_file_name").html_safe
31
+ elsif (attachment = object.try(attribute_name)).try(:attached?)
32
+ attachment.filename.to_s
33
+ end
34
+ end
35
+
36
+ def remove_file_button
37
+ return unless object.respond_to?(:"#{ remove_attachment_method }=")
38
+
39
+ content_tag(:button, class: 'btn btn-default', type: 'button', data: { dismiss: 'existing-file' }) do
40
+ content_tag(:i, '', class: 'fa fa-remove', data: { :'removed-class' => 'fa fa-refresh' }) +
41
+ @builder.hidden_field(remove_attachment_method, class: 'remove-file-input', value: nil)
42
+ end
43
+ end
44
+
45
+ def remove_attachment_method
46
+ options[:remove_method] || :"remove_#{ attribute_name }"
47
+ end
48
+
49
+ def file_exists?
50
+ @file_exists ||= object.try(:"#{ attribute_name }?") ||
51
+ object.try(attribute_name).try(:attached?)
52
+ end
53
+
54
+ def file_url
55
+ if object.try(:"#{ attribute_name }?")
56
+ object.send(attribute_name)
57
+ elsif object.try(attribute_name).try(:attached?)
58
+ object.try(attribute_name).try(:service_url)
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,6 +2,7 @@ module SimpleFormExtension
2
2
  module Inputs
3
3
  class FileInput < SimpleForm::Inputs::Base
4
4
  include SimpleFormExtension::Translations
5
+ include SimpleFormExtension::FileConcern
5
6
 
6
7
  delegate :content_tag, to: :template
7
8
 
@@ -35,63 +36,6 @@ module SimpleFormExtension
35
36
 
36
37
  existing_file_tag
37
38
  end
38
-
39
- def existing_file_tag
40
- return '' unless file_exists?
41
-
42
- content_tag(:div, class: 'input-group help-block existing-file', data: { provides: 'existing-file'}) do
43
- content_tag(:span, class: 'input-group-addon') do
44
- "#{ _translate('file.existing_file') } : ".html_safe
45
- end +
46
-
47
- content_tag(:div, class: 'btn-group') do
48
- content_tag(:a, class: 'btn btn-default ', href: file_url, target: '_blank', data: { toggle: 'existing-file' }) do
49
- content_tag(:i, '', class: 'fa fa-file') +
50
- "&nbsp;".html_safe +
51
-
52
- existing_file_name
53
- end +
54
-
55
- remove_file_button
56
- end
57
- end
58
- end
59
-
60
- def existing_file_name
61
- return unless file_exists?
62
-
63
- if object.try(:"#{ attribute_name }?")
64
- object.send(:"#{ attribute_name }_file_name").html_safe
65
- elsif (attachment = object.try(attribute_name)).try(:attached?)
66
- attachment.filename.to_s
67
- end
68
- end
69
-
70
- def remove_file_button
71
- return unless object.respond_to?(:"#{ remove_attachment_method }=")
72
-
73
- content_tag(:button, class: 'btn btn-default', type: 'button', data: { dismiss: 'existing-file' }) do
74
- content_tag(:i, '', class: 'fa fa-remove', data: { :'removed-class' => 'fa fa-refresh' }) +
75
- @builder.hidden_field(remove_attachment_method, class: 'remove-file-input', value: nil)
76
- end
77
- end
78
-
79
- def remove_attachment_method
80
- options[:remove_method] || :"remove_#{ attribute_name }"
81
- end
82
-
83
- def file_exists?
84
- @file_exists ||= object.try(:"#{ attribute_name }?") ||
85
- object.try(attribute_name).try(:attached?)
86
- end
87
-
88
- def file_url
89
- if object.try(:"#{ attribute_name }?")
90
- object.send(attribute_name)
91
- elsif object.try(attribute_name).try(:attached?)
92
- object.try(attribute_name)
93
- end
94
- end
95
39
  end
96
40
  end
97
41
  end
@@ -2,6 +2,7 @@ module SimpleFormExtension
2
2
  module Inputs
3
3
  class ImageInput < SimpleForm::Inputs::Base
4
4
  include SimpleFormExtension::Translations
5
+ include SimpleFormExtension::FileConcern
5
6
 
6
7
  delegate :content_tag, :image_tag, to: :template
7
8
 
@@ -27,45 +28,56 @@ module SimpleFormExtension
27
28
  end
28
29
  end +
29
30
 
30
- content_tag(:div, class: 'fileinput-preview thumbnail') do
31
- existing_image_tag
32
- end
31
+ existing_image_tag
33
32
  end
34
33
  end
35
34
 
36
35
  def existing_image_tag
37
36
  # If an existing paperclip image is found
38
- if image_exists?
37
+ if file_exists?
38
+ file_preview_and_remove_button
39
+ else
40
+ content_tag(:div, class: 'fileinput-preview thumbnail') do
41
+ content_tag(:div, '', class: 'empty-thumbnail')
42
+ end
43
+ end
44
+ end
45
+
46
+ def image
47
+ @image ||= object.try(attribute_name)
48
+ end
49
+
50
+ def file_preview_and_remove_button
51
+ if image.variable? || image.previewable?
39
52
  container_style = 'position: relative; height: 100%; width: 100%; min-height: 50px;min-width: 58px; display: block;'
40
53
 
41
- content_tag(:div, style: container_style, data: { provides: 'existing-file' }) do
42
- image_tag(image_url, style: 'height: 100%; width: 100%; display: block;', data: { toggle: 'existing-file' }) +
43
- remove_image_button
54
+ content_tag(:div, class: 'fileinput-preview thumbnail') do
55
+ content_tag(:div, style: container_style, data: { provides: 'existing-file' }) do
56
+ image_tag(
57
+ file_url,
58
+ style: 'height: 100%; width: 100%; display: block;', data: { toggle: 'existing-file' }
59
+ ) +
60
+ remove_image_button
61
+ end
44
62
  end
45
63
  else
46
- content_tag(:div, '', class: 'empty-thumbnail')
64
+ super
47
65
  end
48
66
  end
49
67
 
50
- # Returns true if a paperclip or active_storage attachment already exists
51
- # for that model field
52
- #
53
- def image_exists?
54
- object.try(:"#{ attribute_name }?") ||
55
- object.try(attribute_name).try(:attached?)
56
- end
57
-
58
68
  # Returns the paperclip or active_storage attachment url to show in the
59
69
  # preview thumbnail
60
70
  #
61
- def image_url
71
+ def file_url
62
72
  if object.try(:"#{ attribute_name }?")
63
73
  object.send(attribute_name).url(image_style)
64
- elsif (image = object.try(attribute_name)).try(:attached?)
65
- if image.variable?
66
- image.variant(resize: "400x150>")
74
+ elsif (attachment = object.try(attribute_name)).try(:attached?)
75
+ if attachment.variable?
76
+ attachment.variant(resize: "400x150>")
77
+ elsif attachment.previewable?
78
+ attachment.preview(resize: "400x150>")
67
79
  else
68
- image
80
+ attachment.service_url
69
81
  end
70
82
  end
71
83
  end
@@ -81,10 +93,6 @@ module SimpleFormExtension
81
93
  end
82
94
  end
83
95
 
84
- def remove_attachment_method
85
- options[:remove_method] || :"remove_#{ attribute_name }"
86
- end
87
-
88
96
  def image_style
89
97
  styles = object.send(attribute_name).styles.map(&:first)
90
98
  # Check if there's a :thumb or :thumbnail style in attachment definition
@@ -1,3 +1,3 @@
1
1
  module SimpleFormExtension
2
- VERSION = "1.4.15"
2
+ VERSION = "1.4.16"
3
3
  end
@@ -9,6 +9,7 @@ module SimpleFormExtension
9
9
  eager_autoload do
10
10
  autoload :Translations
11
11
  autoload :ResourceNameHelper
12
+ autoload :FileConcern
12
13
  autoload :Components
13
14
  autoload :Inputs
14
15
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_extension
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.15
4
+ version: 1.4.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glyph-fr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-22 00:00:00.000000000 Z
11
+ date: 2019-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -118,6 +118,7 @@ files:
118
118
  - lib/simple_form_extension/components/icons.rb
119
119
  - lib/simple_form_extension/components/popovers.rb
120
120
  - lib/simple_form_extension/ext/form_builder.rb
121
+ - lib/simple_form_extension/file_concern.rb
121
122
  - lib/simple_form_extension/inputs.rb
122
123
  - lib/simple_form_extension/inputs/boolean_input.rb
123
124
  - lib/simple_form_extension/inputs/collection_check_boxes_input.rb