diff --git a/Gemfile b/Gemfile index a6679f069..8a54e1751 100644 --- a/Gemfile +++ b/Gemfile @@ -76,6 +76,7 @@ gem 'pundit' # super admin gem 'administrate', '>= 0.19.0' gem 'administrate-field-active_storage' +gem 'administrate-field-belongs_to_search' ##--- gems for pubsub service ---## # https://karolgalanciak.com/blog/2019/11/30/from-activerecord-callbacks-to-publish-slash-subscribe-pattern-and-event-driven-design/ diff --git a/Gemfile.lock b/Gemfile.lock index bf109b551..264f65389 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,6 +116,11 @@ GEM administrate-field-active_storage (0.4.2) administrate (>= 0.2.2) rails (>= 7.0) + administrate-field-belongs_to_search (0.8.0) + administrate (>= 0.3, < 1.0) + jbuilder (~> 2) + rails (>= 4.2, < 7.1) + selectize-rails (~> 0.6) annotate (3.2.0) activerecord (>= 3.2, < 8.0) rake (>= 10.4, < 14.0) @@ -831,6 +836,7 @@ DEPENDENCIES acts-as-taggable-on administrate (>= 0.19.0) administrate-field-active_storage + administrate-field-belongs_to_search annotate attr_extras audited (~> 5.3) diff --git a/app/dashboards/account_dashboard.rb b/app/dashboards/account_dashboard.rb index fea975c0a..de6ab42bb 100644 --- a/app/dashboards/account_dashboard.rb +++ b/app/dashboards/account_dashboard.rb @@ -18,8 +18,8 @@ class AccountDashboard < Administrate::BaseDashboard end ATTRIBUTE_TYPES = { - id: Field::Number, - name: Field::String, + id: Field::Number.with_options(searchable: true), + name: Field::String.with_options(searchable: true), created_at: Field::DateTime, updated_at: Field::DateTime, users: CountField, diff --git a/app/dashboards/account_user_dashboard.rb b/app/dashboards/account_user_dashboard.rb index d757f4da5..9e8b2c48b 100644 --- a/app/dashboards/account_user_dashboard.rb +++ b/app/dashboards/account_user_dashboard.rb @@ -8,9 +8,9 @@ class AccountUserDashboard < Administrate::BaseDashboard # which determines how the attribute is displayed # on pages throughout the dashboard. ATTRIBUTE_TYPES = { - account: Field::BelongsTo.with_options(searchable: true, searchable_field: 'name', order: 'id DESC'), - user: Field::BelongsTo.with_options(searchable: true, searchable_field: 'name', order: 'id DESC'), - inviter: Field::BelongsTo.with_options(class_name: 'User', searchable: true, searchable_field: 'name'), + account: Field::BelongsToSearch.with_options(class_name: 'Account', searchable: true, searchable_field: [:name, :id], order: 'id DESC'), + user: Field::BelongsToSearch.with_options(class_name: 'User', searchable: true, searchable_field: [:name, :email, :id], order: 'id DESC'), + inviter: Field::BelongsToSearch.with_options(class_name: 'User', searchable: true, searchable_field: [:name, :email, :id], order: 'id DESC'), id: Field::Number, role: Field::Select.with_options(collection: AccountUser.roles.keys), created_at: Field::DateTime, diff --git a/app/dashboards/user_dashboard.rb b/app/dashboards/user_dashboard.rb index 9573d8447..e00d06a72 100644 --- a/app/dashboards/user_dashboard.rb +++ b/app/dashboards/user_dashboard.rb @@ -9,7 +9,7 @@ class UserDashboard < Administrate::BaseDashboard # on pages throughout the dashboard. ATTRIBUTE_TYPES = { account_users: Field::HasMany, - id: Field::Number, + id: Field::Number.with_options(searchable: true), avatar_url: AvatarField, avatar: Field::ActiveStorage.with_options( destroy_url: proc do |_namespace, _resource, attachment| @@ -28,9 +28,9 @@ class UserDashboard < Administrate::BaseDashboard confirmed_at: Field::DateTime, confirmation_sent_at: Field::DateTime, unconfirmed_email: Field::String, - name: Field::String, + name: Field::String.with_options(searchable: true), display_name: Field::String, - email: Field::String, + email: Field::String.with_options(searchable: true), tokens: Field::String.with_options(searchable: false), created_at: Field::DateTime, updated_at: Field::DateTime, diff --git a/app/views/fields/belongs_to_search/_index.html.erb b/app/views/fields/belongs_to_search/_index.html.erb new file mode 100644 index 000000000..3f327b4b2 --- /dev/null +++ b/app/views/fields/belongs_to_search/_index.html.erb @@ -0,0 +1,28 @@ +<%# +# BelongsToSearch Index Partial + +This partial renders a belongs_to relationship, +to be displayed on a resource's index page. + +By default, the relationship is rendered as a link to the associated object. + +## Local variables: + +- `field`: + An instance of Administrate::Field::BelongsToSearch. + A wrapper around the belongs_to relationship pulled from the database. +%> + +<% if field.data %> + <% if field.data.is_a? User %> + <%= link_to( + field.display_associated_resource, + super_admin_user_path(field.data), + ) %> + <% elsif existing_action?(field.associated_class, :show) %> + <%= link_to( + field.display_associated_resource, + [namespace, field.data], + ) %> + <% end %> +<% end %>