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

feature: disable belongs_to creation from association #2057

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions app/components/avo/fields/belongs_to_field/edit_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@
<%= @form.hidden_field @field.id_input_foreign_key %>
<% end %>
<% end %>
<% create_href = create_path(Avo.resource_manager.get_resource_by_model_class(type.to_s)) %>
<% if !disabled && create_href.present? %>
<%= link_to t("avo.create_new_item", item: type.to_s.downcase),
create_href,
class: "text-sm"
%>
<% if field.can_create? %>
<% create_href = create_path(Avo.resource_manager.get_resource_by_model_class(type.to_s)) %>
<% if !disabled && create_href.present? %>
<%= link_to t("avo.create_new_item", item: type.to_s.downcase),
create_href,
class: "text-sm"
%>
<% end %>
<% end %>
<% end %>
</div>
Expand Down Expand Up @@ -125,8 +127,10 @@
<%= @form.hidden_field @field.id_input_foreign_key %>
<% end %>
<% end %>
<% if !disabled && create_path.present? %>
<%= link_to t("avo.create_new_item", item: @field.name.downcase), create_path, class: "text-sm" %>
<% if field.can_create? %>
<% if !disabled && create_path.present? %>
<%= link_to t("avo.create_new_item", item: @field.name.downcase), create_path, class: "text-sm" %>
<% end %>
<% end %>
<% end %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def initialize(id, **args, &block)
@polymorphic_help = args[:polymorphic_help]
@target = args[:target]
@use_resource = args[:use_resource] || nil
@can_create = args[:can_create].nil? ? true : args[:can_create]
end

def value
Expand Down Expand Up @@ -264,6 +265,10 @@ def name
super
end

def can_create?
@can_create
end

private

def get_model_class(model)
Expand Down
5 changes: 3 additions & 2 deletions spec/dummy/app/avo/resources/course.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Avo::Resources::Course < Avo::BaseResource
self.stimulus_controllers = "course-resource toggle-fields"

def fields
field :links, as: :has_many, searchable: true, placeholder: "Click to choose a link",
discreet_pagination: true

field :id, as: :id
field :name, as: :text, html: {
Expand Down Expand Up @@ -72,6 +70,9 @@ def fields
as: :select,
options: Course.cities.values.flatten.map { |city| [city, city] }.to_h,
display_value: false

field :links, as: :has_many, searchable: true, placeholder: "Click to choose a link",
discreet_pagination: true
end

def filters
Expand Down
16 changes: 16 additions & 0 deletions spec/system/avo/create_via_belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,20 @@
)
end
end

context 'disable' do
it 'dont show the link', :aggregate_failures do
Avo::Resources::CourseLink.with_temporary_items do
field :course, as: :belongs_to, searchable: true, can_create: false
end

visit '/admin/resources/course_links/new'

within field_wrapper(:course) do
expect(page).not_to have_text 'Create new course'
end

Avo::Resources::CourseLink.restore_items_from_backup
end
end
end
Loading