Skip to content

Commit

Permalink
feat: init
Browse files Browse the repository at this point in the history
  • Loading branch information
bastos committed Oct 5, 2024
0 parents commit 584d86e
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Omakase Ruby styling for Rails
inherit_gem: { rubocop-rails-omakase: rubocop.yml }
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source "https://rubygems.org"

gem "rake"

group :development do
gem "rubocop-rails-omakase", require: false
gem "ruby-lsp"
end
89 changes: 89 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (7.2.1)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.8)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
drb (2.2.1)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
logger (1.6.1)
minitest (5.25.1)
parallel (1.26.3)
parser (3.3.5.0)
ast (~> 2.4.1)
racc
prism (1.0.0)
racc (1.8.1)
rack (3.1.7)
rainbow (3.1.1)
rake (13.2.1)
rbs (3.5.3)
logger
regexp_parser (2.9.2)
rubocop (1.66.1)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.4, < 3.0)
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
parser (>= 3.3.1.0)
rubocop-minitest (0.36.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails (2.26.2)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.52.0, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rails-omakase (1.0.0)
rubocop
rubocop-minitest
rubocop-performance
rubocop-rails
ruby-lsp (0.18.1)
language_server-protocol (~> 3.17.0)
prism (~> 1.0)
rbs (>= 3, < 4)
sorbet-runtime (>= 0.5.10782)
ruby-progressbar (1.13.0)
securerandom (0.3.1)
sorbet-runtime (0.5.11577)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.6.0)

PLATFORMS
arm64-darwin-23
ruby

DEPENDENCIES
rake
rubocop-rails-omakase
ruby-lsp

BUNDLED WITH
2.5.18
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Tiago B Silva

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# README

This template is designed for personal use and open-source projects. It provides a quick start for Rails applications with essential features pre-configured, requiring minimal setup.

- **Database**: SQLite3 for simplicity and ease of setup
- **Defaults**:
- Solid Cache for efficient caching
- Solid Queue for background job processing
- Solid Cable for real-time features
- **Frontend**:
- Tailwind CSS for rapid, utility-first styling
- Importmap for managing JavaScript modules
- **Authentication**: Google OAuth integration for secure user sign-in

[Learn more about Rails application templates](https://guides.rubyonrails.org/rails_application_templates.html)


## Usage
```sh
rails new <app_name> -j importmap -c tailwind -d sqlite3 -m https://raw.githubusercontent.com/bastos/rails-template/refs/heads/main/template.rb
```

## License

This project is released under the [MIT License](https://opensource.org/licenses/MIT).
155 changes: 155 additions & 0 deletions template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
gem "dotenv-rails"
gem "solid_cache"
gem "solid_queue"
gem "solid_cable"
gem "omniauth"
gem "omniauth-google-oauth2"
gem "omniauth-rails_csrf_protection"
gem "letter_opener", group: :development
gem "webmock", group: :test
gem "mocha", group: :test

after_bundle do
git add: "."

git commit: "-m 'chore: initial commit'"

generate :model, "User",
"name:string",
"email:string:index",
"authentication_provider:string:index",
"authentication_uid:string:index"

rails_command "solid_cache:install"

rails_command "solid_queue:install"

rails_command "solid_cable:install"

environment "config.action_mailer.delivery_method = :letter_opener", env: "development"

environment "config.action_mailer.perform_deliveries = true", env: "development"

insert_into_file "config/database.yml", after: "production:\n" do <<~YAML.indent(2)
primary:
<<: *default
database: storage/production.sqlite3
cache:
<<: *default
database: storage/production_cache.sqlite3
migrations_paths: db/cache_migrate
queue:
<<: *default
database: storage/production_queue.sqlite3
migrations_paths: db/queue_migrate
cable:
<<: *default
database: storage/production_cable.sqlite3
migrations_paths: db/cable_migrate
YAML
end

insert_into_file "app/models/user.rb", after: "class User < ApplicationRecord\n" do <<~RUBY.indent(2)
validates :authentication_provider, presence: true
validates :authentication_uid, presence: true
validates :email, presence: true
def self.from_omniauth(auth)
where(authentication_uid: auth.uid, authentication_provider: auth.provider).first_or_initialize do |user|
user.email = auth.info.email
user.name = auth.info.name
end
end
RUBY
end

insert_into_file "app/controllers/application_controller.rb",
after: "class ApplicationController < ActionController::Base\n" do <<~RUBY.indent(2)
helper_method :current_user, :user_signed_in?
def current_user
@current_user ||= User.find_by(id: session[:user_id]) if session[:user_id]
end
def user_signed_in?
!!current_user
end
RUBY
end

generate :controller, "Sessions", "--skip-routes", "--skip-helper"

insert_into_file "app/controllers/sessions_controller.rb", after: "SessionsController < ApplicationController\n" do <<~RUBY.indent(2)
def create
user = User.from_omniauth(request.env["omniauth.auth"])
if user.save
session[:user_id] = user.id
redirect_to root_path, notice: "Signed in successfully"
else
redirect_to root_path, alert: "Failed to sign in"
end
end
def destroy
session[:user_id] = nil
redirect_to root_path, notice: "Signed out successfully"
end
RUBY
end

generate :controller, "Home"

insert_into_file "app/controllers/home_controller.rb", after: "class HomeController < ApplicationController\n" do <<~RUBY.indent(2)
def index
end
RUBY
end

create_file "app/views/home/index.html.erb", <<~HTML.indent(4)
<h1>Welcome to the home page</h1>
HTML

route <<~RUBY.indent(2)
get "auth/:provider/callback", to: "sessions#create"
get "auth/failure", to: redirect("/")
post "signout", to: "sessions#destroy", as: "signout"
root "home#index"
RUBY

initializer "omniauth.rb", <<~RUBY
Rails.application.config.middleware.use OmniAuth::Builder do
if Rails.env.production?
provider :google_oauth2, Rails.application.credentials.google.oauth2.client_id!, Rails.application.credentials.google.oauth2.client_secret!
else
# Change this to your Google OAuth client ID and secret as above or using environment variables
provider :developer
end
end
OmniAuth.config.allowed_request_methods = [ :post ]
OmniAuth.config.full_host = Rails.env.production? ? "https://example.com" : "http://127.0.0.1:3000"
RUBY

insert_into_file "app/views/layouts/application.html.erb", after: "<body>\n" do <<~HTML.indent(4)
<header>
<% if user_signed_in? %>
<%= button_to "Sign out", signout_path, method: :post, data: { turbo: false } %>
<% else %>
<% if Rails.env.development? %>
<%= button_to "Sign in", '/auth/developer', method: :post, data: { turbo: false } %>
<% else %>
<%= button_to "Sign in", '/auth/google_oauth2', method: :post, data: { turbo: false } %>
<% end %>
<% end %>
</header>
HTML
end

rails_command "db:drop db:create db:migrate"

run "bundle exec rubocop -a"

git add: "."

git commit: "-m 'feat: scaffold application'"
end

0 comments on commit 584d86e

Please sign in to comment.