Class: Blabs::BlogPostsController

Inherits:
BlabsController show all
Defined in:
app/controllers/blabs/blog_posts_controller.rb

Instance Method Summary collapse

Methods inherited from BlabsController

#blabs_admin_required, #blabs_current_user_name, #check_is_admin, #pretty_blog_post_path, #pretty_blog_post_url

Instance Method Details

#createObject



56
57
58
59
60
61
62
# File 'app/controllers/blabs/blog_posts_controller.rb', line 56

def create
  if params[:preview]
    preview
  else
    create_blog_post
  end
end

#destroyObject



85
86
87
88
89
90
91
# File 'app/controllers/blabs/blog_posts_controller.rb', line 85

def destroy
  find_blog_post
  not_found unless @blog_post
  @blog_post.destroy
  flash[:notice] = "Blog post deleted successfully"
  redirect_to "/blog"
end

#editObject



64
65
66
67
68
69
# File 'app/controllers/blabs/blog_posts_controller.rb', line 64

def edit
  find_blog_post
  @images = Blabs::Image.order("created_at desc")
  not_found unless @blog_post
  render layout: "blabs"
end

#feedObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/blabs/blog_posts_controller.rb', line 33

def feed
  # this will be the name of the feed displayed on the feed reader
  @title = Blabs.blog_title

  # the news items
  @posts = Blabs::BlogPost.where(published: true).order("created_at desc")

  # this will be our Feed's update timestamp
  @updated = @posts.first.updated_at unless @posts.empty?
  respond_to do |format|
    format.atom { render layout: false }

    # we want the RSS feed to redirect permanently to the ATOM feed
    format.rss { redirect_to feed_path(format: :atom), status: :moved_permanently }
  end
end

#indexObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/blabs/blog_posts_controller.rb', line 14

def index
  if blabs_admin_required
    if params[:tag]
      @blog_posts = Blabs::BlogPost.tagged_with(params[:tag]).order("created_at desc")
    else
      @blog_posts = Blabs::BlogPost.order("created_at desc")
    end
  else

    if params[:tag]
      @blog_posts = Blabs::BlogPost.where(published: true).tagged_with(params[:tag]).order("created_at desc")
    else
      @blog_posts = Blabs::BlogPost.where(published: true).order("created_at desc")
    end


  end
end

#newObject



50
51
52
53
54
# File 'app/controllers/blabs/blog_posts_controller.rb', line 50

def new
  @blog_post = Blabs::BlogPost.new(author: blabs_current_user_name)
  @images = Blabs::Image.order("created_at desc")
  render layout: "blabs"
end

#showObject



5
6
7
8
9
10
11
12
# File 'app/controllers/blabs/blog_posts_controller.rb', line 5

def show
  if blabs_admin_required
    find_blog_post
  else
    @blog_post = Blabs::BlogPost.where(path: params[:id], published: true).first
  end
  not_found unless @blog_post
end

#updateObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/controllers/blabs/blog_posts_controller.rb', line 71

def update
  find_blog_post
  not_found unless @blog_post
  if @blog_post.update_attributes(blog_post_params)
    @blog_post.toggle_publish!(params[:publish])
    flash[:notice] = "Blog post updated successfully"
    redirect_to pretty_blog_post_path(@blog_post)
  else
    @images = Image.all
    flash[:error] = "There was a problem updating this blog post"
    render :edit, layout: "blabs"
  end
end