Ademar's Blog Code Template

Public
This the base app blog setup
Icons/chart bar
Used 34 times
Created by
A Ademar Tutor

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/XnJs8r"
Template Source

Review the code before running this template on your machine.

# Install Bootstrap and dependencies
run "bundle add bootstrap"
run "bundle add sassc-rails"

# Run bundle install
Bundler.with_unbundled_env { run "bundle install" }

# Replace application.css with application.scss and import Bootstrap
run "rm app/assets/stylesheets/application.css"
run "touch app/assets/stylesheets/application.scss"
run "echo '@import \"bootstrap\";' >> app/assets/stylesheets/application.scss"

# Add Bootstrap and Popper.js to JavaScript (consider Webpacker for Rails 6+)
run "echo 'import \"popper\"' >> app/javascript/application.js"
run "echo 'import \"bootstrap\"' >> app/javascript/application.js"

# Update precompile list in assets initializer
run "echo 'Rails.application.config.assets.precompile += %w( bootstrap.min.js popper.js)' >> config/initializers/assets.rb"


# Update application.html.erb
run <<~HEREDOC
  echo '<!DOCTYPE html>
  <html>
    <head>
      <title>App</title>
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <%= csrf_meta_tags %>
      <%= csp_meta_tag %>

      <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
      <%= javascript_importmap_tags %>
    </head>

    <body>
      <div class="container-fluid">
        <nav class="navbar navbar-expand-lg bg-light">
          <div class="container">
            <a class="navbar-brand">
              <h3>Your App</h3>
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
              <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
              <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                <li class="nav-item">
                  <a class="nav-link active" aria-current="page" href="#">About</a>
                </li>
                <li class="nav-item">
                  <a class="nav-link active" href="#">Features</a>
                </li>

                <li class="nav-item">
                  <a class="nav-link active" aria-current="page" href="#">Blog</a>
                </li>

                <li class="nav-item">
                  <a class="nav-link active" aria-current="page" href="#">Contact</a>
                </li>
              </ul>
              <form class="">
                <%= link_to "Log in", new_user_session_path, class: "btn btn-outline-primary mr-2" %>
                <%= link_to "Sign up", new_user_registration_path, class: "btn btn-outline-success" %>
              </form>

            </div>
          </div>
        </nav>
        <%= yield %>
      </div>
    </body>
  </html>' > app/views/layouts/application.html.erb
HEREDOC

# Update Home#index
run "rails generate controller Home index"
run <<~HEREDOC
  echo '<div class="container">
    <h1 class="mt-5">Public Homepage</h1>
    <p class="lead">This is the public homepage generated by app template of Ademar.</p>
  </div>' > app/views/home/index.html.erb
HEREDOC

run "echo 'Rails.application.routes.draw do
  get \"up\" => \"rails/health#show\", as: :rails_health_check
  root \"home#index\"
end' > config/routes.rb"

# Install Devise
run "bundle add devise"
# Run bundle install
Bundler.with_unbundled_env { run "bundle install" }

rails_command "generate devise:install"

run "rails generate devise User"
run "rails db:migrate"


# Update devise views with Bootstrap
run "sed -i '' '247s/.*/  config.scoped_views = true/' config/initializers/devise.rb"
run "rails generate devise:views Users"

run <<~HEREDOC
  echo '<div class="container">
    <div class="row justify-content-md-center mt-5">
      <div class="col-md-4 ">
        <div class="card">
          <div class="card-body">
            <h2 class="card-title">Sign up</h2>
            <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
              <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">Email address</label>
                <%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
              </div>
              <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">Password</label>
                <% if @minimum_password_length %>
                <em>(<%= @minimum_password_length %> characters minimum)</em>
                <% end %><br />
                <%= f.password_field :password, autocomplete: "new-password", class: "form-control" %>
              </div>
              <div class="mb-3">
                <%= f.label :password_confirmation %><br />
                <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control" %>
              </div>
              <div class="mb-3 d-grid">
                <%= f.submit "Sign up", class: "btn btn-success" %>
              </div>
            <% end %>
            <%= render "users/shared/links" %>
          </div>
        </div>
      </div>
    </div>
  </div>' > app/views/users/registrations/new.html.erb
HEREDOC

run <<~HEREDOC
  echo '<div class="container">
    <div class="row justify-content-md-center mt-5">
      <div class="col-md-4 ">
        <div class="card">
          <div class="card-body">
            <h2 class="card-title">Log in</h2>
            <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
              <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">Email address</label>
                <%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control" %>
              </div>
              <div class="mb-3">
                <label for="exampleFormControlInput1" class="form-label">Password</label>
                <%= f.password_field :password, class: "form-control" %>
              </div>
              <div class="mb-3 d-grid">
                <%= f.submit "Sign in", class: "btn btn-success" %>
              </div>
            <% end %>
            <%= render "users/shared/links" %>
          </div>
        </div>
      </div>
    </div>
  </div>' > app/views/users/sessions/new.html.erb
HEREDOC

Comments

Sign up or Login to leave a comment.