Skip to content

Commit aa8f281

Browse files
authored
Rails: switch over to minimal Rails (#876)
I've been running this on my own instance for a couple of weeks and it seems to be stable. I ran into some issues with CSRF tokens which I've fixed, but aside from that it seems like it works!
1 parent 3f21c47 commit aa8f281

33 files changed

+469
-228
lines changed

.rubocop_todo.yml

+1
Original file line numberDiff line numberDiff line change
@@ -398,5 +398,6 @@ Style/StaticClass:
398398
# Offense count: 3
399399
Style/TopLevelMethodDefinition:
400400
Exclude:
401+
- 'bin/setup'
401402
- 'spec/integration/feed_importing_spec.rb'
402403
- 'spec/support/active_record.rb'

Gemfile

-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ gem "pg"
1919
gem "puma", "~> 6.0"
2020
gem "rack-ssl"
2121
gem "sass"
22-
gem "sinatra"
23-
gem "sinatra-activerecord"
24-
gem "sinatra-contrib"
25-
gem "sinatra-flash"
2622
gem "sprockets"
27-
gem "sprockets-helpers"
2823
gem "sprockets-rails"
2924
gem "thread"
3025
gem "uglifier"

Gemfile.lock

-30
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ GEM
142142
mini_portile2 (2.8.1)
143143
minitest (5.17.0)
144144
msgpack (1.6.0)
145-
multi_json (1.15.0)
146145
multi_xml (0.6.0)
147-
mustermann (3.0.0)
148-
ruby2_keywords (~> 0.0.1)
149146
net-imap (0.3.4)
150147
date
151148
net-protocol
@@ -174,8 +171,6 @@ GEM
174171
nio4r (~> 2.0)
175172
racc (1.6.2)
176173
rack (2.2.6.2)
177-
rack-protection (3.0.5)
178-
rack
179174
rack-ssl (1.4.1)
180175
rack
181176
rack-test (2.0.2)
@@ -263,7 +258,6 @@ GEM
263258
rubocop (~> 1.33)
264259
rubocop-capybara (~> 2.17)
265260
ruby-progressbar (1.11.0)
266-
ruby2_keywords (0.0.5)
267261
rubyzip (2.3.2)
268262
sass (3.7.4)
269263
sass-listen (~> 4.0.0)
@@ -281,27 +275,9 @@ GEM
281275
simplecov_json_formatter (~> 0.1)
282276
simplecov-html (0.12.3)
283277
simplecov_json_formatter (0.1.4)
284-
sinatra (3.0.5)
285-
mustermann (~> 3.0)
286-
rack (~> 2.2, >= 2.2.4)
287-
rack-protection (= 3.0.5)
288-
tilt (~> 2.0)
289-
sinatra-activerecord (2.0.26)
290-
activerecord (>= 4.1)
291-
sinatra (>= 1.0)
292-
sinatra-contrib (3.0.5)
293-
multi_json
294-
mustermann (~> 3.0)
295-
rack-protection (= 3.0.5)
296-
sinatra (= 3.0.5)
297-
tilt (~> 2.0)
298-
sinatra-flash (0.3.0)
299-
sinatra (>= 1.0.0)
300278
sprockets (4.2.0)
301279
concurrent-ruby (~> 1.0)
302280
rack (>= 2.2.4, < 4)
303-
sprockets-helpers (1.4.0)
304-
sprockets (>= 2.2)
305281
sprockets-rails (3.4.2)
306282
actionpack (>= 5.2)
307283
activesupport (>= 5.2)
@@ -311,7 +287,6 @@ GEM
311287
tins (~> 1.0)
312288
thor (1.2.1)
313289
thread (0.2.2)
314-
tilt (2.0.11)
315290
timecop (0.9.6)
316291
timeout (0.3.1)
317292
tins (1.32.1)
@@ -374,12 +349,7 @@ DEPENDENCIES
374349
sass
375350
selenium-webdriver
376351
simplecov
377-
sinatra
378-
sinatra-activerecord
379-
sinatra-contrib
380-
sinatra-flash
381352
sprockets
382-
sprockets-helpers
383353
sprockets-rails
384354
thread
385355
timecop

Rakefile

+3-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
# frozen_string_literal: true
22

3-
require "bundler"
4-
Bundler.setup
3+
require_relative "config/application"
4+
5+
Rails.application.load_tasks
56

6-
require "rubygems"
7-
require "net/http"
8-
require "active_record"
97
require "active_support/core_ext/kernel/reporting"
108
require "delayed_job"
119
require "delayed_job_active_record"
1210

13-
require "sinatra/activerecord/rake"
14-
ActiveRecord::Tasks::DatabaseTasks.db_dir = "db"
15-
16-
require "./app"
17-
18-
desc "Load app files"
19-
task :environment do
20-
require_relative "./app/jobs/fetch_feed_job"
21-
require_relative "./app/tasks/fetch_feeds"
22-
require_relative "./app/tasks/change_password"
23-
require_relative "./app/tasks/remove_old_stories"
24-
end
25-
26-
desc "Open an irb session preloaded with the app"
27-
task :console do
28-
sh "irb -r ./app.rb"
29-
end
30-
3111
desc "Fetch all feeds."
3212
task fetch_feeds: :environment do
3313
FetchFeeds.new(Feed.all).fetch_all

app/assets/config/manifest.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//= link_tree ../images
2+
//= link_directory ../stylesheets .css
3+
//= link_directory ../javascripts .js

app/assets/javascripts/application.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ _.templateSettings = {
1111
evaluate: /\{\{(.+?)\}\}/g
1212
};
1313

14+
function CSRFToken() {
15+
const tokenTag = document.getElementsByName('csrf-token')[0];
16+
17+
return (tokenTag && tokenTag.content) || '';
18+
}
19+
20+
function requestHeaders() {
21+
return { 'X-CSRF-Token': CSRFToken() };
22+
}
23+
1424
var Story = Backbone.Model.extend({
1525
defaults: function() {
1626
return {
@@ -33,7 +43,7 @@ var Story = Backbone.Model.extend({
3343

3444
open: function() {
3545
if (!this.get("keep_unread")) this.set("is_read", true);
36-
if (this.shouldSave()) this.save();
46+
if (this.shouldSave()) this.save(null, { headers: requestHeaders() });
3747

3848
if(this.collection){
3949
this.collection.closeOthers(this);
@@ -54,7 +64,7 @@ var Story = Backbone.Model.extend({
5464
this.set("is_read", false);
5565
}
5666

57-
if (this.shouldSave()) this.save();
67+
if (this.shouldSave()) this.save(null, { headers: requestHeaders() });
5868
},
5969

6070
toggleStarred: function() {
@@ -64,7 +74,7 @@ var Story = Backbone.Model.extend({
6474
this.set("is_starred", true);
6575
}
6676

67-
if (this.shouldSave()) this.save();
77+
if (this.shouldSave()) this.save(null, { headers: requestHeaders() });
6878
},
6979

7080
close: function() {
@@ -156,7 +166,7 @@ var StoryView = Backbone.View.extend({
156166
if (backgroundTab) backgroundTab.blur();
157167
window.focus();
158168
if (!this.model.get("keep_unread")) this.model.set("is_read", true);
159-
if (this.model.shouldSave()) this.model.save();
169+
if (this.model.shouldSave()) this.model.save(null, { headers: requestHeaders() });
160170
} else {
161171
this.model.toggle();
162172
window.scrollTo(0, this.$el.offset().top);

app/commands/fever_api/response.rb

-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
# frozen_string_literal: true
22

3-
require_relative "authentication"
4-
5-
require_relative "read_groups"
6-
require_relative "read_feeds"
7-
require_relative "read_feeds_groups"
8-
require_relative "read_favicons"
9-
require_relative "read_items"
10-
require_relative "read_links"
11-
12-
require_relative "sync_unread_item_ids"
13-
require_relative "sync_saved_item_ids"
14-
15-
require_relative "write_mark_item"
16-
require_relative "write_mark_feed"
17-
require_relative "write_mark_group"
18-
193
module FeverAPI
204
API_VERSION = 3
215

app/controllers/application_controller.rb

-16
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
11
# frozen_string_literal: true
22

33
class ApplicationController < ActionController::Base
4-
before_action :append_view_path
54
before_action :complete_setup
65
before_action :authenticate_user
7-
after_action :rotate_flash
86

97
private
108

11-
# needed for Sinatra
12-
def append_view_path
13-
super("./app/views")
14-
end
15-
169
def complete_setup
1710
redirect_to("/setup/password") unless UserRepository.setup_complete?
1811
end
1912

20-
def flash
21-
@flash ||= Sinatra::Flash::FlashHash.new(session[:flash])
22-
end
23-
helper_method :flash
24-
25-
def rotate_flash
26-
session[:flash] = flash.next # for Sinatra
27-
end
28-
2913
def authenticate_user
3014
return if current_user
3115

app/controllers/fever_controller.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# frozen_string_literal: true
22

3+
require_relative "../commands/fever_api/response"
4+
35
class FeverController < ApplicationController
46
skip_before_action :complete_setup, only: [:index, :update]
57
skip_before_action :authenticate_user, only: [:index, :update]
8+
protect_from_forgery with: :null_session, only: [:update]
69
before_action :authenticate_fever
710

811
def index

app/views/layouts/application.html.erb

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<%= content_for(:title) %>
66
<%= t('layout.title') %>
77
</title>
8+
<%= csrf_meta_tags %>
89
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
910
<meta name="viewport" content="width=device-width, initial-scale=1" />
1011
<link rel="shortcut icon" href="/img/favicon.png">

bin/rails

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
APP_PATH = File.expand_path("../config/application", __dir__)
5+
require_relative "../config/boot"
6+
require "rails/commands"

bin/rake

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require_relative "../config/boot"
5+
require "rake"
6+
Rake.application.run

bin/setup

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "fileutils"
5+
6+
# path to your application root.
7+
APP_ROOT = File.expand_path("..", __dir__)
8+
9+
def system!(*args)
10+
system(*args) || abort("\n== Command #{args} failed ==")
11+
end
12+
13+
FileUtils.chdir(APP_ROOT) do
14+
# This script is a way to set up or update your development environment
15+
# automatically. This script is idempotent, so that you can run it at any
16+
# time and get an expectable outcome. Add necessary setup steps to this file.
17+
18+
puts "== Installing dependencies =="
19+
system! "gem install bundler --conservative"
20+
system("bundle check") || system!("bundle install")
21+
22+
# puts "\n== Copying sample files =="
23+
# unless File.exist?("config/database.yml")
24+
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
25+
# end
26+
27+
puts "\n== Preparing database =="
28+
system! "bin/rails db:prepare"
29+
30+
puts "\n== Removing old logs and tempfiles =="
31+
system! "bin/rails log:clear tmp:clear"
32+
33+
puts "\n== Restarting application server =="
34+
system! "bin/rails restart"
35+
end

config.ru

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# frozen_string_literal: true
22

3-
require "rubygems"
4-
require "bundler"
3+
# This file is used by Rack-based servers to start the application.
54

65
require "active_support/core_ext/kernel/reporting"
7-
Bundler.require
6+
require_relative "config/environment"
87

9-
require "./app"
10-
run Stringer
8+
run Rails.application
9+
Rails.application.load_server

config/application.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "boot"
4+
5+
require "rails"
6+
# Pick the frameworks you want:
7+
require "active_model/railtie"
8+
# require "active_job/railtie"
9+
require "active_record/railtie"
10+
# require "active_storage/engine"
11+
require "action_controller/railtie"
12+
# require "action_mailer/railtie"
13+
# require "action_mailbox/engine"
14+
# require "action_text/engine"
15+
require "action_view/railtie"
16+
# require "action_cable/engine"
17+
require "rails/test_unit/railtie"
18+
19+
# Require the gems listed in Gemfile, including any gems
20+
# you've limited to :test, :development, or :production.
21+
Bundler.require(*Rails.groups)
22+
23+
module Stringer
24+
class Application < Rails::Application
25+
# Initialize configuration defaults for originally generated Rails version.
26+
config.load_defaults(7.0)
27+
28+
# Configuration for the application, engines, and railties goes here.
29+
#
30+
# These settings can be overridden in specific environments using the files
31+
# in config/environments, which are processed later.
32+
#
33+
# config.time_zone = "Central Time (US & Canada)"
34+
config.eager_load_paths << Rails.root.join("app/commands/feeds")
35+
config.eager_load_paths << Rails.root.join("app/commands/stories")
36+
config.eager_load_paths << Rails.root.join("app/commands/users")
37+
38+
# Don't generate system test files.
39+
config.generators.system_tests = nil
40+
41+
config.active_record.belongs_to_required_by_default = false
42+
end
43+
end

config/boot.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# frozen_string_literal: true
2+
3+
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
4+
5+
require "bundler/setup" # Set up gems listed in the Gemfile.

config/environment.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
# Load the Rails application.
4+
require_relative "application"
5+
6+
# Initialize the Rails application.
7+
Rails.application.initialize!

0 commit comments

Comments
 (0)