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

Add link to record support to belongs to field #2870

Merged
merged 6 commits into from
Jul 2, 2024
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<%= index_field_wrapper **field_wrapper_args do %>
<%= link_to @field.label, helpers.resource_view_path(record: @field.value, resource: @field.target_resource)
%>
<%= link_to @field.label, helpers.resource_view_path(
record: @field.index_link_to_record,
resource: @field.index_link_to_resource
) %>
<% end %>
18 changes: 18 additions & 0 deletions lib/avo/fields/belongs_to_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class BelongsToField < BaseField
attr_reader :allow_via_detaching
attr_reader :attach_scope
attr_reader :polymorphic_help
attr_reader :link_to_record

def initialize(id, **args, &block)
args[:placeholder] ||= I18n.t("avo.choose_an_option")
Expand All @@ -84,6 +85,7 @@ def initialize(id, **args, &block)
@target = args[:target]
@use_resource = args[:use_resource] || nil
@can_create = args[:can_create].nil? ? true : args[:can_create]
@link_to_record = args[:link_to_record].present? ? args[:link_to_record] : false
end

def value
Expand Down Expand Up @@ -263,6 +265,22 @@ def default_name
super
end

def index_link_to_resource
if @link_to_record.present?
@resource
else
target_resource
end
end

def index_link_to_record
if @link_to_record.present?
get_record
else
value
end
end

def can_create?
@can_create
end
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/avo/resources/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def fields
as: :date_time,
picker_format: "Y-m-d H:i:S",
format: "cccc, d LLLL yyyy, HH:mm ZZZZ" # Wednesday, 10 February 1988, 16:00 GMT
field :user, as: :belongs_to, use_resource: Avo::Resources::CompactUser
field :user, as: :belongs_to, use_resource: Avo::Resources::CompactUser, link_to_record: true
field :commentable, as: :belongs_to, polymorphic_as: :commentable, types: [::Post, ::Project]
end

Expand Down
13 changes: 13 additions & 0 deletions spec/features/avo/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@
let!(:post) { create :post, user: admin }

it { is_expected.to have_text admin.name }
it { is_expected.to have_link admin.name, href: "/admin/resources/users/#{admin.slug}" }
end

describe "with a related user with link to record enabled" do
let!(:user) { create :user, first_name: "Alicia" }
let!(:comment) { create :comment, body: "a comment", user: user }

subject do
visit "/admin/resources/comments"
find("[data-resource-id='#{comment.to_param}'] [data-field-id='user']")
end

it { is_expected.to have_link user.name, href: "/admin/resources/comments/#{comment.id}" }
end

describe "without a related user" do
Expand Down
Loading