Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON editor is not present when creating nested resources #7

Open
sedubois opened this issue Oct 28, 2019 · 2 comments
Open

JSON editor is not present when creating nested resources #7

sedubois opened this issue Oct 28, 2019 · 2 comments

Comments

@sedubois
Copy link

I have a Post which has_many Post::Translations, which themselves have a JSON tags property. When creating the post with its nested resource(s), the tag editor does not appear (http://localhost:3000/admin/posts/new):

Screenshot 2019-10-28 at 21 38 41

However it does appear when creating the translation sub-resource directly (http://localhost:3000/admin/post/translations/new):

Screenshot 2019-10-28 at 21 42 44

Also, the editor does appear when editing an existing post:

Screenshot 2019-10-28 at 21 57 15

The dashboard is configured as follows:

# app/dashboards/post_dashboard.rb
class PostDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    translations: NestedHasMany.with_options(class_name: "Post::Translation"),
    ...
  }.freeze
  FORM_ATTRIBUTES = %i[
  translations
  ...
  ].freeze
end

# app/dashboards/post/translation_dashboard.rb
class Post::TranslationDashboard < Administrate::BaseDashboard
  ATTRIBUTE_TYPES = {
    ...
    tags: Field::JSONB,
    ...
  }.freeze
end

# app/fields/nested_has_many.rb
require "administrate/field/base"

class NestedHasMany < Administrate::Field::NestedHasMany
  def self.permitted_attribute(associated_resource, _options = nil)
    {
      "#{associated_resource}_attributes".to_sym =>
        associated_attributes(associated_resource, _options),
    }
  end

  def self.associated_attributes(associated_resource, _options)
    dashboard_class = dashboard_for_resource(associated_resource, _options)
    DEFAULT_ATTRIBUTES + dashboard_class.new.permitted_attributes
  end

  # Hack: reimplement Administrate::Field::NestedHasMany.dashboard_for_resource to preserve namespace
  def self.dashboard_for_resource(resource, _options)
    class_name = _options && _options[:class_name] || resource.to_s.classify
    "#{class_name}Dashboard".constantize
  end

  def association_name
    options.fetch(
      :association_name,
      associated_class_name.underscore.pluralize[/([^\/]*)$/, 1],
    )
  end
end

As my tags cannot be nil (as enforced by DB schema), this issue prevents creating new posts and is therefore quite blocking.

Using Rails 5.2.3, Administrate 0.12.0, administrate-field-jsonb 0.4.0, and administrate-field-nested_has_many 1.1.0 although it's mostly overridden; code is included for reference above but in principle unrelated to this issue.

@volkov-sergey
Copy link
Member

@sedubois as far as I understand you have a problem with displaying editors that were added after page initialized. We are using this JS https://github.com/codica2/administrate-field-jsonb/blob/master/app/assets/javascripts/administrate-field-jsonb/components/editor.js to initialize the editor. You need to run this code every time you add a new entity with NestedHasMany.

@sedubois
Copy link
Author

@volkov-sergey I see here that the JS is included with Administrate::Engine.add_javascript 'administrate-field-jsonb/application'. That code is inside an "engine" about which I don't know anything. Do you know how/where that "engine" should be loaded?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants