Browse Source

Add templates management

master
Norore 4 years ago
parent
commit
120b53300e
  1. 3
      app/assets/javascripts/targets.coffee
  2. 3
      app/assets/javascripts/templates.coffee
  3. 13
      app/assets/stylesheets/application.sass
  4. 3
      app/assets/stylesheets/targets.scss
  5. 3
      app/assets/stylesheets/templates.scss
  6. 2
      app/controllers/groups_controller.rb
  7. 1
      app/controllers/sites_controller.rb
  8. 45
      app/controllers/templates_controller.rb
  9. 2
      app/helpers/templates_helper.rb
  10. 5
      app/models/template.rb
  11. 49
      app/views/config/index.html.erb
  12. 35
      app/views/templates/_form.html.erb
  13. 19
      app/views/templates/_target_fields.html.erb
  14. 5
      app/views/templates/edit.html.erb
  15. 5
      app/views/templates/new.html.erb
  16. 1
      config/routes.rb
  17. 5
      spec/controllers/targets_controller_spec.rb
  18. 5
      spec/controllers/templates_controller_spec.rb
  19. 15
      spec/helpers/targets_helper_spec.rb
  20. 15
      spec/helpers/templates_helper_spec.rb

3
app/assets/javascripts/targets.coffee

@ -0,0 +1,3 @@
# 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/

3
app/assets/javascripts/templates.coffee

@ -0,0 +1,3 @@
# 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/

13
app/assets/stylesheets/application.sass

@ -98,6 +98,7 @@ main
display: none
nav.tabs-menu
margin: 0 1.2rem
color: $link-color
border-bottom-color: $border-color-base
.tabs-menu-link
@ -107,6 +108,12 @@ nav.tabs-menu
border-bottom-color: $border-color-base
text-decoration: none
.tabs-content
padding: 0 1rem 1rem 1rem
border: 1px solid $border-color-base
border-radius: 1rem
box-shadow: 0rem 0rem .4rem $border-color-base
form
fieldset.block
border: .1rem solid darken($border-color-base, 40%)
@ -118,3 +125,9 @@ form
font-weight: bold
border: 1px solid $border-color-base
border-left-width: thick
ul
li.separator
border-bottom: 1px solid #c3c3c3
li.separator:last-child
border-bottom: none

3
app/assets/stylesheets/targets.scss

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

3
app/assets/stylesheets/templates.scss

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

2
app/controllers/groups_controller.rb

@ -40,7 +40,7 @@ class GroupsController < ApplicationController
params.require(:group).permit(
:id, :name, :template_id,
targets_attributes: %i[id name css from to group_id _destroy],
sites_attributes: %i[id name url reference group_id _destroy]
sites_attributes: %i[id name url group_id _destroy]
)
end

1
app/controllers/sites_controller.rb

@ -1,4 +1,5 @@
class SitesController < ApplicationController
before_action :set_group, only: [:edit, :update, :destroy]
def index
@sites = Site.all.includes(:group).order(:group_id, :url)
end

45
app/controllers/templates_controller.rb

@ -0,0 +1,45 @@
class TemplatesController < ApplicationController
before_action :set_template, only: [:edit, :update, :destroy]
def new
@template = ::Template.new
end
def create
@template = ::Template.new(template_params)
if @template.save
redirect_to config_index_path, notice: 'Template has been successfully created.'
else
render :new
end
end
def edit
end
def update
if @template.update(template_params)
redirect_to config_index_path, notice: 'Template has been successfully updated.'
else
render :edit
end
end
def destroy
@template.destroy
redirect_to config_index_path, notice: 'Template has been successfully removed.'
end
private
def set_template
@template = ::Template.find(params[:id])
end
def template_params
params.require(:template).permit(
:id, :name,
targets_attributes: %i[id name css from to group_id _destroy],
)
end
end

2
app/helpers/templates_helper.rb

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

5
app/models/template.rb

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

49
app/views/config/index.html.erb

@ -10,22 +10,22 @@
<%= link_to t(:new_group), new_group_path %>
</div>
<ul class="">
<ul class="unstyled">
<% @groups.each do |group| %>
<li><%= link_to (group.name||group.id), edit_group_path(group) %>&nbsp;&nbsp;&nbsp;&nbsp;
<li><%= link_to (group.name||group.id), edit_group_path(group) %> |
<%= 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>
<li><%= :targets %>:</li>
<ul class="">
<% group.targets.each do |target| %>
<li><%= :name %>: <%= target.name||target.id %></li>
<% if target.css %>
<li><%= :css %>: <%= content_tag :code do target.css end %></li>
<% unless target.css.blank? %>
<li class="separator"><%= :css %>: <%= content_tag :code do target.css end %></li>
<% else %>
<li><%= :from %>: <%= content_tag :code do target.from end %></li>
<li><%= :to %>: <%= content_tag :code do target.to end %></li>
<li class="separator"><%= :to %>: <%= content_tag :code do target.to end %></li>
<% end %>
<% end %>
</ul>
@ -33,7 +33,7 @@
<% end %>
<% if group.template %>
<ul class="">
<li>template:</li>
<li><%= :template %>:</li>
<ul>
<% group.template.each do |template| %>
<li><%= :template %>: <%= template.name||template.id %></li>
@ -43,7 +43,7 @@
<% end %>
<% unless group.sites.empty? %>
<ul class="sites">
<li>list of <%= group.sites.count %> sites: <button>+</button></li>
<li><%= t(:list_of) %> <%= group.sites.count %> sites: <button>+</button></li>
<ul class="hidden">
<% group.sites.order(:name, :url).each do |site| %>
<li><%= link_to (site.name || site.url), site %></li>
@ -59,20 +59,27 @@
</div>
</div>
<div id="tab2" class="tabs-content-item">
<ul class="">
<div class="mbs">
<%= link_to t(:nem_template), new_template_path %>
</div>
<ul class="unstyled">
<% @templates.each do |template| %>
<li><%= template.name||template.id %></li>
<li>
<%= link_to (template.name||template.id), edit_template_path(template) %> |
<%= link_to :remove, template, method: :delete, data: { confirm: "Are you sure you want to remove this template ("+(template.name||template.id)+") and all related data? This cannot be revert!" } %>
</li>
<% unless template.targets.empty? %>
<ul class="">
<li>targets:</li>
<li><%= :targets %>:</li>
<ul class="">
<% template.targets.each do |target| %>
<li><%= :name %>: <%= target.name||target.id %></li>
<% if target.css %>
<li><%= :css %>: <%= content_tag :code do target.css end %></li>
<% unless target.css.blank? %>
<li class="separator"><%= :css %>: <%= content_tag :code do target.css end %></li>
<% else %>
<li><%= :from %>: <%= content_tag :code do target.from end %></li>
<li><%= :to %>: <%= content_tag :code do target.to end %></li>
<li class="separator"><%= :to %>: <%= content_tag :code do target.to end %></li>
<% end %>
<% end %>
</ul>
@ -80,18 +87,24 @@
<% end %>
<% end %>
</ul>
<div class="mts">
<%= link_to t(:nem_template), new_template_path %>
</div>
</div>
<div id="tab3" class="tabs-content-item">
<ul class="">
<% @sites.each do |site| %>
<li><%= link_to (site.name||site.url), site %></li>
<li>
<%= link_to (site.name||site.url), site %>
</li>
<% unless site.targets.empty? %>
<ul class="">
<li>targets:</li>
<li><%= :targets %>:</li>
<ul class="">
<% site.targets.each do |target| %>
<li><%= :name %>: <%= target.name||target.id %></li>
<% if target.css %>
<% unless target.css.blank? %>
<li><%= :css %>: <%= content_tag :code do target.css end %></li>
<% else %>
<li><%= :from %>: <%= content_tag :code do target.from end %></li>
@ -103,7 +116,7 @@
<% end %>
<% if site.template %>
<ul>
<li>template: <%= site.template.name||site.template.id %></li>
<li><%= :template %>: <%= site.template.name||site.template.id %></li>
</ul>
<% end %>
<% end %>

35
app/views/templates/_form.html.erb

@ -0,0 +1,35 @@
<%= form_with(model: template, local: true, remote: true, class: "mts") do |form| %>
<% if template.errors.any? %>
<div id="alert--error">
<h2><%= pluralize(template.errors.count, "error") %> prohibited this template from being saved:</h2>
<ul>
<% template.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :template %></legend>
<div class="auto-grid has-gutter mbs">
<%= form.label :name, :name, class: 'txtright' %>
<%= form.text_field :name %>
</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>
<div class="actions">
<%= form.submit :submit %>
</div>
<% end %>

19
app/views/templates/_target_fields.html.erb

@ -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 %>
<%= f.label :from, :from %>:
<%= f.text_field :from %>
<%= f.label :to, :to %>:
<%= f.text_field :to %>
<%= f.hidden_field :_destroy %>
<%= link_to :delete, "#", class: "remove_fields" %>
</div>
</fieldset>

5
app/views/templates/edit.html.erb

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

5
app/views/templates/new.html.erb

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

1
config/routes.rb

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

5
spec/controllers/targets_controller_spec.rb

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

5
spec/controllers/templates_controller_spec.rb

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

15
spec/helpers/targets_helper_spec.rb

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the TargetsHelper. For example:
#
# describe TargetsHelper 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 TargetsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

15
spec/helpers/templates_helper_spec.rb

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the TemplatesHelper. For example:
#
# describe TemplatesHelper 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 TemplatesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save