Class: Crowdblog::Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Crowdblog::Post
- Defined in:
- app/models/crowdblog/post.rb
Constant Summary collapse
- LEGACY_TITLE_REGEXP =
/(\d+-\d+-\d+)-(.*)/
Class Method Summary collapse
- .all_posts_json ⇒ Object
- .by_author(author_id) ⇒ Object
- .last_published(number) ⇒ Object
- .order_by_publish_date ⇒ Object
- .published ⇒ Object
- .published_and_ordered ⇒ Object
- .scoped_for(user) ⇒ Object
Instance Method Summary collapse
-
#allowed_to_update_permalink? ⇒ Boolean
INSTANCE METHODS.
- #day ⇒ Object
- #formatted_published_date ⇒ Object
- #html_body ⇒ Object
- #legacy(string, email) ⇒ Object
- #month ⇒ Object
- #publish_if_allowed(transition, user) ⇒ Object
- #regenerate_permalink ⇒ Object
-
#url_params ⇒ Object
Use this methods to generate the post url always use with the splat operator.
Class Method Details
.all_posts_json ⇒ Object
42 43 44 45 |
# File 'app/models/crowdblog/post.rb', line 42 def all_posts_json order_by_publish_date.to_json only: [:id, :title, :state, :published_at], methods: [:author_email, :published?] end |
.by_author(author_id) ⇒ Object
47 48 49 |
# File 'app/models/crowdblog/post.rb', line 47 def () published_and_ordered.where(author_id: ) end |
.last_published(number) ⇒ Object
51 52 53 |
# File 'app/models/crowdblog/post.rb', line 51 def last_published(number) published_and_ordered.limit(number) end |
.order_by_publish_date ⇒ Object
55 56 57 |
# File 'app/models/crowdblog/post.rb', line 55 def order_by_publish_date order('published_at DESC, created_at DESC, id DESC') end |
.published ⇒ Object
59 60 61 |
# File 'app/models/crowdblog/post.rb', line 59 def published where(state: 'published') end |
.published_and_ordered ⇒ Object
63 64 65 |
# File 'app/models/crowdblog/post.rb', line 63 def published_and_ordered published.order_by_publish_date.includes(:author) end |
.scoped_for(user) ⇒ Object
67 68 69 |
# File 'app/models/crowdblog/post.rb', line 67 def scoped_for(user) user.is_publisher? ? Post : user. end |
Instance Method Details
#allowed_to_update_permalink? ⇒ Boolean
INSTANCE METHODS
79 80 81 |
# File 'app/models/crowdblog/post.rb', line 79 def allowed_to_update_permalink? !self.published? end |
#day ⇒ Object
83 84 85 |
# File 'app/models/crowdblog/post.rb', line 83 def day "%02d" % published_at.day end |
#formatted_published_date ⇒ Object
87 88 89 |
# File 'app/models/crowdblog/post.rb', line 87 def formatted_published_date published_at.strftime("%b %d, %Y") end |
#html_body ⇒ Object
91 92 93 94 95 |
# File 'app/models/crowdblog/post.rb', line 91 def html_body @@renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :space_after_headers => true) @@renderer.render(self.body).html_safe end |
#legacy(string, email) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'app/models/crowdblog/post.rb', line 97 def legacy(string, email) results = string.match(LEGACY_TITLE_REGEXP) self.published_at = "#{results[1]}" user = User.find_by_email(email) || User.create!(email: email) self. = user self.save self.publish self.update_attribute(:permalink, results[2]) end |
#month ⇒ Object
107 108 109 |
# File 'app/models/crowdblog/post.rb', line 107 def month "%02d" % published_at.month end |
#publish_if_allowed(transition, user) ⇒ Object
111 112 113 114 115 116 |
# File 'app/models/crowdblog/post.rb', line 111 def publish_if_allowed(transition, user) if user.is_publisher? self.publisher = user self.send(transition) end end |
#regenerate_permalink ⇒ Object
118 119 120 |
# File 'app/models/crowdblog/post.rb', line 118 def regenerate_permalink self.permalink = title.parameterize end |
#url_params ⇒ Object
Use this methods to generate the post url always use with the splat operator
Example:
post_url(*post.url_params)
130 131 132 |
# File 'app/models/crowdblog/post.rb', line 130 def url_params [self.year, self.month, self.day, self.permalink, 'html'] end |