Class: Effective::AjaxController

Inherits:
ApplicationController
  • Object
show all
Includes:
Select2AjaxController
Defined in:
app/controllers/effective/ajax_controller.rb

Instance Method Summary collapse

Methods included from Select2AjaxController

#respond_with_select2_ajax

Instance Method Details

#organizationsObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/effective/ajax_controller.rb', line 37

def organizations
  EffectiveResources.authorize!(self, :organizations, :ajax)

  raise('the effective memberships gem is required') unless defined?(EffectiveMemberships)

  klass = EffectiveMemberships.Organization
  raise('an EffectiveMemberships.Organization is required') unless klass.try(:effective_memberships_organization?)

  collection = klass.all

  respond_with_select2_ajax(collection) do |organization|
    data = { title: organization.title, email: organization.email }

    { 
      id: organization.to_param, 
      text: to_select2(organization),
      data: data
    }
  end
end

#usersObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/controllers/effective/ajax_controller.rb', line 7

def users
  EffectiveResources.authorize!(self, :users, :ajax)

  with_organizations = current_user.class.try(:effective_memberships_organization_user?)

  collection = current_user.class.all
  collection = collection.includes(:organizations) if with_organizations

  # Display only related organization users
  if with_organizations && params[:related]
    collection = collection.where(id: current_user.organizations.flat_map(&:users))
  end

  respond_with_select2_ajax(collection, skip_authorize: true) do |user|
    data = { first_name: user.first_name, last_name: user.last_name, email: user.email }

    if with_organizations
      data[:company] = user.organizations.first.try(:to_s)
      data[:organization_id] = user.organizations.first.try(:id)
      data[:organization_type] = user.organizations.first.try(:class).try(:name)
    end

    { 
      id: user.to_param, 
      text: to_select2(user, with_organizations),
      data: data
    }
  end
end