Skip to content

Commit

Permalink
Secret key base (#13)
Browse files Browse the repository at this point in the history
* Make secret key base mandatory
* remove path option to local gem in examples
* Fix todos hotwire e2e tests
* Refactor singleton methods
  • Loading branch information
AlexB52 authored Apr 19, 2024
1 parent 39af591 commit 7886b32
Show file tree
Hide file tree
Showing 16 changed files with 44 additions and 37 deletions.
4 changes: 3 additions & 1 deletion examples/server-falcon-app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Run the application
# $ ruby server-falcon-app.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'

require "bundler/inline"

gemfile(true) do
source "https://rubygems.org"

gem 'uni_rails', path: '/Users/alex/projects/uni_rails'
gem 'uni_rails'
gem 'falcon'
end

Expand Down
4 changes: 3 additions & 1 deletion examples/server-puma-app.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# Run the application
# $ ruby server-puma-app.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'

require "bundler/inline"

gemfile(true) do
source "https://rubygems.org"

gem 'uni_rails', path: '/Users/alex/projects/uni_rails'
gem 'uni_rails'
gem 'puma'
end

Expand Down
3 changes: 2 additions & 1 deletion examples/todos-api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Run the application
# $ ruby todos-api.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'
ENV['DATABASE_URL'] = "sqlite3:///#{Dir.pwd}/todos-api.sqlite"

require "bundler/inline"
Expand All @@ -9,7 +10,7 @@
source "https://rubygems.org"

gem 'uni_rails'
gem 'sqlite3'
gem 'sqlite3', '~> 1.7'
end

require 'uni_rails'
Expand Down
2 changes: 2 additions & 0 deletions examples/todos-hotwire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Run the application
# $ ruby todos-hotwire.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'
ENV['DATABASE_URL'] = "sqlite3:///#{Dir.pwd}/todos-hotwire.sqlite"

require "bundler/inline"
Expand All @@ -11,6 +12,7 @@

gem 'uni_rails'
gem 'sqlite3', '~> 1.7'
gem 'debug'
end

require 'uni_rails'
Expand Down
3 changes: 2 additions & 1 deletion examples/todos-scaffold.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Run the application
# $ ruby todos-scaffold.rb

ENV['SECRET_KEY_BASE'] = 'my_secret_key_base'
ENV['DATABASE_URL'] = "sqlite3:///#{Dir.pwd}/todos-scaffold.sqlite"

require "bundler/inline"
Expand All @@ -9,7 +10,7 @@
source "https://rubygems.org"

gem 'uni_rails'
gem 'sqlite3'
gem 'sqlite3', '~> 1.7'
gem 'jbuilder' # jbuilder allows the .jbuilder extension for views
end

Expand Down
1 change: 1 addition & 0 deletions features/server-puma/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ services:
- .:/usr/src/app
environment:
- APP_HOST=uni_rails:3000
- SECRET_KEY_BASE=whatever
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"]
interval: 3s
Expand Down
1 change: 1 addition & 0 deletions features/todos-api/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
environment:
- APP_HOST=uni_rails:3000
- DATABASE_URL=sqlite3:////usr/src/app/database.sqlite
- SECRET_KEY_BASE=whatever
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"]
interval: 3s
Expand Down
4 changes: 3 additions & 1 deletion features/todos-hotwire/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,18 @@ def todo_params
Your todos
</h2>
<%= turbo_frame_tag "todos-container", class: "block max-w-2xl w-full bg-gray-100 py-8 px-4 border border-gray-200 rounded shadow-sm" do %>
<div class="border-b border-gray-200 w-full">
<div id="nav-bar" class="border-b border-gray-200 w-full">
<ul class="flex space-x-2 justify-center">
<li>
<%= link_to "Incomplete",
todos_path(status: "incomplete"),
id: "nav-bar-action-incomplete",
class: "inline-block py-4 px-4 text-sm font-medium text-center text-gray-500 border-b-2 border-transparent hover:text-gray-600 hover:border-gray-300"
%>
<li>
<%= link_to "Complete",
todos_path(status: "complete"),
id: "nav-bar-action-complete",
class: "inline-block py-4 px-4 text-sm font-medium text-center text-gray-500 border-b-2 border-transparent hover:text-gray-600 hover:border-gray-300"
%>
</li>
Expand Down
4 changes: 2 additions & 2 deletions features/todos-hotwire/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def test_create_a_new_todo
private

def assert_complete(n)
click_on 'Complete'
find('#nav-bar-action-complete').click
assert_equal n, all('ul#todos li').length
end

def assert_incomplete(n)
click_on 'Incomplete'
find('#nav-bar-action-incomplete').click
assert_equal n, all('ul#todos li').length
end
end
Expand Down
1 change: 1 addition & 0 deletions features/todos-hotwire/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
environment:
- APP_HOST=uni_rails:3000
- DATABASE_URL=sqlite3:////usr/src/app/database.sqlite
- SECRET_KEY_BASE=whatever
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"]
interval: 3s
Expand Down
1 change: 1 addition & 0 deletions features/todos-scaffold/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
environment:
- APP_HOST=uni_rails:3000
- DATABASE_URL=sqlite3:////usr/src/app/database.sqlite
- SECRET_KEY_BASE=whatever
healthcheck:
test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"]
interval: 3s
Expand Down
14 changes: 11 additions & 3 deletions lib/uni_rails.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# frozen_string_literal: true

if ENV['SECRET_KEY_BASE'].nil?
raise StandardError, <<~ERROR
SECRET_KEY_BASE environment variable is required
Provide ENV['SECRET_KEY_BASE'] in your file or export the variable to your profile
ERROR
end

require "rails"
require_relative "uni_rails/version"
require_relative "uni_rails/helpers"
Expand Down Expand Up @@ -29,9 +37,9 @@ def self.enable_turbo_rails!
end
end

UniRails::App::Javascript.dependencies = {
App::Javascript.dependencies.merge!(
"turbo" => "https://unpkg.com/@hotwired/[email protected]/dist/turbo.es2017-umd.js"
}
)
end

def self.rackup_handler=(handler)
Expand All @@ -58,7 +66,7 @@ def self.run(**webrick_options)
end

def self.import_maps(dependencies)
UniRails::App::Javascript.dependencies = dependencies
UniRails::App::Javascript.dependencies.merge! dependencies
end

def self.javascript(content)
Expand Down
2 changes: 1 addition & 1 deletion lib/uni_rails/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class App < Rails::Application
config.eager_load = true
config.logger = Logger.new(STDOUT)
config.log_level = :debug
config.secret_key_base = "whatever"
config.secret_key_base = ENV.fetch('SECRET_KEY_BASE')

config.after_initialize do
ActionController::Base.view_paths = Views.view_paths
Expand Down
8 changes: 2 additions & 6 deletions lib/uni_rails/app/css.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@ class App
class CSS
include Singleton

def self.css=(content)
instance.css = content
end

def self.css
instance.css
class << self
delegate :css, :css=, to: :instance
end

attr_accessor :css
Expand Down
26 changes: 6 additions & 20 deletions lib/uni_rails/app/javascript.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@ class App
class Javascript
include Singleton

def self.imports
instance.imports
class << self
delegate :imports,
:dependencies, :dependencies=,
:javascript, :javascript=,
to: :instance
end

def self.default_dependencies=(dependencies)
instance.default_dependencies = dependencies
end

def self.dependencies=(dependencies)
instance.dependencies = instance.default_dependencies.merge(dependencies)
end

def self.javascript
instance.javascript
end

def self.javascript=(content)
instance.javascript = content
end

attr_accessor :dependencies, :default_dependencies, :javascript
attr_accessor :dependencies, :javascript
def initialize
@default_dependencies = {}
@dependencies = {}
@javascript = ""
end
Expand Down
3 changes: 3 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# frozen_string_literal: true

ENV['SECRET_KEY_BASE'] = 'something'

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "uni_rails"

require "minitest/autorun"
require "debug"

0 comments on commit 7886b32

Please sign in to comment.