Implementing custom_url logic

This commit is contained in:
Absurdon 2017-11-12 01:26:00 +01:00 committed by Absurdon
parent 0f0393ecc5
commit 8c0378579b
9 changed files with 190 additions and 7 deletions

View file

@ -1,3 +0,0 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/

View file

@ -0,0 +1,13 @@
#custom-urls-panel {
th {
font-weight: bold;
}
.hidden {
display: none;
}
input[type=text] {
margin-right: 8px;
}
}

View file

@ -61,3 +61,4 @@
@import "pages/issues"; @import "pages/issues";
@import "pages/servers"; @import "pages/servers";
@import "pages/groups"; @import "pages/groups";
@import "pages/custom_urls";

View file

@ -0,0 +1,13 @@
#custom-urls-panel {
th {
font-weight: bold;
}
.hidden {
display: none;
}
input[type=text] {
margin-right: 8px;
}
}

View file

@ -61,3 +61,4 @@
@import "pages/issues"; @import "pages/issues";
@import "pages/servers"; @import "pages/servers";
@import "pages/groups"; @import "pages/groups";
@import "pages/custom_urls";

View file

@ -1,10 +1,23 @@
class CustomUrlsController < ApplicationController class CustomUrlsController < ApplicationController
def administrate def administrate
raise AccessError unless cuser && cuser.admin? raise AccessError unless cuser && cuser.admin?
@custom_urls = CustomUrl.all
@custom_url = CustomUrl.new
end end
def create def create
raise AccessError unless request.xhr? administrate
@custom_url.name = params[:custom_url][:name]
@custom_url.article_id = params[:custom_url][:article_id].to_i
if @custom_url.save
flash[:notice] = "Created URL with name #{@custom_url.name}"
redirect_to custom_urls_url
else
flash[:error] = 'Error creating URL!'
render :administrate
end
end end
def show def show
@ -17,9 +30,48 @@ class CustomUrlsController < ApplicationController
def update def update
raise AccessError unless request.xhr? raise AccessError unless request.xhr?
response = {}
if cuser && cuser.admin?
url = CustomUrl.find(params[:id]) rescue nil
if url
if url.update_attributes(params[:custom_url])
response[:status] = 200
response[:message] = 'Successfully updated!'
response[:obj] = url
else
response[:status] = 400
message = 'Update failed! Errors:'
url.errors.full_messages.each do |error|
message += "\n * " + error
end
response[:message] = message
end
else
response[:status] = 404
response[:message] = 'Error! No Custom URL with this id exists.'
end
else
response[:status] = 403
response[:message] = 'You are not allowed to do this!'
end
render json: response, status: response[:status]
end end
def destroy def destroy
raise AccessError unless request.xhr? raise AccessError unless request.xhr?
response = {}
if cuser && cuser.admin?
url = CustomUrl.destroy(params[:id])
response[:status] = 200
response[:message] = "Successfully deleted url with name: #{url.name}!"
else
response[:status] = 403
response[:message] = 'You are not allowed to do this!'
end
render json: response, status: response[:status]
end end
end end

View file

@ -5,5 +5,8 @@ class CustomUrl < ActiveRecord::Base
validates :name, validates :name,
length: {in: 2..10}, length: {in: 2..10},
uniqueness: true, uniqueness: true,
format: /\A[a-z\-]{2,10}\Z/ format: /\A[a-z]+([\-])?[a-z]+\Z/
validates :article_id,
presence: true
end end

View file

@ -0,0 +1,42 @@
showEdit = function (url_id) {
var parent = $('#' + url_id);
parent.find('> td').toggleClass('hidden');
};
submitEdit = function (url_id) {
var parent = $('#' + url_id);
var form = parent.find('form');
$.post('<%= custom_urls_url %>/' + url_id, form.serialize())
.done(function (data) {
var nameField = parent.children('.name');
var articleField = parent.children('.article');
nameField.text(data.obj.name);
articleField.text(data.obj.title);
parent.find('> td').toggleClass('hidden');
alert(data.message);
}).fail(function (errorRes) {
var error = JSON.parse(errorRes.responseText);
alert(error.message);
});
}
deleteUrl = function (url_id) {
var confirmed = confirm('Are you sure you want to delete this item?');
if(confirmed) {
$.ajax({
url: '<%= custom_urls_url %>/' + url_id,
type: 'DELETE'
}).done(function (data) {
var trID = '#' + url_id;
$(trID).remove();
alert(data.message);
}).fail(function (errorRes) {
var error = JSON.parse(errorRes.responseText);
alert(error.message);
});
}
}

View file

@ -1,2 +1,63 @@
<h1>Site#administrate</h1> <div id="custom-urls-panel">
<p>Find me in app/views/site/administrate.html.erb</p> <h1 class="fancy"><span>Custom URLs - Admin Panel</span></h1>
<div>
<p>Don't delete custom URLs linked from the menu!</p>
</div>
<div>
<table id="custom-urls" class="striped">
<thead>
<tr>
<th>Name</th>
<th>Article</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @custom_urls.each do |url| %>
<tr class="<%= cycle('odd', 'even') %>" id="<%= url.id %>">
<td class="name"><%= url.name %></td>
<td class="article"><%= url.article.title %></td>
<td>
<%= link_to_function icon('pencil'), "showEdit(#{url.id})" %>
<%= link_to_function icon('times'), "deleteUrl(#{url.id})" %>
</td>
<td class="edit hidden" colspan="3">
<%= form_for url do |f| %>
<div class="fields">
<div class="inline">
<%= f.text_field :name %>
</div>
<%= f.select :article_id, Article.all.collect {|t| [t.title, t.id]} %>
<%= link_to_function 'Save', "submitEdit(#{url.id})", class: 'button' %>
</div>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% unless @custom_url.errors.empty? %>
<div>
<p><b>Errors:</b></p>
<ul>
<% @custom_url.errors.full_messages.each do |error| %>
<li><%= error %></li>
<% end %>
</ul>
</div>
<br>
<% end %>
<%= form_for @custom_url do |f| %>
<div class="fields">
<div class="inline">
<%= f.text_field :name %>
</div>
<%= f.select :article_id, Article.all.collect {|t| [t.title, t.id]} %>
<%= f.submit 'Add', class: 'button' %>
</div>
<% end %>
</div>
<script type="text/javascript">
<%= render 'controls.js' %>
</script>
</div>