activeadmin-hotwire_combobox_filters 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 50fd003a869037e3a173d2457fbf2b294166da458de471339cde16716fe4f64f
4
+ data.tar.gz: ea45045933bec40c20703a35c89040559a03b4aa79dde3865a1e549c9939dd70
5
+ SHA512:
6
+ metadata.gz: ad30fc1e85226f1bc6f0fefa0199c13945122c23e32441430940a138ef355b3684f7e2d096c8b1a5229cf60a76b645723cf902f5457692d1a4826dc5319289d1
7
+ data.tar.gz: 3eb7bf504703e5341b92fae5fc96be479f174a7b3ad2f2850b70d52503c9072db49700aa66a3321e9bea5920eb9f3488589cf71d9edd9a9b2572158f484c1c8e
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # ActiveAdmin Hotwire Combobox Filters
2
+
3
+ This gem adds a hotwire combobox to ActiveAdmin sidebar filters for related resources (`belongs_to`, `has_many`, etc.).
4
+
5
+ # Installation
6
+
7
+ ```
8
+ gem "activeadmin-hotwire_combobox_filters"
9
+ ```
10
+
11
+ # Usage
12
+
13
+ The gem automatically replaces the default ActiveAdmin filters with hotwire_combobox filters after being required.
14
+
15
+ # Thanks and credits
16
+
17
+ Thanks to @josefarias for the awesome [hotwire_combobox](https://github.com/josefarias/hotwire_combobox) gem.
18
+ Thanks to all the team at [ActiveAdmin](https://github.com/activeadmin/activeadmin) for the awesome gem.
@@ -0,0 +1,23 @@
1
+ <%= combobox_style_tag %>
2
+ <style>
3
+ [role='combobox'] {
4
+ background-color: inherit;
5
+ }
6
+ .dark:root {
7
+ --tw-border-opacity: 1;
8
+ --hw-border-color: rgb(75 85 99 / var(--tw-border-opacity));
9
+ --hw-active-bg-color: rgb(31 41 55/var(--tw-border-opacity));
10
+ --tw-bg-opacity: 1;
11
+ --hw-component-bg-color: rgb(55 65 81 / var(--tw-bg-opacity));
12
+ --hw-option-bg-color: rgb(55 65 81 / var(--tw-bg-opacity));
13
+
14
+ /* --hw-group-color: #57595C;
15
+ --hw-group-bg-color: #FFFFFF;
16
+ --hw-invalid-color: #EF4444;
17
+ --hw-dialog-label-color: #1D1D1D;
18
+ --hw-focus-color: #2563EB; */
19
+
20
+ /* --hw-handle-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3E%3C/svg%3E");
21
+ --hw-handle-image--queried: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24'%3E%3Cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M6 18 18 6M6 6l12 12'/%3E%3C/svg%3E"); */
22
+ }
23
+ </style>
@@ -0,0 +1 @@
1
+ <%= async_combobox_options(combobox_results, next_page:) %>
@@ -0,0 +1,23 @@
1
+ # rubocop:disable Naming/FileName
2
+ # rubocop:enable Naming/FileName
3
+ require "active_admin"
4
+ require "hotwire_combobox"
5
+
6
+ module ActiveAdminHotwireComboboxFilters
7
+ class Engine < ::Rails::Engine
8
+ config.to_prepare do
9
+ require "activeadmin_hotwire_combobox_filters/dsl"
10
+ ActiveAdmin::ResourceDSL.include(ActiveAdminHotwireComboboxFilters::DSL)
11
+
12
+ require "activeadmin_hotwire_combobox_filters/resource_extension"
13
+ ActiveAdmin::Resource.include(ActiveAdminHotwireComboboxFilters::ResourceExtension)
14
+
15
+ require "activeadmin_hotwire_combobox_filters/fieldset_override"
16
+ HotwireCombobox::Component::Markup::Fieldset.prepend(ActiveAdminHotwireComboboxFilters::FieldsetOverride)
17
+ end
18
+ end
19
+ end
20
+
21
+ require "activeadmin_hotwire_combobox_filters/inputs/filters_select_input"
22
+ require "activeadmin_hotwire_combobox_filters/inputs/hotwire_combobox_input"
23
+ require "activeadmin_hotwire_combobox_filters/form_builder_extension"
@@ -0,0 +1,43 @@
1
+ module ActiveAdminHotwireComboboxFilters
2
+ module DSL
3
+ DISPLAY_NAME_METHODS = %w[display_name full_name name username login title email to_s].freeze
4
+
5
+ def run_registration_block(&block) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
6
+ resource_class = config.resource_class
7
+ unless resource_class.respond_to?(:to_combobox_display)
8
+ resource_class.class_eval do
9
+ define_method :to_combobox_display do
10
+ method = DISPLAY_NAME_METHODS.find { |m| self.respond_to?(m) }
11
+ self.public_send(method) if method.present?
12
+ end
13
+ end
14
+ end
15
+
16
+ new_block = proc do
17
+ collection_action :combobox_search, method: :get do
18
+ method = (resource_class.attribute_aliases.keys & DISPLAY_NAME_METHODS).first
19
+ method = if resource_class.column_names.include?(resource_class.attribute_aliases[method])
20
+ resource_class.attribute_aliases[method]
21
+ else
22
+ (DISPLAY_NAME_METHODS & resource_class.column_names).first
23
+ end
24
+ if method.blank?
25
+ raise NoMethodError,
26
+ "No display method found for #{resource_class.name}. Methods searched: #{DISPLAY_NAME_METHODS.join(', ')}"
27
+ end
28
+
29
+ @records = Pundit.policy_scope!(current_user, resource_class).ransack("#{method}_cont" => params[:q]).result
30
+ .page(params[:page]).per(Kaminari.config.default_per_page)
31
+
32
+ @combobox_results = @records.pluck(method, :id)
33
+ @next_page = @records.next_page
34
+ render "activeadmin_hotwire_combobox_filters/combobox_search",
35
+ locals: { combobox_results: @combobox_results, next_page: @next_page }
36
+ end
37
+
38
+ instance_exec(&block) if block
39
+ end
40
+ instance_exec(&new_block)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveAdminHotwireComboboxFilters
2
+ module FieldsetOverride
3
+ private
4
+
5
+ def prefilled_display # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6
+ return if multiselect? || !hidden_field_value
7
+
8
+ if async_src && associated_object
9
+ associated_object.to_combobox_display
10
+ elsif async_src && form_object&.respond_to?(name) # rubocop:disable Lint/RedundantSafeNavigation
11
+ form_object.public_send name
12
+ # special case for filtering Ransack::Search object
13
+ elsif async_src && !form_object&.public_send(name).nil?
14
+ options&.find { |option| option.value == value }&.autocompletable_as || hidden_field_value
15
+ # end special case
16
+ else
17
+ options.find_by_value(hidden_field_value)&.autocompletable_as || hidden_field_value # rubocop:disable Rails/DynamicFindBy
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveAdmin
2
+ class FormBuilder < ::Formtastic::FormBuilder
3
+ def input(method, options = {})
4
+ options[:as] ||= :hotwire_combobox if association?(method)
5
+ super
6
+ end
7
+
8
+ private
9
+
10
+ def association?(method) = reflection_for(method) # &.macro == :belongs_to
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ ActiveAdmin::Inputs::Filters::SelectInput.class_eval do
2
+ def select_html # rubocop:disable Metrics/AbcSize
3
+ current_value = object.public_send(input_name)
4
+
5
+ options = []
6
+ if current_value.present?
7
+ value_record = Pundit.policy_scope!(Current.user, reflection.klass).find_by(id: current_value)
8
+ options = HotwireCombobox::Listbox::Item.collection_for(
9
+ template, [value_record], render_in: {}, include_blank: nil, display: :to_combobox_display
10
+ )
11
+ end
12
+
13
+ path = template.public_send(:"combobox_search_admin_#{reflection.klass.name.underscore.pluralize}_path")
14
+
15
+ builder.combobox(input_name, path, value: current_value, options:)
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveAdmin
2
+ module Inputs
3
+ class HotwireComboboxInput < Formtastic::Inputs::SelectInput
4
+ def select_html
5
+ path = template.public_send(:"combobox_search_admin_#{reflection.klass.name.underscore.pluralize}_path")
6
+
7
+ template.render(partial: "activeadmin_hotwire_combobox_filters/combobox_styles") <<
8
+ builder.combobox(input_name, path)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveAdminHotwireComboboxFilters
2
+ module ResourceExtension
3
+ def filters_sidebar_section
4
+ ActiveAdmin::SidebarSection.new :filters, only: :index, if: -> { active_admin_config.filters.any? } do
5
+ render partial: "activeadmin_hotwire_combobox_filters/combobox_styles"
6
+
7
+ h3 I18n.t("active_admin.sidebars.filters", default: "Filters"), class: "filters-form-title"
8
+ active_admin_filters_form_for assigns[:search], **active_admin_config.filters
9
+ end
10
+ end
11
+ end
12
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin-hotwire_combobox_filters
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Viktor Fonic
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2025-01-05 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activeadmin
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 4.0.0.beta7
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 4.0.0.beta7
26
+ - !ruby/object:Gem::Dependency
27
+ name: hotwire_combobox
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rails
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '7.0'
47
+ type: :runtime
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '7.0'
54
+ description: Adds Hotwire-powered combobox filters to ActiveAdmin
55
+ email:
56
+ - activeadmin-hotwire_combobox_filters@ada-dev.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - README.md
62
+ - app/views/activeadmin_hotwire_combobox_filters/_combobox_styles.html.erb
63
+ - app/views/activeadmin_hotwire_combobox_filters/combobox_search.turbo_stream.erb
64
+ - lib/activeadmin-hotwire_combobox_filters.rb
65
+ - lib/activeadmin_hotwire_combobox_filters/dsl.rb
66
+ - lib/activeadmin_hotwire_combobox_filters/fieldset_override.rb
67
+ - lib/activeadmin_hotwire_combobox_filters/form_builder_extension.rb
68
+ - lib/activeadmin_hotwire_combobox_filters/inputs/filters_select_input.rb
69
+ - lib/activeadmin_hotwire_combobox_filters/inputs/hotwire_combobox_input.rb
70
+ - lib/activeadmin_hotwire_combobox_filters/resource_extension.rb
71
+ homepage: https://github.com/vfonic/activeadmin-hotwire_combobox_filters
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ rubygems_mfa_required: 'true'
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubygems_version: 3.6.2
91
+ specification_version: 4
92
+ summary: Hotwire Combobox filters for ActiveAdmin
93
+ test_files: []