Class: Crowdblog::Post

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_posts_jsonObject



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



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

.publishedObject



59
60
61
# File 'app/models/crowdblog/post.rb', line 59

def published
  where(state: 'published')
end

.published_and_orderedObject



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

Instance Method Details

#allowed_to_update_permalink?Boolean

INSTANCE METHODS

Returns:

  • (Boolean)


79
80
81
# File 'app/models/crowdblog/post.rb', line 79

def allowed_to_update_permalink?
  !self.published?
end

#dayObject



83
84
85
# File 'app/models/crowdblog/post.rb', line 83

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

#formatted_published_dateObject



87
88
89
# File 'app/models/crowdblog/post.rb', line 87

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

#html_bodyObject



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.author = user
  self.save
  self.publish
  self.update_attribute(:permalink, results[2])
end

#monthObject



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


118
119
120
# File 'app/models/crowdblog/post.rb', line 118

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)


130
131
132
# File 'app/models/crowdblog/post.rb', line 130

def url_params
  [self.year, self.month, self.day, self.permalink, 'html']
end