Skip to content

Commit c2e62c0

Browse files
committed
Browsing without login by setting environment variable
- Enable readonly browsing when DMEMO_ALLOW_ANONYMOUS_TO_READ is set - Hide favorite tables without login user. - Update RSpec for anonymous login
1 parent eb830d3 commit c2e62c0

File tree

10 files changed

+74
-40
lines changed

10 files changed

+74
-40
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def current_user
1212

1313
def require_login
1414
return if current_user
15+
return if Rails.application.config.allow_anonymous_to_read && ["index", "show"].include?(params[:action])
1516
redirect_to google_oauth2_path(state: request.fullpath)
1617
end
1718

app/controllers/top_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class TopController < ApplicationController
22
def show
33
@database_memos = DatabaseMemo.all.includes(:data_source, schema_memos: :table_memos).sort_by(&:display_order)
4-
@favorite_tables = TableMemo.where(id: current_user.favorite_tables.pluck(:table_memo_id))
4+
if current_user
5+
@favorite_tables = TableMemo.where(id: current_user.favorite_tables.pluck(:table_memo_id))
6+
end
57
end
68
end

app/views/shared/_main_sidebar.html.haml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
%aside.main-sidebar
22
%section.sidebar
33
.user-panel
4-
.pull-left.image
5-
= image_tag current_user.image_url, class: "img-rounded"
6-
.pull-left.info
7-
%p= current_user.name
4+
- if current_user
5+
.pull-left.image
6+
= image_tag current_user.image_url, class: "img-rounded"
7+
.pull-left.info
8+
%p= current_user.name
89

910
= form_for @search_result, method: :get, html: { class: "sidebar-form" } do |f|
1011
.input-group

app/views/shared/_navbar.html.haml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
%nav.navbar.navbar-static-top{ role: "navigation" }
22
.navbar-custom-menu
33
%ul.nav.navbar-nav
4-
%li
5-
= link_to setting_path do
6-
%i.fa.fa-gear
7-
Setting
8-
%li
9-
= link_to edit_user_path(current_user) do
10-
%i.fa.fa-user
11-
= current_user.name
12-
%li
13-
= link_to logout_path, method: :delete do
14-
%i.fa.fa-sign-out
15-
Sign-out
4+
- if current_user
5+
%li
6+
= link_to setting_path do
7+
%i.fa.fa-gear
8+
Setting
9+
%li
10+
= link_to edit_user_path(current_user) do
11+
%i.fa.fa-user
12+
= current_user.name
13+
%li
14+
= link_to logout_path, method: :delete do
15+
%i.fa.fa-sign-out
16+
Sign-out
17+
- else
18+
%li
19+
= link_to google_oauth2_path(state: request.fullpath) do
20+
%i.fa.fa-sign-in
21+
Sign-in

app/views/table_memos/_column_memo.html.haml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
- if column_memo.logs.present?
1010
= link_to column_memo_logs_path(column_memo.id), class: "pull-right colorbox" do
1111
%i.fa.fa-clock-o
12-
= link_to edit_column_memo_path(column_memo), class: "pull-right colorbox" do
13-
%i.fa.fa-edit
12+
- if current_user
13+
= link_to edit_column_memo_path(column_memo), class: "pull-right colorbox" do
14+
%i.fa.fa-edit
1415
.column-description
1516
- if column_memo.description.present?
1617
= column_memo.description_html

app/views/table_memos/show.html.haml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
- content_for :header do
22
%h1.page-header
33
= @table_memo.name
4-
- favorited_status = @table_memo.favorited_by?(current_user) ? "favorited" : "unfavorited"
5-
%span.favorite-table-block{ class: favorited_status }
6-
= link_to table_memo_favorite_table_path(@table_memo.id), method: :delete, remote: true, class: "unfavorite-table-link" do
7-
%i.fa.fa-star
8-
= link_to table_memo_favorite_table_path(@table_memo.id), method: :post, remote: true, class: "favorite-table-link" do
9-
%i.fa.fa-star-o
4+
- if current_user
5+
- favorited_status = @table_memo.favorited_by?(current_user) ? "favorited" : "unfavorited"
6+
%span.favorite-table-block{ class: favorited_status }
7+
= link_to table_memo_favorite_table_path(@table_memo.id), method: :delete, remote: true, class: "unfavorite-table-link" do
8+
%i.fa.fa-star
9+
= link_to table_memo_favorite_table_path(@table_memo.id), method: :post, remote: true, class: "favorite-table-link" do
10+
%i.fa.fa-star-o
1011
= link_to edit_table_memo_path(@table_memo) do
1112
%i.fa.fa-edit
1213

app/views/top/show.html.haml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
%td
2626
= database_memo.schema_memos.map(&:table_memos).flatten.map(&:name).join(", ").truncate(100)
2727

28-
.box
29-
.box-header.with-border
30-
%h2.box-title Favorite tables
28+
- if current_user
29+
.box
30+
.box-header.with-border
31+
%h2.box-title Favorite tables
3132

32-
.box-body
33-
%table.table.table-hover.table-bordered.table-striped{ role: "grid" }
34-
%tr
35-
%th Name
36-
%th Description
37-
= render partial: "table_memo", collection: @favorite_tables
33+
.box-body
34+
%table.table.table-hover.table-bordered.table-striped{ role: "grid" }
35+
%tr
36+
%th Name
37+
%th Description
38+
= render partial: "table_memo", collection: @favorite_tables

app/views/users/index.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
- if user.admin?
2828
%i.fa.fa-check
2929
%td
30-
- if current_user.editable_user?(user.id)
30+
- if current_user.try!(:editable_user?, user.id)
3131
= link_to edit_user_path(user) do
3232
%i.fa.fa-edit

config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ class Application < Rails::Application
1818
config.active_record.belongs_to_required_by_default = false
1919

2020
config.eager_load_paths << "#{Rails.root}/lib/autoload"
21+
config.allow_anonymous_to_read = ENV.has_key? 'ALLOW_ANONYMOUS_TO_READ'
2122
end
2223
end

spec/requests/top_spec.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,34 @@
44
before do
55
FactoryBot.create(:data_source)
66
SynchronizeDataSources.run
7-
login!
87
end
98

109
describe "#show" do
11-
it "shows top page" do
12-
get root_path
13-
expect(page).to have_content("DatabaseMEMO")
14-
expect(page).to have_selector("a[href='/databases/dmemo']")
10+
context "with signed-in" do
11+
before { login! }
12+
it "shows top page" do
13+
get root_path
14+
expect(page).to have_content("DatabaseMEMO")
15+
expect(page).to have_selector("a[href='/databases/dmemo']")
16+
end
17+
end
18+
19+
context "with not signed-in" do
20+
context 'with disallowing anonymous to read' do
21+
it "redirects" do
22+
get root_path
23+
expect(response.location).to match('http://www.example.com/auth/google_oauth2.*?')
24+
end
25+
end
26+
27+
context 'with allowing anonymous to read' do
28+
before { Rails.application.config.allow_anonymous_to_read = true }
29+
it "shows top page" do
30+
get root_path
31+
expect(page).to have_content("DatabaseMEMO")
32+
expect(page).to have_selector("a[href='/databases/dmemo']")
33+
end
34+
end
1535
end
1636
end
1737
end

0 commit comments

Comments
 (0)