Skip to content

Commit

Permalink
feature: multi action flow (#1994)
Browse files Browse the repository at this point in the history
* feature: multi action flow

* spec

* mv actions_filter_spec.rb from feature to system

* add turbo_power as dependency & bundle exec appraisal

* revert unnecessary changes
  • Loading branch information
Paul-Bob authored Nov 1, 2023
1 parent ec68e3a commit 3f6110b
Show file tree
Hide file tree
Showing 25 changed files with 182 additions and 13 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,5 @@ gem "image_processing", "~> 1.12"
gem "prefixed_ids"

gem "mapkick-rb", "~> 0.1.4"

gem "turbo_power", "~> 0.5.0"
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -419,6 +420,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.0)
Expand Down Expand Up @@ -518,6 +521,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
17 changes: 8 additions & 9 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,18 @@ def respond(response)
end

respond_to do |format|
format.html do
format.turbo_stream do
# Flash the messages collected from the action
flash_messages messages

if response[:type] == :redirect
path = response[:path]

if path.respond_to? :call
path = instance_eval(&path)
end

redirect_to path, **{allow_other_host: response[:allow_other_host], status: response[:status]}.compact
elsif response[:type] == :reload
render turbo_stream: turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: response[:path]).handle,
nil,
response[:redirect_args][:turbo_frame],
**response[:redirect_args].except(:turbo_frame)
)
else
redirect_back fallback_location: resources_path(resource: @resource)
end
end
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/js/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import 'mapkick/bundle'

import { Alert, Popover } from 'tailwindcss-stimulus-components'
import { Application } from '@hotwired/stimulus'
import TurboPower from 'turbo_power'

TurboPower.initialize(Turbo.StreamActions)

const application = Application.start()

Expand Down
1 change: 1 addition & 0 deletions avo.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "active_link_to"
spec.add_dependency "view_component", ">= 2.54.0"
spec.add_dependency "turbo-rails"
spec.add_dependency "turbo_power", "~> 0.5.0"
spec.add_dependency "addressable"
spec.add_dependency "meta-tags"
spec.add_dependency "dry-initializer"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.0_ruby_3.0.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_6.0_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -413,6 +414,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (1.2.11)
thread_safe (~> 0.1)
unaccent (0.4.0)
Expand Down Expand Up @@ -514,6 +517,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.0_ruby_3.2.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_6.0_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -413,6 +414,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (1.2.11)
thread_safe (~> 0.1)
unaccent (0.4.0)
Expand Down Expand Up @@ -514,6 +517,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.1_ruby_3.0.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_6.1_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -416,6 +417,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
Expand Down Expand Up @@ -517,6 +520,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_6.1_ruby_3.2.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_6.1_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -416,6 +417,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
Expand Down Expand Up @@ -517,6 +520,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7.0_ruby_3.0.3.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_7.0_ruby_3.0.3.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -422,6 +423,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
Expand Down Expand Up @@ -523,6 +526,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
1 change: 1 addition & 0 deletions gemfiles/rails_7.0_ruby_3.2.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ gem "sprockets-rails"
gem "image_processing", "~> 1.12"
gem "prefixed_ids"
gem "mapkick-rb", "~> 0.1.4"
gem "turbo_power", "~> 0.5.0"

group :development do
gem "standard"
Expand Down
4 changes: 4 additions & 0 deletions gemfiles/rails_7.0_ruby_3.2.2.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PATH
meta-tags
pagy
turbo-rails
turbo_power (~> 0.5.0)
view_component (>= 2.54.0)
zeitwerk (>= 2.6.2)

Expand Down Expand Up @@ -422,6 +423,8 @@ GEM
actionpack (>= 6.0.0)
activejob (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.5.0)
turbo-rails (~> 1.3)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
Expand Down Expand Up @@ -523,6 +526,7 @@ DEPENDENCIES
sprockets-rails
standard
test-prof
turbo_power (~> 0.5.0)
tzinfo-data
web-console (>= 3.3.0)
webdrivers (>= 5.3.0)
Expand Down
5 changes: 2 additions & 3 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ def silent
self
end

def redirect_to(path = nil, allow_other_host: nil, status: nil, &block)
def redirect_to(path = nil, **args, &block)
response[:type] = :redirect
response[:allow_other_host] = allow_other_host
response[:status] = status
response[:redirect_args] = args
response[:path] = if block.present?
block
else
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"tailwindcss-stimulus-components": "^3.0.3",
"tippy.js": "^6.3.0",
"trix": "^2.0.5",
"turbo_power": "^0.5.0",
"urijs": "^1.19.11"
},
"devDependencies": {
Expand Down
27 changes: 27 additions & 0 deletions spec/dummy/app/avo/actions/pre_update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PreUpdate < Avo::BaseAction
self.name = "Update"
self.message = "Set the fields you want to update."

with_options as: :boolean do
field :first_name
field :last_name
field :user_email
field :active
field :admin
end

def handle(**args)
arguments = Base64.encode64 Avo::Services::EncryptionService.encrypt(
message: {
render_first_name: args[:fields][:first_name],
render_last_name: args[:fields][:last_name],
render_user_email: args[:fields][:user_email],
render_active: args[:fields][:active],
render_admin: args[:fields][:admin]
},
purpose: :action_arguments
)

redirect_to "/admin/resources/users/actions?action_id=Update&arguments=#{arguments}", turbo_frame: "actions_show"
end
end
34 changes: 34 additions & 0 deletions spec/dummy/app/avo/actions/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Update < Avo::BaseAction
self.name = "Update"
self.message = ""
self.visible = -> do
false
end

{
first_name: :text,
last_name: :text,
user_email: :text,
active: :boolean,
admin: :boolean
}.each do |field_name, field_type|
field field_name.to_sym, as: field_type, visible: -> (resource:) {
Avo::Services::EncryptionService.decrypt(
message: Base64.decode64(resource.params[:arguments]),
purpose: :action_arguments
).dig("render_#{field_name}".to_sym)
}
end

def handle(models:, fields:, **args)
non_roles_fields = fields.slice!(:admin)

models.each { |model| model.update!(non_roles_fields) }

fields.each do |field_name, field_value|
models.each { |model| model.update! roles: model.roles.merge!({"#{field_name}": field_value}) }
end

succeed "User(s) updated!"
end
end
2 changes: 2 additions & 0 deletions spec/dummy/app/avo/resources/user_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class UserResource < Avo::BaseResource
action ToggleAdmin
action Sub::DummyAction
action DownloadFile
action PreUpdate
action Update

filter UserNamesFilter
filter IsAdmin
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

RSpec.feature "Actions", type: :feature do
RSpec.feature "Actions", type: :system do
it "shows the different types of alerts" do
visit "/admin/resources/users"

Expand Down
Loading

0 comments on commit 3f6110b

Please sign in to comment.