Browse Source

Add sites without group management

master
Norore 4 years ago
parent
commit
5b9e24dcf3
  1. 4
      app/controllers/groups_controller.rb
  2. 46
      app/controllers/sites_controller.rb
  3. 4
      app/controllers/templates_controller.rb
  4. 1
      app/models/site.rb
  5. 19
      app/views/config/index.html.erb
  6. 3
      app/views/groups/_form.html.erb
  7. 44
      app/views/sites/_form.html.erb
  8. 19
      app/views/sites/_target_fields.html.erb
  9. 5
      app/views/sites/edit.html.erb
  10. 5
      app/views/sites/new.html.erb
  11. 10
      app/views/sites/show.html.erb
  12. 2
      config/routes.rb

4
app/controllers/groups_controller.rb

@ -1,5 +1,5 @@
class GroupsController < ApplicationController
before_action :set_group, only: [:edit, :update, :destroy]
before_action :set_group, only: %i[edit update destroy]
def new
@group = Group.new
@ -33,7 +33,7 @@ class GroupsController < ApplicationController
private
def set_group
@group = Group.find(params[:id])
@group = Group.find params[:id]
end
def group_params

46
app/controllers/sites_controller.rb

@ -1,10 +1,52 @@
class SitesController < ApplicationController
before_action :set_group, only: [:edit, :update, :destroy]
before_action :set_site, only: %i[show edit update destroy]
def index
@sites = Site.all.includes(:group).order(:group_id, :url)
end
def show
@site = Site.find params[:id]
end
def new
@site = Site.new
end
def create
@site = Site.new(site_params)
if @site.save
redirect_to config_index_path, notice: 'Site has been successfully created.'
else
render :new
end
end
def edit
end
def update
if @site.update(site_params)
redirect_to config_index_path, notice: 'Site has been successfully updated.'
else
render :edit
end
end
def destroy
@site.destroy
redirect_to config_index_path, notice: 'Site has been successfully removed.'
end
private
def set_site
@site = Site.find params[:id]
end
def site_params
params.require(:site).permit(
:id, :name, :url, :template_id, :group_id,
targets_attributes: %i[id name css from to site_id _destroy]
)
end
end

4
app/controllers/templates_controller.rb

@ -1,5 +1,5 @@
class TemplatesController < ApplicationController
before_action :set_template, only: [:edit, :update, :destroy]
before_action :set_template, only: %i[edit update destroy]
def new
@template = ::Template.new
@ -33,7 +33,7 @@ class TemplatesController < ApplicationController
private
def set_template
@template = ::Template.find(params[:id])
@template = ::Template.find params[:id]
end
def template_params

1
app/models/site.rb

@ -6,6 +6,7 @@ class Site < ApplicationRecord
attribute :reference, :compressed_text
validates :url, presence: true
accepts_nested_attributes_for :targets, allow_destroy: true, reject_if: lambda{ |a| a[:name].blank? }
def self.[](url)
self.where(url: url).first

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

@ -35,9 +35,7 @@
<ul class="">
<li><%= :template %>:</li>
<ul>
<% group.template.each do |template| %>
<li><%= :template %>: <%= template.name||template.id %></li>
<% end %>
<li><%= :template %>: <%= group.template.name||group.template.id %></li>
</ul>
</ul>
<% end %>
@ -60,7 +58,7 @@
</div>
<div id="tab2" class="tabs-content-item">
<div class="mbs">
<%= link_to t(:nem_template), new_template_path %>
<%= link_to t(:new_template), new_template_path %>
</div>
<ul class="unstyled">
@ -89,14 +87,19 @@
</ul>
<div class="mts">
<%= link_to t(:nem_template), new_template_path %>
<%= link_to t(:new_template), new_template_path %>
</div>
</div>
<div id="tab3" class="tabs-content-item">
<div class="mbs">
<%= link_to t(:new_site), new_site_path %>
</div>
<ul class="">
<% @sites.each do |site| %>
<li>
<%= link_to (site.name||site.url), site %>
<%= link_to (site.name||site.url), edit_site_path(site) %> |
<%= link_to :show, site %>
</li>
<% unless site.targets.empty? %>
<ul class="">
@ -121,6 +124,10 @@
<% end %>
<% end %>
</ul>
<div class="mts">
<%= link_to t(:new_site), new_site_path %>
</div>
</div>
</div>
</div>

3
app/views/groups/_form.html.erb

@ -20,8 +20,7 @@
<%= 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} %>
{include_blank: true} %>
</div>
</fieldset>

44
app/views/sites/_form.html.erb

@ -0,0 +1,44 @@
<%= form_with(model: site, local: true, remote: true, class: "mts") do |form| %>
<% if site.errors.any? %>
<div id="alert--error">
<h2><%= pluralize(site.errors.count, "error") %> prohibited this site from being saved:</h2>
<ul>
<% site.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<fieldset class="mbs pas block">
<legend class="h4-like"><%= :site %></legend>
<div class="auto-grid has-gutter mbs">
<%= form.label :name, :name, class: 'txtright' %>:
<%= form.text_field :name %>
<%= form.label :url, :url, class: 'txtright' %>:
<%= form.url_field :url %>
<%= form.label :template_id, :template_id, class: 'txtright' %>
<%= form.select :template_id,
::Template.all.collect {|t| [t.name, t.id]},
{include_blank: true},
{prompt: 'Select template'} %>
</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/sites/_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/sites/edit.html.erb

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

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

@ -0,0 +1,5 @@
<h1 class="txtcenter"><%= t(:new_site) %></h1>
<%= render 'form', site: @site %>
<%= link_to :back, config_index_path %>

10
app/views/sites/show.html.erb

@ -1,4 +1,4 @@
<h3><%= link_to (@site.name || @site.url), @site.url %></h3>
<h3><%= link_to (@site.name||@site.url), @site.url %></h3>
<div class="alert">
<strong>group:</strong> <%= @site.group&.name || :none %><br>
@ -8,7 +8,9 @@
<strong>changed_at:</strong> <%= l @site.changed_at, format: :long if @site.changed_at %><br>
</div>
<%= link_to (:back), sites_path %>
<div class="mbs">
<%= link_to (:back), sites_path %>
</div>
<% @site.diffs.each do |diff| %>
<ul class="unstyled mts">
@ -34,4 +36,6 @@
</ul>
<% end %>
<%= link_to (:back), sites_path %>
<div class="mts">
<%= link_to (:back), sites_path %>
</div>

2
config/routes.rb

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

Loading…
Cancel
Save