semantic_date_time_tags 0.1.19 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/ruby.yml +30 -0
- data/.rubocop.yml +6 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +3 -1
- data/Gemfile.lock +147 -108
- data/Guardfile +4 -2
- data/README.md +1 -1
- data/Rakefile +7 -5
- data/lib/semantic_date_time_tags.rb +3 -1
- data/lib/semantic_date_time_tags/engine.rb +4 -2
- data/lib/semantic_date_time_tags/format_parser.rb +30 -29
- data/lib/semantic_date_time_tags/railtie.rb +5 -3
- data/lib/semantic_date_time_tags/tag.rb +46 -58
- data/lib/semantic_date_time_tags/tag/date.rb +8 -9
- data/lib/semantic_date_time_tags/tag/date_range.rb +22 -20
- data/lib/semantic_date_time_tags/tag/date_time.rb +14 -15
- data/lib/semantic_date_time_tags/tag/time.rb +8 -9
- data/lib/semantic_date_time_tags/version.rb +3 -1
- data/lib/semantic_date_time_tags/view_helpers.rb +8 -8
- data/semantic_date_time_tags.gemspec +19 -16
- data/test/semantic_date_time_tags/format_parser_test.rb +43 -43
- data/test/semantic_date_time_tags/semantic_date_range_test.rb +71 -0
- data/test/semantic_date_time_tags/semantic_date_tag_test.rb +61 -0
- data/test/semantic_date_time_tags/semantic_date_time_tag_test.rb +49 -0
- data/test/semantic_date_time_tags/semantic_time_tag_test.rb +90 -0
- data/test/test_helper.rb +10 -13
- metadata +23 -17
- data/.coveralls.yml +0 -1
- data/.travis.yml +0 -13
- data/test/semantic_date_time_tags/view_helpers_test.rb +0 -224
@@ -1,10 +1,12 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
require_relative
|
6
|
-
require_relative
|
7
|
-
require_relative
|
3
|
+
require "action_view"
|
4
|
+
|
5
|
+
require_relative "tag"
|
6
|
+
require_relative "tag/date_range"
|
7
|
+
require_relative "tag/date_time"
|
8
|
+
require_relative "tag/date"
|
9
|
+
require_relative "tag/time"
|
8
10
|
|
9
11
|
module SemanticDateTimeTags
|
10
12
|
module ViewHelpers
|
@@ -14,8 +16,6 @@ module SemanticDateTimeTags
|
|
14
16
|
end
|
15
17
|
end
|
16
18
|
|
17
|
-
# =====================================================================
|
18
|
-
|
19
19
|
# accepts datetime and date
|
20
20
|
def semantic_date_range_tag(date_from, date_to, options = {})
|
21
21
|
SemanticDateTimeTags::Tag::DateRange.new(date_from, date_to, options).to_html
|
@@ -1,28 +1,31 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
6
|
+
require "semantic_date_time_tags/version"
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
9
|
+
spec.name = "semantic_date_time_tags"
|
8
10
|
spec.version = SemanticDateTimeTags::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.homepage =
|
13
|
-
spec.license =
|
11
|
+
spec.authors = ["Tomas Celizna"]
|
12
|
+
spec.email = ["tomas.celizna@gmail.com"]
|
13
|
+
spec.summary = "Rails helpers for handling dates and time."
|
14
|
+
spec.homepage = "https://github.com/tomasc/semantic_date_time_tags"
|
15
|
+
spec.license = "MIT"
|
14
16
|
|
15
17
|
spec.files = `git ls-files -z`.split("\x0")
|
16
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = [
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "rails", ">= 3"
|
19
23
|
|
20
|
-
spec.
|
24
|
+
spec.add_development_dependency "bundler"
|
25
|
+
spec.add_development_dependency "guard"
|
26
|
+
spec.add_development_dependency "guard-minitest"
|
27
|
+
spec.add_development_dependency "minitest"
|
28
|
+
spec.add_development_dependency "rake"
|
21
29
|
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency 'coveralls'
|
24
|
-
spec.add_development_dependency 'guard'
|
25
|
-
spec.add_development_dependency 'guard-minitest'
|
26
|
-
spec.add_development_dependency 'minitest'
|
27
|
-
spec.add_development_dependency 'rake'
|
30
|
+
spec.add_development_dependency "rubocop-rails_config"
|
28
31
|
end
|
@@ -1,82 +1,82 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "semantic_date_time_tags/format_parser"
|
4
|
+
require "test_helper"
|
3
5
|
|
4
6
|
describe SemanticDateTimeTags::FormatParser do
|
5
7
|
subject { SemanticDateTimeTags::FormatParser.new(format, string) }
|
6
8
|
let(:to_html) { subject.to_html }
|
7
9
|
|
8
|
-
#
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
let(:format) { '%d / %m / %Y' }
|
13
|
-
let(:string) { '12 / 12 / 2014' }
|
10
|
+
describe "#to_html" do
|
11
|
+
describe "d / m / Y" do
|
12
|
+
let(:format) { "%d / %m / %Y" }
|
13
|
+
let(:string) { "12 / 12 / 2014" }
|
14
14
|
|
15
|
-
it
|
16
|
-
to_html.must_equal '<span class="day d">12</span><span class="sep"> / </span><span class="month m">12</span><span class="sep"> / </span><span class="year Y">2014</span>'
|
15
|
+
it "wraps the components into span tags" do
|
16
|
+
_(to_html).must_equal '<span class="day d">12</span><span class="sep"> / </span><span class="month m">12</span><span class="sep"> / </span><span class="year Y">2014</span>'
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
describe
|
21
|
-
let(:format) {
|
22
|
-
let(:string) {
|
20
|
+
describe "d m Y" do
|
21
|
+
let(:format) { "%-d %b %Y" }
|
22
|
+
let(:string) { "12 December 2014" }
|
23
23
|
|
24
|
-
it
|
25
|
-
to_html.must_equal '<span class="day d">12</span><span class="sep"> </span><span class="month b">December</span><span class="sep"> </span><span class="year Y">2014</span>'
|
24
|
+
it "wraps the components into span tags" do
|
25
|
+
_(to_html).must_equal '<span class="day d">12</span><span class="sep"> </span><span class="month b">December</span><span class="sep"> </span><span class="year Y">2014</span>'
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
describe
|
30
|
-
let(:format) {
|
31
|
-
let(:string) {
|
29
|
+
describe "d / m / Y" do
|
30
|
+
let(:format) { "%I.%M %p" }
|
31
|
+
let(:string) { "10.00 AM" }
|
32
32
|
|
33
|
-
it
|
34
|
-
to_html.must_equal '<span class="hours I">10</span><span class="sep">.</span><span class="minutes M">00</span><span class="sep"> </span><span class="ampm p">AM</span>'
|
33
|
+
it "wraps the components into span tags" do
|
34
|
+
_(to_html).must_equal '<span class="hours I">10</span><span class="sep">.</span><span class="minutes M">00</span><span class="sep"> </span><span class="ampm p">AM</span>'
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
-
describe
|
39
|
-
let(:format) {
|
40
|
-
let(:string) {
|
38
|
+
describe "d / m / Y" do
|
39
|
+
let(:format) { "%a, %b %e, %Y" }
|
40
|
+
let(:string) { "Sun, Jan 1, 2015" }
|
41
41
|
|
42
|
-
it
|
43
|
-
to_html.must_equal '<span class="day a">Sun</span><span class="sep">, </span><span class="month b">Jan</span><span class="sep"> </span><span class="day e">1</span><span class="sep">, </span><span class="year Y">2015</span>'
|
42
|
+
it "wraps the components into span tags" do
|
43
|
+
_(to_html).must_equal '<span class="day a">Sun</span><span class="sep">, </span><span class="month b">Jan</span><span class="sep"> </span><span class="day e">1</span><span class="sep">, </span><span class="year Y">2015</span>'
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
|
-
describe
|
48
|
-
let(:format) {
|
49
|
-
let(:string) {
|
47
|
+
describe "A d / m / Y" do
|
48
|
+
let(:format) { "%A %d / %m / %Y" }
|
49
|
+
let(:string) { "Saturday 12 / 12 / 2014" }
|
50
50
|
|
51
|
-
it
|
52
|
-
to_html.must_equal '<span class="day A">Saturday</span><span class="sep"> </span><span class="day d">12</span><span class="sep"> / </span><span class="month m">12</span><span class="sep"> / </span><span class="year Y">2014</span>'
|
51
|
+
it "wraps the components into span tags" do
|
52
|
+
_(to_html).must_equal '<span class="day A">Saturday</span><span class="sep"> </span><span class="day d">12</span><span class="sep"> / </span><span class="month m">12</span><span class="sep"> / </span><span class="year Y">2014</span>'
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
describe
|
57
|
-
let(:format) {
|
58
|
-
let(:string) {
|
56
|
+
describe "%-l:%M %P" do
|
57
|
+
let(:format) { "%-l:%M %P" }
|
58
|
+
let(:string) { "12:30 am" }
|
59
59
|
|
60
|
-
it
|
61
|
-
to_html.must_include '<span class="ampm P">'
|
60
|
+
it "marks up the am/pm" do
|
61
|
+
_(to_html).must_include '<span class="ampm P">'
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
describe
|
66
|
-
let(:format) {
|
65
|
+
describe ":cs" do
|
66
|
+
let(:format) { "%A, %-d. %B, %Y" }
|
67
67
|
let(:string) { "Čtvrtek, 16. Červen, 2016" }
|
68
68
|
|
69
|
-
it
|
70
|
-
to_html.must_equal "<span class=\"day A\">Čtvrtek</span><span class=\"sep\">, </span><span class=\"day d\">16</span><span class=\"sep\">. </span><span class=\"month B\">Červen</span><span class=\"sep\">, </span><span class=\"year Y\">2016</span>"
|
69
|
+
it "deals fine with accented characters" do
|
70
|
+
_(to_html).must_equal "<span class=\"day A\">Čtvrtek</span><span class=\"sep\">, </span><span class=\"day d\">16</span><span class=\"sep\">. </span><span class=\"month B\">Červen</span><span class=\"sep\">, </span><span class=\"year Y\">2016</span>"
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
74
|
-
describe
|
75
|
-
let(:format) {
|
74
|
+
describe "additional string" do
|
75
|
+
let(:format) { "%d.%m.%Y %H.%M hrs" }
|
76
76
|
let(:string) { "09.09.2014 19.00 hrs" }
|
77
77
|
|
78
|
-
it
|
79
|
-
to_html.must_equal "<span class=\"day d\">09</span><span class=\"sep\">.</span><span class=\"month m\">09</span><span class=\"sep\">.</span><span class=\"year Y\">2014</span><span class=\"sep\"> </span><span class=\"hours H\">19</span><span class=\"sep\">.</span><span class=\"minutes M\">00</span><span class=\"str\"> hrs</span>"
|
78
|
+
it "preserves additional strings" do
|
79
|
+
_(to_html).must_equal "<span class=\"day d\">09</span><span class=\"sep\">.</span><span class=\"month m\">09</span><span class=\"sep\">.</span><span class=\"year Y\">2014</span><span class=\"sep\"> </span><span class=\"hours H\">19</span><span class=\"sep\">.</span><span class=\"minutes M\">00</span><span class=\"str\"> hrs</span>"
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "semantic_date_time_tags/view_helpers"
|
5
|
+
|
6
|
+
describe SemanticDateTimeTags::ViewHelpers do
|
7
|
+
include SemanticDateTimeTags::ViewHelpers
|
8
|
+
|
9
|
+
let(:date_object) { Date.parse("31/10/#{Date.today.year}") }
|
10
|
+
let(:date_tomorrow_object) { Date.parse("31/10/#{Date.today.year}") + 1.day }
|
11
|
+
let(:time_object) { Time.parse("31/10/#{Date.today.year}") }
|
12
|
+
|
13
|
+
describe "#semantic_date_range_tag" do
|
14
|
+
let(:date_time_object_from) { DateTime.parse("31/10/#{Date.today.year}") }
|
15
|
+
let(:date_time_object_to) { DateTime.parse("11/11/#{Date.today.year}") }
|
16
|
+
|
17
|
+
let(:date_time_object_from_morning) { DateTime.parse("14/11/#{Date.today.year} 11:00") }
|
18
|
+
let(:date_time_object_to_afternoon) { DateTime.parse("14/11/#{Date.today.year} 15:00") }
|
19
|
+
|
20
|
+
it "returns the from date wrapped correctly" do
|
21
|
+
_(semantic_date_range_tag(date_object, date_tomorrow_object)).must_match(/<time.+?semantic.+?date.+?from.+?>.+?<time.+?semantic.+?date.+?to.+?>/)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "adds same_year and current_year class to wrapping span" do
|
25
|
+
_(semantic_date_range_tag(date_object, date_tomorrow_object)).must_match(/\A<span.+?date_range.+?same_year.+?current_year.+?>/)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "adds same_day to wrapping span" do
|
29
|
+
_(semantic_date_range_tag(date_time_object_from, date_time_object_from)).must_match(/\A<span.+?date_range.+?same_day.+?>/)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "adds same_time to wrapping span" do
|
33
|
+
_(semantic_date_range_tag(date_time_object_from, date_time_object_from)).must_match(/\A<span.+?date_range.+?same_day.+?same_time.+?>/)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "adds am to wrapping span if both times in morning" do
|
37
|
+
_(semantic_date_range_tag(date_time_object_from_morning - 1.hour, date_time_object_from_morning)).must_match(/\A<span.+?date_range.+?same_meridian.+?>/)
|
38
|
+
_(semantic_date_range_tag(date_time_object_from_morning, date_time_object_to_afternoon)).wont_match(/\A<span.+?date_range.+?same_meridian.+?>/)
|
39
|
+
_(semantic_date_range_tag(date_time_object_to_afternoon, date_time_object_to_afternoon + 1.hour)).must_match(/\A<span.+?date_range.+?same_meridian.+?>/)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "accepts datetime objects" do
|
43
|
+
_(semantic_date_range_tag(date_time_object_from, date_time_object_to)).must_match(/<time.+?from.+?<\/time>/)
|
44
|
+
_(semantic_date_range_tag(date_time_object_from, date_time_object_to)).must_match(/<time.+?to.+?<\/time>/)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "has an alias of semantic_date_tim_range_tag" do
|
48
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object)).must_match(/<time.+?semantic.+?date.+?from.+?>/)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "adds locale class" do
|
52
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object)).must_match(/class=\".+\s#{I18n.locale}\s.+\"/i)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "does not include separator attribute" do
|
56
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object, separator: "–")).wont_match(/separator=/)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "allows to pass :format" do
|
60
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object, format: :test)).must_include "~"
|
61
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object, format: :test)).must_include 'data-format="test"'
|
62
|
+
_(semantic_time_tag(time_object, format: "%a, %b %-d, %Y, %-l:%M %P")).must_include 'data-format="%a, %b %-d, %Y, %-l:%M %P"'
|
63
|
+
_(semantic_time_tag(time_object, format: "%a, %b %-d, %Y, %-l:%M %P")).wont_include " format="
|
64
|
+
end
|
65
|
+
|
66
|
+
it "allows to pass data attributes as a Hash" do
|
67
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object, data: { name: "value" })).must_match /\<span.+?data-name="value".+?\>/
|
68
|
+
_(semantic_date_time_range_tag(date_object, date_tomorrow_object, time_data: { time_name: "time value" })).must_match /\<time.+?data-time-name="time value".+?\>/
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "semantic_date_time_tags/view_helpers"
|
5
|
+
|
6
|
+
describe SemanticDateTimeTags::ViewHelpers do
|
7
|
+
include SemanticDateTimeTags::ViewHelpers
|
8
|
+
|
9
|
+
let(:date_object) { Date.parse("31/10/#{Date.today.year}") }
|
10
|
+
let(:date_tomorrow_object) { Date.parse("31/10/#{Date.today.year}") + 1.day }
|
11
|
+
let(:time_object) { Time.parse("31/10/#{Date.today.year}") }
|
12
|
+
|
13
|
+
describe "#semantic_date_tag" do
|
14
|
+
let(:date_object_day) { date_object.strftime("%-d") }
|
15
|
+
let(:date_object_month) { date_object.strftime("%-m") }
|
16
|
+
let(:date_object_year) { date_object.year }
|
17
|
+
|
18
|
+
it "should only work with a date or datetime object" do
|
19
|
+
_(proc { semantic_date_tag(time_object) }).must_raise RuntimeError
|
20
|
+
end
|
21
|
+
|
22
|
+
it "wraps everything in a time tag by default" do
|
23
|
+
_(semantic_date_tag(date_object)).must_match(/\A<time.+?<\/time>\z/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "wraps everything in a span tag if passed as argument" do
|
27
|
+
_(semantic_date_tag(date_object, tag_name: :span)).must_match(/\A<span.+?<\/span>\z/)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns year, month and day wrapped in a span tags" do
|
31
|
+
_(semantic_date_tag(date_object)).must_match(Regexp.new("<span.+?year.+?>#{date_object_year}</span>"))
|
32
|
+
_(semantic_date_tag(date_object)).must_match(Regexp.new("<span.+?month.+?>#{date_object_month}</span>"))
|
33
|
+
_(semantic_date_tag(date_object)).must_match(Regexp.new("<span.+?day.+?>#{date_object_day}</span>"))
|
34
|
+
end
|
35
|
+
|
36
|
+
it "adds current_date class if date is today" do
|
37
|
+
_(semantic_date_tag(Date.today)).must_include "current_date"
|
38
|
+
_(semantic_date_tag(Date.today - 1.day)).wont_include "current_date"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "adds current class to year span if date is this year" do
|
42
|
+
_(semantic_date_tag(Date.today)).must_include "current_year"
|
43
|
+
_(semantic_date_tag(Date.today - 1.year)).wont_include "current_year"
|
44
|
+
end
|
45
|
+
|
46
|
+
it "adds locale class" do
|
47
|
+
_(semantic_date_tag(Date.today)).must_match(/class=\".+\s#{I18n.locale}\s.+\"/i)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "allows to pass :format" do
|
51
|
+
_(semantic_date_tag(Date.today, format: :test)).must_include "~"
|
52
|
+
_(semantic_date_tag(Date.today, format: :test)).must_include 'data-format="test"'
|
53
|
+
_(semantic_date_tag(Date.today, format: "%a, %b %-d, %Y")).must_include 'data-format="%a, %b %-d, %Y"'
|
54
|
+
_(semantic_date_tag(Date.today, format: "%a, %b %-d, %Y")).wont_include " format="
|
55
|
+
end
|
56
|
+
|
57
|
+
it "allows to pass data attributes as a Hash" do
|
58
|
+
_(semantic_date_tag(Date.today, data: { name: "value" })).must_include 'data-name="value"'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "semantic_date_time_tags/view_helpers"
|
5
|
+
|
6
|
+
describe SemanticDateTimeTags::ViewHelpers do
|
7
|
+
include SemanticDateTimeTags::ViewHelpers
|
8
|
+
|
9
|
+
let(:date_object) { Date.parse("31/10/#{Date.today.year}") }
|
10
|
+
let(:date_tomorrow_object) { Date.parse("31/10/#{Date.today.year}") + 1.day }
|
11
|
+
let(:time_object) { Time.parse("31/10/#{Date.today.year}") }
|
12
|
+
|
13
|
+
describe "#semantic_date_time_tag" do
|
14
|
+
let(:date_time_object) { DateTime.parse("31/10/#{Date.today.year}") }
|
15
|
+
let(:date_time_object_noon) { DateTime.parse("31/10/#{Date.today.year}").noon }
|
16
|
+
let(:date_time_object_midnight) { DateTime.parse("31/10/#{Date.today.year}").midnight }
|
17
|
+
|
18
|
+
it "only works with a time or date_time object" do
|
19
|
+
_(proc { semantic_date_time_tag(time_object) }).must_raise RuntimeError
|
20
|
+
end
|
21
|
+
|
22
|
+
it "wraps the whole thing in a time tag" do
|
23
|
+
_(semantic_date_time_tag(date_time_object)).must_match(/\A<time.+?<\/time>\z/)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "adds noon as data-in-words if time is noon" do
|
27
|
+
_(semantic_date_time_tag(date_time_object_noon)).must_match(/\A<time.+?data-in-words=\"noon\".+?<\/time>\z/)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "adds midnight as data-in-words if time is midnight" do
|
31
|
+
_(semantic_date_time_tag(date_time_object_midnight)).must_match(/\A<time.+?data-in-words=\"midnight\".+?<\/time>\z/)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "adds locale class" do
|
35
|
+
_(semantic_date_time_tag(date_time_object)).must_match(/class=\".+\s#{I18n.locale}\s.+\"/i)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "allows to pass :format" do
|
39
|
+
_(semantic_date_time_tag(date_time_object, format: :test)).must_include "~"
|
40
|
+
_(semantic_date_time_tag(date_time_object, format: :test)).must_include 'data-format="test"'
|
41
|
+
_(semantic_date_time_tag(date_time_object, format: "%a, %b %-d, %Y, %-l:%M %P")).must_include 'data-format="%a, %b %-d, %Y, %-l:%M %P"'
|
42
|
+
_(semantic_date_time_tag(date_time_object, format: "%a, %b %-d, %Y, %-l:%M %P")).wont_include " format="
|
43
|
+
end
|
44
|
+
|
45
|
+
it "allows to pass data attributes as a Hash" do
|
46
|
+
_(semantic_date_time_tag(date_time_object, data: { name: "value" })).must_include 'data-name="value"'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "test_helper"
|
4
|
+
require "semantic_date_time_tags/view_helpers"
|
5
|
+
|
6
|
+
describe SemanticDateTimeTags::ViewHelpers do
|
7
|
+
include SemanticDateTimeTags::ViewHelpers
|
8
|
+
|
9
|
+
let(:date_object) { Date.parse("31/10/#{Date.today.year}") }
|
10
|
+
let(:date_tomorrow_object) { Date.parse("31/10/#{Date.today.year}") + 1.day }
|
11
|
+
let(:time_object) { Time.parse("31/10/#{Date.today.year}") }
|
12
|
+
|
13
|
+
describe "#semantic_time_tag" do
|
14
|
+
let(:time_object_hours) { time_object.strftime("%H") }
|
15
|
+
let(:time_object_midnight) { Time.new(Date.today.year, 11, 3, 24, 00) }
|
16
|
+
let(:time_object_minutes) { time_object.strftime("%M") }
|
17
|
+
let(:time_object_noon) { Time.new(Date.today.year, 11, 3, 12, 00) }
|
18
|
+
let(:time_object_whole_hour) { Time.new(2014, 8, 21, 15) }
|
19
|
+
let(:time_object_whole_minute) { Time.new(2014, 8, 21, 15, 30) }
|
20
|
+
let(:time_object_before_noon) { Time.new(2014, 8, 21, 11, 00) }
|
21
|
+
let(:time_object_after_noon) { Time.new(2014, 8, 21, 12, 01) }
|
22
|
+
|
23
|
+
it "does not work with a date object" do
|
24
|
+
_(proc { semantic_time_tag(date_object) }).must_raise RuntimeError
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns hours wrapped in a span tag" do
|
28
|
+
_(semantic_time_tag(time_object)).must_match(Regexp.new("<span.+?hours.+?H.+?>#{time_object_hours}</span>"))
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns minutes wrapped in a span tag" do
|
32
|
+
_(semantic_time_tag(time_object)).must_match(Regexp.new("<span.+?minutes.+?M.+?>#{time_object_minutes}</span>"))
|
33
|
+
end
|
34
|
+
|
35
|
+
it "wraps the whole thing in a time tag by default" do
|
36
|
+
_(semantic_time_tag(time_object)).must_match(/\A<time.+?<\/time>\z/)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "wraps the whole thing in a span tag if passed as argument" do
|
40
|
+
_(semantic_time_tag(time_object, tag_name: :span)).must_match(/\A<span.+?<\/span>\z/)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "adds whole_hour class if time is whole hour" do
|
44
|
+
_(semantic_time_tag(time_object_whole_hour)).must_match(/\A<time.+?whole_hour.+?<\/time>\z/)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "adds whole_minute class if time is whole minute" do
|
48
|
+
_(semantic_time_tag(time_object_whole_minute)).must_match(/\A<time.+?whole_minute.+?<\/time>\z/)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "adds noon class if time is noon" do
|
52
|
+
_(semantic_time_tag(time_object_noon)).must_match(/\A<time.+?noon.+?<\/time>\z/)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "adds midnight class if time is midnight" do
|
56
|
+
_(semantic_time_tag(time_object_midnight)).must_match(/\A<time.+?midnight.+?<\/time>\z/)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "adds am class if time is before noon" do
|
60
|
+
_(semantic_time_tag(time_object_before_noon)).must_match(/\A<time.+?am.+?<\/time>\z/)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "adds pm class if time is after noon" do
|
64
|
+
_(semantic_time_tag(time_object_after_noon)).must_match(/\A<time.+?pm.+?<\/time>\z/)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "adds noon as data-in-words if time is noon" do
|
68
|
+
_(semantic_time_tag(time_object_noon)).must_match(/\A<time.+?data-in-words=\"noon\".+?<\/time>\z/)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "adds midnight as data-in-words if time is midnight" do
|
72
|
+
_(semantic_time_tag(time_object_midnight)).must_match(/\A<time.+?data-in-words=\"midnight\".+?<\/time>\z/)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "adds locale class" do
|
76
|
+
_(semantic_time_tag(time_object)).must_match(/class=\".+\s#{I18n.locale}\s.+\"/i)
|
77
|
+
end
|
78
|
+
|
79
|
+
it "allows to pass :format" do
|
80
|
+
_(semantic_time_tag(time_object, format: :test)).must_include "~"
|
81
|
+
_(semantic_time_tag(time_object, format: :test)).must_include 'data-format="test"'
|
82
|
+
_(semantic_time_tag(time_object, format: "%-l:%M %P")).must_include 'data-format="%-l:%M %P"'
|
83
|
+
_(semantic_time_tag(time_object, format: "%-l:%M %P")).wont_include " format="
|
84
|
+
end
|
85
|
+
|
86
|
+
it "allows to pass data attributes as a Hash" do
|
87
|
+
_(semantic_time_tag(time_object, data: { name: "value" })).must_include 'data-name="value"'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|