Skip to content

Commit

Permalink
Use searchable select for user invitation form
Browse files Browse the repository at this point in the history
Load users quota state via ajax.
  • Loading branch information
tf committed Oct 23, 2017
1 parent c4e20df commit ff831e3
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 30 deletions.
8 changes: 8 additions & 0 deletions admins/pageflow/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ module Pageflow
.order(:name)
end)

searchable_select_options(name: :member_addable,
text_attribute: :name,
scope: lambda do
AccountPolicy::Scope.new(current_user, Account)
.member_addable
.order(:name)
end)

form :partial => 'form'

show :title => :name do |account|
Expand Down
7 changes: 7 additions & 0 deletions admins/pageflow/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,13 @@ module Pageflow
end
end

collection_action :quota_state do
@account = Pageflow::Account.find(params[:account_id])
authorize!(:add_member_to, @account)

render(layout: false)
end

collection_action 'me', title: I18n.t('pageflow.admin.users.account'), method: [:get, :patch] do
@user = User.find(current_user.id)

Expand Down
40 changes: 20 additions & 20 deletions app/assets/javascripts/pageflow/admin/users.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
jQuery(function($) {
$('.admin_users form.pageflow_invitation_form').each(function() {
var quotaStates = $('.quota_state', this);
var quotaStateContainer = $('#quota_state_container', this);
var accountSelect = $('#invitation_form_membership_entity_id', this);
var fieldsets = $('#invitation_form_details', this).add('fieldset.actions', this);

function filterQuotaStatesBySelectedAccount() {
function updateQutaState() {
var selectedAccountId = accountSelect.val();

quotaStates.each(function(){
var quotaState = $(this);
var accountId = quotaState.data('accountId').toString();
$.get('/admin/users/quota_state?account_id=' + selectedAccountId)
.success(function(html) {
quotaStateContainer.html(html);
updateForm();
});
}

if (accountId !== selectedAccountId) {
$(quotaState).hide();
}
else {
$(quotaState).show();
function updateForm() {
if (quotaAvailable()) {
fieldsets.show();
}
else {
fieldsets.hide();
}
}

if ($(quotaState).data('state') === 'available') {
fieldsets.show();
}
else {
fieldsets.hide();
}
}
});
function quotaAvailable() {
return !!quotaStateContainer.find('[data-state=available]').length;
}

filterQuotaStatesBySelectedAccount();
accountSelect.on('change', filterQuotaStatesBySelectedAccount);
accountSelect.on('change', updateQutaState);
updateForm();
});
});
13 changes: 9 additions & 4 deletions app/models/pageflow/invitation_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def existing_member
initial_account && initial_account.users.find_by_email(user.email)
end

def initial_account
@initial_account ||=
if initial_account_id
available_accounts.find_by_id(initial_account_id)
else
available_accounts.first
end
end

private

def existing_user
Expand All @@ -44,10 +53,6 @@ def existing_user
User.find_by_email(user.email)
end

def initial_account
@initial_account ||= available_accounts.find_by_id(initial_account_id)
end

def initial_account_id
@attributes.fetch(:membership, {})[:entity_id]
end
Expand Down
15 changes: 10 additions & 5 deletions app/views/admin/users/invitation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@

<%= f.inputs do %>
<%= m.input :entity_id,
as: :select,
collection: @invitation_form.available_accounts,
as: :searchable_select,
ajax: {
resource: Pageflow::Account,
collection_name: :member_addable
},
include_blank: false,
label: Pageflow::Membership.human_attribute_name(:account) %>

<% @invitation_form.available_accounts.each do |account| %>
<%= users_quota_state(account) %>
<% end %>
<div id="quota_state_container">
<% if @invitation_form.initial_account %>
<%= users_quota_state(@invitation_form.initial_account) %>
<% end %>
</div>
<% end %>

<%= f.inputs "Details", id: 'invitation_form_details' do %>
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/users/quota_state.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= users_quota_state(@account) %>
33 changes: 32 additions & 1 deletion spec/controllers/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,41 @@ def self.name
end
end

describe 'get #quota_state' do
render_views

it 'allows to display user quota state for account manager' do
account = create(:account)

sign_in(create(:user, :manager, on: account))
get(:quota_state, account_id: account)

expect(response.body).to have_selector('.quota_state')
end

it 'does not render layout' do
account = create(:account)

sign_in(create(:user, :manager, on: account))
get(:quota_state, account_id: account)

expect(response.body).not_to have_selector('body.active_admin')
end

it 'is forbidden for account publisher' do
account = create(:account)

sign_in(create(:user, :publisher, on: account))
get(:quota_state, account_id: account)

expect(response).to have_http_status(302)
end
end

describe 'get #invitation' do
render_views

it 'displays quota state description' do
it 'displays quota state description of selected account' do
account = create(:account)

Pageflow.config.quotas.register(:users, QuotaDouble.available)
Expand Down

0 comments on commit ff831e3

Please sign in to comment.