shortcode 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/README.md +5 -5
- data/lib/shortcode.rb +1 -1
- data/lib/shortcode/version.rb +1 -1
- data/spec/presenter_spec.rb +49 -4
- data/spec/support/fixtures/item.txt +1 -0
- data/spec/support/fixtures/item_presenter_attributes_output.html +4 -0
- data/spec/support/fixtures/item_presenter_output.html +4 -0
- metadata +8 -2
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -127,7 +127,7 @@ an exception will be raised.
|
|
127
127
|
|
128
128
|
### Presenters
|
129
129
|
|
130
|
-
Sometimes the data passed to the template from the shortcode it not enough. Lets say you want to render a gallery of images using id numbers of images stored in a database, e.g. `[gallery ids="1,2,3,4"]`. This is where presenters can help, they allow you to modify the `@content` and `@attributes` variables before they are sent to the template for rendering. Presenters are simple classes that define four methods. The class method `for` should return the name of the shortcode (as a symbol) it should be applied to. The classes `initialize` method received the `attributes`, `content` and `additional_attributes` variables. Finally the class should define `content` and `attributes` methods.
|
130
|
+
Sometimes the data passed to the template from the shortcode it not enough. Lets say you want to render a gallery of images using id numbers of images stored in a database, e.g. `[gallery ids="1,2,3,4"]`. This is where presenters can help, they allow you to modify the `@content` and `@attributes` variables before they are sent to the template for rendering. Presenters are simple classes that define four methods. The class method `for` should return the name of the shortcode (as a symbol) it should be applied to, the `for` method can also return an array of symbols if the presenter is to be used for multiple shortcodes. The classes `initialize` method received the `attributes`, `content` and `additional_attributes` variables. Finally the class should define `content` and `attributes` methods.
|
131
131
|
|
132
132
|
In a rails app you could return image records to the template using something like this:
|
133
133
|
|
@@ -135,13 +135,13 @@ In a rails app you could return image records to the template using something li
|
|
135
135
|
class GalleryPresenter
|
136
136
|
|
137
137
|
def self.for
|
138
|
+
# An array can also be returned if the presenter should be applied
|
139
|
+
# to multiple shortcodes, e.g. [:gallery, :enhanced_gallery]
|
138
140
|
:gallery
|
139
141
|
end
|
140
142
|
|
141
143
|
def initialize(attributes, content, additional_attributes)
|
142
|
-
@attributes = attributes
|
143
144
|
@content = content
|
144
|
-
@additional_attributes = additional_attributes
|
145
145
|
end
|
146
146
|
|
147
147
|
def content
|
@@ -172,7 +172,6 @@ class GalleryPresenter
|
|
172
172
|
end
|
173
173
|
|
174
174
|
def initialize(attributes, content, additional_attributes)
|
175
|
-
@attributes = attributes
|
176
175
|
@content = content
|
177
176
|
@additional_attributes = additional_attributes
|
178
177
|
end
|
@@ -192,7 +191,8 @@ class GalleryPresenter
|
|
192
191
|
end
|
193
192
|
end
|
194
193
|
|
195
|
-
# The hash containing the images attribute is passed through to the presenter
|
194
|
+
# The hash containing the images attribute is passed through to the presenter
|
195
|
+
# as the additional_attributes argument
|
196
196
|
Shortcode.process('[gallery]', { images: @post.images })
|
197
197
|
|
198
198
|
```
|
data/lib/shortcode.rb
CHANGED
data/lib/shortcode/version.rb
CHANGED
data/spec/presenter_spec.rb
CHANGED
@@ -9,7 +9,26 @@ class MyPresenter
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(attributes, content, additional_attributes)
|
12
|
-
@
|
12
|
+
@content = content
|
13
|
+
@additional_attributes = additional_attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
def content
|
17
|
+
@content
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
@additional_attributes || { title: "my custom title" }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class MultiplePresenter
|
26
|
+
|
27
|
+
def self.for
|
28
|
+
[:quote, :item]
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(attributes, content, additional_attributes)
|
13
32
|
@content = content
|
14
33
|
@additional_attributes = additional_attributes
|
15
34
|
end
|
@@ -25,12 +44,14 @@ end
|
|
25
44
|
|
26
45
|
describe Shortcode::Presenter do
|
27
46
|
|
28
|
-
let(:simple_quote)
|
29
|
-
let(:
|
30
|
-
let(:attributes_output) { load_fixture :simple_quote_presenter_attributes_output, :html }
|
47
|
+
let(:simple_quote) { load_fixture :simple_quote }
|
48
|
+
let(:item) { load_fixture :item }
|
31
49
|
|
32
50
|
describe "using a custom presenter" do
|
33
51
|
|
52
|
+
let(:presenter_output) { load_fixture :simple_quote_presenter_output, :html }
|
53
|
+
let(:attributes_output) { load_fixture :simple_quote_presenter_attributes_output, :html }
|
54
|
+
|
34
55
|
before do
|
35
56
|
Shortcode.register_presenter MyPresenter
|
36
57
|
end
|
@@ -45,4 +66,28 @@ describe Shortcode::Presenter do
|
|
45
66
|
|
46
67
|
end
|
47
68
|
|
69
|
+
describe "using a single presenter for multiple shortcodes" do
|
70
|
+
|
71
|
+
let(:quote_presenter_output) { load_fixture :simple_quote_presenter_output, :html }
|
72
|
+
let(:item_presenter_output) { load_fixture :item_presenter_output, :html }
|
73
|
+
|
74
|
+
let(:quote_attributes_output) { load_fixture :simple_quote_presenter_attributes_output, :html }
|
75
|
+
let(:item_attributes_output) { load_fixture :item_presenter_attributes_output, :html }
|
76
|
+
|
77
|
+
before do
|
78
|
+
Shortcode.register_presenter MultiplePresenter
|
79
|
+
end
|
80
|
+
|
81
|
+
it "uses the custom attributes" do
|
82
|
+
expect(Shortcode.process(simple_quote ).gsub("\n",'')).to eq(quote_presenter_output)
|
83
|
+
expect(Shortcode.process(item ).gsub("\n",'')).to eq(item_presenter_output)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "passes through additional attributes" do
|
87
|
+
expect(Shortcode.process(simple_quote, { title: 'Additional attribute title' }).gsub("\n",'')).to eq(quote_attributes_output)
|
88
|
+
expect(Shortcode.process(item, { title: 'Additional attribute title' }).gsub("\n",'')).to eq(item_attributes_output)
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
48
93
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
[item title="Example title 1"]Example content 1[/item]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: shortcode
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-06-
|
12
|
+
date: 2014-06-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: parslet
|
@@ -166,6 +166,9 @@ files:
|
|
166
166
|
- spec/support/fixtures/complex_snippet_output.html
|
167
167
|
- spec/support/fixtures/full_quote.txt
|
168
168
|
- spec/support/fixtures/full_quote_output.html
|
169
|
+
- spec/support/fixtures/item.txt
|
170
|
+
- spec/support/fixtures/item_presenter_attributes_output.html
|
171
|
+
- spec/support/fixtures/item_presenter_output.html
|
169
172
|
- spec/support/fixtures/long_text.txt
|
170
173
|
- spec/support/fixtures/quote_with_extras.txt
|
171
174
|
- spec/support/fixtures/quote_with_extras_output.html
|
@@ -236,6 +239,9 @@ test_files:
|
|
236
239
|
- spec/support/fixtures/complex_snippet_output.html
|
237
240
|
- spec/support/fixtures/full_quote.txt
|
238
241
|
- spec/support/fixtures/full_quote_output.html
|
242
|
+
- spec/support/fixtures/item.txt
|
243
|
+
- spec/support/fixtures/item_presenter_attributes_output.html
|
244
|
+
- spec/support/fixtures/item_presenter_output.html
|
239
245
|
- spec/support/fixtures/long_text.txt
|
240
246
|
- spec/support/fixtures/quote_with_extras.txt
|
241
247
|
- spec/support/fixtures/quote_with_extras_output.html
|