module BackOffice::Rest

Public Instance Methods

create() click to toggle source
# File lib/back_office/rest.rb, line 5
def create
  resource.attributes = resource_params

  if resource.save
    created
    success
  else
    failure
  end
end
destroy() click to toggle source
# File lib/back_office/rest.rb, line 25
def destroy
  if resource.destroy
    deleted
    success
  else
    failure
  end
end
update() click to toggle source
# File lib/back_office/rest.rb, line 16
def update
  if resource.update(resource_params)
    updated
    success
  else
    failure
  end
end

Private Instance Methods

after_path() click to toggle source
# File lib/back_office/rest.rb, line 113
def after_path
  case action_name.to_sym
  when :create, :update then resource
  when :destroy         then url_for(controller: controller_name, action: :index)
  end
end
build_params() click to toggle source
# File lib/back_office/rest.rb, line 61
def build_params
  {}
end
build_resource() click to toggle source
# File lib/back_office/rest.rb, line 57
def build_resource
  resource_class.new(build_params)
end
created() click to toggle source
# File lib/back_office/rest.rb, line 120
def created; end
deleted() click to toggle source
# File lib/back_office/rest.rb, line 124
def deleted; end
failure() click to toggle source
# File lib/back_office/rest.rb, line 73
def failure
  respond_to do |format|
    format.js
    format.json { json_failure }
    format.html { html_failure }
  end
end
find_resource() click to toggle source
# File lib/back_office/rest.rb, line 45
def find_resource
  find_scope.find(params[:id]) if params[:id].present?
end
find_scope() click to toggle source
# File lib/back_office/rest.rb, line 49
def find_scope
  resource_class
end
html_failure() click to toggle source
# File lib/back_office/rest.rb, line 93
def html_failure
  render(resource.new_record? ? :new : :edit)
end
html_success() click to toggle source
# File lib/back_office/rest.rb, line 81
def html_success
  redirect_to after_path, notice: success_notice
end
json_failure() click to toggle source
# File lib/back_office/rest.rb, line 97
def json_failure
  render status: :unprocessable_entity, json: { errors: resource.errors.messages }
end
json_success() click to toggle source
# File lib/back_office/rest.rb, line 85
def json_success
  case action_name.to_sym
  when :create  then render :show, status: :created
  when :update  then render :show, status: :ok
  when :destroy then head   :ok
  end
end
param_key() click to toggle source
# File lib/back_office/rest.rb, line 41
def param_key
  resource_class.model_name.param_key
end
permitted_attributes() click to toggle source
# File lib/back_office/rest.rb, line 105
def permitted_attributes
  []
end
resource() click to toggle source
# File lib/back_office/rest.rb, line 36
def resource
  instance_variable_get("@#{param_key}") ||
  instance_variable_set("@#{param_key}", (find_resource || build_resource))
end
resource_class() click to toggle source
# File lib/back_office/rest.rb, line 53
def resource_class
  controller_name.classify.constantize
end
resource_params() click to toggle source
# File lib/back_office/rest.rb, line 101
def resource_params
  params.require(param_key).permit(permitted_attributes)
end
success() click to toggle source
# File lib/back_office/rest.rb, line 65
def success
  respond_to do |format|
    format.js
    format.json { json_success }
    format.html { html_success }
  end
end
success_notice() click to toggle source
# File lib/back_office/rest.rb, line 109
def success_notice
  nil
end
updated() click to toggle source
# File lib/back_office/rest.rb, line 122
def updated; end