Module: Bunko::Models::PostMethods
- Extended by:
- ActiveSupport::Concern
- Includes:
- Publishable, Sluggable, WordCountable
- Defined in:
- lib/bunko/models/post_methods.rb,
lib/bunko/models/post_methods/sluggable.rb,
lib/bunko/models/post_methods/publishable.rb,
lib/bunko/models/post_methods/word_countable.rb
Defined Under Namespace
Modules: Publishable, Sluggable, WordCountable
Instance Method Summary collapse
- #excerpt(length: nil, omission: "...") ⇒ Object
- #published_date(format = :long) ⇒ Object
-
#to_param ⇒ Object
Instance methods.
Methods included from Publishable
Methods included from WordCountable
#reading_time, #reading_time_text
Instance Method Details
#excerpt(length: nil, omission: "...") ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bunko/models/post_methods.rb', line 46 def excerpt(length: nil, omission: "...") return nil unless content.present? # Use configured default if length not specified length ||= Bunko.configuration.excerpt_length # Strip HTML tags using Rails sanitizer (more robust than regex) text = ActionView::Base.full_sanitizer.sanitize(content.to_s) # Clean up extra whitespace text = text.gsub(/\s+/, " ").strip # Return full text if shorter than length return text if text.length <= length # Truncate to word boundary truncated = text[0...length] last_space = truncated.rindex(" ") || length "#{truncated[0...last_space]}#{omission}" end |
#published_date(format = :long) ⇒ Object
68 69 70 71 72 |
# File 'lib/bunko/models/post_methods.rb', line 68 def published_date(format = :long) return nil unless published_at.present? I18n.l(published_at, format: format) end |
#to_param ⇒ Object
Instance methods
42 43 44 |
# File 'lib/bunko/models/post_methods.rb', line 42 def to_param slug end |