Class: Blogaze::Controllers::Admin::Posts

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

Instance Method Summary collapse

Methods inherited from Controller

#get_settings, #title, #view_file

Constructor Details

#initializePosts

Returns a new instance of Posts.



15
16
17
18
19
20
# File 'lib/blogaze/controllers/admin/posts.rb', line 15

def initialize
  super

  # Set title
  title "Posts"
end

Instance Method Details

#createObject

Create post



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/blogaze/controllers/admin/posts.rb', line 67

def create
  data = {
    :title => request[:title],
    :body => request[:body],
    :user_id => @userinfo.id,
    :post_tags => request[:post_tags].is_a?(String) ? request[:post_tags].gsub(', ', ',').split(',') : []
  }
  @post = ::Blogaze::Models::Post.new(data)

  if @post.valid?
    @post.save
    flash[:success] = "Post created successfully"
    redirect Posts.r('/')
  else
    @post. = @post..join(', ')
    respond(view_file('admin/posts/new'))
  end
end

#delete(post_id) ⇒ Object

Delete post

Parameters:

  • post_id (Integer)


128
129
130
131
132
133
134
# File 'lib/blogaze/controllers/admin/posts.rb', line 128

def delete(post_id)
  post = ::Blogaze::Models::Post[post_id]
  post.delete
  ::Blogaze::Models::TagsRelationship.where(:object_type => 'post', :object_id => post.id).delete
  flash[:success] = "Post deleted successfully"
  redirect Posts.r('/')
end

#edit(post_id) ⇒ Object

Edit post

Parameters:

  • post_id (Integer)


91
92
93
94
95
96
97
98
99
100
101
# File 'lib/blogaze/controllers/admin/posts.rb', line 91

def edit(post_id)
  @post = ::Blogaze::Models::Post[post_id]

   = []
  @post.tags.each do |tag|
    .push tag.name
  end
  @post. = .join(', ')

  respond(view_file('admin/posts/edit'))
end

#indexObject

Post listing



51
52
53
54
# File 'lib/blogaze/controllers/admin/posts.rb', line 51

def index
  @posts = ::Blogaze::Models::Post.order(:id.desc).all
  respond(view_file('admin/posts/index'))
end

#newObject

New post form



59
60
61
62
# File 'lib/blogaze/controllers/admin/posts.rb', line 59

def new
  @post = ::Blogaze::Models::Post.new
  respond(view_file('admin/posts/new'))
end

#save(post_id) ⇒ Object

Save post

Parameters:

  • post_id (Integer)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/blogaze/controllers/admin/posts.rb', line 108

def save(post_id)
  @post = ::Blogaze::Models::Post[post_id]
  @post.title = request[:title]
  @post.body = request[:body]
  @post. = request[:post_tags].is_a?(String) ? request[:post_tags].gsub(', ', ',').split(',') : []

  if @post.valid?
    @post.save
    flash[:success] = "Post saved successfully"
    redirect Posts.r('/')
  else
    respond(view_file('admin/posts/edit'))
  end
end