diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b73d75a..74a7e46d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +### Unreleased + +* Add title handling to StateBuilder [#487](https://github.com/platanus/activeadmin_addons/pull/487) + ### 1.10.1 * Backport [#477](https://github.com/platanus/activeadmin_addons/pull/477) to have ActiveAdmin v3 compatibility [#479](https://github.com/platanus/activeadmin_addons/pull/479) diff --git a/docs/aasm_integration.md b/docs/aasm_integration.md index 427e57df..d74b35d4 100644 --- a/docs/aasm_integration.md +++ b/docs/aasm_integration.md @@ -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}" } +``` diff --git a/lib/activeadmin_addons/addons/state_builder.rb b/lib/activeadmin_addons/addons/state_builder.rb index d0e6c222..aeecbd78 100644 --- a/lib/activeadmin_addons/addons/state_builder.rb +++ b/lib/activeadmin_addons/addons/state_builder.rb @@ -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 @@ -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 diff --git a/spec/features/state_builder_spec.rb b/spec/features/state_builder_spec.rb index 84d51c54..dab9f7a6 100644 --- a/spec/features/state_builder_spec.rb +++ b/spec/features/state_builder_spec.rb @@ -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