Giter Club home page Giter Club logo

rails-templates's Introduction

Nimble logo

Rails Templates


Our templates offer a rich boilerplate to jump start Rails-based application development and are based on our experience with building complex applications over the years.

Get Started

Requirements

  • Install ruby and set your local ruby version to 3.3.1
  • Install rails 7.1.3
  • Install node 22.1.0 (For creating web application)

πŸ“ If running on Apple M1, to build docker image, please make sure to set platform to AMD64 by export DOCKER_DEFAULT_PLATFORM=linux/amd64

Use the template

In order to use the template, initialize a new app with the following parameters:

rails new <app_name> -m https://raw.githubusercontent.com/nimblehq/rails-templates/main/template.rb

Supported template options:

  • --api - create an api-only application

To apply the template on an existing application, run following rails command:

rails app:template LOCATION=https://raw.githubusercontent.com/nimblehq/rails-templates/main/template.rb

# To apply on an api application
rails app:template LOCATION=https://raw.githubusercontent.com/nimblehq/rails-templates/main/template.rb API=true

# To apply a specific addon
rails app:template LOCATION=https://raw.githubusercontent.com/nimblehq/rails-templates/main/template.rb ADDON=<addon name>

Available Addons:

  • docker
  • nginx
  • phrase
  • bootstrap
  • slim
  • devise

After the template finishes generating all the files, run the following command to start the rails server.

make dev

Read more about Rails Application Template in the official Rails Guides.

Contribution Resources

License

This project is Copyright (c) 2014 and onwards. It is free software, and may be redistributed under the terms specified in the LICENSE file.

About

Nimble

This project is maintained and funded by Nimble.

We love open source and do our part in sharing our work with the community! See our other projects or hire our team to help build your product.

rails-templates's People

Contributors

andyduong1920 avatar ankitkalia1195 avatar bterone avatar caherrerapa avatar carryall avatar dependabot[bot] avatar fikrikarim avatar goose97 avatar hanam1ni avatar hoangmirs avatar htoo-eain-lwin avatar junan avatar khangbui22 avatar lahphim avatar longnd avatar malparty avatar mosharaf13 avatar noor-ahmed avatar olivierobert avatar przbadu avatar rafayet-monon avatar rafayetn avatar rosle avatar sang-github avatar svnlto avatar tolsee avatar tteerawat avatar tyrro avatar yutna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-templates's Issues

Rails 5.1.4 NOT support PG gem on version ~> 1.0.0

We can NOT use the Rails generator to create the model since the Rails version 5.1.4 NOT support pg gem on version 1.0.0.

We need to set the pg gem in Gemfile to something like this.

gem 'pg', '~> 0.21.0'

Rails Cache

Over the years, we have rarely used caching with Rails. And when we did, it was added afterwards in the life of the project, not at the beginning. But performance-wise, the apps we develop would gain a lot. And in terms of efficiency, it would be easier to develop with caching setup and in mind from the start.

Rails 5.2 improve a lot the built-in cache feature, so I think it's time to revisit this topic.


  • Review what's new in Rails caching, especially Redis Cache Store
  • Enable caching by default in the Rails Template
  • Implement a base application with caching working from the start

Dockerfile in master doesn't let you create an app, it raises syntax error

To replicate just run the rails -n from master. Also use http://hadolint.lukasmartinelli.ch/

 identical  lib/assets/.keep
       exist  log
   identical  log/.keep
       exist  public
   identical  public/404.html
   identical  public/422.html
   identical  public/500.html
   identical  public/apple-touch-icon-precomposed.png
   identical  public/apple-touch-icon.png
   identical  public/favicon.ico
   identical  public/robots.txt
       exist  tmp
   identical  tmp/.keep
       exist  tmp/cache
       exist  tmp/cache/assets
       exist  vendor
   identical  vendor/.keep
   identical  package.json
      remove  config/initializers/cors.rb
      remove  config/initializers/new_framework_defaults_5_1.rb
       apply  /Users/carlos/workspace/rails-templates/rails_docker.rb
/Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/actions.rb:224:in `instance_eval': /Users/carlos/workspace/rails-templates/rails_docker.rb:33: syntax error, unexpected tCONSTANT, expecting end-of-input (SyntaxError)
remove_file 'Dockerfile'
                       ^
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/actions.rb:224:in `apply'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/railties-5.1.1/lib/rails/generators/app_base.rb:166:in `apply_rails_template'
	from (eval):1:in `apply_rails_template'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `block in invoke_all'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `each'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `map'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:133:in `invoke_all'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/group.rb:232:in `dispatch'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/base.rb:444:in `start'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/railties-5.1.1/lib/rails/commands/application/application_command.rb:24:in `perform'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/command.rb:27:in `run'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor/invocation.rb:126:in `invoke_command'
	from /Users/carlos/.rvm/gems/ruby-2.4.1/gems/thor-0.19.4/lib/thor.rb:369:in `dispatch'

Add Basic Layouts for Devise's Auth Pages

We're using Devise for every project, and every time we're creating sign-in and sign-up pages that are quite similar in style.

In order to implement faster, we should have a basic layout defined in the template directly which could then be styled for each project.

Adjust the Chrome Driver version

Why

The Chrome Driver 2.46 doesn't support the current Chrome Stable Version (Chrome > 77.x) anymore, we have to upgrade Chrome Driver to latest stable version too

882693398-image

1965039742-image

Solution

  • Using the latest Chrome Driver version on https://chromedriver.chromium.org/downloads

  • Don't hardcode the Chrome Driver Version in shared/rspec/support/webdrivers.rb, instead moving it to ENV variable shared/config/application.yml, so later we can easy to adjust it on CI

Should we enable the Style/FrozenStringLiteralComment?

Should we enable the Style/FrozenStringLiteralComment

Style/FrozenStringLiteralComment:
  Enabled: false

Once we enabled that rule (by removing the block ⬆️), we need to run rubocop -a to add the # frozen_string_literal: true to each Ruby files.

Add wait_on_first_by_default in Capybara's configuration

To set wait_on_first_by_default in Capybara configuration to true because when we use first method in Capybara it won't wait for element to render completely if we chain with click method it will click immediately sometimes it cause test to fail, by adding this configuration will make Capybara wait until the first element appear then invoke chain method.

# spec/support/capybara.rb
...
Capybara.wait_on_first_by_default = true
...

Remove Turbolinks-related data attributes in the layout

As currently we're not including the turbolinks in the project by default. We've already removed turbolinks in gemfile and javascript. But in the layout, there are still turbolinks-related attributes.

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

We should remove those too.

Upgrade Node.js version.

In the Dockerfile, the Node.js version still set to version 8. I think we should upgrade the version number to the latest version of the LTS release. (Now the latest LTS version is 10.16.3)

Make template play nicely with --api

When creating a new rails application using rails new test_app -m ./rails_docker.rb -T --api , the process fails as rails_docker.rb is referencing javascript asset paths that don't exist in api mode.

It'd be good to have some guards around code that is looking for directories to be available.

Rails 5.1 support

Known issues with heroku:
If you are not using webpacker, add sass-rails, uglifier gems

Error about `.env.test` when rails-templates generate files while bootstrap the project.

This error occurs when you start the project by using rails-template. since the rails-templates no .env.test file. It has only .env file.

To prevent this error you must to modify the rails_docker.rb to this.

# from
copy_file 'rails_docker/.env.test', '.env.test'
gsub_file '.env.test', '#{app_name}', "#{app_name}"
# to
copy_file 'rails_docker/.env', '.env'
gsub_file '.env', '#{app_name}', "#{app_name}"

Replace the `figaro` gem to `dotenv-rails` gem

The problem is we want to share the ENV variable between Rails and Node JS on the development environment.

For Rails, the figaro use the application.yml to export the ENV variables.

For Node JS, we use the dotenv to load the ENV on development environment, the ENV variables store in .env file.

So the problem now is we have 2 places to store the ENV variable for the development environment, 1 is application.yml for Rails and 1 is .env for Node JS.

So on Rails, if we change the Gem from figaro to dotenv-rails, we can use the same .env file both Rails and Node JS.

Add Rubocop extensions

Issue

In order to increase our test coverage, it would be good to extended Rubocop base cops with Rails-spcific cops:

What's Next?

Add dependencies in Gemfile

Missing Redis configuration in Docker file.

I can NOT run the Procfile.dev to start the application server because it missing the Redis configuration in the docker file.

please add this to the docker file.

redis:
    image: redis:4.0.9
    container_name: <app-name>_redis
    ports:
      - "6379:6379"

Webpacker doesn’t compile files in packs-test for test env

In spec/support/webpack.rb file, We need to specify the NODE_ENV to test environment. Otherwise, it will compile in the development environment instead.

# Before
RSpec.configure do |config|
  config.before(:suite) do
    if ENV['TEST_SKIP_ASSET'].blank?
      puts 'Prepare webpack assets for test environment'

      `RAILS_ENV=test bin/webpack`
    end
  end
end

# After
RSpec.configure do |config|
  config.before(:suite) do
    if ENV['TEST_SKIP_ASSET'].blank?
      puts 'Prepare webpack assets for test environment'

      `RAILS_ENV=test NODE_ENV=test bin/webpack`
    end
  end
end

Upgrade ruby to 2.5.3

The base image point currently points to ruby:2.4.2-slim. So in an effort to keep it up to date, we should move this to ruby:2.5.0-slim.

It could be done at the same time as #58

Setup Webpacker by default

As it will be the default asset pipeline in Rails 6 and we used in all projects, we should get the head start and make it the default.

Use Sass functions for convert `px` unit in stylesheets.

Convert CSS unit from px to rem or em is a very tedious task. For improve more productive when you are working on stylesheets. rails-templates will provide rem and em Sass functions.

These functions will look like this when we are using.

rem()

.sample-1 {
  padding: rem(20px); // padding: 1.25rem;
}

.sample-2 {
  padding: rem(20px 16px); // padding: 1.25rem 1rem;
}

.sample-3 {
  padding: rem(20px 16px 32px); // padding: 1.25rem 1rem 2rem;
}

.sample-4 {
  padding: rem(20px 16px 32px auto); // padding: 1.25rem 1rem 2rem auto;
}

em()

.sample-1 {
  padding: em(20px); // padding: 1.25em;
}

.sample-2 {
  padding: em(20px 16px); // padding: 1.25em 1em;
}

.sample-3 {
  padding: em(20px 16px 32px); // padding: 1.25em 1em 2em;
}

.sample-4 {
  padding: em(20px 16px 32px auto); // padding: 1.25em 1em 2em auto;
}

Change capybara default_max_wait_time back to default

Right now the default Capybara default_max_wait_time on Rails template is 30s on local machine and 60s on CI. But is it the best setting? πŸ™ˆ

One example is from Braive. When we use Capybara.default_max_wait_time = 30s or 60s like from the Rails template, the Backend, Clinician and Page - System tests become around ~14min. This is much slower than the previous test, which had Capybara.default_max_wait_time = 5s, who have around ~7min testing time.

I think we have to keep the Capybara.default_max_wait_time = 5s or even 2s, like the default, because we already have retry system in place. And when looking at data from this and this, it seems some specs are still failing, even though we had wait for 60s:
image
See this PR's comment for link to the CI to see the build time.

In ideal world, Capybara shouldn't wait for anything. We already disable all animations when testing (at least on Braive), so Capybara isn't waiting for CSS animation. For waiting for AJAX request, better to use a wait_for_ajax helper:

def wait_for_ajax
    Timeout.timeout(Capybara.default_max_wait_time) do
      loop until finished_all_ajax_requests?
    end
  end

And if we have to wait for something specific, we can use :wait option on each expectation:

expect(page).to have_text('Lorem ipsum', wait: 3)

To be honest, I'm still not sure why some tests are failing randomly, and it isn't passed even when we had wait for 60s. But I think it's faster just to fail early, and make Rspec retry catch the failure and rerun the spec.

Code alignment

The current Rubocop configuration prefer this style

image

For me, I think it's nicer if we change the Rubocop configuration to support this style (aligned hash key, has value, aligned the equal)

Pasted_Image_6_11_19__14_19

To change that, we need to update the RuboCop/Cop/Layout/AlignHash in the rubocop.yml file.

image

And update the RuboCop/Cop/Layout/ExtraSpacing in the rubocop.yml file.

image

To format the code, we can use the Rubocop autocorrect feature rubocop -a

If we prefer the new style, I will open a PR to update the Rails template also open a PR to update the convention on Compass.

Setup a test pipeline to test every pull request

Rationale

As we do use the template on a regular basis (basically only when we start a new project), some issues due to outdated configuration or just plain human mistakes can make their way into the template. It's also hard to do a major Rails version jump by just changing the configuration (that's why the template is still on Rails 5.1.x 😒 ).

The solution would be to set up a test pipeline just like for any project.

Solution

The tests must do the following:

  • Create a new Rails application based on the template. Since #70, this would not even require to clone this repo to work
  • Tests that the web app can be accessed via the Docker container in all environment. Basically testing the accessibility of port 3000 (development) and 80
  • Tests that a PG and Redis containers exist and can be accessed

Codebase spec failed on `spec/support/disable_animation.rb` file.

Because we use RuboCop disabled comment but forgot to re-enable it in the below of the method. To solve this problem we should add the re-enable comment to the below of the method.

# spec/support/disable_animation.rb

# From

# rubocop:disable Metrics/MethodLength
# rubocop:disable Naming/HeredocDelimiterNaming
# rubocop:disable Layout/IndentHeredoc:
def inject(fragment)
  #....
end

# To 

# rubocop:disable Metrics/MethodLength
# rubocop:disable Naming/HeredocDelimiterNaming
# rubocop:disable Layout/IndentHeredoc:
def inject(fragment)
  #....
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Naming/HeredocDelimiterNaming
# rubocop:enable Layout/IndentHeredoc:

Clean up the default gems

Many gems in the default gems are unused or not the best ones anymore so it's time to trim the fat out.

Let's remove the following gems:

  • carrierwave: redundant with Actie Storage
  • carrierwave-aws
  • chronic: nobody uses it
  • font-awesome-sass

Replace the following gems:

Using `dns-prefetch` in meta tag

For speed improvements, an easy win is to use dns-prefetch for the application assets host and core third-party domains:

<!DOCTYPE html>
<html>
  <head>
    <title> <%= yield(:title) %> </title>
      <% if ActionController::Base.asset_host %>
        <link rel="dns-prefetch" href="<%= ActionController::Base.asset_host %>" />
      <% end %>
      <link rel="dns-prefetch" href="http://www.google-analytics.com">

More info: https://rubyplus.com/articles/5001-Speeding-up-Page-Load-in-Rails-Applications

Can NOT generate the template via Github URL.

I can NOT generate the template from Github URL. I need to clone from the development branch and generate from the local files.

Expect

rails new < app_name > -m https://raw.githubusercontent.com/nimbl3/rails-templates/master/rails_docker.rb -T

Actual

rails new < app_name > -m ./rails-templates/rails_docker.rb -T

Should we change from the master branch to the development branch?

Add `gem webpacker`

Should we add gem webpacker to the Gemfile.txt?
Because It has some issues when trying to create a new project with React environment using webpack.

I use this command --webpack=react.

Should we add the scsslint and eslint to the codebase_spec.rb

Issue

As we have SCSS linte and ES linter, and we check that manually, it works but sometimes we forgot to run it and because there is no test for that, so the invalid code can go to production 😱 we should add it to the codebase_spec.rb

  it 'does not offend scsslint' do
    expect(`scss-lint .`).to be_empty
  end

  it 'does not offend eslint' do
    expect(`yarn run eslint .`).to include 'Done'
  end

Then we can get something like this

image

Vote


Change release tagging strategy

The current tags were following semantic versioning following Rails but it's not really flexible as we cannot push updates more than once per Rails version πŸ™ƒ

So let's change all the past releases tags to follow semantic versioning not binded with Rails e.g. 1.0.0, 1.1.0...

Tidy up logging

https://github.com/roidrage/lograge

Instead of having an unparsable amount of logging output like this:

Started GET "/" for 127.0.0.1 at 2012-03-10 14:28:14 +0100
Processing by HomeController#index as HTML
  Rendered text template within layouts/application (0.0ms)
  Rendered layouts/_assets.html.erb (2.0ms)
  Rendered layouts/_top.html.erb (2.6ms)
  Rendered layouts/_about.html.erb (0.3ms)
  Rendered layouts/_google_analytics.html.erb (0.4ms)
Completed 200 OK in 79ms (Views: 78.8ms | ActiveRecord: 0.0ms)

you get a single line with all the important information, like this:

method=GET path=/jobs/833552.json format=json controller=JobsController  action=show status=200 duration=58.33 view=40.43 db=15.26

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.