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
7070 end
7171end
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+ ```
Original file line number Diff line number Diff 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
3757end
3858
Original file line number Diff line number Diff line change 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
74118end
You can’t perform that action at this time.
0 commit comments