Skip to content

Commit

Permalink
Merge branch 'main' into refactor/plugin-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Sep 29, 2024
2 parents 0ec2c52 + f9d12f5 commit a78c86b
Show file tree
Hide file tree
Showing 40 changed files with 423 additions and 333 deletions.
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ group :development do

gem "actual_db_schema"

gem "derailed_benchmarks", "~> 2.1"
gem "derailed_benchmarks", "~> 2.1", ">= 2.1.2"

# gem "ruby-statistics", "< 4"
# Keep version look on <4 until this PR gets merged and released
# https://github.com/zombocom/derailed_benchmarks/pull/239
gem "ruby-statistics", "< 4"
end

group :test do
Expand Down
25 changes: 13 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ GIT

GIT
remote: https://github.com/rails/rails.git
revision: 219e75354b516af0144035a49abcbfa6a380a2f2
revision: b88d9af34fbc1c84ce2769ba02584eab2c28ac6e
branch: main
specs:
actioncable (8.0.0.alpha)
Expand Down Expand Up @@ -158,17 +158,17 @@ GEM
money-rails (~> 1.12)
avo-record_link_field (0.0.1)
aws-eventstream (1.3.0)
aws-partitions (1.977.0)
aws-sdk-core (3.206.0)
aws-partitions (1.979.0)
aws-sdk-core (3.209.1)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.9)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.91.0)
aws-sdk-core (~> 3, >= 3.205.0)
aws-sdk-kms (1.94.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sigv4 (~> 1.5)
aws-sdk-s3 (1.163.0)
aws-sdk-core (~> 3, >= 3.205.0)
aws-sdk-s3 (1.166.0)
aws-sdk-core (~> 3, >= 3.207.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.5)
aws-sigv4 (1.10.0)
Expand Down Expand Up @@ -404,8 +404,8 @@ GEM
mapkick-rb (0.1.5)
marcel (1.0.4)
matrix (0.4.2)
memory_profiler (1.0.2)
meta-tags (2.22.0)
memory_profiler (1.1.0)
meta-tags (2.22.1)
actionpack (>= 6.0.0, < 8.1)
method_source (1.1.0)
mini_histogram (0.3.1)
Expand Down Expand Up @@ -631,7 +631,7 @@ GEM
tilt (2.4.0)
timeout (0.4.1)
tty-which (0.5.0)
turbo-rails (2.0.9)
turbo-rails (2.0.10)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
turbo_power (0.6.2)
Expand Down Expand Up @@ -661,7 +661,7 @@ GEM
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
webrick (1.8.1)
webrick (1.8.2)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
Expand Down Expand Up @@ -700,7 +700,7 @@ DEPENDENCIES
cuprite
database_cleaner-active_record
debug
derailed_benchmarks (~> 2.1)
derailed_benchmarks (~> 2.1, >= 2.1.2)
devise
dotenv-rails
erb-formatter
Expand Down Expand Up @@ -739,6 +739,7 @@ DEPENDENCIES
rspec-retry (~> 0.6.2)
rubocop
rubocop-shopify
ruby-statistics (< 4)
rubycritic
simplecov
simplecov-cobertura
Expand Down
38 changes: 28 additions & 10 deletions app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,33 @@ class Avo::ActionsComponent < Avo::BaseComponent
prop :icon, _Nilable(String)
prop :size, Avo::ButtonComponent::SIZE, default: :md
prop :title, _Nilable(String)
prop :color, Symbol, default: :primary
prop :color, _Nilable(Symbol) do |value|
value || :primary
end
prop :include, _Nilable(ACTION_FILTER), default: [].freeze do |include|
Array(include).to_set
end
prop :label, String do |label|
label || I18n.t("avo.actions")
prop :custom_list, _Boolean, default: false
prop :label, _Nilable(String) do |label|
if @custom_list
label
else
label || I18n.t("avo.actions")
end
end
prop :style, Avo::ButtonComponent::STYLE, default: :outline
prop :actions, _Array(Avo::BaseAction), default: [].freeze
prop :actions, _Array(_Any), default: [].freeze
prop :exclude, _Nilable(ACTION_FILTER), default: [].freeze do |exclude|
Array(exclude).to_set
end
prop :resource, _Nilable(Avo::BaseResource)
prop :view, _Nilable(Avo::ViewInquirer)
prop :host_component, _Nilable(_Any)

delegate_missing_to :@host_component

def after_initialize
filter_actions
filter_actions unless @custom_list
end

def render?
Expand Down Expand Up @@ -61,8 +71,8 @@ def on_index_page?
!on_record_page?
end

def icon(action)
svg action.icon, class: "h-5 shrink-0 mr-1 inline pointer-events-none"
def icon(icon)
svg icon, class: "h-5 shrink-0 mr-1 inline pointer-events-none"
end

def render_item(action)
Expand All @@ -71,6 +81,14 @@ def render_item(action)
render_divider(action)
when Avo::BaseAction
render_action_link(action)
when defined?(Avo::Advanced::Resources::Controls::Action) && Avo::Advanced::Resources::Controls::Action
render_action_link(action.action, icon: action.icon)
when defined?(Avo::Advanced::Resources::Controls::LinkTo) && Avo::Advanced::Resources::Controls::LinkTo
link_to action.args[:path],
class: action.args.delete(:class) || "flex items-center px-4 py-3 w-full text-black font-semibold text-sm hover:bg-primary-100",
**action.args.except(:path, :label, :icon) do
raw("#{icon(action.args[:icon])} #{action.args[:label]}")
end
end
end

Expand All @@ -81,19 +99,19 @@ def render_divider(action)
render Avo::DividerComponent.new(label)
end

def render_action_link(action)
def render_action_link(action, icon: nil)
link_to action.link_arguments(resource: @resource, arguments: action.arguments).first,
data: action_data_attributes(action),
title: action.action_name,
class: action_css_class(action) do
raw("#{icon(action)} #{action.action_name}")
raw("#{icon(icon || action.icon)} #{action.action_name}")
end
end

def action_data_attributes(action)
{
action_name: action.action_name,
"turbo-frame": Avo::ACTIONS_TURBO_FRAME_ID,
"turbo-frame": Avo::MODAL_FRAME_ID,
action: "click->actions-picker#visitAction",
"actions-picker-target": action.standalone ? "standaloneAction" : "resourceAction",
disabled: is_disabled?(action),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
icon: 'heroicons/outline/link',
color: :primary,
style: :text,
'data-turbo-frame': 'attach_modal' do %>
data: {
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do %>
<%= t('avo.attach_item', item: @field.name.humanize(capitalize: false)) %>
<% end %>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/components/avo/resource_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def render_attach_button(control)
color: :primary,
style: :text,
data: {
turbo_frame: :attach_modal,
turbo_frame: Avo::MODAL_FRAME_ID,
target: :attach
} do
control.label
Expand Down Expand Up @@ -282,7 +282,7 @@ def render_action(action)
title: action.title,
size: action.size,
data: {
turbo_frame: Avo::ACTIONS_TURBO_FRAME_ID,
turbo_frame: Avo::MODAL_FRAME_ID,
action_name: action.action.action_name,
tippy: action.title ? :tooltip : nil,
action: "click->actions-picker#visitAction",
Expand Down
7 changes: 3 additions & 4 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,13 @@ def respond
# Trigger download, removes modal and flash the messages
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
when :navigate_to_action
frame_id = Avo::ACTIONS_TURBO_FRAME_ID
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])

turbo_stream.turbo_frame_set_src(frame_id, src)
turbo_stream.turbo_frame_set_src(Avo::MODAL_FRAME_ID, src)
when :redirect
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
Expand All @@ -118,7 +117,7 @@ def respond
when :close_modal
# Close the modal and flash the messages
[
turbo_stream.close_action_modal,
turbo_stream.close_modal,
turbo_stream.flash_alerts
]
else
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ def index
@association_field = find_association_field(resource: @parent_resource, association: params[:related_name])

if @association_field.present? && @association_field.scope.present?
@query = Avo::ExecutionContext.new(target: @association_field.scope, query: @query, parent: @parent_record).handle
@query = Avo::ExecutionContext.new(
target: @association_field.scope,
query: @query,
parent: @parent_record,
resource: @resource,
parent_resource: @parent_resource
).handle
end

super
Expand Down Expand Up @@ -58,6 +64,16 @@ def new
[@attachment_resource.new(record: record).record_title, record.to_param]
end
end

@url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths("resources", params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
end

def create
Expand Down
14 changes: 11 additions & 3 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def new

# Handle special cases when creating a new record via a belongs_to relationship
if params[:via_belongs_to_resource_class].present?
return render turbo_stream: turbo_stream.append("attach_modal", partial: "avo/base/new_via_belongs_to")
return render turbo_stream: turbo_stream.append(Avo::MODAL_FRAME_ID, partial: "avo/base/new_via_belongs_to")
end

set_actions
Expand Down Expand Up @@ -305,8 +305,15 @@ def cast_nullable(params)
def set_index_params
@index_params = {}

# projects.has_many.users
if @related_resource.present?
key = "#{@record.to_global_id}.has_many.#{@resource.class.to_s.parameterize}"
session[key] = params[:page] || session[key]
page_from_session = session[key]
end

# Pagination
@index_params[:page] = params[:page] || 1
@index_params[:page] = params[:page] || page_from_session || 1
@index_params[:per_page] = Avo.configuration.per_page

if cookies[:per_page].present?
Expand Down Expand Up @@ -608,7 +615,8 @@ def set_component_for(view, fallback_view: nil)
end

def apply_pagination
@pagy, @records = @resource.apply_pagination(index_params: @index_params, query: pagy_query)
# Set `trim_extra` to false in associations so the first page has the `page=1` param assigned
@pagy, @records = @resource.apply_pagination(index_params: @index_params, query: pagy_query, trim_extra: @related_resource.blank?)
end

def apply_sorting
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def flash_alerts
template: @view_context.render(Avo::FlashAlertsComponent.new(flashes: @view_context.flash.discard))
end

def close_action_modal
def close_modal
turbo_stream_action_tag :replace,
target: Avo::ACTIONS_TURBO_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1})
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1})
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/javascript/js/controllers/actions_picker_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default class extends Controller {
}

get actionsShowTurboFrame() {
return document.querySelector('turbo-frame#actions_show')
return document.querySelector(`turbo-frame#${window.Avo.configuration.modal_frame_id}`)
}

enableTarget() {
Expand Down
2 changes: 1 addition & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<%= render Avo::LoadingComponent.new(title: "...") %>
<% end %>
<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID do %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<div
data-controller="<%= ["action", @action.get_stimulus_controllers].join(" ") %>"
data-action-no-confirmation-value="<%= @action.no_confirmation %>"
Expand Down
17 changes: 3 additions & 14 deletions app/views/avo/associations/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
<%= turbo_frame_tag 'attach_modal' do %>
<%
url = Avo::Services::URIService.parse(avo.root_url.to_s)
.append_paths('resources', params[:resource_name], params[:id], params[:related_name])
.append_query(
{
view: @resource&.view&.to_s,
for_attribute: @field&.try(:for_attribute)
}.compact
)
.to_s
%>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID do %>
<%= form_with scope: 'fields',
url: url,
url: @url,
local: true,
data: {
'turbo-frame': '_top'
turbo_frame: :_top
} do |form| %>
<%= render Avo::ModalComponent.new do |c| %>
<% c.with_heading do %>
Expand Down
1 change: 0 additions & 1 deletion app/views/avo/partials/_attach_modal.html.erb

This file was deleted.

1 change: 1 addition & 0 deletions app/views/avo/partials/_javascript.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
param_key: '<%= Avo::DynamicFilters.configuration.param_key %>'
}
<% end %>
Avo.configuration.modal_frame_id = '<%= ::Avo::MODAL_FRAME_ID %>'
<% end %>
3 changes: 1 addition & 2 deletions app/views/layouts/avo/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
</div>
</div>
</div>
<%= turbo_frame_tag Avo::ACTIONS_TURBO_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/attach_modal" %>
<%= turbo_frame_tag Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1} %>
<%= render partial: "avo/partials/alerts" %>
<%= render partial: "avo/partials/scripts" %>
<%= render partial: "avo/partials/confirm_dialog" %>
Expand Down
3 changes: 2 additions & 1 deletion gemfiles/rails_6.1_ruby_3.1.4.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ group :development do
gem "rubocop-shopify", require: false
gem "rubycritic", require: false
gem "actual_db_schema"
gem "derailed_benchmarks", "~> 2.1", group: :development
gem "derailed_benchmarks", "~> 2.1", ">= 2.1.2"
gem "ruby-statistics", "< 4"
end

group :test do
Expand Down
Loading

0 comments on commit a78c86b

Please sign in to comment.