Class: Blogaze::Controllers::Posts

Inherits:
Blogaze::Controller show all
Defined in:
lib/blogaze/controllers/posts.rb

Instance Method Summary collapse

Methods inherited from Blogaze::Controller

#get_settings, #initialize, #view_file

Constructor Details

This class inherits a constructor from Blogaze::Controller

Instance Method Details

#tag(slug, page = 1) ⇒ Object

Lists posts belonging to the tag.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/blogaze/controllers/posts.rb', line 51

def tag(slug, page = 1)
  @tag = ::Blogaze::Models::Tag[:slug => slug]
  return respond(view_file('pages/notfound')) if @tag.nil?

  # Get relationships with posts and put the post IDs
  # into an array for use when selecting posts to be
  # paginated.
  post_ids = []
  relations = ::Blogaze::Models::TagsRelationship.where(:object_type => 'post', :tag_id => @tag.id)
  relations.each do |rel|
    # Due to object_id being a Ruby method, we need to
    # get the object ID the alternative way.
    post_ids.push rel[:object_id]
  end

  puts post_ids

  # Get the data and paginate it.
  data = ::Blogaze::Models::Post.where(:id => post_ids).order(:published_at.desc)
  puts data.sql
  @posts = paginate(data, :limit => @settings[:posts_per_page].to_i, :page => page.to_i)

  respond(view_file(:index))
end

#view(slug) ⇒ Object

View post



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/blogaze/controllers/posts.rb', line 18

def view(slug)
  @post = ::Blogaze::Models::Post.filter(:slug => slug).first
  return respond(view_file('pages/notfound')) if @post.nil?

  # Set title
  @title = @post.title + ' - ' + @settings[:title]

  # Instead of a controller just for comment creation
  # let's just put it here, that way we can display
  # errors with the new comment form.
  if request[:new_comment].to_i == 1
    data = {
      :post_id => @post.id,
      :author => request[:author],
      :body => request[:body],
      :in_moderation => @settings[:moderate_comments]
    }
    @comment = ::Blogaze::Models::Comment.new(data)
    if @comment.valid?
      @comment.save
      flash[:success] = "Your comment has been posted"
      redirect @post.href
    end
  else
    @comment = ::Blogaze::Models::Comment.new
  end

  respond(view_file('posts/view'))
end