layout_values 0.0.2 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/helpers/layout_values_helper.rb +22 -15
- data/lib/layout_values/version.rb +1 -1
- data/spec/layout_values_helper_spec.rb +121 -0
- data/spec/spec_helper.rb +5 -0
- metadata +15 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edbfe809cb1d8a53d7773d46f2ee28e8646eabb0
|
4
|
+
data.tar.gz: 8e9e20b070cf93cf875906f8ccc59faaeda25297
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0fd4c8d44130bd12178059795bba2992b3ade282684e492fde11274faeee3ba87c6b874b24b39e5ca43c7210b95724cb3be32ef0daa604940144ebb14dff2268
|
7
|
+
data.tar.gz: 4f7b5d9d8c6b005ee3ac27176180d02a9957e02b6fc3642fd94215e6686d34363113c990d7ff010504b1c836fba256ec06306fa9b9537f605a8fbd3564c9ccce
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
1
|
+
# for #present?
|
2
|
+
require "active_support/core_ext/string"
|
2
3
|
|
4
|
+
module LayoutValuesHelper
|
3
5
|
%i(title meta_description meta_keywords).each do |method|
|
4
6
|
define_method method do |value = nil|
|
5
7
|
if value.present?
|
@@ -11,25 +13,30 @@ module LayoutValuesHelper
|
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
|
15
16
|
private
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
17
|
+
|
18
|
+
def layout_value(key, options = {})
|
19
|
+
options = {
|
20
|
+
default_value_key: "#{controller.controller_name}.#{controller.action_name}.#{key}",
|
21
|
+
decorator_key: "layout.#{key}.format",
|
22
|
+
default_decorator_key: "layout.#{key}.default",
|
23
|
+
}.merge(options)
|
24
|
+
|
25
|
+
# Read the value
|
26
|
+
value = if content_for?(key)
|
27
|
+
content_for(key)
|
28
|
+
else
|
29
|
+
t(options[:default_value_key], default: "")
|
30
|
+
end
|
23
31
|
|
24
32
|
# Decorate
|
25
|
-
if value.present?
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
33
|
+
value = if value.present?
|
34
|
+
t(options[:decorator_key], key.to_sym => value, default: value)
|
35
|
+
else
|
36
|
+
t(options[:default_decorator_key], default: value)
|
37
|
+
end
|
30
38
|
|
31
39
|
# Translation helpers tend to add HTML
|
32
40
|
strip_tags value
|
33
41
|
end
|
34
|
-
|
35
42
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require_relative "../app/helpers/layout_values_helper"
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe LayoutValuesHelper do
|
5
|
+
class MockView
|
6
|
+
include LayoutValuesHelper
|
7
|
+
|
8
|
+
def content_for?(*)
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def content_for(*)
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def t(*)
|
17
|
+
""
|
18
|
+
end
|
19
|
+
|
20
|
+
def strip_tags(v)
|
21
|
+
v
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:view) { MockView.new }
|
26
|
+
let(:controller) {
|
27
|
+
double(:controller, controller_name: "pages", action_name: "index")
|
28
|
+
}
|
29
|
+
before { allow(view).to receive(:controller) { controller } }
|
30
|
+
|
31
|
+
methods = %i(title meta_description meta_keywords)
|
32
|
+
methods.each do |method|
|
33
|
+
describe "##{method}" do
|
34
|
+
describe "with an argument" do
|
35
|
+
it "sets the content_for" do
|
36
|
+
allow(view).to receive(:content_for)
|
37
|
+
view.send(method, "bar")
|
38
|
+
expect(view).to have_received(:content_for).with(method, "bar")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns the value it sets" do
|
42
|
+
expect(view.send(method, "bar")).to eq("bar")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "with no arguments" do
|
47
|
+
describe "without a content_for" do
|
48
|
+
before do
|
49
|
+
allow(view).to receive(:content_for?).with(method) { false }
|
50
|
+
end
|
51
|
+
|
52
|
+
it "defaults to ''" do
|
53
|
+
expect(view.send(method)).to eq("")
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns the controller locale value" do
|
57
|
+
allow(view).to receive(:t).with("pages.index.#{method}",
|
58
|
+
default: "") {
|
59
|
+
"translated"
|
60
|
+
}
|
61
|
+
allow(view).to receive(:t).with("layout.#{method}.format",
|
62
|
+
method => "translated",
|
63
|
+
default: "translated") {
|
64
|
+
"formatted translated"
|
65
|
+
}
|
66
|
+
|
67
|
+
expect(view.send(method)).to eq("formatted translated")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "returns the default controller locale value" do
|
71
|
+
allow(view).to receive(:t).with("pages.index.#{method}",
|
72
|
+
default: "") { "" }
|
73
|
+
allow(view).to receive(:t).with("layout.#{method}.default",
|
74
|
+
default: "") { "default" }
|
75
|
+
|
76
|
+
expect(view.send(method)).to eq("default")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "with a content_for" do
|
81
|
+
before do
|
82
|
+
allow(view).to receive(:content_for?).with(method) { true }
|
83
|
+
allow(view).to receive(:content_for).with(method) { "foo" }
|
84
|
+
end
|
85
|
+
|
86
|
+
it "returns the content formatted" do
|
87
|
+
allow(view).to receive(:t).with("layout.#{method}.format",
|
88
|
+
method => "foo",
|
89
|
+
default: "foo") { "foo - my site" }
|
90
|
+
|
91
|
+
expect(view.send(method)).to eq("foo - my site")
|
92
|
+
end
|
93
|
+
|
94
|
+
it "strips tags" do
|
95
|
+
allow(view).to receive(:t).with("layout.#{method}.format",
|
96
|
+
method => "foo",
|
97
|
+
default: "foo") { "foo - my site" }
|
98
|
+
allow(view).to receive(:strip_tags) { "stripped" }
|
99
|
+
|
100
|
+
expect(view.send(method)).to eq("stripped")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "with an empty content_for" do
|
105
|
+
before do
|
106
|
+
allow(view).to receive(:content_for?).with(method) { true }
|
107
|
+
allow(view).to receive(:content_for).with(method) { "" }
|
108
|
+
end
|
109
|
+
|
110
|
+
it "returns the locales default" do
|
111
|
+
allow(view).to receive(:t).with("layout.#{method}.default",
|
112
|
+
default: "") { "default" }
|
113
|
+
|
114
|
+
expect(view.send(method)).to eq("default")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: layout_values
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sunny Ripert
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.
|
19
|
+
version: 4.1.11
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.
|
26
|
+
version: 4.1.11
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -45,14 +45,16 @@ executables: []
|
|
45
45
|
extensions: []
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
48
51
|
- app/helpers/layout_values_helper.rb
|
52
|
+
- lib/layout_values.rb
|
49
53
|
- lib/layout_values/engine.rb
|
50
54
|
- lib/layout_values/version.rb
|
51
|
-
-
|
52
|
-
-
|
53
|
-
|
54
|
-
- README.md
|
55
|
-
homepage: http://github.com/sunny
|
55
|
+
- spec/layout_values_helper_spec.rb
|
56
|
+
- spec/spec_helper.rb
|
57
|
+
homepage: https://github.com/sunny/layout_values
|
56
58
|
licenses: []
|
57
59
|
metadata: {}
|
58
60
|
post_install_message:
|
@@ -71,8 +73,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
73
|
version: '0'
|
72
74
|
requirements: []
|
73
75
|
rubyforge_project:
|
74
|
-
rubygems_version: 2.
|
76
|
+
rubygems_version: 2.4.5
|
75
77
|
signing_key:
|
76
78
|
specification_version: 4
|
77
79
|
summary: Add helpers to indicate page titles and meta description.
|
78
|
-
test_files:
|
80
|
+
test_files:
|
81
|
+
- spec/layout_values_helper_spec.rb
|
82
|
+
- spec/spec_helper.rb
|