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



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

def create
  enforce_permission_to :create, :blogpost
  @form = form(Decidim::Blogs::PostForm).from_params(params, current_component: 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_entity
    end
  end
end

#destroyObject



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

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



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

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

#indexObject



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

def index; end

#newObject



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

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

#showObject

Raises:

  • (ActionController::RoutingError)


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

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

#updateObject



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

def update
  enforce_permission_to :update, :blogpost, blogpost: post
  @form = form(PostForm).from_params(params, current_component: 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_entity
    end
  end
end