Skip to content

Commit

Permalink
[feature] scaffold renderer at API Namespace creation
Browse files Browse the repository at this point in the history
addresses: #1588

Upon API Namespace creation, an index page (for rendering a list of entities) and show page (for rendering a specific entity by ID) are generated along with Snippets for rendering list and show views. For convenience, a breadcrumb navigation bar and creation form are included in the generated pages. 


# create a namespace with rendering enabled
<img width="1164" alt="Screen Shot 2023-08-22 at 7 44 24 PM" src="https://github.com/restarone/violet_rails/assets/35935196/ac171cc2-3d53-4240-88b8-09f07c52e1b0">

# auto generated index and show pages
<img width="1212" alt="Screen Shot 2023-08-22 at 7 44 35 PM" src="https://github.com/restarone/violet_rails/assets/35935196/b70fe82c-d933-40ac-a864-0699d2c5ca75">


# rendered list view

<img width="1728" alt="Screen Shot 2023-08-22 at 7 50 32 PM" src="https://github.com/restarone/violet_rails/assets/35935196/def0398a-8de6-4b58-b18a-41ca48218690">

# rendered show view

<img width="1728" alt="Screen Shot 2023-08-22 at 7 49 53 PM" src="https://github.com/restarone/violet_rails/assets/35935196/8883963c-5ed1-4489-9a13-db8d861f637f">
  • Loading branch information
donrestarone authored Aug 23, 2023
1 parent 5fbeb9a commit a69e609
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Ignore bundler config.
/.bundle

# ignore DS store on mac
**/.DS_Store

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/comfy/admin/api_namespaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def api_namespace_params
:properties,
:requires_authentication,
:namespace_type,
:has_form,
:is_renderable,
non_primitive_properties_attributes: [:id, :label, :field_type, :content, :attachment, :allow_attachments, :_destroy],
category_ids: [],
associations: [:type, :namespace, :dependent]
Expand Down
84 changes: 81 additions & 3 deletions app/models/api_namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ class ApiNamespace < ApplicationRecord

friendly_id :name, use: :slugged

attr_accessor :has_form
attr_accessor :is_renderable

after_save :update_api_form

after_save :add_foreign_key, if: -> { attributes.key?('associations') && self.saved_change_to_associations? }

after_create_commit :generate_renderable_entities, if: -> { is_renderable }

has_many :api_resources, dependent: :destroy
accepts_nested_attributes_for :api_resources
Expand Down Expand Up @@ -116,13 +118,13 @@ class ApiNamespace < ApplicationRecord
}

def update_api_form
if has_form == '1'
if is_renderable == '1'
if api_form.present?
api_form.update({ properties: form_properties })
else
create_api_form({ properties: form_properties })
end
elsif has_form == '0' && api_form.present?
elsif is_renderable == '0' && api_form.present?
api_form.destroy
end
end
Expand Down Expand Up @@ -420,4 +422,80 @@ def add_foreign_key
end
end
end

def generate_renderable_entities
site = Comfy::Cms::Site.first
# grab default layout -- might not be present in heavily customized apps
layout = site.layouts.find_by(identifier: 'default')
if layout.present?
index_page = layout.pages.find_or_create_by(
site_id: site.id,
label: self.name.pluralize,
slug: self.slug,
is_restricted: self.requires_authentication?,
)

site.snippets.create(
label: "#{self.name.pluralize}-show",
identifier: "#{self.slug}-show",
content: "
<nav aria-label='breadcrumb'>
<ol class='breadcrumb'>
<li class='breadcrumb-item'><a href='#{index_page.full_path}'>#{self.name.pluralize}</a></li>
<li class='breadcrumb-item active' aria-current='page'>ID: <%= @api_resource.id %></li>
</ol>
</nav>
<div>hello from <%= @api_resource.id %></div>
"
)

Comfy::Cms::Fragment.create!(
identifier: 'content',
record: index_page,
tag: 'wysiwyg',
content: "
<div>
<h1>#{self.name.pluralize}</h1>
<div class='container'>#{self.snippet}</div>
{{ cms:helper render_api_namespace_resource_index '#{self.slug}', order: { created_at: 'DESC' } } }}
</div>
"
)

show_page = layout.pages.find_or_create_by(
site_id: site.id,
label: "#{self.name.pluralize}-show",
slug: "#{self.slug}-show",
is_restricted: self.requires_authentication?,
)
site.snippets.create(
label: self.name.pluralize,
identifier: self.slug,
content: "
<div class='my-3'>
<% @api_resources.each do |resource| %>
<a href='/<%= resource.api_namespace.slug %>-show?id=<%= resource.id %>' class='list-group-item list-group-item-action flex-column align-items-start'>
<div class='d-flex w-100 justify-content-between'>
<h5 class='mb-1'><%= resource.id %></h5>
<small class='text-muted'><%= resource.created_at %></small>
</div>
</a>
<% end %>
</div>
"
)

Comfy::Cms::Fragment.create!(
identifier: 'content',
record: show_page,
tag: 'wysiwyg',
content: "
<div>
<h1>#{self.name} show</h1>
{{ cms:helper render_api_namespace_resource '#{self.slug}' }}
</div>
"
)
end
end
end
2 changes: 1 addition & 1 deletion app/views/comfy/admin/api_namespaces/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
= f.check_box :requires_authentication
.field
= f.label "Renderable (Form, and representation)"
= f.check_box :has_form, checked: @api_namespace.api_form.present?
= f.check_box :is_renderable, checked: @api_namespace.api_form.present?

- unless has_only_uncategorized_access?(current_user.api_accessibility)
= render "comfy/admin/cms/categories/form", form: f
Expand Down
20 changes: 10 additions & 10 deletions test/controllers/admin/comfy/api_namespaces_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ class Comfy::Admin::ApiNamespacesControllerTest < ActionDispatch::IntegrationTes
assert_redirected_to api_namespaces_url
end

test "should create api_form if has_form params is true" do
test "should create api_form if is_renderable params is true" do
sign_in(@user)
assert_difference('ApiForm.count') do
post api_namespaces_url, params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, properties: @api_namespace.properties.to_json, has_form: "1" ,requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
post api_namespaces_url, params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, properties: @api_namespace.properties.to_json, is_renderable: "1" ,requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
end
api_namespace = ApiNamespace.last
assert api_namespace.api_form
Expand All @@ -85,31 +85,31 @@ class Comfy::Admin::ApiNamespacesControllerTest < ActionDispatch::IntegrationTes
}.to_json

assert_difference('ApiForm.count') do
post api_namespaces_url, params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, properties: properties, has_form: "1" ,requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
post api_namespaces_url, params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, properties: properties, is_renderable: "1" ,requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
end
api_namespace = ApiNamespace.last
assert api_namespace.api_form
assert_equal api_namespace.api_form.properties["age"]["type_validation"], 'tel'
end

test "should create api_form if has_form params is true when updating" do
test "should create api_form if is_renderable params is true when updating" do
sign_in(@user)
assert_difference('ApiForm.count') do
patch api_namespace_url(api_namespaces(:two)), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, has_form: '1', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
patch api_namespace_url(api_namespaces(:two)), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, is_renderable: '1', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
end
end

test "should reomve api_form if has_form params is false when updating" do
test "should reomve api_form if is_renderable params is false when updating" do
sign_in(@user)
assert_difference('ApiForm.count', -1) do
patch api_namespace_url(@api_namespace), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, has_form: '0', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
patch api_namespace_url(@api_namespace), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, is_renderable: '0', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
end
end

test "should not create api_form if api_form already exists" do
sign_in(@user)
assert_no_difference('ApiForm.count') do
patch api_namespace_url(@api_namespace), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, has_form: '1', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
patch api_namespace_url(@api_namespace), params: { api_namespace: { name: @api_namespace.name, namespace_type: @api_namespace.namespace_type, is_renderable: '1', properties: @api_namespace.properties, requires_authentication: @api_namespace.requires_authentication, version: @api_namespace.version } }
end
end

Expand Down Expand Up @@ -606,7 +606,7 @@ class Comfy::Admin::ApiNamespacesControllerTest < ActionDispatch::IntegrationTes
sign_in(@user)
properties = {"attr_1"=>true, "attr_2"=>true, "attr_3"=>true, "attr_4"=>true}

@api_namespace.has_form = '1'
@api_namespace.is_renderable = '1'
@api_namespace.update(properties: properties)

get api_namespace_url(@api_namespace)
Expand Down Expand Up @@ -774,7 +774,7 @@ class Comfy::Admin::ApiNamespacesControllerTest < ActionDispatch::IntegrationTes

test "#index: Rendering tab should include documentation on form snippet and API HTML renderer snippets" do
sign_in(@user)
@api_namespace.has_form = "1"
@api_namespace.is_renderable = "1"
properties = { test_id: 123, obj:{ a:"b", c:"d"}, title: "Hello World", published: true, arr:[ 1, 2, 3], alpha_arr: ["a", "b"] }

@api_namespace.update(properties: properties)
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/admin/comfy/api_resources_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,8 @@ class Comfy::Admin::ApiResourcesControllerTest < ActionDispatch::IntegrationTest
assert_equal expected_message, flash[:alert]
end

test "should able to create api_resource if has_form params is set false" do
@api_namespace.has_form = '0';
test "should able to create api_resource if is_renderable params is set false" do
@api_namespace.is_renderable = '0';
@api_namespace.save;
payload_as_stringified_json = "{\"age\":26,\"alive\":true,\"last_name\":\"Teng\",\"first_name\":\"Jennifer\"}"

Expand Down
2 changes: 1 addition & 1 deletion test/plugin_fixtures/sync_attribute_to_api_namespace.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sync_attribute_to_api_namespace_plugin:
# Adding the new-attribute in ApiNamespace
new_properties = @api_namespace.properties.merge(attribute_name => default_value)
@api_namespace.has_form = @api_namespace.api_form.present? ? '1' : '0'
@api_namespace.is_renderable = @api_namespace.api_form.present? ? '1' : '0'
@api_namespace.update!(properties: new_properties)
# Making the new-attribute non-renderable
Expand Down

0 comments on commit a69e609

Please sign in to comment.