-
-
Save effkay/853703 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Extranett | |
| module Responders | |
| module SenchaResponder | |
| def to_json | |
| render :json => compose_json, :status => http_status, :location => location | |
| end | |
| private | |
| def compose_json | |
| [:success, :message, :total, :data, :errors].inject({}) { |json, part| json.merge(send(part)) } | |
| end | |
| def errors | |
| has_errors? ? { :errors => resource.errors } : {} | |
| end | |
| def data | |
| !has_errors? && (controller.action_name != 'destroy') ? { :data => resource } : {} | |
| end | |
| def total | |
| controller.action_name == 'index' && resource.present? ? { :total => resource.count } : {} | |
| end | |
| def success | |
| http_status_code >= 400 ? { :success => false } : { :success => true } | |
| end | |
| def message | |
| options = mount_i18n_options(http_status) | |
| message = I18n.t options[:default].shift, options | |
| message.present? ? { :message => message } : {} | |
| end | |
| def location | |
| options[:location] || detect_location | |
| end | |
| def detect_location | |
| resource.present? && controller.action_name == 'create' && http_status == :created ? controller.url_for(resource) : nil | |
| end | |
| def http_status | |
| options[:status] || detect_http_status | |
| end | |
| def detect_http_status | |
| if has_errors? | |
| :unprocessable_entity | |
| else | |
| controller.action_name == 'create' ? :created : :ok | |
| end | |
| end | |
| def http_status_code | |
| http_status.class == Symbol ? Rack::Utils::SYMBOL_TO_STATUS_CODE[http_status] : http_status | |
| end | |
| # I18n code taken from the Responders gem | |
| def mount_i18n_options(status) | |
| resource_name = if resource.class.respond_to?(:model_name) | |
| resource.class.model_name.human | |
| else | |
| resource.class.name.underscore.humanize | |
| end | |
| options = { | |
| :default => flash_defaults_by_namespace(status), | |
| :resource_name => resource_name, | |
| :downcase_resource_name => resource_name.downcase | |
| } | |
| if has_errors? | |
| options.merge!({ :count => resource.errors.size }) | |
| end | |
| if controller.respond_to?(:interpolation_options, true) | |
| options.merge!(controller.send(:interpolation_options)) | |
| end | |
| options | |
| end | |
| def flash_defaults_by_namespace(status) | |
| defaults = [] | |
| slices = controller.controller_path.split('/') | |
| while slices.size > 0 | |
| defaults << :"message.#{ slices.fill(controller.controller_name, -1).join('.') }.#{ controller.action_name }.#{ status }" | |
| defaults << :"message.#{ slices.fill(:actions, -1).join('.') }.#{ controller.action_name }.#{ status }" | |
| slices.shift | |
| end | |
| defaults << "" | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment