Retrospring/app/controllers/services_controller.rb

62 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2014-12-12 12:43:09 -08:00
class ServicesController < ApplicationController
skip_before_action :verify_authenticity_token, only: :create
2014-12-12 12:43:09 -08:00
before_action :authenticate_user!
def index
@services = current_user.services
end
def create
service = Service.initialize_from_omniauth(omniauth_hash)
service.user = current_user
2014-12-12 12:43:09 -08:00
if service.save
flash[:success] = t(".success")
2014-12-12 12:43:09 -08:00
else
flash[:error] = if service.errors.details[:uid]&.any? { |err| err[:error] == :taken }
t(".duplicate", service: service.type.split("::").last.titleize, app: APP_CONFIG["site_name"])
else
t(".error")
end
2014-12-12 12:43:09 -08:00
end
redirect_to origin || services_path
2014-12-12 12:43:09 -08:00
end
def update
service = current_user.services.find(params[:id])
service.post_tag = params[:service][:post_tag].tr("@", "")
if service.save
flash[:success] = t(".success")
else
flash[:error] = t(".error")
end
redirect_to services_path
end
2014-12-12 12:43:09 -08:00
def failure
Rails.logger.info "oauth error: #{params.inspect}"
flash[:error] = t(".error")
2014-12-12 12:43:09 -08:00
redirect_to services_path
end
def destroy
@service = current_user.services.find(params[:id])
@service.destroy
flash[:success] = t(".success")
2014-12-12 12:43:09 -08:00
redirect_to services_path
end
2014-12-12 13:35:23 -08:00
private
def origin
request.env["omniauth.origin"]
end
2014-12-12 13:35:23 -08:00
def omniauth_hash
request.env["omniauth.auth"]
end
2014-12-12 12:43:09 -08:00
end