From 8c0378579bda87893c7ba0eb2a539028d56bb5b2 Mon Sep 17 00:00:00 2001 From: Absurdon Date: Sun, 12 Nov 2017 01:26:00 +0100 Subject: [PATCH] Implementing custom_url logic --- app/assets/javascripts/custom_urls.js.coffee | 3 - .../themes/default/pages/_custom_urls.scss | 13 ++++ .../stylesheets/themes/default/theme.css.scss | 1 + .../themes/flat/pages/_custom_urls.scss | 13 ++++ .../stylesheets/themes/flat/theme.css.scss | 1 + app/controllers/custom_urls_controller.rb | 54 ++++++++++++++- app/models/custom_url.rb | 5 +- app/views/custom_urls/_controls.js.erb | 42 ++++++++++++ app/views/custom_urls/administrate.html.erb | 65 ++++++++++++++++++- 9 files changed, 190 insertions(+), 7 deletions(-) delete mode 100644 app/assets/javascripts/custom_urls.js.coffee create mode 100644 app/assets/stylesheets/themes/default/pages/_custom_urls.scss create mode 100644 app/assets/stylesheets/themes/flat/pages/_custom_urls.scss create mode 100644 app/views/custom_urls/_controls.js.erb diff --git a/app/assets/javascripts/custom_urls.js.coffee b/app/assets/javascripts/custom_urls.js.coffee deleted file mode 100644 index 7615679..0000000 --- a/app/assets/javascripts/custom_urls.js.coffee +++ /dev/null @@ -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/ diff --git a/app/assets/stylesheets/themes/default/pages/_custom_urls.scss b/app/assets/stylesheets/themes/default/pages/_custom_urls.scss new file mode 100644 index 0000000..39a9ec6 --- /dev/null +++ b/app/assets/stylesheets/themes/default/pages/_custom_urls.scss @@ -0,0 +1,13 @@ +#custom-urls-panel { + th { + font-weight: bold; + } + + .hidden { + display: none; + } + + input[type=text] { + margin-right: 8px; + } +} \ No newline at end of file diff --git a/app/assets/stylesheets/themes/default/theme.css.scss b/app/assets/stylesheets/themes/default/theme.css.scss index b109fbe..d05b84e 100644 --- a/app/assets/stylesheets/themes/default/theme.css.scss +++ b/app/assets/stylesheets/themes/default/theme.css.scss @@ -61,3 +61,4 @@ @import "pages/issues"; @import "pages/servers"; @import "pages/groups"; +@import "pages/custom_urls"; diff --git a/app/assets/stylesheets/themes/flat/pages/_custom_urls.scss b/app/assets/stylesheets/themes/flat/pages/_custom_urls.scss new file mode 100644 index 0000000..39a9ec6 --- /dev/null +++ b/app/assets/stylesheets/themes/flat/pages/_custom_urls.scss @@ -0,0 +1,13 @@ +#custom-urls-panel { + th { + font-weight: bold; + } + + .hidden { + display: none; + } + + input[type=text] { + margin-right: 8px; + } +} \ No newline at end of file diff --git a/app/assets/stylesheets/themes/flat/theme.css.scss b/app/assets/stylesheets/themes/flat/theme.css.scss index b109fbe..91840bc 100644 --- a/app/assets/stylesheets/themes/flat/theme.css.scss +++ b/app/assets/stylesheets/themes/flat/theme.css.scss @@ -61,3 +61,4 @@ @import "pages/issues"; @import "pages/servers"; @import "pages/groups"; +@import "pages/custom_urls"; \ No newline at end of file diff --git a/app/controllers/custom_urls_controller.rb b/app/controllers/custom_urls_controller.rb index fc69578..8105b41 100644 --- a/app/controllers/custom_urls_controller.rb +++ b/app/controllers/custom_urls_controller.rb @@ -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 diff --git a/app/models/custom_url.rb b/app/models/custom_url.rb index 1617271..0c7bbaa 100644 --- a/app/models/custom_url.rb +++ b/app/models/custom_url.rb @@ -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 diff --git a/app/views/custom_urls/_controls.js.erb b/app/views/custom_urls/_controls.js.erb new file mode 100644 index 0000000..be3769e --- /dev/null +++ b/app/views/custom_urls/_controls.js.erb @@ -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); + }); + } +} diff --git a/app/views/custom_urls/administrate.html.erb b/app/views/custom_urls/administrate.html.erb index 98a4a57..47a7e02 100644 --- a/app/views/custom_urls/administrate.html.erb +++ b/app/views/custom_urls/administrate.html.erb @@ -1,2 +1,63 @@ -

Site#administrate

-

Find me in app/views/site/administrate.html.erb

+
+

Custom URLs - Admin Panel

+
+

Don't delete custom URLs linked from the menu!

+
+
+ + + + + + + + + + <% @custom_urls.each do |url| %> + + + + + + + <% end %> + +
NameArticleActions
<%= url.name %><%= url.article.title %> + <%= link_to_function icon('pencil'), "showEdit(#{url.id})" %> + <%= link_to_function icon('times'), "deleteUrl(#{url.id})" %> +
+ <% unless @custom_url.errors.empty? %> +
+

Errors:

+
    + <% @custom_url.errors.full_messages.each do |error| %> +
  • <%= error %>
  • + <% end %> +
+
+
+ <% end %> + <%= form_for @custom_url do |f| %> +
+
+ <%= f.text_field :name %> +
+ <%= f.select :article_id, Article.all.collect {|t| [t.title, t.id]} %> + <%= f.submit 'Add', class: 'button' %> +
+ <% end %> +
+ +