module ActivityNotification::CommonController

Module included in controllers to select target

Constants

DEFAULT_VIEW_DIRECTORY

Protected Instance Methods

controller_path() click to toggle source

Returns controller path. This method is called from target_view_path method and can be overridden. @api protected @return [String] “activity_notification” as controller path

# File lib/activity_notification/controllers/common_controller.rb, line 65
def controller_path
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
end
error_response(error_info = {}) click to toggle source

Returns error response as Hash @api protected @return [Hash] Error message

# File lib/activity_notification/controllers/common_controller.rb, line 89
def error_response(error_info = {})
  { gem: "activity_notification", error: error_info }
end
load_index() click to toggle source

Loads resource index with request parameters. This method is to be overridden. @api protected @return [Array] Array of resource index

# File lib/activity_notification/controllers/common_controller.rb, line 57
def load_index
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
end
render_invalid_parameter(message) click to toggle source

Render Invalid Parameter error with 400 status @api protected @return [void]

# File lib/activity_notification/controllers/common_controller.rb, line 104
def render_invalid_parameter(message)
  render status: 400, json: error_response(code: 400, message: "Invalid parameter", type: message)
end
render_resource_not_found(error = nil) click to toggle source

Render Resource Not Found error with 404 status @api protected @return [void]

# File lib/activity_notification/controllers/common_controller.rb, line 96
def render_resource_not_found(error = nil)
  message_type = error.respond_to?(:message) ? error.message : error
  render status: 404, json: error_response(code: 404, message: "Resource not found", type: message_type)
end
render_unprocessable_entity(message) click to toggle source

Render Invalid Parameter error with 400 status @api protected @return [void]

# File lib/activity_notification/controllers/common_controller.rb, line 119
def render_unprocessable_entity(message)
  render status: 422, json: error_response(code: 422, message: "Unprocessable entity", type: message)
end
return_back_or_ajax() click to toggle source

Returns JavaScript view for ajax request or redirects to back as default. @api protected @return [Response] JavaScript view for ajax request or redirects to back as default

# File lib/activity_notification/controllers/common_controller.rb, line 126
def return_back_or_ajax
  set_index_options
  respond_to do |format|
    if request.xhr?
      load_index if params[:reload].to_s.to_boolean(true)
      format.js
    else
      redirect_back(fallback_location: { action: :index }, **@index_options) and return
    end
  end
end
set_index_options() click to toggle source

Sets options to load resource index from request parameters. This method is to be overridden. @api protected @return [Hash] options to load resource index

# File lib/activity_notification/controllers/common_controller.rb, line 49
def set_index_options
  raise NotImplementedError, "You have to implement #{self.class}##{__method__}"
end
set_target() click to toggle source

Sets @target instance variable from request parameters. @api protected @return [Object] Target instance (Returns HTTP 400 when request parameters are invalid)

# File lib/activity_notification/controllers/common_controller.rb, line 24
def set_target
  if (target_type = params[:target_type]).present?
    target_class = target_type.to_model_class
    @target = params[:target_id].present? ?
      target_class.find_by!(id: params[:target_id]) :
      target_class.find_by!(id: params["#{target_type.to_resource_name[/([^\/]+)$/]}_id"])
  else
    render status: 400, json: error_response(code: 400, message: "Invalid parameter", type: "Parameter is missing or the value is empty: target_type")
  end
end
set_view_prefixes() click to toggle source

Sets view prefixes for target view path. @api protected

# File lib/activity_notification/controllers/common_controller.rb, line 82
def set_view_prefixes
  lookup_context.prefixes.prepend(target_view_path)
end
target_view_path() click to toggle source

Returns path of the target view templates. Do not make this method public unless Rendarable module calls controller's target_view_path method to render resources. @api protected

# File lib/activity_notification/controllers/common_controller.rb, line 72
def target_view_path
  target_type = @target.to_resources_name
  view_path = [controller_path, target_type].join('/')
  lookup_context.exists?(action_name, view_path) ?
    view_path :
    [controller_path, DEFAULT_VIEW_DIRECTORY].join('/')
end
validate_param(param_name) click to toggle source

Validate param and return HTTP 400 unless it presents. @api protected @param [String, Symbol] param_name Parameter name to validate @return [void]

# File lib/activity_notification/controllers/common_controller.rb, line 112
def validate_param(param_name)
  render_invalid_parameter("Parameter is missing: #{param_name}") if params[param_name].blank?
end
validate_target(belonging_model) click to toggle source

Validate target with belonging model (e.g. Notification and Subscription) @api protected @param [Object] belonging_model belonging model (e.g. Notification and Subscription) @return Nil or render HTTP 403 status

# File lib/activity_notification/controllers/common_controller.rb, line 39
def validate_target(belonging_model)
  if @target.present? && belonging_model.target != @target
    render status: 403, json: error_response(code: 403, message: "Forbidden because of invalid parameter", type: "Wrong target is specified")
  end
end