Module: Lesli::Interfaces::Controllers::Discussions

Defined in:
app/controllers/lesli/interfaces/controllers/discussions.rb

Instance Method Summary collapse

Instance Method Details

#createJson

Returns Json that contains wheter the creation of the discussion was successful or not. If it is not successful, it returs an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let data = {
    ticket_discussion: {
        content: "This is a comment on a ticket!"
    }
};
this.http.post(`127.0.0.1/help/tickets/${ticket_id}/discussions`, data);

Returns:

  • (Json)

    Json that contains wheter the creation of the discussion was successful or not. If it is not successful, it returs an error message



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/lesli/interfaces/controllers/discussions.rb', line 98

def create
    discussion_model = discussion_model() # If there is a custom discussion model, it must be returned in this method
    cloud_object_model = discussion_model.cloud_object_model

    set_cloud_object
    new_discussion_params = { 
        #"#{discussion_model.table_name}_id": discussion_params[:discussion_parent_id],
        content: discussion_params[:content],
        user_creator: current_user,
        cloud_object: @cloud_object
    }
    

    discussion = discussion_model.new(new_discussion_params)
    if discussion.save
        translations_path = @cloud_object.class.name.gsub("Cloud", "").underscore.pluralize.gsub("/", ".")
        cloud_object_class_translation = I18n.t("#{translations_path}.view_title_main")

        "#{cloud_object_model}::Subscriber".constantize.notify_subscribers(
            current_user,
            discussion.cloud_object,
            "discussion_created",
            subject: "#{cloud_object_class_translation} (#{@cloud_object.global_identifier}): #{I18n.t("core.shared.view_title_notification_discussions_created")}",
            body: "#{discussion.user_creator.full_name} #{I18n.t("core.shared.view_text_notification_discussion_created_body")}: '#{discussion.content}'",
            url: "/#{@cloud_object.class.name.split("::").last.pluralize.downcase}/#{@cloud_object.url_identifier}?tab=discussions"
        ) if Object.const_defined?("#{cloud_object_model}::Subscriber")
        

        if block_given?
            yield(cloud_object, discussion)
        else
            # Returning the 200 HTTP response
            respond_with_successful(discussion.show(current_user))
        end
    else
        respond_with_error(discussion.errors.full_messages)
    end
end

#destroyJson

Returns A response that contains wheter the discussion was deleted or not. If it is not successful, it returns an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let discussion_id = 22;
this.http.delete(`127.0.0.1/help/tickets/${ticket_id}/discussions/${discussion_id}`);

Returns:

  • (Json)

    A response that contains wheter the discussion was deleted or not. If it is not successful, it returns an error message



172
173
174
175
176
177
178
179
180
181
# File 'app/controllers/lesli/interfaces/controllers/discussions.rb', line 172

def destroy
    set_discussion
    return respond_with_not_found unless @discussion
    
    if @discussion.destroy
        respond_with_successful
    else
        respond_with_error(@discussion.errors.full_messages.to_sentence)
    end
end

#indexJson

Returns Json that contains a list of all discussions related to a cloud_object.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
this.http.get(`127.0.0.1/help/tickets/${ticket_id}/discussions`);

Returns:

  • (Json)

    Json that contains a list of all discussions related to a cloud_object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/lesli/interfaces/controllers/discussions.rb', line 46

def index
    discussion_model = discussion_model() # If there is a custom discussion model, it must be returned in this method
    cloud_object_model = discussion_model.cloud_object_model

    @discussions = discussion_model.index(
        current_user, 
        params["#{cloud_object_model.name.demodulize.underscore}_id".to_sym], 
        @query
    )
    if block_given?
        yield(@discussions)
    else
        #respond_with_successful(@discussions)
        respond_with_pagination(@discussions)
    end
end

#showJSON

Returns The json information about the selected discussion.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let discussion_id = 5;
this.http.get(`127.0.0.1/help/tickets/${ticket_id}/discussions/${discussion_id}`);

Returns:

  • (JSON)

    The json information about the selected discussion



72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/lesli/interfaces/controllers/discussions.rb', line 72

def show
    set_discussion
    return respond_with_not_found unless @discussion

    if block_given?
        yield(@discussion)
    else
        return respond_with_successful(@discussion)
    end
end

#updateJson

Returns Json that contains wheter the update of the discussion was successful or not. If it is not successful, it returs an error message.

Examples:

# Executing this controller's action from javascript's frontend
let ticket_id = 1;
let discussion_id = 22;
data = {
    discussion: {
        content: "This is the new content of the discussion"
    }
};
this.http.patch(`127.0.0.1/help/tickets/${ticket_id}/discussions/${discussion_id}`, data);

Returns:

  • (Json)

    Json that contains wheter the update of the discussion was successful or not. If it is not successful, it returs an error message



152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/lesli/interfaces/controllers/discussions.rb', line 152

def update
    set_discussion
    return respond_with_not_found unless @discussion
    return respond_with_unauthorized unless @discussion.is_editable_by?(current_user)

    if @discussion.update(discussion_params)
        respond_with_successful
    else
        respond_with_error(@discussion.errors.full_messages.to_sentence)
    end
end