Skip to content

Instantly share code, notes, and snippets.

View MaryKuz's full-sized avatar

Maria Kuz MaryKuz

View GitHub Profile
@MaryKuz
MaryKuz / Create your token
Created March 27, 2019 13:24
Create your token
function MyToken() {
balanceOf[msg.sender] = 21000000;
}
@MaryKuz
MaryKuz / Change ProductsController and UsersController
Created March 28, 2019 12:25
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 March 28, 2019 12:25
Add authorization to SessionsController
class Api::SessionsController < ApplicationController
skip_before_action :authenticate, only: [:create]
....
end
@MaryKuz
MaryKuz / Adding Code to ApplicationController
Created March 28, 2019 12:23
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 March 28, 2019 12:19
Start writing app/controllers/sessions_controller.rb
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 March 28, 2019 12:17
Add the error ActiveModel::StrictValidationFailed in rescue_from
rescue_from ActiveRecord::RecordInvalid, ActiveModel::StrictValidationFailed do
render :errors, status: :unprocessable_entity
end
@MaryKuz
MaryKuz / Cover Class with Tests
Created March 28, 2019 12:15
Cover class class with the `spec/lib/session_spec.rb` 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 }
@MaryKuz
MaryKuz / Check User and Email
Created March 28, 2019 12:11
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 March 28, 2019 12:09
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 March 28, 2019 12:07
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