Class: Blogaze::Controllers::Posts

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

Instance Method Summary collapse

Methods inherited from Controller

#get_settings, #initialize, #title, #view_file

Constructor Details

This class inherits a constructor from Blogaze::Controllers::Controller

Instance Method Details

#tag(slug, page = 1) ⇒ Object

Lists posts belonging to the tag.

Parameters:

  • slug (String)

    Tag slug

  • page (Integer) (defaults to: 1)

    Page number



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/blogaze/controllers/posts.rb', line 56

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

  # Set title
  title "Posts tagged #{@tag.name}"

  # 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

Parameters:

  • slug (String)

    Post slug



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
47
48
# File 'lib/blogaze/controllers/posts.rb', line 20

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

  # 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