Add group management

master
Norore 4 years ago
parent 12d3a9c117
commit aff69e6053
  1. 6
      app/assets/javascripts/application.coffee
  2. 49
      app/assets/javascripts/groupe.coffee
  3. 15
      app/assets/stylesheets/application.sass
  4. 3
      app/assets/stylesheets/groupe.scss
  5. 48
      app/controllers/groups_controller.rb
  6. 25
      app/helpers/application_helper.rb
  7. 2
      app/helpers/group_helper.rb
  8. 2
      app/models/group.rb
  9. 5
      app/views/application/_headers.erb
  10. 18
      app/views/config/index.html.erb
  11. 52
      app/views/groups/_form.html.erb
  12. 13
      app/views/groups/_site_fields.html.erb
  13. 19
      app/views/groups/_target_fields.html.erb
  14. 5
      app/views/groups/edit.html.erb
  15. 5
      app/views/groups/new.html.erb
  16. 1
      config/routes.rb
  17. 5
      spec/controllers/groupe_controller_spec.rb
  18. 15
      spec/helpers/groupe_helper_spec.rb

@ -11,3 +11,9 @@ $(document).on 'click', 'form .add_fields', (event) ->
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(document).on 'click', 'form .add_fields_below', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).after($(this).data('fields').replace(regexp, time))
event.preventDefault()

@ -0,0 +1,49 @@
# 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://coffeescript.org/
$(document).on 'keyup', "form", (event) ->
$("input:text[name='css']").each (index, event) ->
frominput = $(this).next().next()
toinput = $(frominput).next().next()
if $(this).val()
$(frominput).prop('disabled', true)
$(toinput).prop('disabled', true)
else
$(frominput).prop('disabled', false)
$(toinput).prop('disabled', false)
$("input:text[name='from']").each (index, event) ->
cssinput = $(this).prev().prev()
if $(this).val()
$(cssinput).prop('disabled', true)
else
$(cssinput).prop('disabled', false)
$("input:text[name='to']").each (index, event) ->
cssinput = $(this).prev().prev().prev().prev()
if $(this).val()
$(cssinput).prop('disabled', true)
else
$(cssinput).prop('disabled', false)
$("form").ready ->
$("input:text[name='css']").each (index, event) ->
frominput = $(this).next().next()
toinput = $(frominput).next().next()
if $(this).val()
$(frominput).prop('disabled', true)
$(toinput).prop('disabled', true)
else
$(frominput).prop('disabled', false)
$(toinput).prop('disabled', false)
$("input:text[name='from']").each (index, event) ->
cssinput = $(this).prev().prev()
if $(this).val()
$(cssinput).prop('disabled', true)
else
$(cssinput).prop('disabled', false)
$("input:text[name='to']").each (index, event) ->
cssinput = $(this).prev().prev().prev().prev()
if $(this).val()
$(cssinput).prop('disabled', true)
else
$(cssinput).prop('disabled', false)

@ -21,7 +21,6 @@ header
top: 0
width: 100%
//height: $navbar-height
background-color: $header-bg-color
color: $background-base
@ -50,7 +49,6 @@ header
&:hover
background-color: darken($header-bg-color-contrast, 25%)
&.active
// background-color: $color-success
background-color: $header-bg-color-contrast
@ -87,7 +85,6 @@ main
div.nodata
font-style: oblique
a
color: $link-color
@ -109,3 +106,15 @@ nav.tabs-menu
font-weight: bold
border-bottom-color: $border-color-base
text-decoration: none
form
fieldset.block
border: .1rem solid darken($border-color-base, 40%)
box-shadow: 0rem 0rem .4rem $border-color-base
border-radius: .8rem
legend
background-color: lighten($header-bg-color-contrast, 30%)
color: $link-color-base
font-weight: bold
border: 1px solid $border-color-base
border-left-width: thick

@ -0,0 +1,3 @@
// Place all the styles related to the groupe controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

@ -0,0 +1,48 @@
class GroupsController < ApplicationController
before_action :set_group, only: [:edit, :update, :destroy]
def new
@group = Group.new
end
def create
@group = Group.new(group_params)
if @group.save
redirect_to config_index_path, notice: 'Group has been successfully created.'
else
render :new
end
end
def edit
@group = Group.find(params[:id])
end
def update
if @group.update(group_params)
redirect_to config_index_path, notice: 'Group has been successfully updated.'
else
render :edit
end
end
def destroy
@group.destroy
redirect_to config_index_path, notice: 'Group has been successfully removed.'
end
private
def set_group
@group = Group.find(params[:id])
end
def group_params
params.require(:group).permit(
:id, :name, :template_id,
targets_attributes: [:id, :name, :css, :from, :to, :_destroy],
sites_attributes: [:id, :name, :url, :_destroy]
)
end
end

@ -0,0 +1,25 @@
module ApplicationHelper
def link_to_add_fields_below(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields_below", data: {id: id, fields: fields.gsub("\n", "")})
end
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
end

@ -0,0 +1,2 @@
module GroupHelper
end

@ -4,6 +4,8 @@ class Group < ApplicationRecord
has_many :targets, dependent: :delete_all
validates :name, uniqueness: true
accepts_nested_attributes_for :targets, allow_destroy: true, reject_if: lambda{ |a| a[:name].blank? }
accepts_nested_attributes_for :sites, allow_destroy: true, reject_if: lambda{ |a| a[:url].blank? }
def self.[](name)
self.where(name: name).first

@ -1,5 +1,5 @@
<header>
<h1>Webmon</h1>
<h1>Alice</h1>
<nav>
<ul>
<li>
@ -8,6 +8,9 @@
<li>
<%= link_to 'Sites', sites_path %>
</li>
<li>
<%= link_to 'Config', config_index_path %>
</li>
</ul>
</nav>
</header>

@ -1,14 +1,20 @@
<div class="tabs js-tabs">
<nav class="tabs-menu">
<a href="#tab1" class="tabs-menu-link is-active"><%= :groups %></a>
<a href="#tab2" class="tabs-menu-link"><%= :templates %></a>
<a href="#tab3" class="tabs-menu-link"><%= :sites %></a>
<a href="#tab1" class="tabs-menu-link is-active"><%= t(:groups) %></a>
<a href="#tab2" class="tabs-menu-link"><%= t(:templates) %></a>
<a href="#tab3" class="tabs-menu-link"><%= t(:sites_without_group) %></a>
</nav>
<div class="tabs-content">
<div id="tab1" class="tabs-content-item">
<div class="mbs">
<%= link_to t(:new_group), new_group_path %>
</div>
<ul class="">
<% @groups.each do |group| %>
<li><%= group.name||group.id %></li>
<li><%= link_to (group.name||group.id), edit_group_path(group) %>&nbsp;&nbsp;&nbsp;&nbsp;
<%= link_to :remove, group, method: :delete, data: { confirm: "Are you sure you want to remove this group ("+(group.name||group.id)+") and all related data? This cannot be revert!" } %>
</li>
<% if group.targets %>
<ul class="">
<li>targets:</li>
@ -47,6 +53,10 @@
<% end %>
<% end %>
</ul>
<div class="mts">
<%= link_to t(:new_group), new_group_path %>
</div>
</div>
<div id="tab2" class="tabs-content-item">
<ul class="">

@ -0,0 +1,52 @@
<%= form_with(model: group, local: true, remote: true, class: "mts") do |form| %>
<% if group.errors.any? %>
<div id="alert--error">
<h2><%= pluralize(group.errors.count, "error") %> prohibited this group from being saved:</h2>
<ul>
<% group.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :group %></legend>
<div class="auto-grid has-gutter mbs">
<%= form.label :name, :name, class: 'txtright' %>
<%= form.text_field :name %>
<%= form.label :template_id, :template_id, class: 'txtright' %>
<%= form.select :template_id,
::Template.all.collect {|t| [t.name, t.id]},
{prompt: :select},
{multiple: :true, size: 5} %>
</div>
</fieldset>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :targets %></legend>
<%= form.fields_for :targets do |builder| %>
<%= render 'target_fields', f: builder %>
<% end %>
<%= link_to_add_fields :add_target, form, :targets %>
</fieldset>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :sites %></legend>
<%= link_to_add_fields_below :add_site, form, :sites %>
<%= form.fields_for :sites do |builder| %>
<%= render 'site_fields', f: builder %>
<% end %>
<%= link_to_add_fields :add_site, form, :sites %>
</fieldset>
<div class="actions">
<%= form.submit :submit %>
</div>
<% end %>

@ -0,0 +1,13 @@
<fieldset class="pan mbs">
<div class="auto-grid has-gutter">
<%= f.label :name, :name %>:
<%= f.text_field :name %>
<%= f.label :url, :url %>:
<%= f.url_field :url, size: 50 %>
<%= f.hidden_field :_destroy %>
<%= link_to :delete, "#", class: "remove_fields" %>
</div>
</fieldset>

@ -0,0 +1,19 @@
<fieldset class="pan mbs">
<div class="auto-grid has-gutter">
<%= f.label :name, :name %>:
<%= f.text_field :name %>
<%= f.label :css, :css %>:
<%= f.text_field :css, name: :css %>
<%= f.label :from, :from %>:
<%= f.text_field :from, name: :from %>
<%= f.label :to, :to %>:
<%= f.text_field :to, name: :to %>
<%= f.hidden_field :_destroy %>
<%= link_to :delete, "#", class: "remove_fields" %>
</div>
</fieldset>

@ -0,0 +1,5 @@
<h1 class="txtcenter"><%= :edit %> <%= @group.name %></h1>
<%= render 'form', group: @group %>
<%= link_to :back, config_index_path %>

@ -0,0 +1,5 @@
<h1 class="txtcenter"><%= :new %></h1>
<%= render 'form', group: @group %>
<%= link_to :back, config_index_path %>

@ -2,4 +2,5 @@ Rails.application.routes.draw do
resources :diffs, only: %i[index show]
resources :sites, only: %i[index show]
resources :config, only: %i[index]
resources :groups, only: %i[new create edit update destroy]
end

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe GroupeController, type: :controller do
end

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the GroupeHelper. For example:
#
# describe GroupeHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe GroupeHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save