Skip to content

Commit f43dce3

Browse files
author
Matt Swanson
committed
finish up first run. add backgrounded feed fetching after import.
1 parent 6ffb08c commit f43dce3

File tree

19 files changed

+197
-70
lines changed

19 files changed

+197
-70
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ group(:development, :testing) do
3131
gem "shotgun"
3232
gem "racksh"
3333
gem "faker"
34+
gem "foreman"
3435
end

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ GEM
5353
activerecord (>= 2.1.0, < 4)
5454
delayed_job (~> 3.0)
5555
diff-lcs (1.2.4)
56+
dotenv (0.7.0)
5657
eventmachine (1.0.3)
5758
faker (1.1.2)
5859
i18n (~> 0.5)
60+
foreman (0.63.0)
61+
dotenv (>= 0.7)
62+
thor (>= 0.13.6)
5963
i18n (0.6.4)
6064
kgio (2.8.0)
6165
loofah (1.2.1)
@@ -104,6 +108,7 @@ GEM
104108
sinatra (>= 1.0.0)
105109
slop (3.4.4)
106110
sqlite3 (1.3.7)
111+
thor (0.18.1)
107112
tilt (1.3.7)
108113
tzinfo (0.3.37)
109114
unicorn (4.6.2)
@@ -120,6 +125,7 @@ DEPENDENCIES
120125
faker
121126
feedbag!
122127
feedzirra!
128+
foreman
123129
loofah
124130
nokogiri
125131
pg

app.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "json"
66

77
require_relative "app/helpers/authentication_helpers"
8+
require_relative "app/repositories/user_repository"
89

910
class Stringer < Sinatra::Base
1011
configure do
@@ -33,6 +34,14 @@ def render_partial(name, locals = {})
3334
redirect '/login'
3435
end
3536
end
37+
38+
get "/" do
39+
if UserRepository.setup_complete?
40+
redirect to("/news")
41+
else
42+
redirect to("/setup/password")
43+
end
44+
end
3645
end
3746

3847
require_relative "app/controllers/stories_controller"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CompleteSetup
2+
def self.complete(user)
3+
user.setup_complete = true
4+
user.save
5+
user
6+
end
7+
end

app/commands/users/create_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ def initialize(repository = User)
44
end
55

66
def create(password)
7-
@repo.create(password: password, password_confirmation: password)
7+
@repo.create(password: password, password_confirmation: password, setup_complete: false)
88
end
99
end

app/controllers/first_run_controller.rb

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,54 @@
11
require_relative "../commands/feeds/import_from_opml"
22
require_relative "../commands/users/create_user"
3+
require_relative "../commands/users/complete_setup"
34
require_relative "../repositories/user_repository"
45
require_relative "../repositories/story_repository"
6+
require_relative "../tasks/fetch_feeds"
57

68
class Stringer < Sinatra::Base
7-
#before /\/(password|import)/ do
8-
# if first_run_completed?
9-
# redirect to("/news")
10-
# end
11-
#end
12-
13-
get "/" do
14-
if first_run_completed?
15-
redirect to("/news")
16-
else
17-
redirect to("/password")
9+
namespace "/setup" do
10+
before do
11+
if UserRepository.setup_complete?
12+
redirect to("/news")
13+
end
1814
end
19-
end
20-
21-
get "/password" do
22-
erb :"first_run/password"
23-
end
2415

25-
post "/password" do
26-
if no_password(params) or password_mismatch?(params)
27-
flash.now[:error] = "Hey, your password confirmation didn't match. Try again."
16+
get "/password" do
2817
erb :"first_run/password"
29-
else
30-
CreateUser.new.create(params[:password])
18+
end
3119

32-
redirect to("/import")
20+
post "/password" do
21+
if no_password(params) or password_mismatch?(params)
22+
flash.now[:error] = "Hey, your password confirmation didn't match. Try again."
23+
erb :"first_run/password"
24+
else
25+
user = CreateUser.new.create(params[:password])
26+
session[:user_id] = user.id
27+
28+
redirect to("/setup/import")
29+
end
3330
end
34-
end
3531

36-
get "/import" do
37-
erb :import
38-
end
32+
get "/import" do
33+
erb :import
34+
end
3935

40-
post "/import" do
41-
ImportFromOpml.import(params["opml_file"][:tempfile].read, true)
36+
post "/import" do
37+
ImportFromOpml.import(params["opml_file"][:tempfile].read, true)
4238

43-
redirect to("/")
44-
end
39+
redirect to("/setup/tutorial")
40+
end
4541

46-
get "/tutorial" do
47-
@sample_stories = StoryRepository.samples
48-
erb :tutorial
49-
end
42+
get "/tutorial" do
43+
FetchFeeds.enqueue(Feed.all)
44+
CompleteSetup.complete(current_user)
5045

51-
private
52-
def first_run_completed?
53-
UserRepository.any?
46+
@sample_stories = StoryRepository.samples
47+
erb :tutorial
48+
end
5449
end
5550

51+
private
5652
def no_password(params)
5753
params[:password].nil? || params[:password] == ""
5854
end

app/helpers/authentication_helpers.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require "sinatra/base"
22

3+
require_relative "../repositories/user_repository"
4+
35
module Sinatra
46
module AuthenticationHelpers
57
def is_authenticated?
@@ -8,8 +10,14 @@ def is_authenticated?
810

911
def needs_authentication?(path)
1012
return false if ENV['RACK_ENV'] == 'test'
13+
return false if !UserRepository.setup_complete?
1114
return false if path == "/login" || path == "/logout"
1215
true
1316
end
17+
18+
def current_user
19+
nil unless is_authenticated?
20+
UserRepository.fetch(session[:user_id])
21+
end
1422
end
1523
end

app/models/user.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
class User < ActiveRecord::Base
2+
attr_accessible :setup_complete
3+
24
attr_accessible :password, :password_confirmation
35
has_secure_password
6+
7+
def setup_complete?
8+
setup_complete
9+
end
410
end
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
require_relative "../models/user"
22

33
class UserRepository
4-
def self.any?
5-
User.any?
4+
def self.fetch(id)
5+
User.find(id)
6+
end
7+
8+
def self.setup_complete?
9+
User.any? && User.first.setup_complete?
610
end
711
end

app/tasks/fetch_feeds.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ def fetch_all
1010
FetchFeed.new(feed).fetch
1111
end
1212
end
13+
14+
def self.enqueue(feeds)
15+
self.new(feeds).delay.fetch_all
16+
end
1317
end

0 commit comments

Comments
 (0)