Module: Padrino::Response::Helpers::Controller
- Defined in:
- lib/padrino-response/helpers/controller.rb
Instance Method Summary collapse
-
#action_name ⇒ Object
Returns name of current action.
-
#api_request ⇒ Object
A request from a non-browser user.
-
#back_or_default(default) ⇒ Object
Returns url.
-
#controller_name ⇒ Object
Returns name of current controller.
-
#human_model_name(object) ⇒ Object
Returns translated, human readable name for specified model.
-
#notify(kind, message, *args, &block) ⇒ Object
Shortcut for
notifier.say
method. -
#try_render(object, detour_name = nil, responder) ⇒ Object
Trys to render and then falls back to to_format.
Instance Method Details
#action_name ⇒ Object
Returns name of current action
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/padrino-response/helpers/controller.rb', line 50 def action_name if request.respond_to? :action action = request.action else action, parameters = Padrino.mounted_apps[0].app_obj.recognize_path request.path_info action = action.to_s action.gsub!(/^#{controller_name}_?/, '') end action = 'index' if action == '' action end |
#api_request ⇒ Object
A request from a non-browser user. Could be ajax, via curl etc.
8 9 10 |
# File 'lib/padrino-response/helpers/controller.rb', line 8 def api_request (request.xhr? or content_type == :json or mime_type(:json) == request.preferred_type.to_s) end |
#back_or_default(default) ⇒ Object
Returns url
85 86 87 88 |
# File 'lib/padrino-response/helpers/controller.rb', line 85 def back_or_default(default) return_to = session.delete(:return_to) return_to || default end |
#controller_name ⇒ Object
Returns name of current controller
65 66 67 |
# File 'lib/padrino-response/helpers/controller.rb', line 65 def controller_name request.controller end |
#human_model_name(object) ⇒ Object
Returns translated, human readable name for specified model.
72 73 74 75 76 77 78 79 80 |
# File 'lib/padrino-response/helpers/controller.rb', line 72 def human_model_name(object) if object.class.respond_to?(:human) object.class.human elsif object.class.respond_to?(:human_name) object.class.human_name else I18n.translate("models.#{object.class.to_s.underscore}", :default => object.class.to_s.humanize) end end |
#notify(kind, message, *args, &block) ⇒ Object
Shortcut for notifier.say
method.
15 16 17 |
# File 'lib/padrino-response/helpers/controller.rb', line 15 def notify(kind, , *args, &block) settings.notifier.say(self, kind, , *args, &block) if settings.notifier end |
#try_render(object, detour_name = nil, responder) ⇒ Object
Trys to render and then falls back to to_format
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/padrino-response/helpers/controller.rb', line 22 def try_render(object, detour_name=nil, responder) begin if responder.layout render "#{controller_name}/#{detour_name || action_name}", :layout => responder.layout, :strict_format => true else render "#{controller_name}/#{detour_name || action_name}", :strict_format => true end rescue Exception => e if api_request if responder.jsend? && object.respond_to?(:attributes) {:status => (responder..include?(:status) ? responder.[:status] : 200), :data => {responder.human_model_name.to_sym => object.attributes}}.to_json else return object.to_json if object.respond_to?(:to_json) end end if content_type == :xml or mime_type(:xml) == request.preferred_type return object.to_xml if object.respond_to?(:to_xml) end # raise ::Padrino::Responder::ResponderError, e.message end end |