Skip to content

Commit

Permalink
feature: add meta to field (#2874)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianthedev authored Jun 19, 2024
1 parent 293290c commit 531df70
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/avo/fields/base_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def initialize(id, **args, &block)
@action = args[:action]
@components = args[:components] || {}
@for_attribute = args[:for_attribute]
@meta = args[:meta]

@args = args

Expand Down Expand Up @@ -281,6 +282,10 @@ def form_field_label
id
end

def meta
Avo::ExecutionContext.new(target: @meta, record: record, resource: @resource, view: @view).handle
end

private

def model_or_class(model)
Expand Down
12 changes: 10 additions & 2 deletions spec/dummy/app/avo/resources/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@ class Avo::Resources::Event < Avo::BaseResource

def fields
field :name, as: :text, link_to_record: true, sortable: true, stacked: true
field :first_user, as: :record_link
field :first_user,
as: :record_link,
meta: -> {
:foo
}
field :event_time, as: :datetime, sortable: true
field :body, as: :trix
field :body,
as: :trix,
meta: {
foo: :bar,
}

if params[:show_location_field] == "1"
# Example for error message when resource is missing
Expand Down
15 changes: 15 additions & 0 deletions spec/features/avo/fields_meta_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe Avo::EventsController, type: :controller do
it "finds the meta value as a hash" do
get :index

expect(assigns(:resource).get_field(:body).meta).to eq({foo: :bar})
end

it "finds the meta value as a block" do
get :index

expect(assigns(:resource).get_field(:first_user).meta).to eq(:foo)
end
end

0 comments on commit 531df70

Please sign in to comment.