Skip to content

Commit

Permalink
Add title handling to StateBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mpapis committed Nov 4, 2023
1 parent bea9423 commit 8ebf279
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
23 changes: 23 additions & 0 deletions docs/aasm_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,26 @@ show do
end
end
```

## Adding a title

You can add a static text title to the state column:

```ruby
state_column :state, title: "Changes when ..."
state_row :state, title: "Changes when ..."
```

You can use model field as a title:

```ruby
state_column :state, title: :state_changed_at
state_row :state, title: :state_changed_at
```

You can use a proc as a title:

```ruby
state_column :state, title: ->(model) { model.change_reason && "Reason: #{model.change_reason}" }
state_row :state, title: ->(model) { model.change_reason && "Reason: #{model.change_reason}" }
```
22 changes: 21 additions & 1 deletion lib/activeadmin_addons/addons/state_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ class StateBuilder < CustomBuilder
def render
raise "you need to install AASM gem first" unless defined? AASM
raise "the #{attribute} is not an AASM state" unless state_attribute?
context.status_tag(model.aasm(machine_name).human_state, class: status_class_for_model)

context.status_tag(
model.aasm(machine_name).human_state, class: status_class_for_model, title: title
)
end

private
Expand All @@ -33,6 +36,23 @@ def machine_name
def class_bindings
@class_bindings ||= DEFAULT_CLASS_BINDINGS.merge(options[:states] || {})
end

def title
return @title if defined? @title

title = options.fetch(:title, nil)
@title =
case title
when String, nil
title
when Symbol
model.send(title)
when Proc
title.call(model)
else
raise "Invalid title type: #{title.class}. It must be a String, Symbol or Proc."
end
end
end
end

Expand Down
44 changes: 44 additions & 0 deletions spec/features/state_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,48 @@
expect(page).to have_css('.stock')
end
end

context "passing title as string" do
before do
register_show(Invoice) do
state_row(:aasm_state, title: 'Enigmatic')
end

visit admin_invoice_path(create_invoice)
end

it "shows title" do
expect(page).to have_selector('span[title="Enigmatic"]')
end
end

context "passing title as string" do
before do
register_show(Invoice) do
state_row(:aasm_state, title: :shipping_status)
end

visit admin_invoice_path(create_invoice)
end

it "shows title" do
expect(page).to have_selector('span[title="stock"]')
end
end

context "passing title as proc" do
before do
register_show(Invoice) do
state_row(:aasm_state, title: lambda { |invoice|
invoice.shipping_status && "Shipping: #{invoice.shipping_status.humanize}"
})
end

visit admin_invoice_path(create_invoice)
end

it "shows title" do
expect(page).to have_selector('span[title="Shipping: Stock"]')
end
end
end

0 comments on commit 8ebf279

Please sign in to comment.