Class: Padrino::Responders::Default

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

Direct Known Subclasses

Jsend

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

Constructor Details

#initializeDefault

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

#classObject

Returns the value of attribute class.



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

def class
  @class
end

#objectObject

Returns the value of attribute object.



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

def object
  @object
end

#optionsObject

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_nameObject



142
143
144
# File 'lib/padrino-response/responders/default.rb', line 142

def action_name
  self.class.action_name
end

#api_requestObject



130
131
132
# File 'lib/padrino-response/responders/default.rb', line 130

def api_request
  self.class.api_request
end

#controller_nameObject



138
139
140
# File 'lib/padrino-response/responders/default.rb', line 138

def controller_name
  self.class.controller_name
end

#defaultObject



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

#deleteObject



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_nameObject



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

Jsend format?

Returns:

  • (Boolean)


14
15
16
# File 'lib/padrino-response/responders/default.rb', line 14

def jsend?
  return false
end

#layoutObject



150
151
152
# File 'lib/padrino-response/responders/default.rb', line 150

def layout
  return @options[:layout] if @options.include?(:layout)   
end

#locationObject



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

#postObject



60
61
62
# File 'lib/padrino-response/responders/default.rb', line 60

def post
  put_or_post :create, 'new'
end

#putObject



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

#requestObject



114
115
116
# File 'lib/padrino-response/responders/default.rb', line 114

def request
  self.class.request
end

#respondObject



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

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/padrino-response/responders/default.rb', line 106

def valid?
  valid = true
  # `valid?` method may override existing errors, so check for those first
  valid &&= object.valid? if object.respond_to?(:valid?)
  valid &&= (object.errors.count == 0) if object.respond_to?(:errors)
  return valid
end