Skip to content

Commit

Permalink
Merge branch 'main' into add_title_description_for_tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Dec 2, 2024
2 parents 409ef07 + f7c53f7 commit 19ed23c
Show file tree
Hide file tree
Showing 55 changed files with 229 additions and 348 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ GIT
PATH
remote: .
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
8 changes: 6 additions & 2 deletions app/components/avo/fields/trix_field/show_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
content_classes << ' hidden' unless @field.always_show
button_classes = 'font-bold inline-block pt-3'
%>
<div data-controller="trix-body">
<%= content_tag :div,
data: {
controller: "trix-body",
trix_body_always_show_value: @field.always_show
} do %>
<div class="<%= content_classes %>" data-trix-body-target="content">
<%= sanitize @field.value.to_s %>
</div>
Expand All @@ -16,5 +20,5 @@
<%= link_to t('avo.less_content'), 'javascript:void(0);', class: button_classes, data: { action: 'click->trix-body#toggleContent' } %>
</div>
<% end %>
</div>
<% end %>
<% end %>
12 changes: 6 additions & 6 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ def respond
turbo_response = case @response[:type]
when :keep_modal_open
# Only render the flash messages if the action keeps the modal open
turbo_stream.flash_alerts
turbo_stream.avo_flash_alerts
when :download
# Trigger download, removes modal and flash the messages
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_modal,
turbo_stream.flash_alerts
turbo_stream.avo_download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.avo_close_modal,
turbo_stream.avo_flash_alerts
]
when :navigate_to_action
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])
Expand All @@ -125,8 +125,8 @@ def respond
when :close_modal
# Close the modal and flash the messages
[
turbo_stream.close_modal,
turbo_stream.flash_alerts
turbo_stream.avo_close_modal,
turbo_stream.avo_flash_alerts
]
else
# Reload the page
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def reload_frame_turbo_streams
turbo_streams = super

# We want to close the modal if the user wants to add just one record
turbo_streams << turbo_stream.close_modal if params[:button] != "attach_another"
turbo_streams << turbo_stream.avo_close_modal if params[:button] != "attach_another"

turbo_streams
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def destroy_fail_action
flash[:error] = destroy_fail_message

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.flash_alerts }
format.turbo_stream { render turbo_stream: turbo_stream.avo_flash_alerts }
end
end

Expand Down Expand Up @@ -660,7 +660,7 @@ def sanitized_sort_direction
def reload_frame_turbo_streams
[
turbo_stream.turbo_frame_reload(params[:turbo_frame]),
turbo_stream.flash_alerts
turbo_stream.avo_flash_alerts
]
end
end
Expand Down
45 changes: 44 additions & 1 deletion app/controllers/avo/charts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,53 @@
module Avo
class ChartsController < BaseController
def distribution_chart
@values_summary = resource.model_class.group(params[:field_id]).reorder("count_all desc").count
@values_summary = summary_query.group(params[:field_id].to_sym).reorder("count_all desc").count

@field_id = params[:field_id]

render "avo/partials/distribution_chart", layout: "avo/blank"
end

private

def is_associated_summary
params[:via_record_id].present? &&
params[:via_resource_class].present? &&
params[:association_name].present?
end

def summary_query
is_associated_summary ? association_scope : resource.query_scope
end

def association_scope
parent_resource_class = Avo.resource_manager.get_resource(params[:via_resource_class])
parent_record = parent_resource_class.find_record(params[:via_record_id], params: params)

parent_resource = parent_resource_class.new(
record: parent_record,
# Explicitly hardcoding the view to 'show' as association summaries are processed solely within this context
view: Avo::ViewInquirer.new("show")
)

parent_resource.detect_fields

association_name = BaseResource.valid_association_name(parent_record, params[:association_name])

parent_field = find_association_field(resource: parent_resource, association: association_name)

association_query = parent_resource.authorization.apply_policy parent_record.send(association_name)

if parent_field.scope.present?
association_query = Avo::ExecutionContext.new(
target: parent_field.scope,
query: association_query,
parent: parent_record,
parent_resource: parent_resource
).handle
end

association_query
end
end
end
6 changes: 3 additions & 3 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module Avo
module TurboStreamActionsHelper
def download(content:, filename:)
def avo_download(content:, filename:)
turbo_stream_action_tag :download, content: content, filename: filename
end

def flash_alerts
def avo_flash_alerts
turbo_stream_action_tag :append,
target: "alerts",
template: @view_context.render(Avo::FlashAlertsComponent.new(flashes: @view_context.flash.discard))
end

def close_modal
def avo_close_modal
turbo_stream_action_tag :replace,
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ export default class extends Controller {
connect() {
if (this.attachmentsDisabledValue) {
// Remove the attachments button
this.controllerTarget.querySelector('.trix-button-group--file-tools').remove()
window.addEventListener('trix-initialize', (event) => {
if (event.target === this.editorTarget) {
this.controllerTarget.querySelector('.trix-button-group--file-tools').remove()
}
})
}

window.addEventListener('trix-file-accept', (event) => {
Expand Down
4 changes: 4 additions & 0 deletions app/javascript/js/controllers/trix_body_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = ['content', 'moreContentButton', 'lessContentButton']

static values = { alwaysShow: Boolean }

connect() {
if (this.alwaysShowValue) return // Ignore checking when alwaysShow is true

this.checkContentHeight()
}

Expand Down
18 changes: 10 additions & 8 deletions app/views/avo/partials/_table_header.html.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
<%
# Currently there isn't a way to summarize records on associations.
# We'd love to support this feature so please send in a PR.
should_summarize = @parent_record.blank?
%>

<thead
data-component-name="avo/partials/table_header"
class="bg-white border-b border-gray-200 pb-1">
Expand Down Expand Up @@ -83,14 +77,22 @@
<%= field.table_header_label %>
<% end %>
<% end %>
<% if field.summarizable && should_summarize %>
<% if field.summarizable %>
<div id="summary-header-<%= field.id %>" class="<%= text_classes %> cursor-pointer" data-action="click->toggle#togglePanel">
<%= svg 'heroicons/solid/chart-bar', class: 'h-3 ml-1' %>
</div>
<turbo-frame
id="summary-frame-<%= field.id %>"
loading="lazy"
src="<%= distribution_chart_path(resource_name: field.resource.model_class, field_id: field.id) %>"
src="<%= distribution_chart_path(
{
resource_name: field.resource.model_class,
field_id: field.id,
via_record_id: @parent_record&.to_param,
via_resource_class: @parent_resource&.class&.to_s,
association_name: @field&.for_attribute || @field&.id
}.compact
) %>"
class="absolute inset-auto bg-white w-full sm:w-[400px] top-0 mt-6 p-4 z-40 shadow-modal rounded overflow-hidden hidden border border-gray-50"
data-toggle-target="panel"
>
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
2 changes: 1 addition & 1 deletion gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PATH
PATH
remote: ..
specs:
avo (3.14.3)
avo (3.14.5)
actionview (>= 6.1)
active_link_to
activerecord (>= 6.1)
Expand Down
1 change: 1 addition & 0 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Avo
COOKIES_KEY = "avo"
MODAL_FRAME_ID = :modal_frame
ACTIONS_BACKGROUND_FRAME = :actions_background
CACHED_SVGS = {}

class LicenseVerificationTemperedError < StandardError; end

Expand Down
20 changes: 20 additions & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ def get_message
).handle
end

def cancel_button_label
Avo::ExecutionContext.new(
target: self.class.cancel_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end

def confirm_button_label
Avo::ExecutionContext.new(
target: self.class.confirm_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end

def handle_action(**args)
processed_fields = if args[:fields].present?
# Fetching the field definitions and not the actual fields (get_fields) because they will break if the user uses a `visible` block and adds a condition using the `params` variable. The params are different in the show method and the handle method.
Expand Down
8 changes: 5 additions & 3 deletions lib/avo/fields/file_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ def path

def value
final_value = super

# On edit view always show the persisted image. Related: issue#3008
if final_value.instance_of?(ActiveStorage::Attached::One) && view == "edit"
persisted_record = record.class.find_by(id: record.id)
final_value = persisted_record.send(@for_attribute || id)
if final_value.instance_of?(ActiveStorage::Attached::One) && @view.edit?
persisted_record = @resource.find_record(@record.to_param)
final_value = persisted_record.send(@for_attribute || @id)
end

final_value
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/avo/fields/trix_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def is_action_text?
private

def disable_attachments?(args)
# If we don't have an attachment_key, we disable attachments. There's no point in having
# attachments if we can't store them.
return false if args[:attachment_key].present?
# Return the value of attachments_disabled if explicitly provided
return args[:attachments_disabled] unless args[:attachments_disabled].nil?

args[:attachments_disabled] == true
# Disable attachments if attachment_key is not present
args[:attachment_key].blank?
end
end
end
Expand Down
12 changes: 7 additions & 5 deletions lib/avo/svg_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ def initialize(filename)

# Use the default static finder logic. If that doesn't find anything, search according to our pattern:
def pathname
found_asset = default_strategy
Avo::CACHED_SVGS[@filename] ||= begin
found_asset = default_strategy

# Use the found asset
return found_asset if found_asset.present?
# Use the found asset
return found_asset if found_asset.present?

paths.find do |path|
File.exist? path
paths.find do |path|
File.exist? path
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/avo/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Avo
VERSION = "3.14.3" unless const_defined?(:VERSION)
VERSION = "3.14.5" unless const_defined?(:VERSION)
end
11 changes: 0 additions & 11 deletions spec/components/avo/actions_component_spec.rb

This file was deleted.

Loading

0 comments on commit 19ed23c

Please sign in to comment.