Class: Padrino::Responders::Jsend

Inherits:
Default
  • Object
show all
Includes:
Padrino::Response::StatusCodes
Defined in:
lib/padrino-response/responders/jsend.rb

Constant Summary

Constants included from Padrino::Response::StatusCodes

Padrino::Response::StatusCodes::STATUS_CODES, Padrino::Response::StatusCodes::SYMBOL_TO_STATUS_CODE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Padrino::Response::StatusCodes

#interpret_status

Methods inherited from Default

#action_name, #api_request, #controller_name, #human_model_name, #layout, #location, #message, #notify, #post, #put, #redirect, #request, #respond, #set_status, #try_render, #valid?

Constructor Details

#initializeJsend

Returns a new instance of Jsend.



7
8
9
# File 'lib/padrino-response/responders/jsend.rb', line 7

def initialize
  @options = {}
end

Instance Attribute Details

#classObject

Returns the value of attribute class.



5
6
7
# File 'lib/padrino-response/responders/jsend.rb', line 5

def class
  @class
end

#objectObject

Returns the value of attribute object.



5
6
7
# File 'lib/padrino-response/responders/jsend.rb', line 5

def object
  @object
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/padrino-response/responders/jsend.rb', line 5

def options
  @options
end

Instance Method Details

#defaultObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/padrino-response/responders/jsend.rb', line 79

def default
  if location
    if request.xhr?
      {:status => :success, :data => { :redirect => location } }.to_json
    else
      redirect location
    end
  else
    try_render
  end
end

#deleteObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/padrino-response/responders/jsend.rb', line 54

def delete
  message = message(:destroy)

  if request.xhr?
    ajax_obj = {
      :status => :success,
      :data => {
        :message => message
      }
    }
  end

  if location
    if request.xhr?
      ajax_obj[:data][:redirect] = location
      return ajax_obj.to_json
    else
      notify(:notice, message)
      redirect location
    end
  else
    try_render
  end
end

#jsend?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/padrino-response/responders/jsend.rb', line 11

def jsend?
  return true
end

#put_or_post(message_type, error_detour) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/padrino-response/responders/jsend.rb', line 15

def put_or_post(message_type, error_detour)
  message = message( message_type )
  if valid?
    if request.xhr?
      ajax_obj = {
        :status => :success,
        :data => {
          object.class.to_s.singularize.downcase => object
        }
      }
    end
    if location
      if request.xhr?
        ajax_obj[:data][:redirect] = location
        return ajax_obj.to_json
      else
        notify(:notice, message)
        redirect location
      end
    else
      try_render
    end
  else
    if request.xhr?
      ajax_obj = {
        :status => :fail,
        :data => {
          :errors => object.errors
        }
      }
      ajax_obj[:data][:redirect] = location if location
      return ajax_obj.to_json
    else
      notify(:error, message)
      try_render error_detour
    end
  end
end