effective_resources 2.25.16 → 2.26.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d03ec0448b11d0b47522003a6c0f5579d590ab74ea6b96090cfc9f32b858b1f5
4
- data.tar.gz: a2e35b26e825cc7c8dbf5f5db0e7670e956b2fe958636533b3ffea2439860c62
3
+ metadata.gz: 76931ae3422d0b14af4c84ed8670f0a03eba10ffb55804850d8d6aa23409d947
4
+ data.tar.gz: 9a8f17357dd11e73cc82070c20825fa63e89701ec80f1de679479c6b1f1123a9
5
5
  SHA512:
6
- metadata.gz: 787cf0d8f97f276fa8f8b4eef9b4676474a88faafb7aed72346eaba3bbfd55cf78fb9c1b409bcb6c8f828d6dbdbb3b13d7c8e68a0cb4ebd2ca09318367b49c39
7
- data.tar.gz: e807a9d2e7af907d14d62b8f665dce653c11072956c7e849306dcc72305ebf1ad016e88c9264a59258fee9bbaa53c0f39fd1c6fa8b3f3dc6be0f0396d6c55596
6
+ metadata.gz: 20bc96638e82faf39e0595eee398104967dbc273e308c15768f32d55e518430698017c63a233b84a192a0be6a7ade9bb3a80c344274271a230e839d55c8b5672
7
+ data.tar.gz: 7eacea35ae629bcd2b6febb4d548bf15c239d3c5422b01633dd967f780b2590c94ca8bf92ca27832a430f45d74e51e90a2150c16715e0b2e7bc4b8c00eb91d78
data/README.md CHANGED
@@ -374,21 +374,20 @@ This gem provides an admin endpoint for select2 AJAX to fetch users.
374
374
  To use this endpoint please add
375
375
 
376
376
  ```
377
- can :admin, :effective_resources
377
+ can :users, :ajax
378
378
  ```
379
379
 
380
380
  And then create a select field like this:
381
381
 
382
382
  ```
383
- = f.select :user_id, current_user.class.all,
384
- ajax_url: effective_resources.users_admin_select2_ajax_index_path
383
+ = f.select :user_id, current_user.class.all, ajax_url: effective_resources.users_effective_ajax_index_path
385
384
  ```
386
385
 
387
- To format the results, add a method to your User class. It can return html.
386
+ To format the results, add a method to your User class. Should return HTML with a span tag
388
387
 
389
388
  ```
390
389
  def to_select2
391
- "<span>#{user.first_name} #{user.last_name}</span> <small>#{user.email}</small>"
390
+ "<span>#{first_name} #{last_name}</span> <small>&lt;#{try(:public_email) || email}&gt;</small>"
392
391
  end
393
392
  ```
394
393
 
@@ -42,7 +42,7 @@ module Effective
42
42
  raise('expected a Hash with id and text params') unless option.kind_of?(Hash) && option[:id] && option[:text]
43
43
  option
44
44
  else
45
- { id: resource.to_param, text: resource.try(:to_select2) || resource.to_s }
45
+ { id: resource.to_param, text: to_select2(resource) }
46
46
  end
47
47
  end
48
48
 
@@ -56,12 +56,13 @@ module Effective
56
56
 
57
57
  private
58
58
 
59
- def to_select2(resource)
60
- if resource.try(:email).present?
61
- "<span>#{resource}</span> <small>#{resource.email}</small>"
62
- else
63
- "<span>#{resource}</span>"
64
- end
59
+ def to_select2(resource, with_organizations = false)
60
+ organizations = Array(resource.try(:organizations)).join(', ') if with_organizations
61
+
62
+ [
63
+ (resource.try(:to_select2) || "<span>#{resource.to_s}</span>"),
64
+ ("<small>#{organizations}</small>" if organizations.present?)
65
+ ].compact.join(' ')
65
66
  end
66
67
 
67
68
  end
@@ -0,0 +1,53 @@
1
+ module Effective
2
+ class AjaxController < ApplicationController
3
+ before_action(:authenticate_user!) if defined?(Devise)
4
+
5
+ include Effective::Select2AjaxController
6
+
7
+ def users
8
+ EffectiveResources.authorize!(self, :users, :ajax)
9
+
10
+ with_organizations = current_user.class.try(:effective_memberships_organization_user?)
11
+
12
+ collection = current_user.class.all
13
+ collection = collection.includes(:organizations) if with_organizations
14
+
15
+ respond_with_select2_ajax(collection, skip_authorize: true) do |user|
16
+ data = { first_name: user.first_name, last_name: user.last_name, email: user.email }
17
+
18
+ if with_organizations
19
+ data[:company] = user.organizations.first.try(:to_s)
20
+ data[:organization_id] = user.organizations.first.try(:id)
21
+ data[:organization_type] = user.organizations.first.try(:class).try(:name)
22
+ end
23
+
24
+ {
25
+ id: user.to_param,
26
+ text: to_select2(user, with_organizations),
27
+ data: data
28
+ }
29
+ end
30
+ end
31
+
32
+ def organizations
33
+ EffectiveResources.authorize!(self, :organizations, :ajax)
34
+
35
+ raise('the effective memberships gem is required') unless defined?(EffectiveMemberships)
36
+
37
+ klass = EffectiveMemberships.Organization
38
+ raise('an EffectiveMemberships.Organization is required') unless klass.try(:effective_memberships_organization?)
39
+
40
+ collection = klass.all
41
+
42
+ respond_with_select2_ajax(collection) do |organization|
43
+ data = { title: organization.title, email: organization.email }
44
+
45
+ {
46
+ id: organization.to_param,
47
+ text: to_select2(organization),
48
+ data: data
49
+ }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -292,11 +292,10 @@ module EffectiveDeviseUser
292
292
  end
293
293
 
294
294
  def to_select2_search_columns
295
- [:email, :public_email, :first_name, :last_name, :name]
295
+ [:email, :public_email, :alternate_email, :first_name, :last_name, :name]
296
296
  end
297
297
 
298
298
  def to_select2
299
- "<span>#{email_to_s}</span> <small>#{email}</small>"
299
+ "<span>#{email_to_s}</span> <small>&lt;#{try(:public_email) || email}&gt;</small>"
300
300
  end
301
-
302
301
  end
data/config/routes.rb CHANGED
@@ -3,12 +3,10 @@ Rails.application.routes.draw do
3
3
  end
4
4
 
5
5
  EffectiveResources::Engine.routes.draw do
6
-
7
- namespace :admin do
8
- resources :select2_ajax, only: [] do
9
- get :users, on: :collection
10
- get :organizations, on: :collection
6
+ namespace :effective do
7
+ resources :ajax, only: [] do
8
+ get :users, on: :collection, as: :users
9
+ get :organizations, on: :collection, as: :organizations
11
10
  end
12
11
  end
13
-
14
12
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '2.25.16'.freeze
2
+ VERSION = '2.26.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.25.16
4
+ version: 2.26.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-27 00:00:00.000000000 Z
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -175,7 +175,6 @@ files:
175
175
  - README.md
176
176
  - app/assets/javascripts/effective_resources.js
177
177
  - app/assets/javascripts/effective_resources/effective_ujs.js
178
- - app/controllers/admin/select2_ajax_controller.rb
179
178
  - app/controllers/concerns/effective/crud_controller.rb
180
179
  - app/controllers/concerns/effective/crud_controller/actions.rb
181
180
  - app/controllers/concerns/effective/crud_controller/dsl.rb
@@ -194,6 +193,7 @@ files:
194
193
  - app/controllers/concerns/effective/wizard_controller/permitted_params.rb
195
194
  - app/controllers/concerns/effective/wizard_controller/save.rb
196
195
  - app/controllers/concerns/effective/wizard_controller/wicked_overrides.rb
196
+ - app/controllers/effective/ajax_controller.rb
197
197
  - app/helpers/effective_resources_email_helper.rb
198
198
  - app/helpers/effective_resources_helper.rb
199
199
  - app/helpers/effective_resources_private_helper.rb
@@ -1,29 +0,0 @@
1
- module Admin
2
- class Select2AjaxController < ApplicationController
3
- before_action(:authenticate_user!) if defined?(Devise)
4
- before_action { EffectiveResources.authorize!(self, :admin, :effective_resources) }
5
-
6
- include Effective::Select2AjaxController
7
-
8
- def users
9
- collection = current_user.class.all
10
-
11
- respond_with_select2_ajax(collection) do |user|
12
- { id: user.to_param, text: user.try(:to_select2) || to_select2(user) }
13
- end
14
- end
15
-
16
- def organizations
17
- raise('the effective memberships gem is required') unless defined?(EffectiveMemberships)
18
-
19
- klass = EffectiveMemberships.Organization
20
- raise('an EffectiveMemberships.Organization is required') unless klass.try(:effective_memberships_organization?)
21
-
22
- collection = klass.all
23
-
24
- respond_with_select2_ajax(collection) do |organization|
25
- { id: organization.to_param, text: organization.try(:to_select2) || to_select2(organization) }
26
- end
27
- end
28
- end
29
- end