Skip to content

Commit

Permalink
Merge pull request #201 from opedrosouza/remove-components
Browse files Browse the repository at this point in the history
Back to defaults
  • Loading branch information
opedrosouza authored Jun 16, 2024
2 parents 7fa1a7a + a673ea4 commit 22f1bc3
Show file tree
Hide file tree
Showing 115 changed files with 582 additions and 1,489 deletions.
7 changes: 7 additions & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DATABASE CONFIG
DB_NAME=boilerplate_development
DB_PASS=password
DB_USER=pedrosouza
DB_HOST=localhost
DB_PORT=5432
DB_URL=postgres://pedrosouza:password@localhost:5432/bolierplate_development
7 changes: 7 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# DATABASE CONFIG
DB_NAME=boilerplate_test
DB_PASS=password
DB_USER=pedrosouza
DB_HOST=localhost
DB_PORT=5432
DB_URL=postgres://pedrosouza:password@localhost:5432/bolierplate_test
12 changes: 9 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
postgres:
image: postgres:latest
env:
POSTGRES_USER: pedrosouza
POSTGRES_PASSWORD: password
POSTGRES_USER: ${{ secrets.DB_USER }}
POSTGRES_PASSWORD: ${{ secrets.DB_PASS }}
POSTGRES_DB: boilerplate_test
ports: ["5432:5432"]
redis:
Expand All @@ -38,7 +38,7 @@ jobs:
with:
node-version: 20.13.1

- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v4
name: Install pnpm
with:
version: 9.1.4
Expand All @@ -55,6 +55,12 @@ jobs:
- name: Run tests
env:
RAILS_MASTER_KEY: ${{ secrets.RAILS_MASTER_KEY }}
DB_HOST: localhost
DB_NAME: boilerplate_test
DB_PORT: 5432
DB_USER: ${{ secrets.DB_USER }}
DB_PASS: ${{ secrets.DB_PASS }}
DB_URL:
RAILS_ENV: test
run: |
bin/rails db:test:prepare
Expand Down
5 changes: 1 addition & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ gem "devise", "~> 4.9", ">= 4.9.4"
gem "devise-i18n", "~> 1.12", ">= 1.12.1"
gem "discard", "~> 1.3"
gem "doorkeeper", "~> 5.7"
gem "dry-initializer", "~> 3.1", ">= 3.1.1"
gem "dotenv-rails", "~> 3.1", ">= 3.1.2"
gem "image_processing", "~> 1.12", ">= 1.12.2"
gem "pagy", "~> 8.4", ">= 8.4.4"
gem "pg", "~> 1.5", ">= 1.5.6"
Expand All @@ -23,8 +23,6 @@ gem "rails-i18n", "~> 7.0", ">= 7.0.9"
gem "stimulus-rails", "~> 1.3", ">= 1.3.3"
gem "turbo-rails", "~> 2.0", ">= 2.0.5"
gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby]
gem "view_component", "~> 3.11"
gem "view_component-contrib", "~> 0.2.2"
gem "vite_rails", "~> 3.0", ">= 3.0.17"

group :development, :test do
Expand All @@ -49,6 +47,5 @@ end
group :development do
gem "annotate", "~> 3.2"
gem "letter_opener", "~> 1.10"
gem "lookbook", "~> 2.3", ">= 2.3.1"
gem "web-console"
end
44 changes: 8 additions & 36 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ GEM
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
css_parser (1.17.1)
addressable
database_cleaner-active_record (2.1.0)
activerecord (>= 5.a)
database_cleaner-core (~> 2.0.0)
Expand All @@ -133,9 +131,12 @@ GEM
docile (1.4.0)
doorkeeper (5.7.0)
railties (>= 5)
dotenv (3.1.2)
dotenv-rails (3.1.2)
dotenv (= 3.1.2)
railties (>= 6.1)
drb (2.2.1)
dry-cli (1.0.0)
dry-initializer (3.1.1)
erubi (1.12.0)
factory_bot (6.4.6)
activesupport (>= 5.0.0)
Expand All @@ -150,8 +151,6 @@ GEM
ffi (1.17.0-x86_64-linux-gnu)
globalid (1.2.1)
activesupport (>= 6.1)
htmlbeautifier (1.4.3)
htmlentities (4.3.4)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
image_processing (1.12.2)
Expand All @@ -172,26 +171,13 @@ GEM
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
lookbook (2.3.1)
activemodel
css_parser
htmlbeautifier (~> 1.3)
htmlentities (~> 4.3.4)
marcel (~> 1.0)
railties (>= 5.0)
redcarpet (~> 3.5)
rouge (>= 3.26, < 5.0)
view_component (>= 2.0)
yard (~> 0.9)
zeitwerk (~> 2.5)
mail (2.8.1)
mini_mime (>= 0.1.1)
net-imap
net-pop
net-smtp
marcel (1.0.4)
matrix (0.4.2)
method_source (1.1.0)
mini_magick (4.12.0)
mini_mime (1.1.5)
minitest (5.23.1)
Expand All @@ -218,7 +204,7 @@ GEM
orm_adapter (0.5.0)
pagy (8.4.4)
parallel (1.25.1)
parser (3.3.2.0)
parser (3.3.3.0)
ast (~> 2.4.1)
racc
pg (1.5.6)
Expand All @@ -233,7 +219,7 @@ GEM
pundit (2.3.2)
activesupport (>= 3.0.0)
racc (1.8.0)
rack (3.1.2)
rack (3.1.3)
rack-cors (2.0.2)
rack (>= 2.0.0)
rack-proxy (0.7.7)
Expand Down Expand Up @@ -285,16 +271,14 @@ GEM
rake (13.2.1)
rdoc (6.7.0)
psych (>= 4.0.0)
redcarpet (3.6.0)
regexp_parser (2.9.2)
reline (0.5.8)
reline (0.5.9)
io-console (~> 0.5)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.3.0)
strscan
rouge (4.2.1)
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
Expand Down Expand Up @@ -335,7 +319,6 @@ GEM
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rspec (3.0.1)
rubocop (~> 1.61)
ruby-next-core (1.0.3)
ruby-progressbar (1.13.0)
ruby-vips (2.2.1)
ffi (~> 1.12)
Expand All @@ -360,13 +343,6 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
view_component (3.12.1)
activesupport (>= 5.2.0, < 8.0)
concurrent-ruby (~> 1.0)
method_source (~> 1.0)
view_component-contrib (0.2.2)
ruby-next-core (>= 0.15.0)
view_component
vite_rails (3.0.17)
railties (>= 5.1, < 8)
vite_ruby (~> 3.0, >= 3.2.2)
Expand All @@ -387,7 +363,6 @@ GEM
websocket-extensions (0.1.5)
xpath (3.2.0)
nokogiri (~> 1.8)
yard (0.9.36)
zeitwerk (2.6.15)

PLATFORMS
Expand All @@ -409,12 +384,11 @@ DEPENDENCIES
devise-i18n (~> 1.12, >= 1.12.1)
discard (~> 1.3)
doorkeeper (~> 5.7)
dry-initializer (~> 3.1, >= 3.1.1)
dotenv-rails (~> 3.1, >= 3.1.2)
factory_bot_rails (~> 6.4, >= 6.4.3)
faker (~> 3.4, >= 3.4.1)
image_processing (~> 1.12, >= 1.12.2)
letter_opener (~> 1.10)
lookbook (~> 2.3, >= 2.3.1)
pagy (~> 8.4, >= 8.4.4)
pg (~> 1.5, >= 1.5.6)
pg_search (~> 2.3, >= 2.3.6)
Expand All @@ -433,8 +407,6 @@ DEPENDENCIES
stimulus-rails (~> 1.3, >= 1.3.3)
turbo-rails (~> 2.0, >= 2.0.5)
tzinfo-data
view_component (~> 3.11)
view_component-contrib (~> 0.2.2)
vite_rails (~> 3.0, >= 3.0.17)
web-console

Expand Down
18 changes: 4 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,17 @@ Every time I had to start a new project I always had to install almost the same
- Authorization using `pundit`.
- Usage of Vite to handle assets/js/css files on the `frontend` folder.
- Usage of Flowbite/Tailwind to facilitate the implementation of beautiful components.
- Usage of `view_component` to create components with `stimulus/hotwire` to feel like a react project.
- Specs ready to go with `rspec`, `factory_bot`, `shoulda_matcher` and `capybara`.
- A set of base components to just use on the project frontend instead of have to write a lot of the same code always. (You can check the components visiting the `/lookbook` route after setup the project on your local environment).
- CI using github actions.

## The Big difference
This project has a few differences from a regular new Rails project the main ones are:

1. Usage of the `vite_rails` gem to handle css/js/assets instead of the `sprockets`
1. Usage of the `vite_rails` gem to handle css/js/images assets instead of the `sprockets`
1. All js/css/images/etc files should be created inside the `app/frontend` folder. This way vite will process it accordingly.
2. Vite also provides helpers to facilitate the usage of our assets on the app
2. Usage of `view_component` gem instead of using Rails partials.
1. I love the way we can write logic inside `components` and getting it working with `stimulus/hotwire` is great and overrides the need for some js library like `react`
2. In the `development` environment we have the `lookbook` to play with our `components` and check how they work.
3. All `components` must be created inside the `app/views/components` folder.
4. To create a new `component` you can just run: `rails g view_component name_of_component`
3. It already came with authentication and authorization, which is being handled by devise/doorkeeper/pundit.
4. It has specs being configured and executed by rspec/factory_bot/capybara.
2. It came with pre configured authentication and authorization for users and admins, which is being handled by devise/doorkeeper/pundit.
3. It has specs configured and executed by rspec/factory_bot/capybara.

## Project Structure
The structure is pretty much the same as regular Rails MVC, the big difference is that we should store all our js/css/assets files inside `app/frontend` folder instead of the `app/assets` folder.
Expand All @@ -40,9 +33,6 @@ The structure is pretty much the same as regular Rails MVC, the big difference i
- The idea is that we can organize our code in a way which just taking a look we can see which that controller is being used.
- Frontend
- This is where all our assets will leave, basically almost all js/css/images/etc will leave inside this folder to processed by vite.
- Views/Components
- All our `view_components` must be created inside the `app/views/components` folder this way vite can process all the js/css/assets files that may cohexist with the component itself. This way we can better organize our code.
- Since `components` are code that relates to `views` I have followed the suggestion of the `view_component-contrib` to store the components inside the `views` folder.


The rest of the folders/structure is pretty much what the base rails gives to us.

3 changes: 2 additions & 1 deletion app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module ApplicationCable

class Connection < ActionCable::Connection::Base

include SetCurrentRequestDetails
include AbstractController::Helpers
include Currentable

identified_by :current_user, :current_account

Expand Down
4 changes: 4 additions & 0 deletions app/controllers/app/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ class App::ApplicationController < ApplicationController

before_action :authenticate_user!

def pundit_user
current_user
end

end
4 changes: 3 additions & 1 deletion app/controllers/app/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class App::HomeController < App::ApplicationController

def index; end
def index
@roles = current_account_user.roles
end

end
3 changes: 1 addition & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ class ApplicationController < ActionController::Base

include Pundit::Authorization
include Pagy::Backend
include SetCurrentRequestDetails
include CurrentHelper
include Currentable
include Searchable

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/auth/confirmations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController
def show
super do |resource|
current_account = Current.account || resource.owned_accounts.inactive.first
current_account.update!(active: true) if current_account.present? && !current_account.active?
sign_in(resource) if resource.errors.empty? && current_account.present?
current_account.active! if current_account.present?
sign_in(resource) if resource.errors.empty? && current_account.active?
end
end

Expand Down
60 changes: 60 additions & 0 deletions app/controllers/concerns/currentable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# frozen_string_literal: true
module Currentable

extend ActiveSupport::Concern

included do |base|
if base < ActionController::Base
before_action :set_request_details
end

helper_method :current_account, :current_account_user, :other_accounts, :account_owner?
end

def current_account
@current_account ||= Current.account
end

def current_account_user
return unless current_account

@current_account_user ||= current_account.account_users.includes(:user).find_by(user: current_user)
end

def other_accounts
@other_accounts ||= current_user.accounts.order(name: :asc).where.not(id: current_account.id)
end

def account_owner?
@account_owner ||= current_account_user&.owner?
end

private

def set_request_details
Current.request_id = request.uuid
Current.user_agent = request.user_agent
Current.ip_address = request.ip
Current.user ||= current_user
Current.account ||= account_from_session || account_from_cookies || fallback_account
end

def account_from_session
return unless user_signed_in? && (account_id = session[:account_id])

current_user.accounts.includes(:users).find_by(id: account_id)
end

def account_from_cookies
return unless user_signed_in? && (account_id = cookies.signed[:account_id])

current_user.accounts.includes(:users).find_by(id: account_id)
end

def fallback_account
return unless user_signed_in?

current_user.owned_accounts.includes(:users).first
end

end
Loading

0 comments on commit 22f1bc3

Please sign in to comment.