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

record_link format_using doesn't work #2747

Open
4 of 11 tasks
xeron opened this issue May 7, 2024 · 7 comments
Open
4 of 11 tasks

record_link format_using doesn't work #2747

xeron opened this issue May 7, 2024 · 7 comments
Labels

Comments

@xeron
Copy link

xeron commented May 7, 2024

Describe the bug

This works:

field :item, as: :record_link, add_via_params: false, name: 'Item', only_on: %i[index show]

This doesn't:

field :item, as: :record_link, add_via_params: false, name: 'Item', only_on: %i[index show],
  format_using: -> { record.item_id }

Whatever I use under format_using, even if just a string – format_using: -> { 'test' } – an exception happens:

Showing /<REDACTED>/vendor/bundle/ruby/3.1.0/gems/avo-3.7.0/app/views/avo/base/index.html.erb where line #2 raised:

undefined method `new' for nil:NilClass

    @label ||= resource_class.new(record: @record)&.record_title

Steps to Reproduce

N/A

Expected behavior & Actual behavior

Record link is generated but instead of using record title it uses format_using output.

Models and resource files

N/A

System configuration

Avo version: 3.7.0
Rails version: 7.1.3.2
Ruby version: 3.1.5

License type:

  • Community
  • Pro
  • Advanced

Are you using Avo monkey patches, overriding views or view components?

  • Yes. If so, please post code samples.
  • No

Screenshots or screen recordings

N/A

Additional context

N/A

Impact

  • High impact (It makes my app un-usable.)
  • Medium impact (I'm annoyed, but I'll live.)
  • Low impact (It's really a tiny thing that I could live with.)

Urgency

  • High urgency (I can't continue development without it.)
  • Medium urgency (I found a workaround, but I'd love to have it fixed.)
  • Low urgency (It can wait. I just wanted you to know about it.)
@adrianthedev
Copy link
Collaborator

Hey @xeron.
Instead of returning record.item_id return an actual record. An ActiveRecord object, not an association foreign_key.
For you it might be something like record.item.

Docs

@xeron
Copy link
Author

xeron commented May 7, 2024

Hey @xeron. Instead of returning record.item_id return an actual record. An ActiveRecord object, not an association foreign_key. For you it might be something like record.item.

Docs

My goal is to provide the link to record.item but format the visual representation as ID instead of record.title (default). So I'm trying to use format_using (https://docs.avohq.io/3.0/field-options.html) and it doesn't work.

My understanding is format_using should not affect the link itself in any way. It could be any string.

@adrianthedev
Copy link
Collaborator

That is partially correct. format_using will output the value of the block, not the label of that record.
So when you tell it format_using: -> { record.item_id }, the value becomes an integer and it will try to create a link from that integer and fail.
It expects it to receive a record.
You can see the implementation here https://github.com/avo-hq/avo-record_link_field

What it could use is a field option where you can specify which property to use, somewhere around here.

# maybe something like this?

field :item, as: :record_link, record_property: :item_id

Would you be able to try to do that in a PR?

@xeron
Copy link
Author

xeron commented May 7, 2024

So when you tell it format_using: -> { record.item_id }, the value becomes an integer and it will try to create a link from that integer and fail.

From my understanding of format_using and other field options that should not be the case. Field accepts a block to generate the value for the functionality you described, right? E.g. this should be possible:

field :item, as: :record_link, add_via_params: false, name: 'Item', only_on: %i[index show],
  format_using: -> { record.item_id || 'or whatever I want here' } do
    record.item # this is default of course because field first argument is already `:item`, but I can use any logic here
end

Returned value from format_using should be used for link_to label directly, without affecting target or trying to call resource_class.new(record: @record)&.record_title on it.

@xeron
Copy link
Author

xeron commented May 7, 2024

I took a look at the code and seems like it happens because in the context of the RecordLink field the record comes from @field.value. Even if we use value as a label it won't work because local context record will be wrong too.

This made me realize that format_using probably doesn't work for belongs_to fields either, and it indeed doesn't:

field :car_class, as: :belongs_to, format_using: -> { 'test' }
undefined method `name' for "test":String

        @record.send title
               ^^^^^

@adrianthedev
Copy link
Collaborator

That's correct. It won't work for belongs_to either.

Think about it like this:

  • the block you pass at the end of the field (which makes it a computed field) sets the value
  • format_using formats what the value of that field is

So, for the record_link field, the value is a record. that's it. not what's extracted from the record as a label, or what is used from it to build the link. Just the record.
Then, it is passed to format_using as the value and you can take from it what you want to display on the page.

Copy link
Contributor

This issue has been marked as stale because there was no activity for the past 15 days.

@github-actions github-actions bot added the Stale label May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants