Class: Padrino::Responders::Default
Direct Known Subclasses
Jsend
Constant Summary
Padrino::Response::StatusCodes::STATUS_CODES, Padrino::Response::StatusCodes::SYMBOL_TO_STATUS_CODE
Instance Attribute Summary collapse
Instance Method Summary
collapse
#interpret_status
Constructor Details
Returns a new instance of Default.
9
10
11
|
# File 'lib/padrino-response/responders/default.rb', line 9
def initialize
@options = {}
end
|
Instance Attribute Details
#class ⇒ Object
Returns the value of attribute class.
7
8
9
|
# File 'lib/padrino-response/responders/default.rb', line 7
def class
@class
end
|
#object ⇒ Object
Returns the value of attribute object.
7
8
9
|
# File 'lib/padrino-response/responders/default.rb', line 7
def object
@object
end
|
#options ⇒ Object
Returns the value of attribute options.
7
8
9
|
# File 'lib/padrino-response/responders/default.rb', line 7
def options
@options
end
|
Instance Method Details
#action_name ⇒ Object
142
143
144
|
# File 'lib/padrino-response/responders/default.rb', line 142
def action_name
self.class.action_name
end
|
#api_request ⇒ Object
130
131
132
|
# File 'lib/padrino-response/responders/default.rb', line 130
def api_request
self.class.api_request
end
|
#controller_name ⇒ Object
138
139
140
|
# File 'lib/padrino-response/responders/default.rb', line 138
def controller_name
self.class.controller_name
end
|
#default ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/padrino-response/responders/default.rb', line 79
def default
if location
redirect location
else
try_render
end
end
|
#delete ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/padrino-response/responders/default.rb', line 64
def delete
message = message(:destroy)
if location
if api_request
redirect location, {:message => message}.to_json
else
notify(:notice, message)
redirect location
end
else
try_render
end
end
|
#human_model_name ⇒ Object
134
135
136
|
# File 'lib/padrino-response/responders/default.rb', line 134
def human_model_name
self.class.human_model_name(object)
end
|
#jsend? ⇒ Boolean
14
15
16
|
# File 'lib/padrino-response/responders/default.rb', line 14
def jsend?
return false
end
|
#layout ⇒ Object
150
151
152
|
# File 'lib/padrino-response/responders/default.rb', line 150
def layout
return @options[:layout] if @options.include?(:layout)
end
|
#location ⇒ Object
146
147
148
|
# File 'lib/padrino-response/responders/default.rb', line 146
def location
@options[:location]
end
|
#message(type) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/padrino-response/responders/default.rb', line 87
def message(type)
return @options[:message] if @options[:message]
return @options[:error_message] if @options[:error_message] and !valid
return @options[:success_message] if @options[:success_message] and valid
return object.errors.full_messages if !valid?
object_notice = "responder.messages.#{controller_name}.#{type}"
alternative_notice = "responder.messages.default.#{type}"
object_notice = I18n.translate(object_notice, :model => human_model_name)
alternative_notice = I18n.translate(alternative_notice, :model => human_model_name)
return object_notice unless object_notice.blank?
return alternative_notice unless alternative_notice.blank?
return 'No message found in locale'
end
|
#notify(kind, message, *args, &block) ⇒ Object
118
119
120
|
# File 'lib/padrino-response/responders/default.rb', line 118
def notify(kind, message, *args, &block)
self.class.notify(kind, message, *args, &block)
end
|
#post ⇒ Object
60
61
62
|
# File 'lib/padrino-response/responders/default.rb', line 60
def post
put_or_post :create, 'new'
end
|
#put ⇒ Object
56
57
58
|
# File 'lib/padrino-response/responders/default.rb', line 56
def put
put_or_post :update, 'edit'
end
|
#put_or_post(message_type, error_detour) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/padrino-response/responders/default.rb', line 32
def put_or_post(message_type, error_detour)
message = message(message_type)
if valid?
if location
if api_request
content = try_render
redirect location, content
else
notify(:notice, message)
redirect location
end
end
else
set_status 400
if api_request
return object.errors.to_json
else
notify(:error, message)
try_render error_detour
end
end
end
|
#redirect(*args) ⇒ Object
126
127
128
|
# File 'lib/padrino-response/responders/default.rb', line 126
def redirect(*args)
self.class.redirect(*args)
end
|
#request ⇒ Object
114
115
116
|
# File 'lib/padrino-response/responders/default.rb', line 114
def request
self.class.request
end
|
#respond ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/padrino-response/responders/default.rb', line 18
def respond
set_status
if self.class.request.put?
put
elsif self.class.request.post?
post
elsif self.class.request.delete?
delete
else
default
end
end
|
#set_status(status = nil) ⇒ Object
154
155
156
157
158
159
160
161
162
|
# File 'lib/padrino-response/responders/default.rb', line 154
def set_status(status=nil)
if status.is_a?(Integer)
self.class.status status
elsif status.is_a?(String)
self.class.status SYMBOL_TO_STATUS_CODE[status]
else
self.class.status SYMBOL_TO_STATUS_CODE[@options[:status]] if @options[:status]
end
end
|
#try_render(detour_name = nil) ⇒ Object
122
123
124
|
# File 'lib/padrino-response/responders/default.rb', line 122
def try_render(detour_name=nil)
self.class.try_render(object, detour_name, self)
end
|
#valid? ⇒ Boolean
106
107
108
109
110
111
112
|
# File 'lib/padrino-response/responders/default.rb', line 106
def valid?
valid = true
valid &&= object.valid? if object.respond_to?(:valid?)
valid &&= (object.errors.count == 0) if object.respond_to?(:errors)
return valid
end
|