showcase 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/showcase/helpers/html_class_attribute.rb +23 -0
- data/lib/showcase/helpers/html_options.rb +12 -6
- data/lib/showcase/helpers/present.rb +1 -1
- data/lib/showcase/version.rb +1 -1
- data/spec/showcase/helpers/html_class_attribute_spec.rb +83 -0
- data/spec/showcase/helpers/html_options_spec.rb +37 -0
- data/spec/showcase/helpers/present_spec.rb +53 -0
- metadata +9 -4
- data/spec/showcase/helpers_spec.rb +0 -49
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 626e99ee876822033c08da67fa23f579028dec83
|
4
|
+
data.tar.gz: 6cb9d3be74ea269b752ed3e4fe3704fd7cdada5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1cfcacd185e4376c897629400c1c322141dad7cc536c7c54d8f153a56b8b6b4e1e805075b97e24ebbe06fe1db5b72a7921489cbde244dfc7fdd64f1481e4f2e
|
7
|
+
data.tar.gz: c5a14c96b7baa1fe72615ce98c17f7dc1fb71136fecaa4ce95e162ff982661ae44ae2c4f9a044c4082b5d9549763a2e24daeb5122d7616ceb6963beb9b5b680e
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Showcase
|
2
|
+
module Helpers
|
3
|
+
class HtmlClassAttribute
|
4
|
+
def initialize(value = nil)
|
5
|
+
@css_classes = value.to_s.strip.split(/\s+/)
|
6
|
+
end
|
7
|
+
|
8
|
+
def <<(value)
|
9
|
+
value = value.to_s.strip.split(/\s+/)
|
10
|
+
@css_classes += value
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_html_attribute
|
14
|
+
if @css_classes.any?
|
15
|
+
@css_classes.uniq.join(' ')
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'showcase/helpers/html_class_attribute'
|
2
|
+
|
1
3
|
module Showcase
|
2
4
|
module Helpers
|
3
5
|
class HtmlOptions
|
@@ -6,15 +8,19 @@ module Showcase
|
|
6
8
|
end
|
7
9
|
|
8
10
|
def add_class!(css_class)
|
9
|
-
|
10
|
-
css_classes = @options[:class].split(/\s+/)
|
11
|
-
css_classes << css_class
|
12
|
-
@options[:class] = css_classes.join(" ")
|
11
|
+
merge_attrs!(class: css_class)
|
13
12
|
end
|
14
13
|
|
15
14
|
def merge_attrs!(options = {})
|
16
|
-
|
17
|
-
|
15
|
+
new_options = (options || {}).symbolize_keys
|
16
|
+
|
17
|
+
if new_options[:class]
|
18
|
+
class_attr = HtmlClassAttribute.new(@options[:class])
|
19
|
+
class_attr << new_options[:class]
|
20
|
+
new_options[:class] = class_attr.to_html_attribute
|
21
|
+
end
|
22
|
+
|
23
|
+
@options.merge!(new_options)
|
18
24
|
end
|
19
25
|
|
20
26
|
def to_h
|
data/lib/showcase/version.rb
CHANGED
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Showcase
|
4
|
+
module Helpers
|
5
|
+
describe HtmlClassAttribute do
|
6
|
+
let(:attribute) { HtmlClassAttribute.new(string) }
|
7
|
+
let(:string) { nil }
|
8
|
+
|
9
|
+
describe '.to_html_attribute' do
|
10
|
+
subject { attribute.to_html_attribute }
|
11
|
+
|
12
|
+
context 'nil' do
|
13
|
+
let(:string) { nil }
|
14
|
+
it { should be_nil }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'empty' do
|
18
|
+
let(:string) { '' }
|
19
|
+
it { should be_nil }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'whitespaces only' do
|
23
|
+
let(:string) { ' ' }
|
24
|
+
it { should be_nil }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with trailing spaces' do
|
28
|
+
let(:string) { ' foo ' }
|
29
|
+
it { should eq 'foo' }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'with multiple classes' do
|
33
|
+
let(:string) { ' foo bar ' }
|
34
|
+
it { should eq 'foo bar' }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#<<' do
|
39
|
+
before do
|
40
|
+
attribute << addition
|
41
|
+
end
|
42
|
+
|
43
|
+
subject { attribute.to_html_attribute }
|
44
|
+
|
45
|
+
context 'with no existing class attribute' do
|
46
|
+
let(:string) { nil }
|
47
|
+
let(:addition) { ' foo ' }
|
48
|
+
|
49
|
+
it { should eq 'foo' }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'with empty class attribute' do
|
53
|
+
let(:string) { '' }
|
54
|
+
let(:addition) { 'foo' }
|
55
|
+
|
56
|
+
it { should eq 'foo' }
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with existing class attribute' do
|
60
|
+
let(:string) { 'bar' }
|
61
|
+
let(:addition) { 'foo' }
|
62
|
+
|
63
|
+
it { should eq 'bar foo' }
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'with the same css class' do
|
67
|
+
let(:string) { 'foo bar' }
|
68
|
+
let(:addition) { 'foo' }
|
69
|
+
|
70
|
+
it { should eq 'foo bar' }
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'multiple additions' do
|
74
|
+
let(:string) { 'foo bar' }
|
75
|
+
let(:addition) { 'foo qux' }
|
76
|
+
|
77
|
+
it { should eq 'foo bar qux' }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Showcase
|
4
|
+
module Helpers
|
5
|
+
describe HtmlOptions do
|
6
|
+
let(:options) { HtmlOptions.new(attributes) }
|
7
|
+
let(:attributes) { {} }
|
8
|
+
|
9
|
+
it 'takes a hash of attributes' do
|
10
|
+
expect(options.to_h).to eq attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#merge_attrs!' do
|
14
|
+
let(:attributes) { { 'id' => 'bar', 'data-alert' => 'true' } }
|
15
|
+
|
16
|
+
it 'merges attributes with the passed ones' do
|
17
|
+
options.merge_attrs!('data-method' => 'post', 'id' => 'foo')
|
18
|
+
expect(options.to_h).to eq({
|
19
|
+
:'data-alert' => 'true',
|
20
|
+
:'data-method' => 'post',
|
21
|
+
:'id' => 'foo'
|
22
|
+
})
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'css classes' do
|
26
|
+
let(:attributes) { { 'class' => 'foo bar' } }
|
27
|
+
|
28
|
+
it 'merges them appropriately' do
|
29
|
+
options.merge_attrs!('class' => 'foo qux')
|
30
|
+
expect(options.to_h).to eq(class: 'foo bar qux')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Showcase
|
4
|
+
module Helpers
|
5
|
+
describe Present do
|
6
|
+
let(:object) { Person.new('Steve Ballmer') }
|
7
|
+
let(:subclass_object) { EnterpriseCustomer.new('Tito Traves') }
|
8
|
+
let(:object_with_no_presenter) { Shop.new }
|
9
|
+
let(:context) { Context.new }
|
10
|
+
|
11
|
+
describe '.present' do
|
12
|
+
it 'instantiates a new presenter, inferring the presenter class' do
|
13
|
+
PersonPresenter.stub(:new).with(object, context).and_return 'Presenter'
|
14
|
+
context.present(object).should == 'Presenter'
|
15
|
+
end
|
16
|
+
context 'with a superclass presenter' do
|
17
|
+
it "instantiates a new presenter, searching presenter class in object ancestors chain" do
|
18
|
+
PersonPresenter.stub(:new).with(subclass_object, context).and_return 'Presenter'
|
19
|
+
context.present(subclass_object).should == 'Presenter'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
context 'with no existing presenter class' do
|
23
|
+
it "raises a PresenterClassNotFound error" do
|
24
|
+
expect {
|
25
|
+
context.present(object_with_no_presenter)
|
26
|
+
}.to raise_error(Showcase::PresenterClassNotFound)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
it 'uses the specified presenter class, when passed' do
|
30
|
+
ProjectPresenter.stub(:new).with(object, context).and_return 'Presenter'
|
31
|
+
context.present(object, ProjectPresenter).should == 'Presenter'
|
32
|
+
end
|
33
|
+
it 'uses the specified context, when passed' do
|
34
|
+
different_context = double
|
35
|
+
context.present(object, ProjectPresenter, different_context).view_context.should == different_context
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.present_collection' do
|
40
|
+
it 'returns a presenter for each object in the collection' do
|
41
|
+
collection = [ Person.new('Mark'), Person.new('Luke') ]
|
42
|
+
|
43
|
+
PersonPresenter.stub(:new).with(collection[0], context).and_return 'foo'
|
44
|
+
PersonPresenter.stub(:new).with(collection[1], context).and_return 'bar'
|
45
|
+
|
46
|
+
presented_collection = context.present_collection(collection)
|
47
|
+
presented_collection.should == [ 'foo', 'bar' ]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: showcase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefano Verna
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/showcase/errors.rb
|
106
106
|
- lib/showcase/helpers/config_object.rb
|
107
107
|
- lib/showcase/helpers/first_nonblank.rb
|
108
|
+
- lib/showcase/helpers/html_class_attribute.rb
|
108
109
|
- lib/showcase/helpers/html_options.rb
|
109
110
|
- lib/showcase/helpers/module_method_builder.rb
|
110
111
|
- lib/showcase/helpers/present.rb
|
@@ -122,7 +123,9 @@ files:
|
|
122
123
|
- spec/fixtures.rb
|
123
124
|
- spec/helpers/config_object_spec.rb
|
124
125
|
- spec/helpers/seo_meta_builder_spec.rb
|
125
|
-
- spec/showcase/
|
126
|
+
- spec/showcase/helpers/html_class_attribute_spec.rb
|
127
|
+
- spec/showcase/helpers/html_options_spec.rb
|
128
|
+
- spec/showcase/helpers/present_spec.rb
|
126
129
|
- spec/showcase/presenter_spec.rb
|
127
130
|
- spec/showcase/traits/base_spec.rb
|
128
131
|
- spec/showcase/traits/link_to_spec.rb
|
@@ -157,7 +160,9 @@ test_files:
|
|
157
160
|
- spec/fixtures.rb
|
158
161
|
- spec/helpers/config_object_spec.rb
|
159
162
|
- spec/helpers/seo_meta_builder_spec.rb
|
160
|
-
- spec/showcase/
|
163
|
+
- spec/showcase/helpers/html_class_attribute_spec.rb
|
164
|
+
- spec/showcase/helpers/html_options_spec.rb
|
165
|
+
- spec/showcase/helpers/present_spec.rb
|
161
166
|
- spec/showcase/presenter_spec.rb
|
162
167
|
- spec/showcase/traits/base_spec.rb
|
163
168
|
- spec/showcase/traits/link_to_spec.rb
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Showcase::Helpers do
|
4
|
-
let(:object) { Person.new('Steve Ballmer') }
|
5
|
-
let(:subclass_object) { EnterpriseCustomer.new('Tito Traves') }
|
6
|
-
let(:object_with_no_presenter) { Shop.new }
|
7
|
-
let(:context) { Context.new }
|
8
|
-
|
9
|
-
describe '.present' do
|
10
|
-
it 'instantiates a new presenter, inferring the presenter class' do
|
11
|
-
PersonPresenter.stub(:new).with(object, context).and_return 'Presenter'
|
12
|
-
context.present(object).should == 'Presenter'
|
13
|
-
end
|
14
|
-
context 'with a superclass presenter' do
|
15
|
-
it "instantiates a new presenter, searching presenter class in object ancestors chain" do
|
16
|
-
PersonPresenter.stub(:new).with(subclass_object, context).and_return 'Presenter'
|
17
|
-
context.present(subclass_object).should == 'Presenter'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
context 'with no existing presenter class' do
|
21
|
-
it "raises a PresenterClassNotFound error" do
|
22
|
-
expect {
|
23
|
-
context.present(object_with_no_presenter)
|
24
|
-
}.to raise_error(Showcase::PresenterClassNotFound)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
it 'uses the specified presenter class, when passed' do
|
28
|
-
ProjectPresenter.stub(:new).with(object, context).and_return 'Presenter'
|
29
|
-
context.present(object, ProjectPresenter).should == 'Presenter'
|
30
|
-
end
|
31
|
-
it 'uses the specified context, when passed' do
|
32
|
-
different_context = double
|
33
|
-
context.present(object, ProjectPresenter, different_context).view_context.should == different_context
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
describe '.present_collection' do
|
38
|
-
it 'returns a presenter for each object in the collection' do
|
39
|
-
collection = [ Person.new('Mark'), Person.new('Luke') ]
|
40
|
-
|
41
|
-
PersonPresenter.stub(:new).with(collection[0], context).and_return 'foo'
|
42
|
-
PersonPresenter.stub(:new).with(collection[1], context).and_return 'bar'
|
43
|
-
|
44
|
-
presented_collection = context.present_collection(collection)
|
45
|
-
presented_collection.should == [ 'foo', 'bar' ]
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|