Skip to content

Instantly share code, notes, and snippets.

@motoroller95
Forked from briu/users.rb
Last active November 13, 2020 11:18
Show Gist options
  • Save motoroller95/bb5bf6f01a7bf47fc52c2c8a57528668 to your computer and use it in GitHub Desktop.
Save motoroller95/bb5bf6f01a7bf47fc52c2c8a57528668 to your computer and use it in GitHub Desktop.
create two classes with same interfaces
# User attributes name surname
class User < ApplicationRecord
# attributes :id, integer
# attributes :name, string
# attributes :email, string
def log!
Log.create!(...)
end
def create_gift!
true
end
end
class GuestUser
# your code here
end
class GiftsController < ApplicationController
def current_user
@current_user = begin
user = User.find_by(token: session[:token])
user = GuestUser.new unless user
user
end
end
def index
current_user.log!
render json: Gift.where(user_id: current_user.id)
end
def create
current_user.log!
if current_user.create_gift!
render json: { success: true }
else
render json: { success: false }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment