Class: Miniblog::Post
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Miniblog::Post
- Defined in:
- app/models/miniblog/post.rb
Constant Summary collapse
- LEGACY_TITLE_REGEXP =
/(\d+-\d+-\d+)-(.*)/
Instance Attribute Summary collapse
-
#transition ⇒ Object
Returns the value of attribute transition.
Class Method Summary collapse
- .all_posts_json ⇒ Object
- .by_author(author_id) ⇒ Object
- .for_admin_index ⇒ Object
- .last_published(number) ⇒ Object
- .order_by_publish_date ⇒ Object
- .ordered_by_state ⇒ 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
- #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.
Instance Attribute Details
#transition ⇒ Object
Returns the value of attribute transition.
16 17 18 |
# File 'app/models/miniblog/post.rb', line 16 def transition @transition end |
Class Method Details
.all_posts_json ⇒ Object
43 44 45 46 47 |
# File 'app/models/miniblog/post.rb', line 43 def all_posts_json includes(:author). order_by_publish_date.to_json only: [:id, :title, :state, :published_at, :ready_for_review], methods: [:author_email, :published?] end |
.by_author(author_id) ⇒ Object
49 50 51 |
# File 'app/models/miniblog/post.rb', line 49 def () published_and_ordered.where(author_id: ) end |
.for_admin_index ⇒ Object
73 74 75 |
# File 'app/models/miniblog/post.rb', line 73 def for_admin_index ordered_by_state.order_by_publish_date end |
.last_published(number) ⇒ Object
53 54 55 |
# File 'app/models/miniblog/post.rb', line 53 def last_published(number) published_and_ordered.limit(number) end |
.order_by_publish_date ⇒ Object
57 58 59 |
# File 'app/models/miniblog/post.rb', line 57 def order_by_publish_date order('published_at DESC, created_at DESC, id DESC') end |
.ordered_by_state ⇒ Object
77 78 79 |
# File 'app/models/miniblog/post.rb', line 77 def ordered_by_state order(:state) end |
.published ⇒ Object
61 62 63 |
# File 'app/models/miniblog/post.rb', line 61 def published where(state: 'published') end |
.published_and_ordered ⇒ Object
65 66 67 |
# File 'app/models/miniblog/post.rb', line 65 def published_and_ordered published.order_by_publish_date end |
.scoped_for(user) ⇒ Object
69 70 71 |
# File 'app/models/miniblog/post.rb', line 69 def scoped_for(user) user.is_publisher? ? all : user. end |
Instance Method Details
#allowed_to_update_permalink? ⇒ Boolean
INSTANCE METHODS
89 90 91 |
# File 'app/models/miniblog/post.rb', line 89 def allowed_to_update_permalink? !self.published? end |
#day ⇒ Object
93 94 95 |
# File 'app/models/miniblog/post.rb', line 93 def day "%02d" % published_at.day end |
#formatted_published_date ⇒ Object
97 98 99 |
# File 'app/models/miniblog/post.rb', line 97 def formatted_published_date published_at.strftime("%b %d, %Y") end |
#html_body ⇒ Object
101 102 103 104 105 106 107 |
# File 'app/models/miniblog/post.rb', line 101 def html_body @@renderer ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, :autolink => true, :fenced_code_blocks => true, :space_after_headers => false) @@renderer.render(self.body).html_safe end |
#month ⇒ Object
109 110 111 |
# File 'app/models/miniblog/post.rb', line 109 def month "%02d" % published_at.month end |
#publish_if_allowed(transition, user) ⇒ Object
113 114 115 116 117 118 |
# File 'app/models/miniblog/post.rb', line 113 def publish_if_allowed(transition, user) if user.is_publisher? self.publisher = user self.send(transition) end end |
#regenerate_permalink ⇒ Object
120 121 122 |
# File 'app/models/miniblog/post.rb', line 120 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)
132 133 134 |
# File 'app/models/miniblog/post.rb', line 132 def url_params [self.year, self.month, self.day, self.permalink, 'html'] end |