Skip to content

Commit ea9bba5

Browse files
authored
Merge pull request #41 from suho/release/0.2.0
2 parents b893c80 + aff2e10 commit ea9bba5

File tree

30 files changed

+529
-47
lines changed

30 files changed

+529
-47
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
require 'csv'
4+
5+
class KeywordsController < ApplicationController
6+
include Pagy::Backend
7+
8+
def index
9+
pagy, keywords = pagy(current_user.keywords)
10+
keyword_presenters = keywords.map { |keyword| KeywordPresenter.new(keyword) }
11+
12+
render locals: {
13+
pagy: pagy,
14+
keyword_presenters: keyword_presenters
15+
}
16+
end
17+
18+
def create
19+
parse_keywords.each do |keyword|
20+
current_user.keywords.create(keyword: keyword)
21+
end
22+
redirect_to keywords_path
23+
end
24+
25+
private
26+
27+
def parse_keywords
28+
keywords_file = params[:keywords_file]
29+
ParseKeywordsService.new(keywords_file).call
30+
rescue GoogleSearch::Errors::KeywordsError => e
31+
flash[:alert] = e.message
32+
[]
33+
end
34+
end

app/helpers/application_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
22

33
module ApplicationHelper
4+
include Pagy::Frontend
45
end

app/lib/google_search/errors.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
module GoogleSearch
4+
module Errors
5+
class KeywordsError < StandardError; end
6+
end
7+
end

app/models/keyword.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Keyword < ApplicationRecord
4+
belongs_to :user
5+
6+
enum status: { in_progress: 0, completed: 1, failed: 2 }
7+
end

app/models/user.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ class User < ApplicationRecord
55
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
66
devise :database_authenticatable, :registerable,
77
:recoverable, :rememberable, :validatable
8+
9+
has_many :keywords, dependent: :destroy
810
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
class KeywordPresenter
4+
def initialize(keyword)
5+
@keyword = keyword
6+
end
7+
8+
def keyword_text
9+
keyword.keyword
10+
end
11+
12+
def formatted_created_at
13+
keyword.created_at.strftime('%F')
14+
end
15+
16+
def status_html
17+
return '<div class="spinner-border spinner-border-sm" role="status"></div>' if keyword.in_progress?
18+
return '<p class="text-success">Completed</p>' if keyword.completed?
19+
return '<p class="text-danger">Failed</p>' if keyword.failed?
20+
end
21+
22+
private
23+
24+
attr_reader :keyword
25+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
class ParseKeywordsService
4+
def initialize(keywords_file)
5+
@keywords_file = keywords_file
6+
end
7+
8+
def call
9+
csv_data = CSV.read(keywords_file.path)
10+
csv_data.map(&:first)
11+
rescue StandardError
12+
raise GoogleSearch::Errors::KeywordsError, I18n.t('keywords.upload.invalid_file')
13+
end
14+
15+
private
16+
17+
attr_reader :keywords_file
18+
end
Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
<h2>Change your password</h2>
1+
<div class="modal-dialog modal-content p-4">
22

3-
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
4-
<%= render "devise/shared/error_messages", resource: resource %>
5-
<%= f.hidden_field :reset_password_token %>
3+
<h2>Change your password</h2>
64

7-
<div class="field">
8-
<%= f.label :password, "New password" %><br />
9-
<% if @minimum_password_length %>
10-
<em>(<%= @minimum_password_length %> characters minimum)</em><br />
11-
<% end %>
12-
<%= f.password_field :password, autofocus: true, autocomplete: "new-password" %>
13-
</div>
5+
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
6+
<%= render "devise/shared/error_messages", resource: resource %>
7+
<%= f.hidden_field :reset_password_token %>
148

15-
<div class="field">
16-
<%= f.label :password_confirmation, "Confirm new password" %><br />
17-
<%= f.password_field :password_confirmation, autocomplete: "new-password" %>
18-
</div>
9+
<div class="form-group">
10+
<%= f.label :password, "New password" %>
11+
<% if @minimum_password_length %>
12+
<em>(<%= @minimum_password_length %> characters minimum)</em>
13+
<% end %>
14+
<%= f.password_field :password, autofocus: true, autocomplete: "new-password", required: true, class: "form-control" %>
15+
</div>
1916

20-
<div class="actions">
21-
<%= f.submit "Change my password" %>
22-
</div>
23-
<% end %>
17+
<div class="form-group">
18+
<%= f.label :password_confirmation, "Confirm new password" %>
19+
<%= f.password_field :password_confirmation, autocomplete: "new-password", required: true, class: "form-control" %>
20+
</div>
2421

25-
<%= render "devise/shared/links" %>
22+
<div class="form-group">
23+
<%= f.submit "Change my password", class: "form-control btn btn-primary" %>
24+
</div>
25+
<% end %>
26+
27+
<%= render "devise/shared/links" %>
28+
29+
</div>
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
<h2>Forgot your password?</h2>
1+
<div class="modal-dialog modal-content p-4">
22

3-
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
4-
<%= render "devise/shared/error_messages", resource: resource %>
3+
<h2>Forgot your password?</h2>
54

6-
<div class="field">
7-
<%= f.label :email %><br />
8-
<%= f.email_field :email, autofocus: true, autocomplete: "email" %>
9-
</div>
5+
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
6+
<%= render "devise/shared/error_messages", resource: resource %>
107

11-
<div class="actions">
12-
<%= f.submit "Send me reset password instructions" %>
13-
</div>
14-
<% end %>
8+
<div class="form-group">
9+
<%= f.label :email %>
10+
<%= f.email_field :email, autofocus: true, autocomplete: "email", class: "form-control", required: true %>
11+
</div>
1512

16-
<%= render "devise/shared/links" %>
13+
<div class="form-group">
14+
<%= f.submit "Send me reset password instructions", class: "form-control btn btn-primary" %>
15+
</div>
16+
<% end %>
17+
18+
</div>

app/views/devise/shared/_links.html.erb

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
<%- if controller_name != 'sessions' %>
2-
<%= link_to "Log in", new_session_path(resource_name), class: "btn btn-outline-dark" %><br/>
2+
<div class="form-group">
3+
<%= link_to "Log in", new_session_path(resource_name), class: "form-control btn btn-outline-dark" %><br/>
4+
</div>
35
<% end %>
46

57
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
6-
<%= link_to "Sign up", new_registration_path(resource_name), class: "btn btn-outline-dark" %><br/>
8+
<div class="form-group">
9+
<%= link_to "Sign up", new_registration_path(resource_name), class: "form-control btn btn-outline-dark" %><br/>
10+
</div>
711
<% end %>
812

913
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
10-
<!-- <%#= link_to "Forgot your password?", new_password_path(resource_name) %><br/>-->
14+
<div class="form-group">
15+
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br/>
16+
</div>
1117
<% end %>
1218

1319
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
14-
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br/>
20+
<div class="form-group">
21+
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br/>
22+
</div>
1523
<% end %>
1624

1725
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
18-
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br/>
26+
<div class="form-group">
27+
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br/>
28+
</div>
1929
<% end %>
2030

2131
<%- if devise_mapping.omniauthable? %>

0 commit comments

Comments
 (0)