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 1 commit
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.allow_creation? %>
<% 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.allow_creation? %>
<% 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
@allow_creation = args[:allow_creation].nil? ? true : args[:allow_creation]
end

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

def allow_creation?
@allow_creation
end

private

def get_model_class(model)
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, allow_creation: 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