Skip to content

Commit

Permalink
Configure syntax_tree to check and format ruby files. Fix gems
Browse files Browse the repository at this point in the history
  • Loading branch information
binos30 committed Jul 21, 2024
1 parent 10ead03 commit 7c1da62
Show file tree
Hide file tree
Showing 28 changed files with 110 additions and 275 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ jobs:
bundler-cache: true

- name: Lint code for consistent style
run: bin/rubocop -f github
run: |
bin/bundle exec stree check "{app,lib,spec,test}/**/*.rb"
bin/rubocop -f github
test:
runs-on: ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ require:
- rubocop-rspec
- rubocop-rspec_rails

# Omakase Ruby styling for Rails
inherit_gem:
# Omakase Ruby styling for Rails
rubocop-rails-omakase: rubocop.yml

# RuboCop and Syntax Tree serve different purposes, but there is overlap with some of RuboCop's functionality.
# Syntax Tree provides a RuboCop configuration file to disable rules that are redundant with Syntax Tree.
syntax_tree: config/rubocop.yml

AllCops:
NewCops: enable
Exclude:
Expand Down
1 change: 1 addition & 0 deletions .streerc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--print-width=115
21 changes: 13 additions & 8 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ group :development, :test do

# A library for generating fake data such as names, addresses, and phone numbers
gem "faker"

## Code Formatting & Linting
# Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
gem "rubocop-rails-omakase", require: false
# Code style checking for RSpec files [https://github.com/rubocop/rubocop-rspec]
gem "rubocop-rspec", require: false
# Code style checking for Rails-related RSpec files [https://github.com/rubocop/rubocop-rspec_rails]
gem "rubocop-rspec_rails", require: false
# Interact with the Ruby syntax tree [https://github.com/ruby-syntax-tree/syntax_tree]
gem "syntax_tree"
end

group :development do
Expand All @@ -99,21 +109,16 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"

# Code Formatting & Linting
gem "prettier_print"
gem "rubocop-rails-omakase", require: false
gem "rubocop-rspec", require: false
gem "rubocop-rspec_rails", require: false
## Code Formatting & Linting
# Configurable tool for analyzing Slim templates [https://github.com/sds/slim-lint]
gem "slim_lint"
gem "syntax_tree"
gem "syntax_tree-haml"
gem "syntax_tree-rbs"

# Optimize queries
gem "bullet"
end

group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
end
17 changes: 0 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ GEM
activerecord (>= 4.0.0)
globalid (1.2.1)
activesupport (>= 6.1)
haml (6.3.0)
temple (>= 0.8.2)
thor
tilt
i18n (1.14.5)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
Expand Down Expand Up @@ -266,8 +262,6 @@ GEM
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rbs (3.5.2)
logger
rdoc (6.7.0)
psych (>= 4.0.0)
redis (5.2.0)
Expand Down Expand Up @@ -368,14 +362,6 @@ GEM
strscan (3.1.0)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
syntax_tree-haml (4.0.3)
haml (>= 5.2)
prettier_print (>= 1.2.1)
syntax_tree (>= 6.0.0)
syntax_tree-rbs (1.0.0)
prettier_print
rbs
syntax_tree (>= 2.0.1)
tailwindcss-rails (2.6.3)
railties (>= 7.0.0)
tailwindcss-rails (2.6.3-aarch64-linux)
Expand Down Expand Up @@ -449,7 +435,6 @@ DEPENDENCIES
local_time
pagy
pg (~> 1.1)
prettier_print
puma (>= 5.0)
rails (~> 7.1.3.4)
redis (>= 4.0.1)
Expand All @@ -464,8 +449,6 @@ DEPENDENCIES
stimulus-rails
stripe
syntax_tree
syntax_tree-haml
syntax_tree-rbs
tailwindcss-rails
turbo-rails
tzinfo-data
Expand Down
57 changes: 11 additions & 46 deletions app/controllers/admin/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ class CategoriesController < AdminController

# GET /admin/categories or /admin/categories.json
def index
@categories =
Category
.filters(params.slice(:name))
.includes(image_attachment: :blob)
.order(:name)
@categories = Category.filters(params.slice(:name)).includes(image_attachment: :blob).order(:name)
@pagy, @categories = pagy(@categories, limit: count_per_page)
end

Expand All @@ -35,32 +31,19 @@ def create # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
if @category.save
format.html do
redirect_to admin_category_url(@category),
notice:
t(
"record.create",
record: Category.name,
name: @category.name
)
end
format.json do
render :show,
status: :created,
location: admin_category_url(@category)
notice: t("record.create", record: Category.name, name: @category.name)
end
format.json { render :show, status: :created, location: admin_category_url(@category) }
else
format.html { render :new, status: :unprocessable_entity }
format.json do
render json: @category.errors, status: :unprocessable_entity
end
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotUnique => e
@category.errors.add(:base, e)
respond_to do |format|
format.html { render :new, status: :unprocessable_entity }
format.json do
render json: @category.errors, status: :unprocessable_entity
end
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end

Expand All @@ -70,30 +53,19 @@ def update # rubocop:disable Metrics/AbcSize
if @category.update(category_params)
format.html do
redirect_to admin_category_url(@category),
notice:
t(
"record.update",
record: Category.name,
name: @category.name
)
end
format.json do
render :show, status: :ok, location: admin_category_url(@category)
notice: t("record.update", record: Category.name, name: @category.name)
end
format.json { render :show, status: :ok, location: admin_category_url(@category) }
else
format.html { render :edit, status: :unprocessable_entity }
format.json do
render json: @category.errors, status: :unprocessable_entity
end
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotUnique => e
@category.errors.add(:base, e)
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json do
render json: @category.errors, status: :unprocessable_entity
end
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end

Expand All @@ -103,18 +75,11 @@ def destroy # rubocop:disable Metrics/AbcSize

respond_to do |format|
format.html do
redirect_to admin_categories_url,
notice:
t(
"record.delete",
record: Category.name,
name: @category.name
)
redirect_to admin_categories_url, notice: t("record.delete", record: Category.name, name: @category.name)
end
format.json { head :no_content }
end
rescue ActiveRecord::DeleteRestrictionError,
ActiveRecord::InvalidForeignKey => e
rescue ActiveRecord::DeleteRestrictionError, ActiveRecord::InvalidForeignKey => e
logger.tagged("Delete Category##{@category.id} Error") { logger.error e }
redirect_to admin_categories_url, alert: e
end
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/admin/dashboard_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ def index # rubocop:disable Metrics/AbcSize
}
@orders_by_day = Order.where("created_at > ?", 7.days.ago).order(:created_at)
@orders_by_day = @orders_by_day.group_by { |order| order.created_at.localtime.to_date }
@revenue_by_day =
@orders_by_day.map { |day, orders| [day.strftime("%A"), orders.sum(&:total)] }
@revenue_by_day = @orders_by_day.map { |day, orders| [day.strftime("%A"), orders.sum(&:total)] }

return unless @revenue_by_day.count < 7
days_of_week = Date::DAYNAMES
data_hash = @revenue_by_day.to_h
current_day_index = Time.current.localtime.wday
next_day_index = (current_day_index + 1) % days_of_week.length
ordered_days_with_current_last =
days_of_week[next_day_index..] + days_of_week[0...next_day_index]
ordered_days_with_current_last = days_of_week[next_day_index..] + days_of_week[0...next_day_index]
complete_ordered_array_with_current_last =
ordered_days_with_current_last.map { |day| [day, data_hash.fetch(day, 0)] }
@revenue_by_day = complete_ordered_array_with_current_last
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def show

def fulfill
@order.fulfill!
redirect_to admin_order_url(@order),
notice: t("record.fulfill", code: @order.order_code)
redirect_to admin_order_url(@order), notice: t("record.fulfill", code: @order.order_code)
rescue ActiveRecord::RecordInvalid => e
logger.tagged("Fulfill Order Error") { logger.error e.message }
flash.now[:alert] = e.message
Expand Down
64 changes: 13 additions & 51 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ class ProductsController < AdminController
# GET /admin/products or /admin/products.json
def index
@products =
Product
.filters(params.slice(:name))
.includes([:category, { images_attachments: :blob }])
.order(:name)
Product.filters(params.slice(:name)).includes([:category, { images_attachments: :blob }]).order(:name)
@pagy, @products = pagy(@products, limit: count_per_page)
end

Expand All @@ -36,32 +33,19 @@ def create # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
if @product.save
format.html do
redirect_to admin_product_url(@product),
notice:
t(
"record.create",
record: Product.name,
name: @product.name
)
end
format.json do
render :show,
status: :created,
location: admin_product_url(@product)
notice: t("record.create", record: Product.name, name: @product.name)
end
format.json { render :show, status: :created, location: admin_product_url(@product) }
else
format.html { render :new, status: :unprocessable_entity }
format.json do
render json: @product.errors, status: :unprocessable_entity
end
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotUnique => e
@product.errors.add(:base, e)
respond_to do |format|
format.html { render :new, status: :unprocessable_entity }
format.json do
render json: @product.errors, status: :unprocessable_entity
end
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end

Expand All @@ -71,30 +55,19 @@ def update # rubocop:disable Metrics/AbcSize
if @product.update(product_params)
format.html do
redirect_to admin_product_url(@product),
notice:
t(
"record.update",
record: Product.name,
name: @product.name
)
end
format.json do
render :show, status: :ok, location: admin_product_url(@product)
notice: t("record.update", record: Product.name, name: @product.name)
end
format.json { render :show, status: :ok, location: admin_product_url(@product) }
else
format.html { render :edit, status: :unprocessable_entity }
format.json do
render json: @product.errors, status: :unprocessable_entity
end
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end
rescue ActiveRecord::RecordNotUnique => e
@product.errors.add(:base, e)
respond_to do |format|
format.html { render :edit, status: :unprocessable_entity }
format.json do
render json: @product.errors, status: :unprocessable_entity
end
format.json { render json: @product.errors, status: :unprocessable_entity }
end
end

Expand All @@ -104,18 +77,11 @@ def destroy # rubocop:disable Metrics/AbcSize

respond_to do |format|
format.html do
redirect_to admin_products_url,
notice:
t(
"record.delete",
record: Product.name,
name: @product.name
)
redirect_to admin_products_url, notice: t("record.delete", record: Product.name, name: @product.name)
end
format.json { head :no_content }
end
rescue ActiveRecord::DeleteRestrictionError,
ActiveRecord::InvalidForeignKey => e
rescue ActiveRecord::DeleteRestrictionError, ActiveRecord::InvalidForeignKey => e
logger.tagged("Delete Product##{@product.id} Error") { logger.error e }
redirect_to admin_products_url, alert: e
end
Expand All @@ -131,13 +97,9 @@ def set_lookups
def set_product # rubocop:disable Metrics/AbcSize
@product =
if action_name == "show"
Product.includes(
[:category, { images_attachments: :blob }]
).find_by_friendly_id(params[:slug])
Product.includes([:category, { images_attachments: :blob }]).find_by_friendly_id(params[:slug])
elsif action_name == "edit"
Product.includes([{ images_attachments: :blob }]).find_by_friendly_id(
params[:slug]
)
Product.includes([{ images_attachments: :blob }]).find_by_friendly_id(params[:slug])
else
Product.find_by_friendly_id(params[:slug])
end
Expand Down
7 changes: 1 addition & 6 deletions app/controllers/site/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@
module Site
class CategoriesController < SiteController
def index
@categories =
Category
.includes(image_attachment: :blob)
.filters(params.slice(:name))
.active
.order(:name)
@categories = Category.includes(image_attachment: :blob).filters(params.slice(:name)).active.order(:name)
@pagy, @categories = pagy_countless(@categories, limit: 10)

respond_to do |format|
Expand Down
Loading

0 comments on commit 7c1da62

Please sign in to comment.