Last active
November 2, 2021 06:55
-
-
Save anujmiddha/d8d987b37466961fab1505de0029eaf6 to your computer and use it in GitHub Desktop.
A Rails application template to setup an app with Web and API end points
This file contains hidden or 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
def user_model | |
file 'app/models/user.rb', <<-CODE | |
# frozen_string_literal: true | |
## User class | |
class User < ApplicationRecord | |
rolify | |
# Include default devise modules. Others available are: | |
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :validatable | |
include DeviseTokenAuth::Concerns::User | |
validates :name, presence: true | |
validates :email, presence: true | |
validates :encrypted_password, presence: true | |
after_create :assign_default_role | |
private | |
def assign_default_role | |
add_role(:new_user) if roles.blank? | |
end | |
end | |
CODE | |
end | |
def webpack_code | |
file 'config/webpack/environment.js', <<-CODE | |
const { environment } = require('@rails/webpacker') | |
const webpack = require('webpack') | |
environment.plugins.prepend('Provide', | |
new webpack.ProvidePlugin({ | |
Popper: ['popper.js', 'default'] | |
}) | |
) | |
module.exports = environment | |
CODE | |
file 'app/assets/stylesheets/application.scss', <<-CODE | |
@import 'bootstrap'; | |
CODE | |
file 'app/assets/stylesheets/bootstrap.scss', <<-CODE | |
@import 'bootstrap/scss/bootstrap'; | |
CODE | |
file 'app/javascript/packs/application.js', <<-CODE | |
// This file is automatically compiled by Webpack, along with any other files | |
// present in this directory. You're encouraged to place your actual application logic in | |
// a relevant structure within app/javascript and only use these pack files to reference | |
// that code so it'll be compiled. | |
require("@rails/ujs").start() | |
require("turbolinks").start() | |
require("@rails/activestorage").start() | |
require("channels") | |
import JQuery from 'jquery'; | |
window.$ = window.JQuery = JQuery; | |
import "bootstrap"; | |
// Uncomment to copy all static images under ../images to the output folder and reference | |
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) | |
// or the `imagePath` JavaScript helper below. | |
// | |
// const images = require.context('../images', true) | |
// const imagePath = (name) => images(name, true) | |
CODE | |
end | |
def base_controllers | |
file 'app/controllers/application_controller.rb', <<-CODE | |
# frozen_string_literal: true | |
## Base application controller class | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
include Pundit | |
protected | |
def authenticate_user! | |
redirect_to root_path, notice: 'Please sign in' unless user_signed_in? | |
end | |
end | |
CODE | |
file 'app/controllers/web/base_controller.rb', <<-CODE | |
# frozen_string_literal: true | |
## Base class for all admin controllers | |
class Admin::BaseController < ApplicationController | |
before_action :authenticate_user! | |
end | |
CODE | |
file 'app/controllers/api/v1/base_controller.rb', <<-CODE | |
# frozen_string_literal: true | |
# noinspection RubyClassModuleNamingConvention | |
# Base class for API controllers | |
class Api::V1::BaseController < ActionController::API | |
include DeviseTokenAuth::Concerns::SetUserByToken | |
include Pundit | |
end | |
CODE | |
end | |
def sample_controllers | |
file 'app/controllers/web/dashboard_controller.rb', <<-CODE | |
class Web::DashboardController < ApplicationController | |
def index | |
end | |
end | |
CODE | |
file 'app/controllers/api/v1/sample_controller.rb', <<-CODE | |
# frozen_string_literal: true | |
class Api::V1::SampleController < Api::V1::BaseController | |
def index | |
render json: { success: true, email: current_user.email }, status: :ok | |
end | |
end | |
CODE | |
end | |
def routes | |
file 'config/routes.rb', <<-CODE | |
# frozen_string_literal: true | |
Rails.application.routes.draw do | |
authenticated :user do | |
root to: 'web/dashboard#index', as: :authenticated_user_root | |
end | |
root to: redirect('/users/sign_in') | |
devise_for :users | |
namespace :web do | |
get 'dashboard/index' | |
end | |
# API end points | |
namespace :api do | |
scope module: :v1 do | |
mount_devise_token_auth_for 'User', at: 'auth' | |
get 'sample/index' | |
end | |
end | |
end | |
CODE | |
, force: true | |
end | |
# We're going to handle bundler and webpacker ourselves. | |
# Setting these options will prevent Rails from running them unnecessarily. | |
self.options = options.merge( | |
skip_bundle: true, | |
skip_webpack_install: true | |
) | |
# Setup Gems | |
gem_group :development, :test do | |
gem 'factory_bot_rails' | |
gem 'rspec-rails' | |
end | |
gem 'database_cleaner' | |
gem 'devise' | |
gem 'devise_token_auth' | |
gem 'faker' | |
gem 'pundit' | |
gem 'rolify' | |
gem 'shoulda-matchers' | |
# Setup user model | |
after_bundle do | |
generate(:'devise:install') | |
generate(:'devise_token_auth:install', 'User', 'auth') | |
generate(:rolify, 'Role', 'User') | |
rails_command('db:migrate') | |
user_model | |
run 'yarn add bootstrap jquery popper.js' | |
webpack_code | |
rails_command 'webpacker:install' | |
base_controllers | |
generate(:controller, 'web/Dashboard', 'index') | |
sample_controllers | |
routes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment