Commit bcccea4d authored by o's avatar o

resource creation according to schema

parent be709b97
Pipeline #4141 failed with stages
in 11 seconds
......@@ -340,6 +340,10 @@ module ApiBackend
get ['resource', 'mine']
end
def resource_schema(what)
get ['resource', 'schema'], {resource_type: what}
end
def resource_list(what, deleted=false)
get ['resource', 'list'], {resource_type: what, deleted: deleted}
end
......
......@@ -4,22 +4,17 @@ class ResourcesAdminController < ApplicationController
def resource_list
['schleuder', 'mailman']
end
def resource_default
case @resource_name
when 'mailman'
'{"listenname": "", "listendomain": "", "mailman_host": ""}'
when 'schleuder'
'{"name": "", "domain": "", "schleuder_host": ""}'
end
end
helper_method :resource_list, :resource_default
helper_method :resource_list
def show
@resource_name = params['resource_name']
if resource_list.include? @resource_name
begin
@resource_schema = api.resource_schema(@resource_name)['schema']
@resources = api.resource_list(@resource_name)['data']
@resources.sort! {|a,b| a['uid'] <=> b['uid']}
@deleted_resources = api.resource_list(@resource_name, true)['deleted']
@deleted_resources.sort! {|a,b| a['uid'] <=> b['uid']}
rescue ApiBackend::ApiError
flash[:danger] = :failed
redirect_to root_path
......@@ -39,10 +34,16 @@ class ResourcesAdminController < ApplicationController
when 'create'
@resource_name = params[:resource_name]
if resource_list.include? @resource_name
data = {}
params.each do |name, v|
if name =~ /data_(.*)/
data[$1] = v
end
end
@res = api.resource_create(
@resource_name,
params[:owner],
JSON.parse(params[:data]))
data)
end
when 'save'
if params['add_owner'].present?
......
......@@ -4,13 +4,22 @@
<% end %>
<hr />
<% if @resources %>
<% if @resource_name %>
<h4>New:</h4>
<%= form_tag do %>
<%= hidden_field_tag 'resource_name', @resource_name %>
<%=label_tag 'data' %>
<%=text_field_tag 'data', resource_default, size: 400 %>
<% @resource_schema.each do |n,v| %>
<%= label_tag n %>
<%= case v
when 'text'
text_field_tag 'data_'+n
when Array
select_tag 'data_'+n, options_for_select(v)
end %>
<br />
<% end %>
<%=label_tag 'owner' %>
<%=text_field_tag 'owner'%>
......@@ -19,6 +28,7 @@
<% end %>
<hr />
<h4>Existing:</h4>
<% @resources.each do |res| %>
<%= form_tag do %>
<%= hidden_field_tag 'uid', res['uid'] %>
......@@ -44,7 +54,7 @@
<hr />
<% end %>
<h3>Deleted:</h3>
<h4>Deleted:</h4>
<% @deleted_resources.each do |res| %>
<%= form_tag do %>
<%= hidden_field_tag 'uid', res['uid'] %>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment