Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use searchable select boxes in admin forms #888

Merged
merged 12 commits into from
Nov 7, 2017
15 changes: 15 additions & 0 deletions admins/pageflow/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ module Pageflow

filter :name

searchable_select_options(text_attribute: :name,
scope: lambda do
Account
.accessible_by(current_ability, :read)
.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
46 changes: 40 additions & 6 deletions admins/pageflow/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ module Pageflow

filter :title
filter :account,
if: ->(_) { authorized?(:index, :accounts) },
collection: -> { Account.accessible_by(current_ability, :read) }
as: :searchable_select,
ajax: true,
if: ->(_) { authorized?(:index, :accounts) }
filter :created_at
filter :edited_at
filter :first_published_at
Expand All @@ -71,19 +72,52 @@ module Pageflow
grouped_by_accounts: authorized?(:see, :accounts))
end

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

searchable_select_options(name: :eligible_themings,
text_attribute: :name,
scope: lambda do |params|
entry = Entry.find(params[:entry_id])

ThemingPolicy::Scope
.new(current_user, Theming)
.themings_allowed_for(entry.account)
end)

form do |f|
f.inputs do
f.input :title, hint: I18n.t('pageflow.admin.entries.title_hint')

if authorized?(:update_account_on, resource)
f.input :account,
collection: eligible_accounts,
f.input(:account,
as: :searchable_select,
include_blank: false,
input_html: {class: 'entry_account_input'}
ajax: {
resource: Entry,
collection_name: :eligible_accounts
},
input_html: {class: 'entry_account_input', style: 'width: 200px'})
end

if authorized?(:update_theming_on, resource) && !f.object.new_record?
f.input :theming, collection: eligible_themings, include_blank: false
f.input(:theming,
as: :searchable_select,
ajax: {
resource: Entry,
collection_name: :eligible_themings,
params: {
entry_id: resource.id
}
},
include_blank: false,
input_html: {style: 'width: 200px'})
end

if authorized?(:configure_folder_for, resource)
Expand Down
57 changes: 57 additions & 0 deletions admins/pageflow/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,63 @@ module Pageflow

form partial: 'form'

searchable_select_options(name: :potential_accounts_for_user,
text_attribute: :name,
scope: lambda do |params|
user = User.find(params[:parent_id])

PotentialMemberships
.creatable_by(current_user)
.accounts_for_user(user)
.order(:name)
end)

searchable_select_options(name: :potential_entries_for_user,
display_text: lambda do |entry|
[entry.try(:account_name), entry.title].compact.join(' / ')
end,
scope: lambda do |params|
user = User.find(params[:parent_id])
entries = PotentialMemberships
.creatable_by(current_user)
.entries_for_user(user)

if can?(:see, :accounts)
entries.include_account_name.order('account_name', :title)
else
entries.order(:title)
end
end,
filter: lambda do |term, scope|
EntryTitleOrAccountNameQuery::Scope.new(term, scope).resolve
end)

searchable_select_options(name: :potential_users_for_account,
text_attribute: :formal_name,
scope: lambda do |params|
account = Account.find(params[:parent_id])
PotentialMemberships
.creatable_by(current_user)
.users_for_account(account)
.order(:last_name, :first_name)
end,
filter: lambda do |term, scope|
UserNameQuery::Scope.new(term, scope).resolve
end)

searchable_select_options(name: :potential_users_for_entry,
text_attribute: :formal_name,
scope: lambda do |params|
entry = Entry.find(params[:parent_id])
PotentialMemberships
.creatable_by(current_user)
.users_for_entry(entry)
.order(:last_name, :first_name)
end,
filter: lambda do |term, scope|
UserNameQuery::Scope.new(term, scope).resolve
end)

controller do
belongs_to :entry, parent_class: Pageflow::Entry, polymorphic: true
belongs_to :account, parent_class: Pageflow::Account, polymorphic: true
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
2 changes: 2 additions & 0 deletions app/assets/javascripts/pageflow/admin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
//= require jquery-ui/droppable
//= require active_admin/searchable_select
//= require select2_locale_de

//= require ./admin/accounts
//= require ./admin/entries
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();
});
});
2 changes: 2 additions & 0 deletions app/assets/stylesheets/pageflow/admin.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import "active_admin/searchable_select";

@import "pageflow/mixins";

@import "pageflow/admin/badge_list";
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/pageflow/admin/entries.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
float: right;
}

span[title] {
td > span[title] {
border-bottom: 1px #aaa dotted;
cursor: default;
}
Expand Down
9 changes: 0 additions & 9 deletions app/helpers/pageflow/admin/entries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ def collection_for_entry_publication_states
I18n.t(state, scope: 'activerecord.values.pageflow/entry.publication_states')
end
end

def eligible_accounts
AccountPolicy::Scope.new(current_user, Account).entry_movable
end

def eligible_themings
ThemingPolicy::Scope.new(current_user, Pageflow::Theming)
.themings_allowed_for(resource.account)
end
end
end
end
Loading