center_image_tag 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWY0ODc3MzcyYTZiYzgxMThjZjQ4MDc2YzkwYjVhMjdkODZhMmU1Yg==
4
+ NjYyOTdmZGU4NDRkNjhkYmExNzBlZDNjNGI3YzYzYzE2NjBlNjExMQ==
5
5
  data.tar.gz: !binary |-
6
- OTZlNTIyNzEzZjczZWI3MjgzZTlhYWFlNGE4MjhiNjQ3ZGYxNDhjMg==
6
+ NGI4OGNjZWQzZWZlNjlhYzEwYzk0MDYxODVkYjk1M2Q2NzNiOTA4ZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDJjNzA1MzU4NGIzYTU2MTg3OGM1MzM4ZTAzMGVhM2NlMmE2MTQzMzc4ZDgx
10
- MTliZDhhNjFkNTFmZTM1NGNjZjI3ZGRlNWYyOTFjY2JkMzU1NGIwNTc3YzY1
11
- OTFjNmU5Y2IzNTA4YjIyZDdkNTQ3MjJjZjk2NTM1ODcyZjA0MDg=
9
+ ZjQ2MmY3NDkyODFmMWE2YzhkOGU4N2YzN2Q1N2FhM2FjODI5NDdjNDFhYjBm
10
+ ZDM0ZWE1YTlkZjUwMDI5NGFmYjZlMTZjY2Y1NzVkZTVjOTVlNTAyOWEwYjhh
11
+ YzE1MWI2MTk5ZWRlNDlmOTU0OWFiNWFlYmY2YzRkMGZkMDQyMTI=
12
12
  data.tar.gz: !binary |-
13
- YjdjZDBmMDE5OTgwZDE2NGUzYmYyNzE0MTBiYzZiN2Y0OTdlMjYwMTc2MDE1
14
- NDhiZTlkNjk4M2JkZTYyMGVmNDU2MWIwYjMyYjdmZDc4Y2ZiOGZhMTFkYjMx
15
- ZThiNmZiMjVkMTk5YzI4ODE3OTVhMTM5N2VkODYxMmQxZTZjMjk=
13
+ ZjA2MjZmZThmOWNjM2JkZDQwMDhhOGI2NDQ3MzdkNDA0NGU2MGI2NDQ3OWRk
14
+ N2FkODlkNTM4YWI1ZDFiMjg1Y2Q0ZDg0YWNlMDZlYmIzOWNiM2VhZGY2NDNi
15
+ YzIzMWI3ZWQ0ZGZlMmMwZWY1OTc2MzFjNDZjNzMzN2RmOTZkM2U=
@@ -1,3 +1,3 @@
1
1
  module CenterImageTag
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -14,6 +14,16 @@ module CenterImageTag
14
14
  tag(:span, class: "vertical-align")
15
15
  end
16
16
  end
17
+ else
18
+ width, height = dimension(options)
19
+ if width && height
20
+ return container_fixed width, height do
21
+ clip do
22
+ image_tag(source, rebuild_options(options, width, height)) +
23
+ tag(:span, class: "vertical-align")
24
+ end
25
+ end
26
+ end
17
27
  end
18
28
 
19
29
  image_tag(source, options)
@@ -26,15 +36,40 @@ module CenterImageTag
26
36
  end
27
37
  end
28
38
 
39
+ def container_fixed(width, height, &block)
40
+ div class: "standard-thumb", style: "width: #{width}px" do
41
+ div class: "thumb-default", style: "padding-bottom: #{height}px", &block
42
+ end
43
+ end
44
+
29
45
  def clip(&block)
30
46
  div class: "thumb-clip" do
31
47
  div class: "thumb-clip-inner", &block
32
48
  end
33
49
  end
34
50
 
51
+ def dimension(options)
52
+ if size = options[:size]
53
+ width, height = size.split("x") if size =~ %r{\A\d+x\d+\z}
54
+ width = height = size if size =~ %r{\A\d+\z}
55
+ else
56
+ width = options[:width]
57
+ height = options[:height]
58
+ end
59
+
60
+ [width, height]
61
+ end
62
+
35
63
  def div(options = {}, &block)
36
64
  content_tag :div, options, &block
37
65
  end
66
+
67
+ def rebuild_options(options, width, height)
68
+ # rebuild options
69
+ options.delete(:size)
70
+ options.delete(:height)
71
+ options.merge(width: width)
72
+ end
38
73
  end
39
74
  end
40
75
 
@@ -2,25 +2,76 @@ require 'spec_helper'
2
2
 
3
3
  module CenterImageTag
4
4
  describe ViewHelper do
5
- it 'renders the image normally if it\'s not fluid or width x height set' do
6
- output = View.new.center_image_tag 'image.png'
7
- expected = "<img alt=\"Image\" src=\"/images/image.png\" />"
8
- expect(output).to eq expected
5
+ describe 'normal' do
6
+ it 'renders the image normally if it\'s not fluid or width x height set' do
7
+ output = View.new.center_image_tag 'image.png'
8
+ expected = "<img alt=\"Image\" src=\"/images/image.png\" />"
9
+ expect(output).to eq expected
10
+ end
9
11
  end
10
12
 
11
- it 'renders the fluid image correctly' do
12
- output = View.new.center_image_tag 'image.png', fluid: "56.25%"
13
- expected = "<div class=\"standard-thumb thumb-fluid\">" +
14
- "<div class=\"thumb-default\" style=\"padding-bottom: 56.25%\">" +
15
- "<div class=\"thumb-clip\">" +
16
- "<div class=\"thumb-clip-inner\">" +
17
- "<img alt=\"Image\" src=\"/images/image.png\" />" +
18
- "<span class=\"vertical-align\" />" +
13
+ describe 'fluid' do
14
+ it 'renders the fluid image correctly' do
15
+ output = View.new.center_image_tag 'image.png', fluid: "56.25%"
16
+ expected = "<div class=\"standard-thumb thumb-fluid\">" +
17
+ "<div class=\"thumb-default\" style=\"padding-bottom: 56.25%\">" +
18
+ "<div class=\"thumb-clip\">" +
19
+ "<div class=\"thumb-clip-inner\">" +
20
+ "<img alt=\"Image\" src=\"/images/image.png\" />" +
21
+ "<span class=\"vertical-align\" />" +
22
+ "</div>" +
19
23
  "</div>" +
20
24
  "</div>" +
21
- "</div>" +
22
- "</div>"
23
- expect(output).to eq expected
25
+ "</div>"
26
+ expect(output).to eq expected
27
+ end
28
+ end
29
+
30
+ describe 'fixed' do
31
+ it 'renders the fixed image correctly from size options (size = widthxheight)' do
32
+ output = View.new.center_image_tag 'image.png', size: "200x100"
33
+ expected = "<div class=\"standard-thumb\" style=\"width: 200px\">" +
34
+ "<div class=\"thumb-default\" style=\"padding-bottom: 100px\">" +
35
+ "<div class=\"thumb-clip\">" +
36
+ "<div class=\"thumb-clip-inner\">" +
37
+ "<img alt=\"Image\" src=\"/images/image.png\" width=\"200\" />" +
38
+ "<span class=\"vertical-align\" />" +
39
+ "</div>" +
40
+ "</div>" +
41
+ "</div>" +
42
+ "</div>"
43
+ expect(output).to eq expected
44
+ end
45
+
46
+ it 'renders the fixed image correctly from size options (single size value)' do
47
+ output = View.new.center_image_tag 'image.png', size: "200"
48
+ expected = "<div class=\"standard-thumb\" style=\"width: 200px\">" +
49
+ "<div class=\"thumb-default\" style=\"padding-bottom: 200px\">" +
50
+ "<div class=\"thumb-clip\">" +
51
+ "<div class=\"thumb-clip-inner\">" +
52
+ "<img alt=\"Image\" src=\"/images/image.png\" width=\"200\" />" +
53
+ "<span class=\"vertical-align\" />" +
54
+ "</div>" +
55
+ "</div>" +
56
+ "</div>" +
57
+ "</div>"
58
+ expect(output).to eq expected
59
+ end
60
+
61
+ it 'renders the fixed image correctly from width and height options' do
62
+ output = View.new.center_image_tag 'image.png', width: "200", height: 100
63
+ expected = "<div class=\"standard-thumb\" style=\"width: 200px\">" +
64
+ "<div class=\"thumb-default\" style=\"padding-bottom: 100px\">" +
65
+ "<div class=\"thumb-clip\">" +
66
+ "<div class=\"thumb-clip-inner\">" +
67
+ "<img alt=\"Image\" src=\"/images/image.png\" width=\"200\" />" +
68
+ "<span class=\"vertical-align\" />" +
69
+ "</div>" +
70
+ "</div>" +
71
+ "</div>" +
72
+ "</div>"
73
+ expect(output).to eq expected
74
+ end
24
75
  end
25
76
  end
26
77
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: center_image_tag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anh Nguyen