Class: Decidim::Blogs::PostsController

Inherits:
ApplicationController show all
Includes:
FormFactory, IconHelper, Flaggable, Paginable
Defined in:
app/controllers/decidim/blogs/posts_controller.rb

Overview

Exposes the blog resource so users can view them

Instance Method Summary collapse

Instance Method Details

#createObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 28

def create
  enforce_permission_to :create, :blogpost
  @form = form(Decidim::Blogs::PostForm).from_params(params, current_component:)

  CreatePost.call(@form) do
    on(:ok) do |new_post|
      flash[:notice] = I18n.t("posts.create.success", scope: "decidim.blogs.admin")
      redirect_to post_path(new_post)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("posts.create.invalid", scope: "decidim.blogs.admin")
      render action: "new", status: :unprocessable_content
    end
  end
end

#destroyObject



67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 67

def destroy
  enforce_permission_to :destroy, :blogpost, blogpost: post

  Decidim.traceability.perform_action!("delete", post, current_user) do
    post.destroy!
  end

  flash[:notice] = I18n.t("posts.destroy.success", scope: "decidim.blogs.admin")

  redirect_to posts_path
end

#editObject



45
46
47
48
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 45

def edit
  enforce_permission_to :update, :blogpost, blogpost: post
  @form = form(PostForm).from_model(post)
end

#indexObject



17
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 17

def index; end

#newObject



23
24
25
26
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 23

def new
  enforce_permission_to :create, :blogpost
  @form = form(Decidim::Blogs::PostForm).instance
end

#showObject

Raises:

  • (ActionController::RoutingError)


19
20
21
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 19

def show
  raise ActionController::RoutingError, "Not Found" unless post
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/decidim/blogs/posts_controller.rb', line 50

def update
  enforce_permission_to :update, :blogpost, blogpost: post
  @form = form(PostForm).from_params(params, current_component:)

  UpdatePost.call(@form, post) do
    on(:ok) do |post|
      flash[:notice] = I18n.t("posts.update.success", scope: "decidim.blogs.admin")
      redirect_to post_path(post)
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("posts.update.invalid", scope: "decidim.blogs.admin")
      render action: "edit", status: :unprocessable_content
    end
  end
end