Class: Decidim::Posts::PostsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, AttachmentsHelper, Flaggable, FormFactory
Defined in:
app/controllers/decidim/posts/posts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#user_has_no_permission_path

Instance Method Details

#change_statusObject



116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/decidim/posts/posts_controller.rb', line 116

def change_status
  @post = Decidim::Posts::Post.find(params[:id])
  # enable_comments: params[:status] ? true : false
  if @post.update(status: params[:status], enable_comments: false)
    render json: { message: 'Status updated successfully', new_content: cell("decidim/posts/post_host", @post).call(:show) }, status: :ok
  else
    render json: { error: 'Failed to update status' }, status: :unprocessable_entity
  end

end

#createObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/decidim/posts/posts_controller.rb', line 53

def create
  enforce_permission_to :create, :post

  @form = form(PostForm).from_params(post_creation_params)

  CreatePost.call(@form) do
    on(:ok) do |post|
      flash[:notice] = I18n.t("posts.create.success", scope: "decidim.posts")
      # TODO: implement javascript to create a new post without reloading the page
      # redirect_to decidim.root_path
      # redirect_to current_component_path
      redirect_to posts_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("posts.create.invalid", scope: "decidim.posts")
      redirect_to posts_path
    end
  end
end

#destroyObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/decidim/posts/posts_controller.rb', line 99

def destroy
  @post = Post.find(params[:id])
  enforce_permission_to :delete, :post, post: @post

  DestroyPost.call(@post, current_user) do
    on(:ok) do
      flash[:notice] = I18n.t("posts.destroy.success", scope: "decidim.posts")
      redirect_to posts_path
    end

    on(:invalid) do
      flash.now[:alert] = I18n.t("posts.destroy.invalid", scope: "decidim.posts")
      redirect_to posts_path
    end
  end
end

#editObject



74
75
76
77
78
# File 'app/controllers/decidim/posts/posts_controller.rb', line 74

def edit
  @post = Post.find(params[:id])
  enforce_permission_to :edit, :post, post: @post
  @form = Decidim::Posts::PostForm.from_model(@post).with_context(context)
end

#indexObject



15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/decidim/posts/posts_controller.rb', line 15

def index
  enforce_permission_to :read, :post

  @posts = posts().where(fixed: [false, nil])
  @fixed_posts = posts.where(fixed: true).order(created_at: :desc)

  @meetings = meetings()

  @non_fixed_objects = (@posts + @meetings).sort_by(&:created_at).reverse
  @non_fixed_objects = Kaminari.paginate_array(@non_fixed_objects).page(params[:page]).per(20)
end

#load_moreObject

action to load more posts and meetings on the index page via ajax



28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/decidim/posts/posts_controller.rb', line 28

def load_more
  enforce_permission_to :read, :post

  @posts = posts().where(fixed: false)
  @meetings = meetings()

  @non_fixed_objects = (@posts + @meetings).sort_by(&:created_at).reverse
  @non_fixed_objects = Kaminari.paginate_array(@non_fixed_objects).page(params[:page]).per(20)

  render partial: "decidim/posts/posts/feed", locals: { objects: @non_fixed_objects }
end

#newObject



48
49
50
51
# File 'app/controllers/decidim/posts/posts_controller.rb', line 48

def new
  enforce_permission_to :create, :post
  @form = form(PostForm).from_params(post_creation_params)
end

#showObject

Raises:

  • (ActionController::RoutingError)


40
41
42
43
44
45
46
# File 'app/controllers/decidim/posts/posts_controller.rb', line 40

def show
  @post = Post.find(params[:id])

  enforce_permission_to :read, :post

  raise ActionController::RoutingError, "Not Found" unless @post
end

#updateObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/decidim/posts/posts_controller.rb', line 80

def update
  @post = Post.find(params[:id])
  enforce_permission_to :edit, :post, post: @post

  @form = form(PostForm).from_params(params)
  UpdatePost.call(@form, current_user, @post) do
    on(:ok) do |post|
      flash[:notice] = I18n.t("posts.update.success", scope: "decidim.posts")
      redirect_to posts_path
      # redirect_to Decidim::ResourceLocatorPresenter.new(@post).path
    end

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