diff --git a/.gitignore b/.gitignore index b7a9037f9..e3837f857 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,9 @@ # Ignore bundler config. /.bundle +# ignore DS store on mac +**/.DS_Store + # Ignore all logfiles and tempfiles. /log/* /tmp/* diff --git a/app/controllers/comfy/admin/api_namespaces_controller.rb b/app/controllers/comfy/admin/api_namespaces_controller.rb index 90fbb48a6..56648f599 100755 --- a/app/controllers/comfy/admin/api_namespaces_controller.rb +++ b/app/controllers/comfy/admin/api_namespaces_controller.rb @@ -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] diff --git a/app/models/api_namespace.rb b/app/models/api_namespace.rb index f46e4f6dd..5f1bc94cb 100755 --- a/app/models/api_namespace.rb +++ b/app/models/api_namespace.rb @@ -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 @@ -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 @@ -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: " + +
hello from <%= @api_resource.id %>
+ " + ) + + Comfy::Cms::Fragment.create!( + identifier: 'content', + record: index_page, + tag: 'wysiwyg', + content: " +
+

#{self.name.pluralize}

+
#{self.snippet}
+ {{ cms:helper render_api_namespace_resource_index '#{self.slug}', order: { created_at: 'DESC' } } }} +
+ " + ) + + 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: " +
+ <% @api_resources.each do |resource| %> + +
+
<%= resource.id %>
+ <%= resource.created_at %> +
+
+ <% end %> +
+ " + ) + + Comfy::Cms::Fragment.create!( + identifier: 'content', + record: show_page, + tag: 'wysiwyg', + content: " +
+

#{self.name} show

+ {{ cms:helper render_api_namespace_resource '#{self.slug}' }} +
+ " + ) + end + end end \ No newline at end of file diff --git a/app/views/comfy/admin/api_namespaces/_form.html.haml b/app/views/comfy/admin/api_namespaces/_form.html.haml index 383a208b1..67d0a9694 100755 --- a/app/views/comfy/admin/api_namespaces/_form.html.haml +++ b/app/views/comfy/admin/api_namespaces/_form.html.haml @@ -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 diff --git a/test/controllers/admin/comfy/api_namespaces_controller_test.rb b/test/controllers/admin/comfy/api_namespaces_controller_test.rb index 0c904deb3..cec4fdd97 100755 --- a/test/controllers/admin/comfy/api_namespaces_controller_test.rb +++ b/test/controllers/admin/comfy/api_namespaces_controller_test.rb @@ -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 @@ -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 @@ -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) @@ -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) diff --git a/test/controllers/admin/comfy/api_resources_controller_test.rb b/test/controllers/admin/comfy/api_resources_controller_test.rb index b74702477..155e430a5 100755 --- a/test/controllers/admin/comfy/api_resources_controller_test.rb +++ b/test/controllers/admin/comfy/api_resources_controller_test.rb @@ -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\"}" diff --git a/test/plugin_fixtures/sync_attribute_to_api_namespace.yml b/test/plugin_fixtures/sync_attribute_to_api_namespace.yml index b9e582112..b2089eec3 100644 --- a/test/plugin_fixtures/sync_attribute_to_api_namespace.yml +++ b/test/plugin_fixtures/sync_attribute_to_api_namespace.yml @@ -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