Skip to content

Commit

Permalink
updated rspec directives
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrisoxide committed Dec 12, 2024
1 parent 713eeb5 commit 489b772
Show file tree
Hide file tree
Showing 81 changed files with 438 additions and 400 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ require:

AllCops:
NewCops: enable
SuggestExtensions: false
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

Expand Down
2 changes: 2 additions & 0 deletions app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Channel < ActionCable::Channel::Base
end
Expand Down
2 changes: 2 additions & 0 deletions app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ApplicationCable
class Connection < ActionCable::Connection::Base
end
Expand Down
166 changes: 85 additions & 81 deletions app/controllers/api/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,103 +1,107 @@
class Api::ItemsController < ApplicationController
# frozen_string_literal: true

include ProductSearch
module Api
# Controller for accessing items (products) in the API
class ItemsController < ApplicationController
include ProductSearch

protect_from_forgery with: :null_session
protect_from_forgery with: :null_session

before_action :doorkeeper_authorize!, only: [:create, :update]
before_action :set_item, only: [:show, :update, :destroy]
before_action :doorkeeper_authorize!, only: %i[create update]
before_action :set_item, only: %i[show update]

rescue_from ActiveRecord::RecordNotFound, with: :product_not_found
rescue_from ActiveRecord::RecordNotFound, with: :product_not_found

def index
@items = product_search(params)
end
def index
@items = product_search(params)
end

def show
if @item
render json: @item
else
render json: { error: "Product '#{params[:id]}' not found" }, status: :not_found
def show
if @item
render json: @item
else
render json: { error: "Product '#{params[:id]}' not found" }, status: :not_found
end
end
end

def create
@item = Product.new(item_params)
def create
@item = Product.new(item_params)

respond_to do |format|
if @item.save
format.json { render :show, status: :created, location: @item }
else
format.json { render json: @item.errors, status: :unprocessable_entity }
respond_to do |format|
if @item.save
format.json { render :show, status: :created, location: @item }
else
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
end

def update
respond_to do |format|
if @item.update(item_params)
format.json { render :show, status: :ok, location: @item }
else
format.json { render json: @item.errors, status: :unprocessable_entity }
def update
respond_to do |format|
if @item.update(item_params)
format.json { render :show, status: :ok, location: @item }
else
format.json { render json: @item.errors, status: :unprocessable_entity }
end
end
end
end

private
private

def set_item
@item = if params[:id] == '00000000000000'
test_item
else
Product.from_param(params[:id])
def set_item
@item = if params[:id] == '00000000000000'
test_item
else
Product.from_param(params[:id])
end
end
end

def product_not_found(_exception)
render json: { error: "Product '#{params[:id]}' not found" }, status: :not_found
end
def product_not_found(_exception)
render json: { error: "Product '#{params[:id]}' not found" }, status: :not_found
end

def item_params
params
.permit(
:gtin,
:name,
:brand_name,
properties: {}
)
end
def item_params
params
.permit(
:gtin,
:name,
:brand_name,
properties: {}
)
end

# TODO the Datakick test item also has images attached.
#
def test_item
Product.new(
gtin: '00000000000000',
properties: {
brand_name: "ayam",
name: "testname",
size: "081216382297",
ingredients: "Chocolate",
serving_size: "34g",
servings_per_container: "10",
calories: 5,
fat_calories: 5,
fat: 0.5,
saturated_fat: 0.5,
trans_fat: 0.5,
polyunsaturated_fat: 0.5,
monounsaturated_fat: 0.5,
cholesterol: 0,
sodium: 0,
potassium: 0,
carbohydrate: 0,
fiber: 0,
sugars: 0,
protein: 0,
author: "MyAuthor",
publisher: "MyPublisher",
pages: 0,
alcohol_by_volume: 40.0,
}
)
# TODO: the Datakick test item also has images attached.
#
def test_item # rubocop:disable Metrics/MethodLength
Product.new(
gtin: '00000000000000',
properties: {
brand_name: 'ayam',
name: 'testname',
size: '081216382297',
ingredients: 'Chocolate',
serving_size: '34g',
servings_per_container: '10',
calories: 5,
fat_calories: 5,
fat: 0.5,
saturated_fat: 0.5,
trans_fat: 0.5,
polyunsaturated_fat: 0.5,
monounsaturated_fat: 0.5,
cholesterol: 0,
sodium: 0,
potassium: 0,
carbohydrate: 0,
fiber: 0,
sugars: 0,
protein: 0,
author: 'MyAuthor',
publisher: 'MyPublisher',
pages: 0,
alcohol_by_volume: 40.0
}
)
end
end
end
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# Base controller
class ApplicationController < ActionController::Base
before_action :set_paper_trail_whodunnit
end
6 changes: 4 additions & 2 deletions app/controllers/concerns/product_search.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module ProductSearch
# frozen_string_literal: true

module ProductSearch # rubocop:disable Style/Documentation
extend ActiveSupport::Concern

included do
Expand All @@ -18,7 +20,7 @@ def product_search(params)
def build_query(query_params)
base_query = Product.full_text_search(query_params.split)

return base_query unless gtin?(query_params)
return base_query unless gtin?(query_params)

base_query.or(Product.by_gtin(query_params))
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HomeController < ApplicationController

def index
# frozen_string_literal: true

end
# Home (dashboard) controller
class HomeController < ApplicationController
def index; end
end
11 changes: 6 additions & 5 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# Controller for products
class ProductsController < ApplicationController
include ProductSearch

Expand All @@ -10,10 +11,10 @@ def index
@products = product_search(params)
end

def show
def show # rubocop:disable Metrics/MethodLength
if @product.nil?
respond_to do |format|
format.html { redirect_to products_path, alert: 'Product not found' }
format.html { redirect_to products_path, alert: 'Product not found' } # rubocop:disable Rails/I18nLocaleTexts
format.json { render json: { error: 'Product not found' }, status: :not_found }
end
else
Expand All @@ -33,15 +34,15 @@ def edit; end
def create
@product = Product.new(product_params)
if @product.save
redirect_to @product, notice: 'Product was successfully created.'
redirect_to @product, notice: 'Product was successfully created.' # rubocop:disable Rails/I18nLocaleTexts
else
render :new
end
end

def update
if @product.update(product_params)
redirect_to @product, notice: 'Product was successfully updated.'
redirect_to @product, notice: 'Product was successfully updated.' # rubocop:disable Rails/I18nLocaleTexts
else
render :edit
end
Expand All @@ -67,7 +68,7 @@ def set_product
@product = Product.from_param(params[:id])
end

def product_params
def product_params # rubocop:disable Metrics/MethodLength
params
.require(:product)
.permit(
Expand Down
60 changes: 1 addition & 59 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Users
# Subclass the Devise RegistrationsController to add reCAPTCHA validation
class RegistrationsController < Devise::RegistrationsController
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.
prepend_before_action :check_captcha, only: [:create] # rubocop:disable Rails/LexicallyScopedActionFilter

def check_captcha
return true if verify_recaptcha(action: 'registration')
Expand All @@ -17,63 +17,5 @@ def check_captcha
render :new
end
end
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]

# GET /resource/sign_up
# def new
# super
# end

# POST /resource
# def create
# super
# end

# GET /resource/edit
# def edit
# super
# end

# PUT /resource
# def update
# super
# end

# DELETE /resource
# def destroy
# super
# end

# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end

# protected

# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end

# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end

# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end

# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
end
end
2 changes: 1 addition & 1 deletion app/controllers/users/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Users
# Subclass the Devise SessionsController to add reCAPTCHA validation
class SessionsController < Devise::SessionsController
prepend_before_action :check_captcha, only: [:create] # Change this to be any actions you want to protect.
prepend_before_action :check_captcha, only: [:create] # rubocop:disable Rails/LexicallyScopedActionFilter

private

Expand Down
2 changes: 0 additions & 2 deletions app/helpers/api/items_helper.rb

This file was deleted.

4 changes: 3 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
module ApplicationHelper
# frozen_string_literal: true

module ApplicationHelper # rubocop:disable Style/Documentation
end
2 changes: 0 additions & 2 deletions app/helpers/home_helper.rb

This file was deleted.

Loading

0 comments on commit 489b772

Please sign in to comment.