Module: Blabs::BlogPostsHelper
- Defined in:
- app/helpers/blabs/blog_posts_helper.rb
Constant Summary collapse
- EXTRACT_LENGTH =
600
Instance Method Summary collapse
- #admin_controls(blog_post) ⇒ Object
- #auto_link_username(body) ⇒ Object
- #display_author(blog_post) ⇒ Object
- #extract(blog_post) ⇒ Object
- #field_error(form, object, field) ⇒ Object
- #formatted_body(text) ⇒ Object
- #index_meta_tags ⇒ Object
- #pretty_date(date) ⇒ Object
- #show_meta_tags ⇒ Object
- #title(title) ⇒ Object
- #validate_layout ⇒ Object
Instance Method Details
#admin_controls(blog_post) ⇒ Object
18 19 20 21 22 23 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 18 def admin_controls(blog_post) edit_link = link_to("Edit", edit_blabs_blog_post_path(blog_post)) publish_link = publish_or_unpublish_link(blog_post) delete_link = link_to("Delete", blog_post, method: :delete, data: { confirm: "Are you sure?" }) raw [edit_link, publish_link, delete_link].join(" • ") end |
#auto_link_username(body) ⇒ Object
85 86 87 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 85 def auto_link_username(body) body.gsub(/@(\w+)/, %Q{<a href="https://twitter.com/\\1">@\\1</a>}) end |
#display_author(blog_post) ⇒ Object
89 90 91 92 93 94 95 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 89 def (blog_post) if blog_post..blank? blog_post. else link_to(blog_post., "https://twitter.com/#{blog_post.author_twitter_username.delete("@")}") end end |
#extract(blog_post) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 48 def extract(blog_post) if blog_post.extract.present? extract = blog_post.extract + " " + link_to("Read more", pretty_blog_post_path(blog_post), :class=>"more") formatted_body(extract) else truncate_html(formatted_body(blog_post.body), length: EXTRACT_LENGTH, omission: "... #{link_to("Read more", pretty_blog_post_path(blog_post), :class=>"more")}", escape: false) end end |
#field_error(form, object, field) ⇒ Object
11 12 13 14 15 16 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 11 def field_error(form, object, field) if object.errors[field] && object.errors[field].size > 0 content_tag 'div', object.errors[field].first, class: 'i i-info form-error' end end |
#formatted_body(text) ⇒ Object
25 26 27 28 29 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 25 def formatted_body(text) = { autolink: true, lax_spacing: true, space_after_headers: true} renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, ) auto_link_username(renderer.render(text)).html_safe end |
#index_meta_tags ⇒ Object
78 79 80 81 82 83 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 78 def content_for(:meta_tags) do concat auto_discovery_link_tag :atom, "/blog/feed" concat auto_discovery_link_tag :rss, "/blog/feed.rss" end end |
#pretty_date(date) ⇒ Object
44 45 46 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 44 def pretty_date(date) date.strftime("#{date.day.ordinalize} %B %Y") unless date.blank? end |
#show_meta_tags ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 59 def content_for(:meta_tags) do concat ("card", "summary") concat ("site", Blabs.twitter_site_username) concat ("creator", @blog_post.try(:author_twitter_username)) concat ("title", @blog_post.title) sample = truncate((formatted_body(@blog_post.body)), length: EXTRACT_LENGTH) concat ("description", sample.strip) concat ("image", @blog_post.post_image_url) concat ("title", @blog_post.title) concat ("description", sample.strip) concat ("image", @blog_post.post_image_url) concat ("url", pretty_blog_post_url(@blog_post)) end end |
#title(title) ⇒ Object
6 7 8 9 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 6 def title(title) content_for(:title, title) content_tag(:h1, title) end |
#validate_layout ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'app/helpers/blabs/blog_posts_helper.rb', line 31 def validate_layout if !content_for?(:title) && !@blog_post..blank? raise "You need to put a title yield in your layouts title tag.\n e.g. <title>My Blog - <%= yield(:title) %> </title>\n" end if !content_for?(:meta_tags) raise "You need to put a meta_tags yield in your layouts head tag.\n e.g. <%= yield(:meta_tags) %> </head>\n" end end |