Created
February 20, 2014 19:44
-
-
Save sanjayginde/9121677 to your computer and use it in GitHub Desktop.
Read more/less
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.expandable | |
.read-more, .read-less | |
// link styles go here (if needed)... | |
.long-content | |
display: none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(document).ready(function($, document){ | |
$('body').on("click", ".expandable .read-more, .expandable .read-less", function(event) { | |
event.preventDefault(); | |
$(event.currentTarget).closest(".expandable").find(".short-content, .long-content").toggle() | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ReadMoreHelper | |
def read_more(text_or_safe_html, length: 250, more_text: 'Read more', less_text: 'Read less', omission: '…') | |
content_tag :div, class: "expandable" do | |
stripped_text = strip_tags(text_or_safe_html) | |
if (stripped_text.length > length) | |
concat content_tag(:div, read_more_short_content(text_or_safe_html, length, more_text, omission), class: "short-content") | |
concat content_tag :div, read_more_long_content(text_or_safe_html, less_text), class: "long-content" | |
else | |
concat text_or_safe_html.html_safe | |
end | |
end | |
end | |
def read_more_short_content(text_or_html, length, more_text, omission) | |
(truncate(strip_tags(text_or_html), length: length, omission: omission) + " " + link_to(more_text, "javascript:;", class: "read-more", data: {ga_event: "read_more"})).html_safe | |
end | |
def read_more_long_content(text_or_html, less_text) | |
(text_or_html + " " + link_to(less_text, "javascript:;", class: "read-less")).html_safe | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment