Module: Lesli::ResponderInterface

Included in:
ApplicationDeviseController, ApplicationLesliController
Defined in:
app/interfaces/lesli/responder_interface.rb

Instance Method Summary collapse

Instance Method Details

#respond_with_action(action, message = "Action Required") ⇒ Object

General method to respond with and order for an action This method exists just for compatibility poruposes, a refactor is needed for this method



199
200
201
# File 'app/interfaces/lesli/responder_interface.rb', line 199

def respond_with_action(action, message = "Action Required")
    respond_with_http(490, { :message => message, :action => action })
end

#respond_with_http(status, payload) ⇒ Object

Just a simple http response for JSON



188
189
190
191
192
193
194
# File 'app/interfaces/lesli/responder_interface.rb', line 188

def respond_with_http(status, payload)
    render(
        :status => status, 
        :content_type => 'application/json', 
        :json => payload.nil? ? "" : payload.to_json
    )
end

#respond_with_json(payload = nil) ⇒ Object

Just a simple json success response for JSON



164
165
166
# File 'app/interfaces/lesli/responder_interface.rb', line 164

def respond_with_json(payload = nil)
    respond_with_http(200, payload)
end

#respond_with_json_not_found(message = nil) ⇒ Object

Just a simple json not found response for JSON



169
170
171
172
173
174
175
# File 'app/interfaces/lesli/responder_interface.rb', line 169

def respond_with_json_not_found(message = nil)
    respond_with_http(404, {
        :status => 404,
        :error => 'Not Found',
        :message => message 
    })
end

#respond_with_json_unauthorized(details) ⇒ Object

Just a simple json unauthorized response for JSON



178
179
180
181
182
183
184
185
# File 'app/interfaces/lesli/responder_interface.rb', line 178

def respond_with_json_unauthorized(details)
    respond_with_http(403, {
        :status => 403,
        :title => 'Unauthorized',
        :message => I18n.t("core.shared.view_text_unauthorized_request"),
        :details => details
    })
end

#respond_with_lesli(turbo: nil, json: nil, html: nil) ⇒ Object

General response builder Support for: turbo, json and html Note: Turbo respond support single stream or array of streams Usage: respond_with(

turbo: [
    stream_notification_success('ticket creado de forma exitosass')
    stream_notification_warning('ticket creado de forma exitosass')
],
json: {
    message: "it works"
},
html: 'lesli/abouts/up'

)



75
76
77
78
79
80
81
# File 'app/interfaces/lesli/responder_interface.rb', line 75

def respond_with_lesli(turbo:nil, json:nil, html:nil)
    respond_to do |format|
        format.html { render(html) }
        format.json { respond_with_json(json) }
        format.turbo_stream { render(turbo_stream: turbo) }
    end
end

#respond_with_not_found(message = nil) ⇒ Object

General error for not found resources Usage: def set_ticket

@ticket = TicketService.new(current_user, query).find(params[:id])
return respond_with_not_found unless @ticket.found?

end



89
90
91
92
93
94
95
96
# File 'app/interfaces/lesli/responder_interface.rb', line 89

def respond_with_not_found message=nil
    @message = message || I18n.t("lesli.shared.message_error_resource_not_found")
    respond_to do |format|
        format.json { respond_with_json_not_found(@message) }
        format.html { render('lesli/errors/not_found', status: :not_found) }
        format.turbo_stream { render('lesli/errors/not_found', status: :not_found) }
    end
end

#respond_with_pagination(payload) ⇒ Object

Standard json structure for paginated data, this is compatible for json, html and turbo responses NOTE: LesliView::Element::Table require this structure

to work properly

IMPORTANT: It is strictly necessary to use the pagination methods

to make this work properly

Usage example tasks = Task .joins(:detail) .page(query[:page]) .per(query[:perPage])

respond_with_pagination(tasks)



49
50
51
52
53
54
55
56
57
58
59
# File 'app/interfaces/lesli/responder_interface.rb', line 49

def respond_with_pagination(payload)
    {
        pagination: {
            page: payload.current_page,
            pages: payload.total_pages,
            total: payload.total_count,
            results: payload.length
        },
        records: payload
    }
end

#respond_with_unauthorized(details = {}) ⇒ Object

General error for unauthorized request Usage:

This method is automatically triggered by the 
LesliShield authentication interface, however
you can use it anywhere by just invoking this method


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/interfaces/lesli/responder_interface.rb', line 103

def respond_with_unauthorized(details = {})

    @error_object = {
        error_role: nil,
        error_details: nil
    }

    # If dev or test, show a clear description about the auth error
    unless Rails.env.production?
        @error_object[:error_details] = details unless details.empty?
        if current_user.present?
            @error_object[:error_role] = "(#{current_user.roles.map(&:name).join(', ')})"
        end
    end

    respond_to do |format|
        format.json { respond_with_json_unauthorized(@error_object) }
        format.html { render('lesli/errors/unauthorized', status: :unauthorized) }
        format.turbo_stream { render('lesli/errors/unauthorized', status: :unauthorized) }
    end
end

#stream_redirection(path) ⇒ Object

Stream a render for a partial that includes javascript code to force redirections even when working with turbo streams



155
156
157
158
159
160
161
# File 'app/interfaces/lesli/responder_interface.rb', line 155

def stream_redirection(path)
    turbo_stream.update(
        "application-lesli-notifications",
        partial: "lesli/partials/turbo/redirection",
        locals: { redirect_path: path }
    )
end