Skip to content

Commit

Permalink
Merge pull request #158 from enpitut2018/category
Browse files Browse the repository at this point in the history
カテゴリ関連
  • Loading branch information
kousukeuo authored Feb 1, 2019
2 parents cfa1383 + 59e53b3 commit 9ffceb7
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ gem "activestorage", ">= 5.2.1.1"
#bootstrapを追加
gem 'bootstrap', '~> 4.1.1'
gem 'jquery-rails'

gem 'jquery-ui-rails'
#iconを追加
gem "font-awesome-rails"

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
json (1.8.6)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
Expand Down Expand Up @@ -432,6 +434,7 @@ DEPENDENCIES
foreman
jbuilder (~> 2.5)
jquery-rails
jquery-ui-rails
listen (>= 3.0.5, < 3.2)
mini_magick (= 4.7.0)
pg (>= 0.18, < 2.0)
Expand Down
3 changes: 2 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require cocoon
//= require cocoon
//= require jquery-ui
23 changes: 23 additions & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,27 @@ p.kaiwa-text:last-child {

.table {
background-color: white;
}

ul.ui-autocomplete {
position: absolute;
list-style: none;
margin: 0;
padding: 0;
border: solid 1px #999;
cursor: default;
li {
background-color: #FFF;
border-top: solid 1px #DDD;
margin: 0;
padding: 2px 15px;
a {
color: #000;
display: block;
padding: 3px;
}
a.ui-state-hover, a.ui-state-active {
background-color: #FFFCB2;
}
}
}
15 changes: 14 additions & 1 deletion app/controllers/documents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class DocumentsController < ApplicationController
before_action :logged_in_user, only: [:index,:new, :update,:show]
before_action :correct_user, only: [:show]
protect_from_forgery except: :test

def index
@user = User.find(current_user.id)
Expand All @@ -13,9 +14,14 @@ def new
end

def create
test = params["test"]
@document = Document.new(document_params)
if @document.save
redirect_to "/documents/#{@document.id}"
if test
redirect_to "/documents/#{@document.id}?test=1"
else
redirect_to "/documents/#{@document.id}"
end
else
render 'new'
end
Expand All @@ -38,7 +44,14 @@ def destroy
else
redirect_to documents_path
end
end

def test
@document = Document.find(params[:id])
@template = @document.template
if @document.destroy
redirect_to @template
end
end

private
Expand Down
29 changes: 28 additions & 1 deletion app/controllers/templates_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,27 @@ def index
@user = User.find(current_user.id)
@my_templates_unreleased=@user.templates.where(scope: 0)
@my_templates_released=@user.templates.where(scope: 1).order('likes_count DESC')

@category_name = []
@my_templates_released.each do |template_released|
@category_name.push(template_released.category.name)
end

@category_name = @category_name.each_with_object(Hash.new(0)){|v,o| o[v]+=1}
@category_name = @category_name.sort do |a, b|
b[1] <=> a[1]
end
@category_name = @category_name.to_h.first(3).to_h

@categories = Category.joins(:templates).select("categories.name").where("templates.scope =1").distinct

end



def new
@template=Template.new
@category=Category.new
@submit='作成'
end

def show
Expand Down Expand Up @@ -78,6 +93,18 @@ def release
end
end

def category_auto_complete
categories = Category.select(:name).where("name like '%" + params[:term] + "%'").order(:name)
categories = categories.map(&:name)
render json: categories.to_json
end

def template_auto_complete
templates = Template.where(scope: 1).select(:title).where("title like '%" + params[:term] + "%'").order(:title)
templates = templates.map(&:title)
render json: templates.to_json
end

private

def template_params
Expand Down
36 changes: 33 additions & 3 deletions app/javascript/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
<input type="text" class="form-control" id="Input" v-model="title">
</div>
<div class="col">
<button type="button" class="btn btn-success" @click="saveNote">ノートを保存</button>
<button v-if="test_flag" type="button" class="btn btn-success" @click="endTest">テストを終了</button>
<button v-else type="button" class="btn btn-success" @click="saveNote">ノートを保存</button>
</div>
</div>

Expand Down Expand Up @@ -205,12 +206,17 @@ export default {
tab1: true,
tab2: false,
record_flag: false,
read_flag: false,
chrome: false
chrome: false,
test_flag: false,
read_flag: false
};
},
mounted: function() {
let test = this.getParams().test;
if (test == 1) {
this.test_flag = true;
}
let path = location.pathname.split("/");
let documentNumber = path[2];
axios
Expand Down Expand Up @@ -318,6 +324,18 @@ export default {
resolve();
});
},
getParams: function() {
var vars = {};
var param = location.search.substring(1).split("&");
for (var i = 0; i < param.length; i++) {
var keySearch = param[i].search(/=/);
var key = "";
if (keySearch != -1) key = param[i].slice(0, keySearch);
var val = param[i].slice(param[i].indexOf("=", 0) + 1);
if (key != "") vars[key] = decodeURI(val);
}
return vars;
},
addAnswerToNote: function() {
this.note +=
` Q${this.count + 1}` + this.questions[this.count].qtext + "\n";
Expand Down Expand Up @@ -597,6 +615,18 @@ export default {
} else {
}
},
endTest: function() {
let path = location.pathname.split("/");
let documentNumber = path[2];
var form = document.createElement("form");
form.method = "POST";
form.action = `/documents/${documentNumber}/test`;
document.body.appendChild(form);
form.submit();
},
speakQuestion: function(text) {
if (this.read_flag == true) {
// 発言を作成
Expand Down
14 changes: 13 additions & 1 deletion app/views/templates/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<div class="form-group">
<%= fields_for @category do |i| %>
<%= i.label :name, "カテゴリ" ,class: 'font-weight-bold'%>
<%= i.text_field :name ,class: 'form-control'%>
<%= i.text_field :name ,class: 'form-control' , id: :category_name%>
<% end %>

<%
Expand All @@ -58,10 +58,22 @@
<% end %>

<script>
$( function() {
$('#micropost_picture').bind('change', function() {
var size_in_megabytes = this.files[0].size/1024/1024;
if (size_in_megabytes > 5) {
alert('ファイルサイズが上限の5MBを超えています。');
}
});

$( "#category_name" ).autocomplete({
autoFocus: true,
source: "/templates/category_auto_complete.json",
minLength: 1,
messages: {
noResults: '',
results: function() {}
}
});
});
</script>
64 changes: 61 additions & 3 deletions app/views/templates/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
<div class="container">
<div class="row">
<div class="col-md-12">
<h2 class="mt-4">Hot カテゴリ</h2>
<div>
<% @category_name.each do |key,value| %>
<%= link_to "#{key}: #{value}", templates_path({"utf8"=>"✓", "select"=>"category", "search"=>"#{key}", "button"=>""}) , class: 'mr-4',style:'font-size: 1.5rem;'%>
<%end%>
</div>

<button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#categories" aria-expanded="false" aria-controls="collapseExample">
すべてのキーワードを見る
</button>
<div class="collapse" id="categories">
<% @categories.each do |category| %>
<%= link_to "#{category.name}", templates_path({"utf8"=>"✓", "select"=>"category", "search"=>"#{category.name}", "button"=>""}) , class: 'mr-4',style:'font-size: 1.5rem;'%>
<%end%>
</div>
<h2 class="mb-4 mt-4">すべてのアシスタント</h2>




<!-- 検索 -->
<div class='mb-4'>
<%= form_tag(templates_path, :method => 'get') do %>
<div class="form-row">
<div>
<%= select_tag :select, options_for_select([["タイトル", 'title'], ["カテゴリ", 'category']]) , class:"custom-select" %>
<%= select_tag :select, options_for_select([["タイトル", 'title'], ["カテゴリ", 'category']]) , class:"custom-select" ,id:"select_key"%>
</div>
<div class="col">
<%= text_field_tag :search ,'',:class => 'form-control',:placeholder =>'アシスタントを検索' %>
<%= text_field_tag :search ,'',:class => 'form-control',:placeholder =>'アシスタントを検索' ,:id=>'template_or_category_name'%>
</div>
<div class="col">
<%= button_tag '検索', class: 'btn btn-primary' %>
Expand Down Expand Up @@ -121,4 +138,45 @@
</div>
</div>
</div>
</div>
</div>

<script>
$(function() {
$( "#template_or_category_name" ).autocomplete({
autoFocus: true,
source: "/templates/template_auto_complete.json",
minLength: 1,
messages: {
noResults: '',
results: function() {}
}
});

var key;
$("#select_key").on("change",function(){
key = this.value;
if(key == 'title'){
$( "#template_or_category_name" ).autocomplete({
autoFocus: true,
source: "/templates/template_auto_complete.json",
minLength: 1,
messages: {
noResults: '',
results: function() {}
}
});
}
else{
$( "#template_or_category_name" ).autocomplete({
autoFocus: true,
source: "/templates/category_auto_complete.json",
minLength: 1,
messages: {
noResults: '',
results: function() {}
}
});
}
});
});
</script>
11 changes: 11 additions & 0 deletions app/views/templates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@
<p class='h4 mt-5'>
<%= link_to "公開する", release_template_path(@template.id), data:{confirm: "本当にこのアシスタントを公開しますか?\n一度公開されると戻せません。"}%>
</p>
<%= form_for(@document) do |f| %>
<div class="form-group">
<%= f.hidden_field :title,value:'テスト' %>
<%= hidden_field_tag 'test', true %>
</div>

<%= f.hidden_field :template_id, :value => @template.id %>
<div class="form-group">
<%= f.submit "このボタンでアシスタントをテストします。公開はされません", class: "btn btn-primary" %>
</div>
<% end %>
<% else %>
<div class="alert alert-warning" role="alert">
質問が一つ以上ないとこのアシスタントは公開できません
Expand Down
12 changes: 11 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@
resources :users
post '/users/new', to: 'users#create'
resources :sessions, :only => [:new,:create]
resources :documents, :only => [:index,:new,:show,:create,:update,:destroy]

resources :documents do
member do
post 'test'
end
end

resources :templates do
member do
get 'release'
end
collection do
get 'category_auto_complete'
get 'template_auto_complete'
end
end
resources :likes, only: [:create, :destroy]
get 'login' => 'sessions#new'
Expand Down

0 comments on commit 9ffceb7

Please sign in to comment.