Skip to content

Instantly share code, notes, and snippets.

Avatar

Maria Kuz MaryKuz

View GitHub Profile
@MaryKuz
MaryKuz / Change ProductsController and UsersController
Created Mar 28, 2019
Change ProductsController and UsersController
View Change ProductsController and UsersController
class Api::ProductsController < ApplicationController
skip_before_action :authenticate
....
end
class Api::UsersController < ApplicationController
skip_before_action :authenticate
@MaryKuz
MaryKuz / Add Authorization to SessionsController
Created Mar 28, 2019
Add authorization to SessionsController
View Add Authorization to SessionsController
class Api::SessionsController < ApplicationController
skip_before_action :authenticate, only: [:create]
....
end
@MaryKuz
MaryKuz / Adding Code to ApplicationController
Created Mar 28, 2019
Adding code to ApplicationController
View Adding Code to ApplicationController
class ApplicationController < ActionController::Base
....
before_action :authenticate
attr_reader :current_user
private
def authenticate
authenticate_or_request_with_http_token do |token, options|
@MaryKuz
MaryKuz / Write SessionController
Created Mar 28, 2019
Start writing app/controllers/sessions_controller.rb
View Write SessionController
class Api::SessionsController < ApplicationController
private
def build_resource
@session = Session.new resource_params
end
def resource
@session ||= Session.new user: current_user
end
@MaryKuz
MaryKuz / Add Error in Rescue Form
Created Mar 28, 2019
Add the error ActiveModel::StrictValidationFailed in rescue_from
View Add Error in Rescue Form
rescue_from ActiveRecord::RecordInvalid, ActiveModel::StrictValidationFailed do
render :errors, status: :unprocessable_entity
end
@MaryKuz
MaryKuz / Cover Class with Tests
Created Mar 28, 2019
Cover class class with the `spec/lib/session_spec.rb` tests
View Cover Class with Tests
require 'rails_helper'
RSpec.describe Session, type: :lib do
it { should be_a ActiveModel::Validations }
let(:session) { Session.new email: 'test@test.com', password: '12345678' }
let(:user) { stub_model User }
subject { session }
View Check User and Email
validate do |model|
if user
model.errors.add :password, 'is invalid' unless user.authenticate password
else
model.errors.add :email, 'not found'
end
end
@MaryKuz
MaryKuz / Start Writing the Session Class
Created Mar 28, 2019
Start writing the Session class
View Start Writing the Session Class
class Session
include ActiveModel::Validations
attr_reader :email, :password, :user
def initialize params
params = params.try(:symbolize_keys) || {}
@user = params[:user]
@MaryKuz
MaryKuz / Place Session class in the lib directory
Created Mar 28, 2019
Place Session class in the lib directory
View Place Session class in the lib directory
module Shop
class Application < Rails::Application
...
config.eager_load_paths << config.root.join('lib').to_s
end
end
@MaryKuz
MaryKuz / create Singleton Resource api_session
Created Mar 28, 2019
Create the singleton resource api/session
View create Singleton Resource api_session
Rails.application.routes.draw do
namespace :api do
...
resource :session, only: [:create, :destroy]
end
end