Class: Miniblog::Post

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/miniblog/post.rb

Constant Summary collapse

LEGACY_TITLE_REGEXP =
/(\d+-\d+-\d+)-(.*)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#transitionObject

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_jsonObject



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 by_author(author_id)
  published_and_ordered.where(author_id: author_id)
end

.for_admin_indexObject



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_dateObject



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_stateObject



77
78
79
# File 'app/models/miniblog/post.rb', line 77

def ordered_by_state
  order(:state)
end

.publishedObject



61
62
63
# File 'app/models/miniblog/post.rb', line 61

def published
  where(state: 'published')
end

.published_and_orderedObject



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.authored_posts
end

Instance Method Details

#allowed_to_update_permalink?Boolean

INSTANCE METHODS

Returns:

  • (Boolean)


89
90
91
# File 'app/models/miniblog/post.rb', line 89

def allowed_to_update_permalink?
  !self.published?
end

#dayObject



93
94
95
# File 'app/models/miniblog/post.rb', line 93

def day
  "%02d" % published_at.day
end

#formatted_published_dateObject



97
98
99
# File 'app/models/miniblog/post.rb', line 97

def formatted_published_date
  published_at.strftime("%b %d, %Y")
end

#html_bodyObject



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

#monthObject



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


120
121
122
# File 'app/models/miniblog/post.rb', line 120

def regenerate_permalink
  self.permalink = title.parameterize
end

#url_paramsObject

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