2014-12-12 12:43:09 -08:00
class ServicesController < ApplicationController
skip_before_action :verify_authenticity_token , :only = > :create
before_action :authenticate_user!
def index
@services = current_user . services
end
def create
service = Service . initialize_from_omniauth ( omniauth_hash )
2022-01-01 09:58:45 -08:00
service . user = current_user
2014-12-12 12:43:09 -08:00
2022-01-01 09:58:45 -08:00
if service . save
2015-06-07 10:03:57 -07:00
flash [ :success ] = t ( 'flash.service.create.success' )
2014-12-12 12:43:09 -08:00
else
2022-01-01 09:58:45 -08:00
if service . errors . details . has_key? ( :uid ) && service . errors . details [ :uid ] . any? { | err | err [ :error ] == :taken }
2022-01-01 13:10:13 -08:00
flash [ :error ] = " The #{ service . type . split ( '::' ) . last . titleize } account you are trying to connect is already connected to another #{ APP_CONFIG [ 'site_name' ] } account. If you are unable to disconnect the account yourself, please send us a Direct Message on Twitter: @retrospring. "
2022-01-01 09:58:45 -08:00
else
flash [ :error ] = t ( 'flash.service.create.error' )
end
2014-12-12 12:43:09 -08:00
end
if origin
redirect_to origin
else
redirect_to services_path
end
end
2022-01-04 16:00:18 -08:00
def update
service = current_user . services . find ( params [ :id ] )
2022-01-05 11:09:34 -08:00
service . post_tag = params [ :service ] [ :post_tag ] . tr ( '@' , '' )
2022-01-04 16:00:18 -08:00
if service . save
flash [ :success ] = " Service updated successfully "
else
flash [ :error ] = " Failed to update service "
end
redirect_to services_path
end
2014-12-12 12:43:09 -08:00
def failure
Rails . logger . info " oauth error: #{ params . inspect } "
2015-06-07 10:03:57 -07:00
flash [ :error ] = t ( 'flash.service.failure' )
2014-12-12 12:43:09 -08:00
redirect_to services_path
end
def destroy
@service = current_user . services . find ( params [ :id ] )
@service . destroy
2015-06-07 10:03:57 -07:00
flash [ :success ] = t ( 'flash.service.destroy' )
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
def omniauth_hash
request . env [ 'omniauth.auth' ]
end
2014-12-12 12:43:09 -08:00
end