Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add title handling to StateBuilder #487

Open
wants to merge 1 commit into
base: v1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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