This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::ProductsController < ApplicationController | |
skip_before_action :authenticate | |
.... | |
end | |
class Api::UsersController < ApplicationController | |
skip_before_action :authenticate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
.... | |
before_action :authenticate | |
attr_reader :current_user | |
private | |
def authenticate | |
authenticate_or_request_with_http_token do |token, options| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::SessionsController < ApplicationController | |
private | |
def build_resource | |
@session = Session.new resource_params | |
end | |
def resource | |
@session ||= Session.new user: current_user | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rescue_from ActiveRecord::RecordInvalid, ActiveModel::StrictValidationFailed do | |
render :errors, status: :unprocessable_entity | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
validate do |model| | |
if user | |
model.errors.add :password, 'is invalid' unless user.authenticate password | |
else | |
model.errors.add :email, 'not found' | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Session | |
include ActiveModel::Validations | |
attr_reader :email, :password, :user | |
def initialize params | |
params = params.try(:symbolize_keys) || {} | |
@user = params[:user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Shop | |
class Application < Rails::Application | |
... | |
config.eager_load_paths << config.root.join('lib').to_s | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rails.application.routes.draw do | |
namespace :api do | |
... | |
resource :session, only: [:create, :destroy] | |
end | |
end |
NewerOlder