module CrudResponder

Constants

VERSION

Public Instance Methods

crud_respond(object, opts = {}) click to toggle source
# File lib/crud_responder.rb, line 11
def crud_respond(object, opts = {})
  method = opts.fetch(:method, CallerExtractor.new(caller).method)
  options = final_options(opts, method, object)
  if object.public_send(method)
    success(options, object, caller)
  else
    error(options, object, caller)
  end
end

Private Instance Methods

default_options(method, object) click to toggle source
# File lib/crud_responder.rb, line 51
def default_options(method, object)
  @_default_options ||= DefaultOptions.new(method, object)
end
default_options_for(opt, method, object) click to toggle source
# File lib/crud_responder.rb, line 47
def default_options_for(opt, method, object)
  default_options(method, object).public_send(opt)
end
error(options, object, kaller) click to toggle source
# File lib/crud_responder.rb, line 28
def error(options, object, kaller)
  flash_error(options[:error_message] || DefaultNotification.new(object, kaller).text(false))
  if options[:error_url]
    redirect_to_url_smart(options[:error_url])
  else
    render options[:error_action]
  end
end
final_options(opts, method, object) click to toggle source
# File lib/crud_responder.rb, line 37
def final_options(opts, method, object)
  @_options ||= begin
    {}.tap do |result|
      DefaultOptions.all_available.each do |opt|
        result[opt] = opts.fetch(opt) { |key| specific_options_for(key) || default_options_for(key, method, object) }
      end
    end
  end
end
flash_error(msg) click to toggle source
# File lib/crud_responder.rb, line 66
def flash_error(msg)
  flash[:alert] = truncate_message(msg)
end
flash_success(msg) click to toggle source
# File lib/crud_responder.rb, line 62
def flash_success(msg)
  flash[:notice] = truncate_message(msg)
end
redirect_to_url_smart(url) click to toggle source
# File lib/crud_responder.rb, line 74
def redirect_to_url_smart(url)
  if url == :back && Rails.version >= '5.1'
    redirect_back(fallback_location: '/')
  else
    redirect_to url
  end
end
specific_options_for(opt) click to toggle source
# File lib/crud_responder.rb, line 55
def specific_options_for(opt)
  return nil unless respond_to?(:crud_responder_default_options, true)
  result = send(:crud_responder_default_options)
  return nil unless result
  result.fetch(opt, nil)
end
success(options, object, kaller) click to toggle source
# File lib/crud_responder.rb, line 23
def success(options, object, kaller)
  flash_success(options[:success_message] || DefaultNotification.new(object, kaller).text(true))
  redirect_to_url_smart(options[:success_url])
end
truncate_message(msg) click to toggle source
# File lib/crud_responder.rb, line 70
def truncate_message(msg)
  truncate(msg.to_s, length: 256, escape: false)
end