mirror of
https://github.com/ENSL/ensl.org.git
synced 2024-12-25 12:01:03 +00:00
Implementing custom_url logic
This commit is contained in:
parent
0f0393ecc5
commit
8c0378579b
9 changed files with 190 additions and 7 deletions
|
@ -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/
|
|
@ -0,0 +1,13 @@
|
|||
#custom-urls-panel {
|
||||
th {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
input[type=text] {
|
||||
margin-right: 8px;
|
||||
}
|
||||
}
|
|
@ -61,3 +61,4 @@
|
|||
@import "pages/issues";
|
||||
@import "pages/servers";
|
||||
@import "pages/groups";
|
||||
@import "pages/custom_urls";
|
||||
|
|
13
app/assets/stylesheets/themes/flat/pages/_custom_urls.scss
Normal file
13
app/assets/stylesheets/themes/flat/pages/_custom_urls.scss
Normal file
|
@ -0,0 +1,13 @@
|
|||
#custom-urls-panel {
|
||||
th {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
|
||||
input[type=text] {
|
||||
margin-right: 8px;
|
||||
}
|
||||
}
|
|
@ -61,3 +61,4 @@
|
|||
@import "pages/issues";
|
||||
@import "pages/servers";
|
||||
@import "pages/groups";
|
||||
@import "pages/custom_urls";
|
|
@ -1,10 +1,23 @@
|
|||
class CustomUrlsController < ApplicationController
|
||||
def administrate
|
||||
raise AccessError unless cuser && cuser.admin?
|
||||
@custom_urls = CustomUrl.all
|
||||
@custom_url = CustomUrl.new
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def show
|
||||
|
@ -17,9 +30,48 @@ class CustomUrlsController < ApplicationController
|
|||
|
||||
def update
|
||||
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
|
||||
|
||||
def destroy
|
||||
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
|
||||
|
|
|
@ -5,5 +5,8 @@ class CustomUrl < ActiveRecord::Base
|
|||
validates :name,
|
||||
length: {in: 2..10},
|
||||
uniqueness: true,
|
||||
format: /\A[a-z\-]{2,10}\Z/
|
||||
format: /\A[a-z]+([\-])?[a-z]+\Z/
|
||||
|
||||
validates :article_id,
|
||||
presence: true
|
||||
end
|
||||
|
|
42
app/views/custom_urls/_controls.js.erb
Normal file
42
app/views/custom_urls/_controls.js.erb
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -1,2 +1,63 @@
|
|||
<h1>Site#administrate</h1>
|
||||
<p>Find me in app/views/site/administrate.html.erb</p>
|
||||
<div id="custom-urls-panel">
|
||||
<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>
|
||||
|
|
Loading…
Reference in a new issue