Class: Decidim::Notify::ConversationsController

Inherits:
ApplicationController show all
Includes:
Broadcasts, NeedsAjaxRescue, FormFactory
Defined in:
app/controllers/decidim/notify/conversations_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#permission_class_chain

Instance Method Details

#createObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/decidim/notify/conversations_controller.rb', line 21

def create
  enforce_permission_to :create, :notes

  @form = form(NoteForm).from_params(params)
  CreateNote.call(@form) do
    on(:ok) do |note, new_chapter|
      broadcast_create_chapter new_chapter if new_chapter
      broadcast_create_note note

      render json: { message: "" }
    end
    on(:invalid) do |message|
      render json: { message: I18n.t("decidim.notify.conversations.create.error", message: message) }, status: :unprocessable_entity
    end
  end
end

#destroyObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/decidim/notify/conversations_controller.rb', line 55

def destroy
  enforce_permission_to :destroy, :notes

  DeleteNote.call(params[:id]) do
    on(:ok) do
      broadcast_destroy_note params[:id]
      render json: { message: "" }
    end
    on(:invalid) do |message|
      render json: { message: I18n.t("decidim.notify.conversations.destroy.error", message: message) }, status: :unprocessable_entity
    end
  end
end

#indexObject



12
13
14
15
16
17
18
19
# File 'app/controllers/decidim/notify/conversations_controller.rb', line 12

def index
  return render :private unless allowed_to? :index, :notes

  @unclassified = Chapter.new(notes: unclassified_notes)
  @participants = Author.for(current_component).participants
  @note_takers = Author.for(current_component).note_takers
  @form = form(NoteForm).instance
end

#updateObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/decidim/notify/conversations_controller.rb', line 38

def update
  enforce_permission_to :update, :notes

  @form = form(NoteForm).from_params(params)
  UpdateNote.call(@form) do
    on(:ok) do |note, new_chapter|
      broadcast_create_chapter new_chapter if new_chapter
      broadcast_update_note note

      render json: { message: "" }
    end
    on(:invalid) do |message|
      render json: { message: I18n.t("decidim.notify.conversations.update.error", message: message) }, status: :unprocessable_entity
    end
  end
end

#usersObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/decidim/notify/conversations_controller.rb', line 69

def users
  enforce_permission_to :create, :notes

  respond_to do |format|
    format.json do
      if (term = params[:term].to_s).present?
        query = Author.for(current_component).joins(:user)
        query = query.where("decidim_notify_authors.code=:code OR decidim_users.name ILIKE :term OR decidim_users.nickname ILIKE :term", code: term.to_i, term: "%#{term}%")

        render json: query.all.collect { |u| { id: u.code, name: u.name, avatar: u.attached_uploader(:avatar).path, nickname: u.nickname, text: format_user_name(u) } }
      else
        render json: []
      end
    end
  end
end