effective_posts 0.3.0 → 0.4.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/controllers/admin/posts_controller.rb +9 -0
- data/app/helpers/effective_posts_helper.rb +20 -10
- data/app/helpers/effective_truncate_html_helper.rb +31 -26
- data/app/views/admin/posts/excerpts.html.haml +19 -0
- data/config/routes.rb +1 -0
- data/lib/effective_posts/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 07e96e05a9ae8a0c782867865c4b12641fc4f178
|
|
4
|
+
data.tar.gz: 680ad65a0e4e13be0ea6aff3a4e67145cba761fc
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d3d2fbdbbcd54ff20a16f66bb308764a97f532bea475000638b5866470775bca8d390de9eeb1b4b344d398ac3de9aa1339052e0a201b2b919086e2ca654b9752
|
|
7
|
+
data.tar.gz: 535787d615ac93871d370ef2323dfacbcdb634f629cdf0e635ae579c5659e01f639013915076f2bbaf63d0dea56fae89366bf9f21d933529495d00562021a98c
|
|
@@ -83,6 +83,15 @@ module Admin
|
|
|
83
83
|
redirect_to effective_posts.admin_posts_path
|
|
84
84
|
end
|
|
85
85
|
|
|
86
|
+
def excerpts
|
|
87
|
+
@page_title = 'Post Excerpts'
|
|
88
|
+
|
|
89
|
+
EffectivePosts.authorized?(self, :index, Effective::Post)
|
|
90
|
+
|
|
91
|
+
@posts = Effective::Post.includes(:regions)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
|
|
86
95
|
private
|
|
87
96
|
|
|
88
97
|
def post_params
|
|
@@ -13,23 +13,38 @@ module EffectivePostsHelper
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
# Only supported options are:
|
|
16
|
-
# :
|
|
17
|
-
# :
|
|
18
|
-
|
|
16
|
+
# :label => 'Read more' to set the label of the 'Read More' link
|
|
17
|
+
# :omission => '...' passed to the final text node's truncate
|
|
18
|
+
# :length => 200 to set the max inner_text length of the content
|
|
19
|
+
# All other options are passed to the link_to 'Read more'
|
|
20
|
+
def post_excerpt(post, opts = {})
|
|
19
21
|
content = effective_region(post, :content, :editable => false) { '<p>Default content</p>'.html_safe }
|
|
20
22
|
|
|
23
|
+
options = {
|
|
24
|
+
:label => 'Read more',
|
|
25
|
+
:omission => '...',
|
|
26
|
+
:length => 200
|
|
27
|
+
}.merge(opts)
|
|
28
|
+
|
|
21
29
|
divider = content.index(Effective::Snippets::ReadMoreDivider::TOKEN)
|
|
22
30
|
length = options.delete(:length)
|
|
31
|
+
omission = options.delete(:omission)
|
|
23
32
|
|
|
24
33
|
if divider.present?
|
|
25
34
|
content[0...divider] + readmore_link(post, options)
|
|
26
|
-
elsif length.present?
|
|
27
|
-
truncate_html(content, length, readmore_link(post, options)
|
|
35
|
+
elsif length.present?
|
|
36
|
+
truncate_html(content, length, omission) + readmore_link(post, options)
|
|
28
37
|
else
|
|
29
38
|
content
|
|
30
39
|
end.html_safe
|
|
31
40
|
end
|
|
32
41
|
|
|
42
|
+
def readmore_link(post, options)
|
|
43
|
+
content_tag(:p, class: 'post-read-more') do
|
|
44
|
+
link_to((options.delete(:label) || 'Read more'), effective_posts.post_path(post), options)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
33
48
|
def link_to_post_category(category, options = {})
|
|
34
49
|
category = category.to_s.downcase
|
|
35
50
|
|
|
@@ -42,9 +57,4 @@ module EffectivePostsHelper
|
|
|
42
57
|
EffectivePosts.use_category_routes ? "/#{category}/#{post.to_param}" : effective_posts.post_path(post, category: category.to_s)
|
|
43
58
|
end
|
|
44
59
|
|
|
45
|
-
def readmore_link(post, options)
|
|
46
|
-
content_tag(:p, class: 'post-read-more') do
|
|
47
|
-
link_to((options.delete(:label) || 'Read more'), effective_posts.post_path(post), options)
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
60
|
end
|
|
@@ -10,36 +10,41 @@ module EffectiveTruncateHtmlHelper
|
|
|
10
10
|
doc.inner_html.html_safe
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
# element is now the last nested element (i.e. an HTML node, NOT a text node) in the HTML, or doc itself if doc has no elements (text node)
|
|
13
|
+
# Truncates HTML or text to a certain inner_text character limit.
|
|
14
|
+
#
|
|
15
|
+
# If given HTML, the underlying markup may be much longer than length, but the displayed text
|
|
16
|
+
# will be no longer than (length + omission) characters.
|
|
17
|
+
def truncate_html(text, length = 200, omission = '...')
|
|
18
|
+
Nokogiri::HTML::DocumentFragment.parse(text).tap { |doc| _truncate_node(doc, length, omission) }.inner_html
|
|
19
|
+
end
|
|
21
20
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
def _truncate_node(node, length, omission)
|
|
22
|
+
if node.inner_text.length <= length
|
|
23
|
+
# Do nothing, we're already reached base case
|
|
24
|
+
elsif node.name == 'a'
|
|
25
|
+
node.remove # I don't want to truncate anything in a link
|
|
26
|
+
elsif node.children.blank?
|
|
27
|
+
# I need to truncate myself, and I'm certainly a text node
|
|
28
|
+
if node.text?
|
|
29
|
+
node.content = truncate(node.content, length: length, separator: ' ', omission: omission)
|
|
30
|
+
else
|
|
31
|
+
Rails.logger.info '[WARNING] effective_posts: unexpected node in children.blank? recursive condition'
|
|
32
|
+
node.remove
|
|
33
|
+
end
|
|
34
|
+
else # Go through all the children, and delete anything after the length has been reached
|
|
35
|
+
child_length = 0
|
|
36
|
+
node.children.each do |child|
|
|
37
|
+
child_length > length ? (child.remove) : (child_length += child.inner_text.length)
|
|
35
38
|
end
|
|
36
39
|
|
|
37
|
-
length
|
|
38
|
-
|
|
40
|
+
# We have now removed all nodes after length, but the last node is longer than our length
|
|
41
|
+
# child_length is the inner_text length of all included nodes
|
|
42
|
+
# And we only have to truncate the last child to get under length
|
|
39
43
|
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
child = node.children.last
|
|
45
|
+
child_max_length = length - (child_length - child.inner_text.length)
|
|
42
46
|
|
|
43
|
-
|
|
47
|
+
_truncate_node(child, child_max_length, omission)
|
|
48
|
+
end
|
|
44
49
|
end
|
|
45
50
|
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
%h2= @page_title
|
|
2
|
+
|
|
3
|
+
- @posts.find_each do |post|
|
|
4
|
+
.row
|
|
5
|
+
.col-sm-12
|
|
6
|
+
%h2.post-title
|
|
7
|
+
%a{:href => effective_post_path(post)}
|
|
8
|
+
= simple_effective_region post, :title do
|
|
9
|
+
= post.title
|
|
10
|
+
.row
|
|
11
|
+
.col-sm-6
|
|
12
|
+
%pre= effective_region(post, :content, :editable => false)
|
|
13
|
+
|
|
14
|
+
.col-sm-6
|
|
15
|
+
%pre= post_excerpt(post)
|
|
16
|
+
|
|
17
|
+
.row
|
|
18
|
+
.col-sm-12
|
|
19
|
+
%hr
|
data/config/routes.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: effective_posts
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Code and Effect
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2015-05-
|
|
11
|
+
date: 2015-05-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|
|
@@ -176,6 +176,7 @@ files:
|
|
|
176
176
|
- app/views/admin/posts/_actions.html.haml
|
|
177
177
|
- app/views/admin/posts/_form.html.haml
|
|
178
178
|
- app/views/admin/posts/edit.html.haml
|
|
179
|
+
- app/views/admin/posts/excerpts.html.haml
|
|
179
180
|
- app/views/admin/posts/index.html.haml
|
|
180
181
|
- app/views/admin/posts/new.html.haml
|
|
181
182
|
- app/views/effective/posts/_post.html.haml
|