Skip to content

Commit 3c538c7

Browse files
committed
Add title handling to StateBuilder
1 parent bea9423 commit 3c538c7

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
### Unreleased
6+
7+
* Add title handling to StateBuilder [#487](https://github.com/platanus/activeadmin_addons/pull/487)
8+
59
### 1.10.1
610

711
* Backport [#477](https://github.com/platanus/activeadmin_addons/pull/477) to have ActiveAdmin v3 compatibility [#479](https://github.com/platanus/activeadmin_addons/pull/479)

docs/aasm_integration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,26 @@ show do
7070
end
7171
end
7272
```
73+
74+
## Adding a title
75+
76+
You can add a static text title to the state column:
77+
78+
```ruby
79+
state_column :state, title: "Changes when ..."
80+
state_row :state, title: "Changes when ..."
81+
```
82+
83+
You can use model field as a title:
84+
85+
```ruby
86+
state_column :state, title: :state_changed_at
87+
state_row :state, title: :state_changed_at
88+
```
89+
90+
You can use a proc as a title:
91+
92+
```ruby
93+
state_column :state, title: ->(model) { model.change_reason && "Reason: #{model.change_reason}" }
94+
state_row :state, title: ->(model) { model.change_reason && "Reason: #{model.change_reason}" }
95+
```

lib/activeadmin_addons/addons/state_builder.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class StateBuilder < CustomBuilder
1111
def render
1212
raise "you need to install AASM gem first" unless defined? AASM
1313
raise "the #{attribute} is not an AASM state" unless state_attribute?
14-
context.status_tag(model.aasm(machine_name).human_state, class: status_class_for_model)
14+
15+
context.status_tag(
16+
model.aasm(machine_name).human_state, class: status_class_for_model, title: title
17+
)
1518
end
1619

1720
private
@@ -33,6 +36,23 @@ def machine_name
3336
def class_bindings
3437
@class_bindings ||= DEFAULT_CLASS_BINDINGS.merge(options[:states] || {})
3538
end
39+
40+
def title
41+
return @title if defined? @title
42+
43+
title = options.fetch(:title, nil)
44+
@title =
45+
case title
46+
when String, nil
47+
title
48+
when Symbol
49+
model.send(title)
50+
when Proc
51+
title.call(model)
52+
else
53+
raise "Invalid title type: #{title.class}. It must be a String, Symbol or Proc."
54+
end
55+
end
3656
end
3757
end
3858

spec/features/state_builder_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,48 @@
7171
expect(page).to have_css('.stock')
7272
end
7373
end
74+
75+
context "passing title as string" do
76+
before do
77+
register_show(Invoice) do
78+
state_row(:aasm_state, title: 'Enigmatic')
79+
end
80+
81+
visit admin_invoice_path(create_invoice)
82+
end
83+
84+
it "shows title" do
85+
expect(page).to have_selector('span[title="Enigmatic"]')
86+
end
87+
end
88+
89+
context "passing title as string" do
90+
before do
91+
register_show(Invoice) do
92+
state_row(:aasm_state, title: :shipping_status)
93+
end
94+
95+
visit admin_invoice_path(create_invoice)
96+
end
97+
98+
it "shows title" do
99+
expect(page).to have_selector('span[title="stock"]')
100+
end
101+
end
102+
103+
context "passing title as proc" do
104+
before do
105+
register_show(Invoice) do
106+
state_row(:aasm_state, title: lambda { |invoice|
107+
invoice.shipping_status && "Shipping: #{invoice.shipping_status.humanize}"
108+
})
109+
end
110+
111+
visit admin_invoice_path(create_invoice)
112+
end
113+
114+
it "shows title" do
115+
expect(page).to have_selector('span[title="Shipping: Stock"]')
116+
end
117+
end
74118
end

0 commit comments

Comments
 (0)