File tree Expand file tree Collapse file tree 3 files changed +88
-1
lines changed
lib/activeadmin_addons/addons Expand file tree Collapse file tree 3 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -70,3 +70,26 @@ show do
70
70
end
71
71
end
72
72
```
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
+ ```
Original file line number Diff line number Diff line change @@ -11,7 +11,10 @@ class StateBuilder < CustomBuilder
11
11
def render
12
12
raise "you need to install AASM gem first" unless defined? AASM
13
13
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
+ )
15
18
end
16
19
17
20
private
@@ -33,6 +36,23 @@ def machine_name
33
36
def class_bindings
34
37
@class_bindings ||= DEFAULT_CLASS_BINDINGS . merge ( options [ :states ] || { } )
35
38
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
36
56
end
37
57
end
38
58
Original file line number Diff line number Diff line change 71
71
expect ( page ) . to have_css ( '.stock' )
72
72
end
73
73
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
74
118
end
You can’t perform that action at this time.
0 commit comments