├── log └── .keep ├── storage └── .keep ├── tmp ├── .keep ├── pids │ └── .keep └── storage │ └── .keep ├── vendor ├── .keep └── javascript │ └── .keep ├── lib ├── assets │ └── .keep └── tasks │ ├── .keep │ └── reset_tags.rake ├── public ├── favicon.ico ├── apple-touch-icon.png ├── apple-touch-icon-precomposed.png ├── robots.txt ├── 500.html ├── 422.html └── 404.html ├── app ├── assets │ ├── images │ │ └── .keep │ ├── config │ │ └── manifest.js │ └── stylesheets │ │ └── application.css ├── models │ ├── concerns │ │ └── .keep │ ├── kernel.rb │ ├── tag.rb │ ├── comment.rb │ ├── post.rb │ ├── application_record.rb │ └── restricted_access.rb ├── controllers │ ├── concerns │ │ └── .keep │ ├── ping_controller.rb │ ├── application_controller.rb │ ├── comments_controller.rb │ └── posts_controller.rb ├── views │ ├── ping │ │ └── index.html.erb │ ├── layouts │ │ ├── mailer.text.erb │ │ ├── mailer.html.erb │ │ └── application.html.erb │ ├── posts │ │ ├── _post.html.erb │ │ ├── new.html.erb │ │ ├── edit.html.erb │ │ ├── index.html.erb │ │ ├── _form.html.erb │ │ └── show.html.erb │ └── comments │ │ ├── new.html.erb │ │ └── _form.html.erb ├── helpers │ ├── posts_helper.rb │ ├── comments_helper.rb │ └── application_helper.rb ├── channels │ └── application_cable │ │ ├── channel.rb │ │ └── connection.rb ├── mailers │ └── application_mailer.rb ├── javascript │ ├── application.js │ └── controllers │ │ ├── hello_controller.js │ │ ├── application.js │ │ └── index.js └── jobs │ └── application_job.rb ├── .ruby-version ├── bin ├── rake ├── importmap ├── rails ├── setup └── bundle ├── config ├── environment.rb ├── initializers │ ├── allowed_ips.rb │ ├── filter_parameter_logging.rb │ ├── permissions_policy.rb │ ├── assets.rb │ ├── inflections.rb │ └── content_security_policy.rb ├── boot.rb ├── cable.yml ├── importmap.rb ├── credentials.yml.enc ├── routes.rb ├── database.yml ├── locales │ └── en.yml ├── storage.yml ├── puma.rb ├── environments │ ├── test.rb │ ├── development.rb │ └── production.rb └── application.rb ├── config.ru ├── db ├── migrate │ ├── 20230209001013_create_posts.rb │ ├── 20230209062605_create_kernels.rb │ ├── 20230209081714_create_tags.rb │ └── 20230209001044_create_comments.rb ├── seeds.rb └── schema.rb ├── Rakefile ├── .gitattributes ├── .gitignore ├── Gemfile ├── Gemfile.lock └── README.md /log/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /storage/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tmp/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/assets/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tmp/pids/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tmp/storage/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/assets/images/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /vendor/javascript/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | ruby-3.2.0 2 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/apple-touch-icon.png: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /public/apple-touch-icon-precomposed.png: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/views/ping/index.html.erb: -------------------------------------------------------------------------------- 1 |

PONG

2 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.text.erb: -------------------------------------------------------------------------------- 1 | <%= yield %> 2 | -------------------------------------------------------------------------------- /app/helpers/posts_helper.rb: -------------------------------------------------------------------------------- 1 | module PostsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/comments_helper.rb: -------------------------------------------------------------------------------- 1 | module CommentsHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/models/kernel.rb: -------------------------------------------------------------------------------- 1 | class Kernel < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /app/helpers/application_helper.rb: -------------------------------------------------------------------------------- 1 | module ApplicationHelper 2 | end 3 | -------------------------------------------------------------------------------- /app/models/tag.rb: -------------------------------------------------------------------------------- 1 | class Tag < ApplicationRecord 2 | belongs_to :taggable, polymorphic: true 3 | end 4 | -------------------------------------------------------------------------------- /app/controllers/ping_controller.rb: -------------------------------------------------------------------------------- 1 | class PingController < ApplicationController 2 | def index 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative "../config/boot" 3 | require "rake" 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/importmap: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require_relative "../config/application" 4 | require "importmap/commands" 5 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | -------------------------------------------------------------------------------- /app/models/comment.rb: -------------------------------------------------------------------------------- 1 | class Comment < ApplicationRecord 2 | belongs_to :post 3 | has_many :tags, as: :taggable 4 | end 5 | -------------------------------------------------------------------------------- /app/channels/application_cable/channel.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Channel < ActionCable::Channel::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /app/models/post.rb: -------------------------------------------------------------------------------- 1 | class Post < ApplicationRecord 2 | has_many :comments, dependent: :destroy 3 | has_many :tags, as: :taggable 4 | end 5 | -------------------------------------------------------------------------------- /app/channels/application_cable/connection.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Connection < ActionCable::Connection::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | default from: "from@example.com" 3 | layout "mailer" 4 | end 5 | -------------------------------------------------------------------------------- /app/views/posts/_post.html.erb: -------------------------------------------------------------------------------- 1 |
2 |

3 | Title: 4 | <%= post.title %> 5 |

6 | 7 |
8 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path("../config/application", __dir__) 3 | require_relative "../config/boot" 4 | require "rails/commands" 5 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative "application" 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/initializers/allowed_ips.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Rails.configuration.to_prepare do 4 | RestrictedAccess.allow_ip("123.456.789.012") 5 | end 6 | -------------------------------------------------------------------------------- /app/assets/config/manifest.js: -------------------------------------------------------------------------------- 1 | //= link_tree ../images 2 | //= link_directory ../stylesheets .css 3 | //= link_tree ../../javascript .js 4 | //= link_tree ../../../vendor/javascript .js 5 | -------------------------------------------------------------------------------- /app/views/posts/new.html.erb: -------------------------------------------------------------------------------- 1 |

New post

2 | 3 | <%= render "form", post: @post %> 4 | 5 |
6 | 7 |
8 | <%= link_to "Back to posts", posts_path %> 9 |
10 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative "config/environment" 4 | 5 | run Rails.application 6 | Rails.application.load_server 7 | -------------------------------------------------------------------------------- /app/javascript/application.js: -------------------------------------------------------------------------------- 1 | // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails 2 | import "@hotwired/turbo-rails" 3 | import "controllers" 4 | -------------------------------------------------------------------------------- /lib/tasks/reset_tags.rake: -------------------------------------------------------------------------------- 1 | desc "Delete all existing tags." 2 | task :reset_tags => :environment do 3 | puts "Deleting all #{Tag.count} tags..." 4 | Tag.delete_all 5 | puts "Done!" 6 | end 7 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::Base 2 | def self.controller_path 3 | @controller_path ||= super.delete_prefix("my_app/application/") 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /app/javascript/controllers/hello_controller.js: -------------------------------------------------------------------------------- 1 | import { Controller } from "@hotwired/stimulus" 2 | 3 | export default class extends Controller { 4 | connect() { 5 | this.element.textContent = "Hello World!" 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | primary_abstract_class 3 | 4 | def to_partial_path 5 | @_to_partial_path ||= super.delete_prefix("my_app/application/") 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 2 | 3 | require "bundler/setup" # Set up gems listed in the Gemfile. 4 | require "bootsnap/setup" # Speed up boot time by caching expensive operations. 5 | -------------------------------------------------------------------------------- /db/migrate/20230209001013_create_posts.rb: -------------------------------------------------------------------------------- 1 | class CreatePosts < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :posts do |t| 4 | t.string :title 5 | 6 | t.timestamps 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /app/views/comments/new.html.erb: -------------------------------------------------------------------------------- 1 |

New comment on "<%= @post.title %>"

2 | 3 | <%= render "form", post: @post, comment: @comment %> 4 | 5 |
6 | 7 |
8 | <%= link_to "Back to post", post_path(@post) %> 9 |
10 | -------------------------------------------------------------------------------- /app/views/posts/edit.html.erb: -------------------------------------------------------------------------------- 1 |

Editing post

2 | 3 | <%= render "form", post: @post %> 4 | 5 |
6 | 7 |
8 | <%= link_to "Show this post", @post %> | 9 | <%= link_to "Back to posts", posts_path %> 10 |
11 | -------------------------------------------------------------------------------- /db/migrate/20230209062605_create_kernels.rb: -------------------------------------------------------------------------------- 1 | class CreateKernels < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :kernels do |t| 4 | t.string :foo 5 | 6 | t.timestamps 7 | end 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: test 6 | 7 | production: 8 | adapter: redis 9 | url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> 10 | channel_prefix: my_app_production 11 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative "config/application" 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # See https://git-scm.com/docs/gitattributes for more about git attribute files. 2 | 3 | # Mark the database schema as having been generated. 4 | db/schema.rb linguist-generated 5 | 6 | # Mark any vendored files as having been vendored. 7 | vendor/* linguist-vendored 8 | -------------------------------------------------------------------------------- /db/migrate/20230209081714_create_tags.rb: -------------------------------------------------------------------------------- 1 | class CreateTags < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :tags do |t| 4 | t.string :name 5 | t.references :taggable, polymorphic: true, null: false 6 | 7 | t.timestamps 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /app/javascript/controllers/application.js: -------------------------------------------------------------------------------- 1 | import { Application } from "@hotwired/stimulus" 2 | 3 | const application = Application.start() 4 | 5 | // Configure Stimulus development experience 6 | application.debug = false 7 | window.Stimulus = application 8 | 9 | export { application } 10 | -------------------------------------------------------------------------------- /db/migrate/20230209001044_create_comments.rb: -------------------------------------------------------------------------------- 1 | class CreateComments < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :comments do |t| 4 | t.text :content 5 | t.belongs_to :post, null: false, foreign_key: true 6 | 7 | t.timestamps 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | # Automatically retry jobs that encountered a deadlock 3 | # retry_on ActiveRecord::Deadlocked 4 | 5 | # Most jobs are safe to ignore if the underlying records are no longer available 6 | # discard_on ActiveJob::DeserializationError 7 | end 8 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/views/posts/index.html.erb: -------------------------------------------------------------------------------- 1 |

<%= notice %>

2 | 3 |

Posts

4 | 5 |
6 | <% @posts.each do |post| %> 7 | <%= render post %> 8 |

9 | <%= link_to "Show this post", post %> 10 |

11 | <% end %> 12 |
13 | 14 | <%= link_to "New post", new_post_path %> 15 | -------------------------------------------------------------------------------- /config/importmap.rb: -------------------------------------------------------------------------------- 1 | # Pin npm packages by running ./bin/importmap 2 | 3 | pin "application", preload: true 4 | pin "@hotwired/turbo-rails", to: "turbo.min.js", preload: true 5 | pin "@hotwired/stimulus", to: "stimulus.min.js", preload: true 6 | pin "@hotwired/stimulus-loading", to: "stimulus-loading.js", preload: true 7 | pin_all_from "app/javascript/controllers", under: "controllers" 8 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) 7 | # Character.create(name: "Luke", movie: movies.first) 8 | -------------------------------------------------------------------------------- /app/models/restricted_access.rb: -------------------------------------------------------------------------------- 1 | module RestrictedAccess 2 | class << self 3 | attr_reader :allowed_ips 4 | 5 | def allow?(request) 6 | request.local? || allowed_ips.include?(request.ip) 7 | end 8 | 9 | def allow_ip(ip) 10 | (@allowed_ips ||= []) << ip 11 | end 12 | end 13 | 14 | class RouteConstraint 15 | def matches?(request) 16 | RestrictedAccess.allow?(request) 17 | end 18 | end 19 | end 20 | -------------------------------------------------------------------------------- /app/views/layouts/application.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MyApp 5 | 6 | <%= csrf_meta_tags %> 7 | <%= csp_meta_tag %> 8 | 9 | <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> 10 | <%= javascript_importmap_tags %> 11 | 12 | 13 | 14 | <%= yield %> 15 | 16 | 17 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure parameters to be filtered from the log file. Use this to limit dissemination of 4 | # sensitive information. See the ActiveSupport::ParameterFilter documentation for supported 5 | # notations and behaviors. 6 | Rails.application.config.filter_parameters += [ 7 | :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn 8 | ] 9 | -------------------------------------------------------------------------------- /config/initializers/permissions_policy.rb: -------------------------------------------------------------------------------- 1 | # Define an application-wide HTTP permissions policy. For further 2 | # information see https://developers.google.com/web/updates/2018/06/feature-policy 3 | # 4 | # Rails.application.config.permissions_policy do |f| 5 | # f.camera :none 6 | # f.gyroscope :none 7 | # f.microphone :none 8 | # f.usb :none 9 | # f.fullscreen :self 10 | # f.payment :self, "https://secure.example.com" 11 | # end 12 | -------------------------------------------------------------------------------- /config/credentials.yml.enc: -------------------------------------------------------------------------------- 1 | K500sTdGoUv21iV4dWHFO7cQ97lwRZS4NIszANPlB0Vc6yV+woCukwh3PhoLey1pJX/ouGMKMdzKNgEbtMv5oQp5ZxVUsDLrUROwoNXzjXQYpCRmdRxYXFH3xOL6CAu/eGpwxKP5BjwLC3BdEqpJ1Cmf/oqVKCqabSfcu5ZG5yGU6ePmEqKG+6+LQvOBjZaKGNZWXe+YfQXLCjW0dx5dtmPcfRDVimHUBXdvrF0juC0Y4xHQ3hnNW69ihXEfLxosXd1PdP8fuqukbN1yWklD57L3aKjwIa2g2KyCUzSIMg3Pc3oG61LgHBLqhoV+NU+//VyJm7AwPtFYUE7MK4J1HA9VwJhCTfGlOKBBrmYAWkIPI4x/66JQmhTyMwBdTBW6sPPPrRSoEHlhuOUk63F1aGyz1NW9HqOw9LSy--lddWWpuBzrqjIPcS--PK1HNJOgSoBqeRYMdBzbAQ== -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 | 4 | scope module: "my_app/application" do 5 | resources :posts do 6 | resources :comments, only: [:new, :destroy, :create] 7 | end 8 | 9 | # Demonstrate that routes load constants from application namespace. 10 | get "ping", to: "ping#index", constraints: RestrictedAccess::RouteConstraint.new 11 | 12 | # Defines the root path route ("/") 13 | root "posts#index" 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /config/initializers/assets.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Version of your assets, change this if you want to expire all your assets. 4 | Rails.application.config.assets.version = "1.0" 5 | 6 | # Add additional assets to the asset load path. 7 | # Rails.application.config.assets.paths << Emoji.images_path 8 | 9 | # Precompile additional assets. 10 | # application.js, application.css, and all non-JS/CSS in the app/assets 11 | # folder are already added. 12 | # Rails.application.config.assets.precompile += %w( admin.js admin.css ) 13 | -------------------------------------------------------------------------------- /app/views/posts/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= form_with(model: post) do |form| %> 2 | <% if post.errors.any? %> 3 |
4 |

<%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:

5 | 6 | 11 |
12 | <% end %> 13 | 14 |
15 | <%= form.label :title, style: "display: block" %> 16 | <%= form.text_field :title %> 17 |
18 | 19 |
20 | <%= form.submit %> 21 |
22 | <% end %> 23 | -------------------------------------------------------------------------------- /app/views/comments/_form.html.erb: -------------------------------------------------------------------------------- 1 | <%= form_with(model: [post, comment]) do |form| %> 2 | <% if comment.errors.any? %> 3 |
4 |

<%= pluralize(comment.errors.count, "error") %> prohibited this post from being saved:

5 | 6 | 11 |
12 | <% end %> 13 | 14 |
15 | <%= form.label :content, style: "display: block" %> 16 | <%= form.text_field :content %> 17 |
18 | 19 |
20 | <%= form.submit %> 21 |
22 | <% end %> 23 | -------------------------------------------------------------------------------- /app/javascript/controllers/index.js: -------------------------------------------------------------------------------- 1 | // Import and register all your controllers from the importmap under controllers/* 2 | 3 | import { application } from "controllers/application" 4 | 5 | // Eager load all controllers defined in the import map under controllers/**/*_controller 6 | import { eagerLoadControllersFrom } from "@hotwired/stimulus-loading" 7 | eagerLoadControllersFrom("controllers", application) 8 | 9 | // Lazy load controllers as they appear in the DOM (remember not to preload controllers in import map!) 10 | // import { lazyLoadControllersFrom } from "@hotwired/stimulus-loading" 11 | // lazyLoadControllersFrom("controllers", application) 12 | -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite. Versions 3.8.0 and up are supported. 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem "sqlite3" 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, "\\1en" 8 | # inflect.singular /^(ox)en/i, "\\1" 9 | # inflect.irregular "person", "people" 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym "RESTful" 16 | # end 17 | -------------------------------------------------------------------------------- /app/assets/stylesheets/application.css: -------------------------------------------------------------------------------- 1 | /* 2 | * This is a manifest file that'll be compiled into application.css, which will include all the files 3 | * listed below. 4 | * 5 | * Any CSS (and SCSS, if configured) file within this directory, lib/assets/stylesheets, or any plugin's 6 | * vendor/assets/stylesheets directory can be referenced here using a relative path. 7 | * 8 | * You're free to add application-wide styles to this file and they'll appear at the bottom of the 9 | * compiled file so the styles you add here take precedence over styles defined in any other CSS 10 | * files in this directory. Styles in this file should be added after the last require_* statement. 11 | * It is generally better to create a new file per style scope. 12 | * 13 | *= require_tree . 14 | *= require_self 15 | */ 16 | -------------------------------------------------------------------------------- /app/views/posts/show.html.erb: -------------------------------------------------------------------------------- 1 |

<%= notice %>

2 | 3 |
4 |

5 | Title: 6 | <%= @post.title %> 7 |

8 | 9 | <% if @post.comments.any? %> 10 |

Comments

11 | 12 | <% @post.comments.reverse_each do |comment| %> 13 |

<%= comment.content %>
Posted <%= time_ago_in_words(comment.created_at) %> ago.

14 | <% end %> 15 | <% else %> 16 |

No comments.

17 | <% end %> 18 |
19 |

<%= link_to "Post a comment", new_post_comment_path(@post) %>

20 |
21 |
22 | 23 |
24 | <%= link_to "Edit this post", edit_post_path(@post) %> | 25 | <%= link_to "Back to posts", posts_path %> 26 | 27 | <%= button_to "Destroy this post", @post, method: :delete %> 28 |
29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-* 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log/* 16 | /tmp/* 17 | !/log/.keep 18 | !/tmp/.keep 19 | 20 | # Ignore pidfiles, but keep the directory. 21 | /tmp/pids/* 22 | !/tmp/pids/ 23 | !/tmp/pids/.keep 24 | 25 | # Ignore uploaded files in development. 26 | /storage/* 27 | !/storage/.keep 28 | /tmp/storage/* 29 | !/tmp/storage/ 30 | !/tmp/storage/.keep 31 | 32 | /public/assets 33 | 34 | # Ignore master key for decrypting credentials and more. 35 | /config/master.key 36 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t "hello" 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t("hello") %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # The following keys must be escaped otherwise they will not be retrieved by 20 | # the default I18n backend: 21 | # 22 | # true, false, on, off, yes, no 23 | # 24 | # Instead, surround them with single quotes. 25 | # 26 | # en: 27 | # "true": "foo" 28 | # 29 | # To learn more, please read the Rails Internationalization guide 30 | # available at https://guides.rubyonrails.org/i18n.html. 31 | 32 | en: 33 | hello: "Hello world" 34 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "fileutils" 3 | 4 | # path to your application root. 5 | APP_ROOT = File.expand_path("..", __dir__) 6 | 7 | def system!(*args) 8 | system(*args) || abort("\n== Command #{args} failed ==") 9 | end 10 | 11 | FileUtils.chdir APP_ROOT do 12 | # This script is a way to set up or update your development environment automatically. 13 | # This script is idempotent, so that you can run it at any time and get an expectable outcome. 14 | # Add necessary setup steps to this file. 15 | 16 | puts "== Installing dependencies ==" 17 | system! "gem install bundler --conservative" 18 | system("bundle check") || system!("bundle install") 19 | 20 | # puts "\n== Copying sample files ==" 21 | # unless File.exist?("config/database.yml") 22 | # FileUtils.cp "config/database.yml.sample", "config/database.yml" 23 | # end 24 | 25 | puts "\n== Preparing database ==" 26 | system! "bin/rails db:prepare" 27 | 28 | puts "\n== Removing old logs and tempfiles ==" 29 | system! "bin/rails log:clear tmp:clear" 30 | 31 | puts "\n== Restarting application server ==" 32 | system! "bin/rails restart" 33 | end 34 | -------------------------------------------------------------------------------- /app/controllers/comments_controller.rb: -------------------------------------------------------------------------------- 1 | class CommentsController < ApplicationController 2 | before_action :set_comment, only: %i[ destroy ] 3 | 4 | # GET /posts/1/comments/new 5 | def new 6 | @post = Post.find(params[:post_id]) 7 | @comment = Comment.new(post_id: params[:post_id]) 8 | end 9 | 10 | def create 11 | @post = Post.find(params[:post_id]) 12 | @comment = @post.comments.create(comment_params) 13 | 14 | redirect_to @post 15 | end 16 | 17 | # DELETE /posts/1/comments/1 or /posts/1/comments/1.json 18 | def destroy 19 | @comment.destroy 20 | 21 | respond_to do |format| 22 | format.html { redirect_to comments_url, notice: "Comment was successfully destroyed." } 23 | format.json { head :no_content } 24 | end 25 | end 26 | 27 | private 28 | # Use callbacks to share common setup or constraints between actions. 29 | def set_comment 30 | @comment = Comment.find(params[:id], post_id: params[:post_id]) 31 | end 32 | 33 | # Only allow a list of trusted parameters through. 34 | def comment_params 35 | params.require(:comment).permit(:content) 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /config/initializers/content_security_policy.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Define an application-wide content security policy. 4 | # See the Securing Rails Applications Guide for more information: 5 | # https://guides.rubyonrails.org/security.html#content-security-policy-header 6 | 7 | # Rails.application.configure do 8 | # config.content_security_policy do |policy| 9 | # policy.default_src :self, :https 10 | # policy.font_src :self, :https, :data 11 | # policy.img_src :self, :https, :data 12 | # policy.object_src :none 13 | # policy.script_src :self, :https 14 | # policy.style_src :self, :https 15 | # # Specify URI for violation reports 16 | # # policy.report_uri "/csp-violation-report-endpoint" 17 | # end 18 | # 19 | # # Generate session nonces for permitted importmap and inline scripts 20 | # config.content_security_policy_nonce_generator = ->(request) { request.session.id.to_s } 21 | # config.content_security_policy_nonce_directives = %w(script-src) 22 | # 23 | # # Report violations without enforcing the policy. 24 | # # config.content_security_policy_report_only = true 25 | # end 26 | -------------------------------------------------------------------------------- /config/storage.yml: -------------------------------------------------------------------------------- 1 | test: 2 | service: Disk 3 | root: <%= Rails.root.join("tmp/storage") %> 4 | 5 | local: 6 | service: Disk 7 | root: <%= Rails.root.join("storage") %> 8 | 9 | # Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 10 | # amazon: 11 | # service: S3 12 | # access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 13 | # secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 14 | # region: us-east-1 15 | # bucket: your_own_bucket-<%= Rails.env %> 16 | 17 | # Remember not to checkin your GCS keyfile to a repository 18 | # google: 19 | # service: GCS 20 | # project: your_project 21 | # credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> 22 | # bucket: your_own_bucket-<%= Rails.env %> 23 | 24 | # Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) 25 | # microsoft: 26 | # service: AzureStorage 27 | # storage_account_name: your_account_name 28 | # storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> 29 | # container: your_container_name-<%= Rails.env %> 30 | 31 | # mirror: 32 | # service: Mirror 33 | # primary: local 34 | # mirrors: [ amazon, google, microsoft ] 35 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # This file is the source Rails uses to define your schema when running `bin/rails 6 | # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7 | # be faster and is potentially less error prone than running all of your 8 | # migrations from scratch. Old migrations may fail to apply correctly if those 9 | # migrations use external dependencies or application code. 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema[7.0].define(version: 2023_02_09_081714) do 14 | create_table "comments", force: :cascade do |t| 15 | t.text "content" 16 | t.integer "post_id", null: false 17 | t.datetime "created_at", null: false 18 | t.datetime "updated_at", null: false 19 | t.index ["post_id"], name: "index_comments_on_post_id" 20 | end 21 | 22 | create_table "kernels", force: :cascade do |t| 23 | t.string "foo" 24 | t.datetime "created_at", null: false 25 | t.datetime "updated_at", null: false 26 | end 27 | 28 | create_table "posts", force: :cascade do |t| 29 | t.string "title" 30 | t.datetime "created_at", null: false 31 | t.datetime "updated_at", null: false 32 | end 33 | 34 | create_table "tags", force: :cascade do |t| 35 | t.string "name" 36 | t.string "taggable_type", null: false 37 | t.integer "taggable_id", null: false 38 | t.datetime "created_at", null: false 39 | t.datetime "updated_at", null: false 40 | t.index ["taggable_type", "taggable_id"], name: "index_tags_on_taggable" 41 | end 42 | 43 | add_foreign_key "comments", "posts" 44 | end 45 | -------------------------------------------------------------------------------- /public/500.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | We're sorry, but something went wrong (500) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

We're sorry, but something went wrong.

62 |
63 |

If you are the application owner check the logs for more information.

64 |
65 | 66 | 67 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers: a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum; this matches the default thread size of Active Record. 6 | # 7 | max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } 8 | min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } 9 | threads min_threads_count, max_threads_count 10 | 11 | # Specifies the `worker_timeout` threshold that Puma will use to wait before 12 | # terminating a worker in development environments. 13 | # 14 | worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" 15 | 16 | # Specifies the `port` that Puma will listen on to receive requests; default is 3000. 17 | # 18 | port ENV.fetch("PORT") { 3000 } 19 | 20 | # Specifies the `environment` that Puma will run in. 21 | # 22 | environment ENV.fetch("RAILS_ENV") { "development" } 23 | 24 | # Specifies the `pidfile` that Puma will use. 25 | pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } 26 | 27 | # Specifies the number of `workers` to boot in clustered mode. 28 | # Workers are forked web server processes. If using threads and workers together 29 | # the concurrency of the application would be max `threads` * `workers`. 30 | # Workers do not work on JRuby or Windows (both of which do not support 31 | # processes). 32 | # 33 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 34 | 35 | # Use the `preload_app!` method when specifying a `workers` number. 36 | # This directive tells Puma to first boot the application and load code 37 | # before forking the application. This takes advantage of Copy On Write 38 | # process behavior so workers use less memory. 39 | # 40 | # preload_app! 41 | 42 | # Allow puma to be restarted by `bin/rails restart` command. 43 | plugin :tmp_restart 44 | -------------------------------------------------------------------------------- /public/422.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The change you wanted was rejected (422) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The change you wanted was rejected.

62 |

Maybe you tried to change something you didn't have access to.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /public/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | The page you were looking for doesn't exist (404) 5 | 6 | 55 | 56 | 57 | 58 | 59 |
60 |
61 |

The page you were looking for doesn't exist.

62 |

You may have mistyped the address or the page may have moved.

63 |
64 |

If you are the application owner check the logs for more information.

65 |
66 | 67 | 68 | -------------------------------------------------------------------------------- /app/controllers/posts_controller.rb: -------------------------------------------------------------------------------- 1 | class PostsController < ApplicationController 2 | before_action :set_post, only: %i[ show edit update destroy ] 3 | 4 | # GET /posts or /posts.json 5 | def index 6 | @posts = Post.all 7 | end 8 | 9 | # GET /posts/1 or /posts/1.json 10 | def show 11 | end 12 | 13 | # GET /posts/new 14 | def new 15 | @post = Post.new 16 | end 17 | 18 | # GET /posts/1/edit 19 | def edit 20 | end 21 | 22 | # POST /posts or /posts.json 23 | def create 24 | @post = Post.new(post_params) 25 | 26 | respond_to do |format| 27 | if @post.save 28 | format.html { redirect_to post_url(@post), notice: "Post was successfully created." } 29 | format.json { render :show, status: :created, location: @post } 30 | else 31 | format.html { render :new, status: :unprocessable_entity } 32 | format.json { render json: @post.errors, status: :unprocessable_entity } 33 | end 34 | end 35 | end 36 | 37 | # PATCH/PUT /posts/1 or /posts/1.json 38 | def update 39 | respond_to do |format| 40 | if @post.update(post_params) 41 | format.html { redirect_to post_url(@post), notice: "Post was successfully updated." } 42 | format.json { render :show, status: :ok, location: @post } 43 | else 44 | format.html { render :edit, status: :unprocessable_entity } 45 | format.json { render json: @post.errors, status: :unprocessable_entity } 46 | end 47 | end 48 | end 49 | 50 | # DELETE /posts/1 or /posts/1.json 51 | def destroy 52 | @post.destroy 53 | 54 | respond_to do |format| 55 | format.html { redirect_to posts_url, notice: "Post was successfully destroyed." } 56 | format.json { head :no_content } 57 | end 58 | end 59 | 60 | private 61 | # Use callbacks to share common setup or constraints between actions. 62 | def set_post 63 | @post = Post.find(params[:id]) 64 | end 65 | 66 | # Only allow a list of trusted parameters through. 67 | def post_params 68 | params.require(:post).permit(:title) 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 | 4 | ruby "3.2.0" 5 | 6 | gem "im", "~> 0.2.2" 7 | 8 | # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" 9 | gem "rails", "~> 7.0.4", ">= 7.0.4.2" 10 | 11 | # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] 12 | gem "sprockets-rails" 13 | 14 | # Use sqlite3 as the database for Active Record 15 | gem "sqlite3", "~> 1.4" 16 | 17 | # Use the Puma web server [https://github.com/puma/puma] 18 | gem "puma", "~> 5.0" 19 | 20 | # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] 21 | gem "importmap-rails" 22 | 23 | # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] 24 | gem "turbo-rails" 25 | 26 | # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] 27 | gem "stimulus-rails" 28 | 29 | # Build JSON APIs with ease [https://github.com/rails/jbuilder] 30 | gem "jbuilder" 31 | 32 | # Use Redis adapter to run Action Cable in production 33 | # gem "redis", "~> 4.0" 34 | 35 | # Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis] 36 | # gem "kredis" 37 | 38 | # Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword] 39 | # gem "bcrypt", "~> 3.1.7" 40 | 41 | # Windows does not include zoneinfo files, so bundle the tzinfo-data gem 42 | gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] 43 | 44 | # Reduces boot times through caching; required in config/boot.rb 45 | gem "bootsnap", require: false 46 | 47 | # Use Sass to process CSS 48 | # gem "sassc-rails" 49 | 50 | # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] 51 | # gem "image_processing", "~> 1.2" 52 | 53 | group :development, :test do 54 | # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem 55 | gem "debug", platforms: %i[ mri mingw x64_mingw ] 56 | end 57 | 58 | group :development do 59 | # Use console on exceptions pages [https://github.com/rails/web-console] 60 | gem "web-console" 61 | 62 | # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] 63 | # gem "rack-mini-profiler" 64 | 65 | # Speed up commands on slow machines / big apps [https://github.com/rails/spring] 66 | # gem "spring" 67 | end 68 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | # The test environment is used exclusively to run your application's 4 | # test suite. You never need to work with it otherwise. Remember that 5 | # your test database is "scratch space" for the test suite and is wiped 6 | # and recreated between test runs. Don't rely on the data there! 7 | 8 | Rails.application.configure do 9 | # Settings specified here will take precedence over those in config/application.rb. 10 | 11 | # Turn false under Spring and add config.action_view.cache_template_loading = true. 12 | config.cache_classes = true 13 | 14 | # Eager loading loads your whole application. When running a single test locally, 15 | # this probably isn't necessary. It's a good idea to do in a continuous integration 16 | # system, or in some way before deploying your code. 17 | config.eager_load = ENV["CI"].present? 18 | 19 | # Configure public file server for tests with Cache-Control for performance. 20 | config.public_file_server.enabled = true 21 | config.public_file_server.headers = { 22 | "Cache-Control" => "public, max-age=#{1.hour.to_i}" 23 | } 24 | 25 | # Show full error reports and disable caching. 26 | config.consider_all_requests_local = true 27 | config.action_controller.perform_caching = false 28 | config.cache_store = :null_store 29 | 30 | # Raise exceptions instead of rendering exception templates. 31 | config.action_dispatch.show_exceptions = false 32 | 33 | # Disable request forgery protection in test environment. 34 | config.action_controller.allow_forgery_protection = false 35 | 36 | # Store uploaded files on the local file system in a temporary directory. 37 | config.active_storage.service = :test 38 | 39 | config.action_mailer.perform_caching = false 40 | 41 | # Tell Action Mailer not to deliver emails to the real world. 42 | # The :test delivery method accumulates sent emails in the 43 | # ActionMailer::Base.deliveries array. 44 | config.action_mailer.delivery_method = :test 45 | 46 | # Print deprecation notices to the stderr. 47 | config.active_support.deprecation = :stderr 48 | 49 | # Raise exceptions for disallowed deprecations. 50 | config.active_support.disallowed_deprecation = :raise 51 | 52 | # Tell Active Support which deprecation messages to disallow. 53 | config.active_support.disallowed_deprecation_warnings = [] 54 | 55 | # Raises error for missing translations. 56 | # config.i18n.raise_on_missing_translations = true 57 | 58 | # Annotate rendered view with file names. 59 | # config.action_view.annotate_rendered_view_with_filenames = true 60 | end 61 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # In the development environment your application's code is reloaded any time 7 | # it changes. This slows down response time but is perfect for development 8 | # since you don't have to restart the web server when you make code changes. 9 | config.cache_classes = false 10 | 11 | # Do not eager load code on boot. 12 | config.eager_load = false 13 | 14 | # Show full error reports. 15 | config.consider_all_requests_local = true 16 | 17 | # Enable server timing 18 | config.server_timing = true 19 | 20 | # Enable/disable caching. By default caching is disabled. 21 | # Run rails dev:cache to toggle caching. 22 | if Rails.root.join("tmp/caching-dev.txt").exist? 23 | config.action_controller.perform_caching = true 24 | config.action_controller.enable_fragment_cache_logging = true 25 | 26 | config.cache_store = :memory_store 27 | config.public_file_server.headers = { 28 | "Cache-Control" => "public, max-age=#{2.days.to_i}" 29 | } 30 | else 31 | config.action_controller.perform_caching = false 32 | 33 | config.cache_store = :null_store 34 | end 35 | 36 | # Store uploaded files on the local file system (see config/storage.yml for options). 37 | config.active_storage.service = :local 38 | 39 | # Don't care if the mailer can't send. 40 | config.action_mailer.raise_delivery_errors = false 41 | 42 | config.action_mailer.perform_caching = false 43 | 44 | # Print deprecation notices to the Rails logger. 45 | config.active_support.deprecation = :log 46 | 47 | # Raise exceptions for disallowed deprecations. 48 | config.active_support.disallowed_deprecation = :raise 49 | 50 | # Tell Active Support which deprecation messages to disallow. 51 | config.active_support.disallowed_deprecation_warnings = [] 52 | 53 | # Raise an error on page load if there are pending migrations. 54 | config.active_record.migration_error = :page_load 55 | 56 | # Highlight code that triggered database queries in logs. 57 | config.active_record.verbose_query_logs = true 58 | 59 | # Suppress logger output for asset requests. 60 | config.assets.quiet = true 61 | 62 | # Raises error for missing translations. 63 | # config.i18n.raise_on_missing_translations = true 64 | 65 | # Annotate rendered view with file names. 66 | # config.action_view.annotate_rendered_view_with_filenames = true 67 | 68 | # Uncomment if you wish to allow Action Cable access from any origin. 69 | # config.action_cable.disable_request_forgery_protection = true 70 | end 71 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'bundle' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "rubygems" 12 | 13 | m = Module.new do 14 | module_function 15 | 16 | def invoked_as_script? 17 | File.expand_path($0) == File.expand_path(__FILE__) 18 | end 19 | 20 | def env_var_version 21 | ENV["BUNDLER_VERSION"] 22 | end 23 | 24 | def cli_arg_version 25 | return unless invoked_as_script? # don't want to hijack other binstubs 26 | return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` 27 | bundler_version = nil 28 | update_index = nil 29 | ARGV.each_with_index do |a, i| 30 | if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN 31 | bundler_version = a 32 | end 33 | next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ 34 | bundler_version = $1 35 | update_index = i 36 | end 37 | bundler_version 38 | end 39 | 40 | def gemfile 41 | gemfile = ENV["BUNDLE_GEMFILE"] 42 | return gemfile if gemfile && !gemfile.empty? 43 | 44 | File.expand_path("../Gemfile", __dir__) 45 | end 46 | 47 | def lockfile 48 | lockfile = 49 | case File.basename(gemfile) 50 | when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) 51 | else "#{gemfile}.lock" 52 | end 53 | File.expand_path(lockfile) 54 | end 55 | 56 | def lockfile_version 57 | return unless File.file?(lockfile) 58 | lockfile_contents = File.read(lockfile) 59 | return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ 60 | Regexp.last_match(1) 61 | end 62 | 63 | def bundler_requirement 64 | @bundler_requirement ||= 65 | env_var_version || 66 | cli_arg_version || 67 | bundler_requirement_for(lockfile_version) 68 | end 69 | 70 | def bundler_requirement_for(version) 71 | return "#{Gem::Requirement.default}.a" unless version 72 | 73 | bundler_gem_version = Gem::Version.new(version) 74 | 75 | bundler_gem_version.approximate_recommendation 76 | end 77 | 78 | def load_bundler! 79 | ENV["BUNDLE_GEMFILE"] ||= gemfile 80 | 81 | activate_bundler 82 | end 83 | 84 | def activate_bundler 85 | gem_error = activation_error_handling do 86 | gem "bundler", bundler_requirement 87 | end 88 | return if gem_error.nil? 89 | require_error = activation_error_handling do 90 | require "bundler/version" 91 | end 92 | return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) 93 | warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" 94 | exit 42 95 | end 96 | 97 | def activation_error_handling 98 | yield 99 | nil 100 | rescue StandardError, LoadError => e 101 | e 102 | end 103 | end 104 | 105 | m.load_bundler! 106 | 107 | if m.invoked_as_script? 108 | load Gem.bin_path("bundler", "bundle") 109 | end 110 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # Code is not reloaded between requests. 7 | config.cache_classes = true 8 | 9 | # Eager load code on boot. This eager loads most of Rails and 10 | # your application in memory, allowing both threaded web servers 11 | # and those relying on copy on write to perform better. 12 | # Rake tasks automatically ignore this option for performance. 13 | config.eager_load = true 14 | 15 | # Full error reports are disabled and caching is turned on. 16 | config.consider_all_requests_local = false 17 | config.action_controller.perform_caching = true 18 | 19 | # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] 20 | # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). 21 | # config.require_master_key = true 22 | 23 | # Disable serving static files from the `/public` folder by default since 24 | # Apache or NGINX already handles this. 25 | config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? 26 | 27 | # Compress CSS using a preprocessor. 28 | # config.assets.css_compressor = :sass 29 | 30 | # Do not fallback to assets pipeline if a precompiled asset is missed. 31 | config.assets.compile = false 32 | 33 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 34 | # config.asset_host = "http://assets.example.com" 35 | 36 | # Specifies the header that your server uses for sending files. 37 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache 38 | # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX 39 | 40 | # Store uploaded files on the local file system (see config/storage.yml for options). 41 | config.active_storage.service = :local 42 | 43 | # Mount Action Cable outside main process or domain. 44 | # config.action_cable.mount_path = nil 45 | # config.action_cable.url = "wss://example.com/cable" 46 | # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] 47 | 48 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 49 | # config.force_ssl = true 50 | 51 | # Include generic and useful information about system operation, but avoid logging too much 52 | # information to avoid inadvertent exposure of personally identifiable information (PII). 53 | config.log_level = :info 54 | 55 | # Prepend all log lines with the following tags. 56 | config.log_tags = [ :request_id ] 57 | 58 | # Use a different cache store in production. 59 | # config.cache_store = :mem_cache_store 60 | 61 | # Use a real queuing backend for Active Job (and separate queues per environment). 62 | # config.active_job.queue_adapter = :resque 63 | # config.active_job.queue_name_prefix = "my_app_production" 64 | 65 | config.action_mailer.perform_caching = false 66 | 67 | # Ignore bad email addresses and do not raise email delivery errors. 68 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 69 | # config.action_mailer.raise_delivery_errors = false 70 | 71 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 72 | # the I18n.default_locale when a translation cannot be found). 73 | config.i18n.fallbacks = true 74 | 75 | # Don't log any deprecations. 76 | config.active_support.report_deprecations = false 77 | 78 | # Use default logging formatter so that PID and timestamp are not suppressed. 79 | config.log_formatter = ::Logger::Formatter.new 80 | 81 | # Use a different logger for distributed setups. 82 | # require "syslog/logger" 83 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") 84 | 85 | if ENV["RAILS_LOG_TO_STDOUT"].present? 86 | logger = ActiveSupport::Logger.new(STDOUT) 87 | logger.formatter = config.log_formatter 88 | config.logger = ActiveSupport::TaggedLogging.new(logger) 89 | end 90 | 91 | # Do not dump schema after migrations. 92 | config.active_record.dump_schema_after_migration = false 93 | end 94 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative "boot" 2 | 3 | require "rails/all" 4 | 5 | # Require the gems listed in Gemfile, including any gems 6 | # you've limited to :test, :development, or :production. 7 | Bundler.require(*Rails.groups) 8 | 9 | module MyApp 10 | module ImReloader 11 | def reload 12 | super.tap { Application.reload } 13 | end 14 | end 15 | 16 | # Define a custom routes reloader which loads route file(s) under the 17 | # Application namespace. This allows application constants to be referenced 18 | # in `config/routes.rb` (and any other route files) at toplevel. 19 | class RoutesReloader < ::Rails::Application::RoutesReloader 20 | def load(path) 21 | super(path, Application) 22 | end 23 | end 24 | 25 | class RailsApplication < Rails::Application 26 | # We need to create the loader here and assign it to the Application 27 | # constant so that initializers can be loaded under it. Those initializers 28 | # cannot actually access constants under it until the 29 | # :setup_application_loader initializer has been run, but that's fine since 30 | # you're not able to access Zeitwerk-loaded constants outside of 31 | # `to_prepare` blocks anyway, and those are only run later in boot. 32 | loader = Im::Loader.new 33 | loader.tag = "rails.main" 34 | loader.inflector = Rails::Autoloaders::Inflector 35 | 36 | def loader.use_relative_model_naming? 37 | true 38 | end 39 | 40 | ::MyApp::Application = loader 41 | 42 | initializer :setup_application_loader, before: :setup_main_autoloader do 43 | app_paths = ActiveSupport::Dependencies.autoload_paths.select do |path| 44 | path.start_with?(Rails.root.to_s) 45 | end 46 | ActiveSupport::Dependencies.autoload_paths -= app_paths 47 | 48 | app_paths.each do |path| 49 | next unless File.directory?(path) 50 | 51 | loader.push_dir(path) 52 | loader.do_not_eager_load(path) unless ActiveSupport::Dependencies.eager_load?(path) 53 | end 54 | 55 | unless config.cache_classes 56 | loader.enable_reloading 57 | 58 | # Ensure that every time Zeitwerk is reloaded, Im is too. 59 | Rails.autoloaders.main.extend(ImReloader) 60 | 61 | loader.on_load do |_cpath, value, _abspath| 62 | if value.is_a?(Class) && value.singleton_class < ActiveSupport::DescendantsTracker 63 | ActiveSupport::Dependencies._autoloaded_tracked_classes << value 64 | end 65 | end 66 | end 67 | 68 | loader.setup 69 | 70 | # We need to add these explicitly because we removed them from autoload paths. 71 | loader.autoloads.keys.each do |path| 72 | if path.end_with?(".rb") 73 | config.watchable_files << path 74 | else 75 | config.watchable_dirs[path] = [:rb] 76 | end 77 | end 78 | end 79 | 80 | def routes_reloader 81 | @routes_reloader ||= RoutesReloader.new 82 | end 83 | 84 | # We patch `load` to catch the call to `Kernel#load` so we can inject the 85 | # application to ensure initializers, rake tasks, and anything else loaded 86 | # from the application context are loaded under it. This way, constants can 87 | # be referenced in initializers, rake tasks, etc at toplevel. 88 | def load(initializer) 89 | super(initializer, Application) 90 | end 91 | 92 | # Initialize configuration defaults for originally generated Rails version. 93 | config.load_defaults 7.0 94 | 95 | # Configuration for the application, engines, and railties goes here. 96 | # 97 | # These settings can be overridden in specific environments using the files 98 | # in config/environments, which are processed later. 99 | # 100 | # config.time_zone = "Central Time (US & Canada)" 101 | # config.eager_load_paths << Rails.root.join("extras") 102 | 103 | # We need to disable this to avoid ActionPack trying to load 104 | # ApplicationHelper when it sees app/helpers/application_helper.rb. This 105 | # won't work since our ApplicationHelper is under MyApp::Application. 106 | config.action_controller.include_all_helpers = false 107 | end 108 | end 109 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (7.0.4.2) 5 | actionpack (= 7.0.4.2) 6 | activesupport (= 7.0.4.2) 7 | nio4r (~> 2.0) 8 | websocket-driver (>= 0.6.1) 9 | actionmailbox (7.0.4.2) 10 | actionpack (= 7.0.4.2) 11 | activejob (= 7.0.4.2) 12 | activerecord (= 7.0.4.2) 13 | activestorage (= 7.0.4.2) 14 | activesupport (= 7.0.4.2) 15 | mail (>= 2.7.1) 16 | net-imap 17 | net-pop 18 | net-smtp 19 | actionmailer (7.0.4.2) 20 | actionpack (= 7.0.4.2) 21 | actionview (= 7.0.4.2) 22 | activejob (= 7.0.4.2) 23 | activesupport (= 7.0.4.2) 24 | mail (~> 2.5, >= 2.5.4) 25 | net-imap 26 | net-pop 27 | net-smtp 28 | rails-dom-testing (~> 2.0) 29 | actionpack (7.0.4.2) 30 | actionview (= 7.0.4.2) 31 | activesupport (= 7.0.4.2) 32 | rack (~> 2.0, >= 2.2.0) 33 | rack-test (>= 0.6.3) 34 | rails-dom-testing (~> 2.0) 35 | rails-html-sanitizer (~> 1.0, >= 1.2.0) 36 | actiontext (7.0.4.2) 37 | actionpack (= 7.0.4.2) 38 | activerecord (= 7.0.4.2) 39 | activestorage (= 7.0.4.2) 40 | activesupport (= 7.0.4.2) 41 | globalid (>= 0.6.0) 42 | nokogiri (>= 1.8.5) 43 | actionview (7.0.4.2) 44 | activesupport (= 7.0.4.2) 45 | builder (~> 3.1) 46 | erubi (~> 1.4) 47 | rails-dom-testing (~> 2.0) 48 | rails-html-sanitizer (~> 1.1, >= 1.2.0) 49 | activejob (7.0.4.2) 50 | activesupport (= 7.0.4.2) 51 | globalid (>= 0.3.6) 52 | activemodel (7.0.4.2) 53 | activesupport (= 7.0.4.2) 54 | activerecord (7.0.4.2) 55 | activemodel (= 7.0.4.2) 56 | activesupport (= 7.0.4.2) 57 | activestorage (7.0.4.2) 58 | actionpack (= 7.0.4.2) 59 | activejob (= 7.0.4.2) 60 | activerecord (= 7.0.4.2) 61 | activesupport (= 7.0.4.2) 62 | marcel (~> 1.0) 63 | mini_mime (>= 1.1.0) 64 | activesupport (7.0.4.2) 65 | concurrent-ruby (~> 1.0, >= 1.0.2) 66 | i18n (>= 1.6, < 2) 67 | minitest (>= 5.1) 68 | tzinfo (~> 2.0) 69 | bindex (0.8.1) 70 | bootsnap (1.16.0) 71 | msgpack (~> 1.2) 72 | builder (3.2.4) 73 | concurrent-ruby (1.2.0) 74 | crass (1.0.6) 75 | date (3.3.3) 76 | debug (1.7.1) 77 | irb (>= 1.5.0) 78 | reline (>= 0.3.1) 79 | erubi (1.12.0) 80 | globalid (1.1.0) 81 | activesupport (>= 5.0) 82 | i18n (1.12.0) 83 | concurrent-ruby (~> 1.0) 84 | im (0.2.2) 85 | importmap-rails (1.1.5) 86 | actionpack (>= 6.0.0) 87 | railties (>= 6.0.0) 88 | io-console (0.6.0) 89 | irb (1.6.2) 90 | reline (>= 0.3.0) 91 | jbuilder (2.11.5) 92 | actionview (>= 5.0.0) 93 | activesupport (>= 5.0.0) 94 | loofah (2.19.1) 95 | crass (~> 1.0.2) 96 | nokogiri (>= 1.5.9) 97 | mail (2.8.1) 98 | mini_mime (>= 0.1.1) 99 | net-imap 100 | net-pop 101 | net-smtp 102 | marcel (1.0.2) 103 | method_source (1.0.0) 104 | mini_mime (1.1.2) 105 | minitest (5.17.0) 106 | msgpack (1.6.0) 107 | net-imap (0.3.4) 108 | date 109 | net-protocol 110 | net-pop (0.1.2) 111 | net-protocol 112 | net-protocol (0.2.1) 113 | timeout 114 | net-smtp (0.3.3) 115 | net-protocol 116 | nio4r (2.5.8) 117 | nokogiri (1.14.1-arm64-darwin) 118 | racc (~> 1.4) 119 | puma (5.6.5) 120 | nio4r (~> 2.0) 121 | racc (1.6.2) 122 | rack (2.2.6.2) 123 | rack-test (2.0.2) 124 | rack (>= 1.3) 125 | rails (7.0.4.2) 126 | actioncable (= 7.0.4.2) 127 | actionmailbox (= 7.0.4.2) 128 | actionmailer (= 7.0.4.2) 129 | actionpack (= 7.0.4.2) 130 | actiontext (= 7.0.4.2) 131 | actionview (= 7.0.4.2) 132 | activejob (= 7.0.4.2) 133 | activemodel (= 7.0.4.2) 134 | activerecord (= 7.0.4.2) 135 | activestorage (= 7.0.4.2) 136 | activesupport (= 7.0.4.2) 137 | bundler (>= 1.15.0) 138 | railties (= 7.0.4.2) 139 | rails-dom-testing (2.0.3) 140 | activesupport (>= 4.2.0) 141 | nokogiri (>= 1.6) 142 | rails-html-sanitizer (1.5.0) 143 | loofah (~> 2.19, >= 2.19.1) 144 | railties (7.0.4.2) 145 | actionpack (= 7.0.4.2) 146 | activesupport (= 7.0.4.2) 147 | method_source 148 | rake (>= 12.2) 149 | thor (~> 1.0) 150 | zeitwerk (~> 2.5) 151 | rake (13.0.6) 152 | reline (0.3.2) 153 | io-console (~> 0.5) 154 | sprockets (4.2.0) 155 | concurrent-ruby (~> 1.0) 156 | rack (>= 2.2.4, < 4) 157 | sprockets-rails (3.4.2) 158 | actionpack (>= 5.2) 159 | activesupport (>= 5.2) 160 | sprockets (>= 3.0.0) 161 | sqlite3 (1.6.0-arm64-darwin) 162 | stimulus-rails (1.2.1) 163 | railties (>= 6.0.0) 164 | thor (1.2.1) 165 | timeout (0.3.1) 166 | turbo-rails (1.3.3) 167 | actionpack (>= 6.0.0) 168 | activejob (>= 6.0.0) 169 | railties (>= 6.0.0) 170 | tzinfo (2.0.6) 171 | concurrent-ruby (~> 1.0) 172 | web-console (4.2.0) 173 | actionview (>= 6.0.0) 174 | activemodel (>= 6.0.0) 175 | bindex (>= 0.4.0) 176 | railties (>= 6.0.0) 177 | websocket-driver (0.7.5) 178 | websocket-extensions (>= 0.1.0) 179 | websocket-extensions (0.1.5) 180 | zeitwerk (2.6.6) 181 | 182 | PLATFORMS 183 | arm64-darwin-21 184 | 185 | DEPENDENCIES 186 | bootsnap 187 | debug 188 | im (~> 0.2.2) 189 | importmap-rails 190 | jbuilder 191 | puma (~> 5.0) 192 | rails (~> 7.0.4, >= 7.0.4.2) 193 | sprockets-rails 194 | sqlite3 (~> 1.4) 195 | stimulus-rails 196 | turbo-rails 197 | tzinfo-data 198 | web-console 199 | 200 | RUBY VERSION 201 | ruby 3.2.0p0 202 | 203 | BUNDLED WITH 204 | 2.4.6 205 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rails on Im 2 | 3 | This is a Rails 7.0 app demonstrating how [Im](https://github.com/shioyama/im) 4 | can be used to "hoist" classes and modules of a Rails application under a 5 | single root namespace, while writing the actual code at toplevel. Among other 6 | benefits, this means you can write your application code exactly as you always 7 | do, but avoid any conflicts with gems or other imported code. 8 | 9 | ## Usage 10 | 11 | This _should_ be just like any Rails app from the outside. Boot it with 12 | `bin/rails server`, go to the `/posts` index page and click around. The point 13 | is not to have a beatiful application, simply to reproduce what `bin/rails 14 | generate scaffold` would normally give you, but with a very different 15 | implementation. 16 | 17 | To see what is going on, have a look first at 18 | [`app/models/post.rb`](https://github.com/shioyama/rails_on_im/blob/main/app/models/post.rb) 19 | and 20 | [`app/models/comment.rb`](https://github.com/shioyama/rails_on_im/blob/main/app/models/comment.rb). 21 | You will see that these files define top-level `Post` 22 | and `Comment` classes, as well as associations between them. In a normal Rails 23 | app, this would mean that you could access `Post` and `Comment` from anywhere, 24 | but this application is implemented differently. 25 | 26 | To see this, start `irb` and try accessing the `Post` model: 27 | 28 | ``` 29 | irb(main):001:0> Post 30 | (irb):1:in `
': uninitialized constant Post (NameError) 31 | 32 | Post 33 | ^^^^ 34 | ``` 35 | 36 | So it is not defined at toplevel, yet the application works. That is because 37 | the application is using Im to "hoist" the autoloaded application constants 38 | under `MyApp::Application`, where you can find them: 39 | 40 | ``` 41 | irb(main):002:0> MyApp::Application::Post 42 | => MyApp::Application::Post (call 'MyApp::Application::Post.connection' to establish a connection) 43 | ``` 44 | 45 | This means, among other things, that you can define constants that would 46 | normally be reserved. The demo app has one such constant: `Kernel`, defined in 47 | `app/models/kernel.rb` at toplevel but loaded at `MyApp::Application::Kernel`: 48 | 49 | ```ruby 50 | Kernel 51 | #=> Kernel 52 | MyApp::Application::Kernel 53 | #=> MyApp::Application::Kernel (call 'MyApp::Application::Kernel.connection' to establish a connection) 54 | ``` 55 | 56 | Note that because the toplevel of the application is its own constant, you can 57 | easily see all application toplevel constants simply by calling `constants` on `MyApp::Application`, like this: 58 | 59 | ```ruby 60 | MyApp::Application.constants 61 | #=> 62 | [:Post, 63 | :RestrictedAccess, 64 | :Kernel, 65 | :Tag, 66 | :Comment, 67 | :ApplicationCable, 68 | :ApplicationController, 69 | :CommentsController, 70 | :PingController, 71 | :PostsController, 72 | :ApplicationHelper, 73 | :CommentsHelper, 74 | :PostsHelper, 75 | :ApplicationJob, 76 | :ApplicationMailer, 77 | :ApplicationRecord] 78 | ``` 79 | 80 | `MyApp::Application` itself is an instance of `Im::Loader`, Im's loader for the 81 | application (see details below). The usual `Application` class, which points to 82 | `Rails.application`, is instead named `MyApp::RailsApplication`. 83 | 84 | ## Details 85 | 86 | Most of the changes required to support the hoisting of constants under 87 | `MyApp::Application` can be found in 88 | [`config/application.rb`](https://github.com/shioyama/rails_on_im/blob/main/config/application.rb). 89 | Other changes are confined to `Application` classes `ApplicationRecord` and 90 | `ApplicationController`, while subclasses are written exactly as they would be 91 | in a normal Rails app. 92 | 93 | ### Autoload paths 94 | 95 | The main issue in using Im to autoload paths is that, while Rails allows for 96 | multiple autoloaders, it uses only one (`Rails.autoloaders.main`) to load both 97 | parts of Rails itself _and_ the application. This is problematic because we 98 | want to let Rails use the normal 99 | [Zeitwerk](https://github.com/shioyama/rails_on_im/blob/main/config/application.rb) 100 | autoloader for Rails code, and only switch the application autoloader to use Im 101 | instead. 102 | 103 | To get around this limitation, the demo app extracts application paths from 104 | `ActiveSupport::Dependencies.autoload_paths`, removes them, and assigns them to 105 | an Im loader with `loader.push_dir`. The code for this is nearly identical to 106 | what Rails uses internally to assign autoload paths to the Zeitwerk loader. 107 | 108 | ### Reloading the Application 109 | 110 | Removing application paths from Rails' default `autoload_paths` has some 111 | secondary effects that need to be handled. The main one is that application 112 | reloading depends on `autoload_paths`, and since we remove the application from 113 | these paths, Rails' default autoloading strategy will not work on Im-managed 114 | paths. 115 | 116 | This is handled first by adding a hook to the instance of `Zeitwerk::Loader` in 117 | `Rails.autoloaders.main`. We simply patch the method so that every time Rails 118 | calls `reload` on it, the application Im loader also gets triggered, ensuring 119 | Im-loaded code is also reloaded. 120 | 121 | Rails also uses `autoload_paths` to determine which files and directories to 122 | watch, thus by default changes to files in a running application in development 123 | mode will not get reloaded. Luckily, Rails has a configuration option to add 124 | `watchable_files` and `watchable_dirs` manually, which we use to iterate 125 | through all the autoloads handled by the Im loader and add them so they are 126 | also watched. 127 | 128 | Finally, Rails adds an `on_load` hook to the Zeitwerk loader to add tracked 129 | classes and their descendants to a registry 130 | (`ActiveSupport::Dependencies._autoloaded_tracked_classes`) when they are 131 | loaded. We simply add the same hook to the Im loader to ensure this works as 132 | expected. 133 | 134 | ### Reloading Routes 135 | 136 | Reloading routes in Rails is done by an instance of 137 | `Rails::Application::RoutesReloader` returned by the `routes_reloader` method 138 | on the application. Routes reloading is independent of Zeitwerk and happens by 139 | calling `Kernel#load` on the routes path, which by default is `config/routes.rb`. 140 | 141 | The problem with this is that any application constant referenced at toplevel 142 | in the routes file will not resolve correctly, since `load` loads the file at 143 | toplevel. We handle this by subclassing `Rails::Application::RoutesReloader` in 144 | order to override `load` to pass the application loader (`MyApp::Application`) 145 | as the second argument, ensuring that constants referenced at toplevel in 146 | `config/routes.rb` will resolve to the application namespace. 147 | 148 | A demonstration of this can be found in 149 | [`config/routes.rb`](https://github.com/shioyama/rails_on_im/blob/main/config/routes.rb) 150 | where the autoloaded constant `RestrictedAccess` is resolved at toplevel to 151 | `app/models/restricted_access.rb`, although the actual constant is hoisted 152 | under `MyApp::Application` (like all other autoloaded constants.) 153 | 154 | ### Initializers and Rake Tasks 155 | 156 | Initializers by default are loaded with `Kernel#load`, like routes. To ensure 157 | that `to_prepare` blocks in these intializers can also access application 158 | constants at toplevel, in our application subclass we simply patch 159 | `load` so that the original call is caught and modified to pass the application 160 | loader as second argument to `Kernel#load`. As a side benefit, this also allows 161 | application constants to be referenced in rake tasks (since rake tasks are 162 | loaded the same way.) 163 | 164 | A demonstration of this working is shown in 165 | [`config/initializers/allowed_ips.rb`](https://github.com/shioyama/rails_on_im/blob/main/config/initializers/allowed_ips.rb), 166 | where we reference `RestrictedAccess` in a `to_prepare` block. This is 167 | correctly resolved to `MyApp::Application::RestrictedAccess`. An example of a 168 | rake task, `:reset_tags` is also included to show that an application constant, 169 | `Tag`, is correctly resolved there as well. 170 | 171 | ### File path conventions 172 | 173 | Another issue that we face with this novel configuration is that certain 174 | conventions around file and route naming break down due to the file structure. 175 | 176 | We have this: 177 | 178 | ``` 179 | app/models/application_record.rb #=> MyApp::Application::ApplicationRecord 180 | app/models/post.rb #=> MyApp::Application::Post 181 | app/models/comment.rb #=> MyApp::Application::Comment 182 | 183 | app/controllers/application_controller.rb #=> MyApp::Application::ApplicationController 184 | app/controllers/posts_controller.rb #=> MyApp::Application::PostsController 185 | app/controllers/comments_controller.rb #=> MyApp::Application::CommentsController 186 | ... 187 | ``` 188 | 189 | While we have our classes namespaced, we still want the application to use 190 | toplevel routes (`/posts`, `/comments`, etc.), map our models to those toplevel 191 | routes, and so on. 192 | 193 | Just a few changes are needed to make this unconventional setup work. 194 | 195 | First, we define a method `use_relative_model_naming?` on `MyApp::Application` 196 | to tell Rails to consider model names as being under a namespace 197 | `my_app/application` when generating url helpers for models. So this ensures 198 | that url helpers, when passed instances of an `MyApp::Application::Post`, will 199 | correctly deduce that its param key is `post` and not `my_app_application_post`. 200 | 201 | To make this work, we also need to wrap our application routes so that they too 202 | map, for example, `/posts` to the correct controller and models. This is easy 203 | to do simply by wrapping all routes in a scope, like this: 204 | 205 | ```ruby 206 | # config/routes.rb 207 | scope module: "my_app/application" do 208 | resources :posts do 209 | ``` 210 | 211 | Rails will also try to load `ApplicationHelper` when it sees 212 | `app/helpers/application_helper.rb`, even though that constant is not defined 213 | at (absolute) toplevel. To avoid this, we disable 214 | `config.action_controller.include_all_helpers`. This is not an ideal fix and 215 | should probaly be revisited, but it works for demonstration purposes. 216 | 217 | Finally, two small changes are needed in `ApplicationController` and 218 | `ApplicationRecord` to put the final pieces in place. In 219 | `ApplicationController`, we override the default `controller_path` to remove 220 | the autogenerated `"my_app/application"` prefix and ensure it returns `posts` 221 | rather than `my_app/application/posts`. Likewise, in `ApplicationRecord`, we 222 | override `to_partial_path` in the same way. 223 | 224 | With these changes, we have a working Rails application in which our code is 225 | written at toplevel, but all constants are defined under a single toplevel 226 | namespace. 227 | 228 | ## Todo 229 | 230 | Unlike initializers and routes files, view files are not loaded but compiled. 231 | This makes it trickier to hoist them under the application namespace. 232 | 233 | I haven't figured this one out yet, so if you need to reference application 234 | constants from views, you currently need to fully qualify them, like this: 235 | 236 | ```erb 237 | <%= form_with(model: [post, MyApp::Application::Comment.new]) do |form| %> 238 | ``` 239 | 240 | I hope to fix this in a later version of this app. 241 | 242 | ## Comments & Feedback 243 | 244 | This is very much an experimental work in progress. If you try it out and find 245 | issues either in the application or in Im itself, feel free to post issues or 246 | pull requests. 247 | --------------------------------------------------------------------------------