effective_resources 2.25.16 → 2.26.1

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: 5c1d1d08ea965b86966add386e7676e3f4961289ea9bc7c2a1f05b81d5dfd6e6
4
+ data.tar.gz: 28e1fd1ec285d91b62c7b0f082de1b3a8183647b67a101240b6d7e6aa4c2a79a
5
5
  SHA512:
6
- metadata.gz: 787cf0d8f97f276fa8f8b4eef9b4676474a88faafb7aed72346eaba3bbfd55cf78fb9c1b409bcb6c8f828d6dbdbb3b13d7c8e68a0cb4ebd2ca09318367b49c39
7
- data.tar.gz: e807a9d2e7af907d14d62b8f665dce653c11072956c7e849306dcc72305ebf1ad016e88c9264a59258fee9bbaa53c0f39fd1c6fa8b3f3dc6be0f0396d6c55596
6
+ metadata.gz: 1f652b7ef3003bb3cf6a439f2bedc84bfd49f7391f3eb4040d3c49d7739142803d393fd08d0bf3620274639ff2b0ff7939c0e682d91e99df8afd79449ce11987
7
+ data.tar.gz: 44f0c457a5cc6692489b2f6809aed69379d28984d662fc0401b9894c940a28edc03d051e196bdfd44702ff9f460c9270c3e5f50c99358c6d056492c1ac4af677
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,58 @@
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
+ # Display only related organization users
16
+ if with_organizations && params[:related]
17
+ collection = collection.where(id: current_user.organizations.flat_map(&:users))
18
+ end
19
+
20
+ respond_with_select2_ajax(collection, skip_authorize: true) do |user|
21
+ data = { first_name: user.first_name, last_name: user.last_name, email: user.email }
22
+
23
+ if with_organizations
24
+ data[:company] = user.organizations.first.try(:to_s)
25
+ data[:organization_id] = user.organizations.first.try(:id)
26
+ data[:organization_type] = user.organizations.first.try(:class).try(:name)
27
+ end
28
+
29
+ {
30
+ id: user.to_param,
31
+ text: to_select2(user, with_organizations),
32
+ data: data
33
+ }
34
+ end
35
+ end
36
+
37
+ def organizations
38
+ EffectiveResources.authorize!(self, :organizations, :ajax)
39
+
40
+ raise('the effective memberships gem is required') unless defined?(EffectiveMemberships)
41
+
42
+ klass = EffectiveMemberships.Organization
43
+ raise('an EffectiveMemberships.Organization is required') unless klass.try(:effective_memberships_organization?)
44
+
45
+ collection = klass.all
46
+
47
+ respond_with_select2_ajax(collection) do |organization|
48
+ data = { title: organization.title, email: organization.email }
49
+
50
+ {
51
+ id: organization.to_param,
52
+ text: to_select2(organization),
53
+ data: data
54
+ }
55
+ end
56
+ end
57
+ end
58
+ 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.1'.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.1
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-16 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