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

Manage spaces filters #792 #900

Merged
merged 4 commits into from
Nov 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0
2.2.5
26 changes: 26 additions & 0 deletions app/assets/javascripts/app/manage/spaces.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
$ ->
if isOnPage 'manage', 'spaces'
mconf.Resources.addToBind ->
mconf.Spaces.New.bind()

window.onpopstate = (event) ->
window.location.href = mconf.Base.urlFromParts(event.state)
event.state

$('input.resource-filter-field').each ->
input = $(this)
field = $(this).attr('data-attr-filter')
baseUrl = $('input.resource-filter').data('load-url')

$(this).on 'click', ->
params = mconf.Base.getUrlParts(String(window.location))
if $(this).is(':checked')
params[field] = $(this).val()
opValue = if params[field] is 'true' then 'false' else 'true'
opElement = $("input[data-attr-filter='#{field}'][value='#{opValue}']")[0]
opElement.checked = false if opElement?.checked
else
delete params[field]

history.pushState(params, '', baseUrl + mconf.Base.urlFromParts(params))
$('input.resource-filter').trigger('update-resources')
16 changes: 16 additions & 0 deletions app/assets/stylesheets/app/manage/spaces.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ body.manage.spaces {
}
}

.search-filters {
margin-top: 10px;

.search-filter-option {
float: left;
width: 20%;
}

.checkbox {
text-align: right;
margin: 0 5px;
float: left;
padding: 0;
}
}

.btn-new-space {
float: right;
}
Expand Down
14 changes: 9 additions & 5 deletions app/controllers/manage_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ def users
end

def spaces
name = params[:q]
partial = params.delete(:partial) # otherwise the pagination links in the view will include this param
words = params[:q].try(:split, /\s+/)
query = Space.with_disabled.search_by_terms(words, can?(:manage, Space)).order("name")

query = Space.with_disabled.order("name")
if name.present?
query = query.where("name like ?", "%#{name}%")
# start applying filters
[:disabled, :approved].each do |filter|
if !params[filter].nil?
val = (params[filter] == 'true') ? true : [false, nil]
query = query.where(filter => val)
end
end

@spaces = query.paginate(:page => params[:page], :per_page => 20)

if request.xhr?
Expand Down
17 changes: 14 additions & 3 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,21 @@ def require_approval?
scope :public_spaces, -> { where(:public => true) }

# Used by select controller method
# For now keep old behavior and search for only one word in the name
scope :search_by_terms, -> (words, include_private=false) {
words = words.join(' ') if words.is_a?(Array)
where('name LIKE ?', "%#{words}%")
query = Space.with_disabled

words ||= []
words = [words] unless words.is_a?(Array)
query_strs = []
query_params = []

words.each do |word|
str = "name LIKE ? OR description LIKE ?"
query_strs << str
query_params += ["%#{word}%", "%#{word}%"]
end

query.where(query_strs.join(' OR '), *query_params.flatten)
}

# Finds all the valid user roles for a Space
Expand Down
6 changes: 5 additions & 1 deletion app/views/manage/_disabled_space.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.thread.thread-space.space-wrapper.space-simple.space-disabled{:class => "#{cycle("thread-even" , "thread-odd")}"}
.thread.thread-space.space-wrapper.space-simple.space-disabled{:class => "#{cycle("thread-even" , "thread-odd")}", :id => "space-#{space.permalink}"}
.logo-in-thread
= raw logo_image(space, :size => '84x64', :title => space.name, :class => 'logo logo-space')

Expand All @@ -10,3 +10,7 @@
.space-text
%span.space-name
= Mconf::Highlighter.highlight(space.name, params[:q].try(:split))

%span.space-description
= Mconf::Highlighter.highlight(sanitize(space.description), params[:q].try(:split))

4 changes: 2 additions & 2 deletions app/views/manage/_enabled_space.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.thread.thread-space.space-wrapper.space-simple{:class => "#{cycle("thread-even" , "thread-odd")}"}
.thread.thread-space.space-wrapper.space-simple{:class => "#{cycle("thread-even" , "thread-odd")}", :id => "space-#{space.permalink}"}
.logo-in-thread
= link_logo_image(space, options_for_tooltip(sanitize(space.name), :size => '84x64', :class => "logo logo-space"))

Expand All @@ -18,7 +18,7 @@
= Mconf::Highlighter.highlight(space.name, params[:q].try(:split))

%span.space-description
= sanitize(first_words(space.description, 100))
= Mconf::Highlighter.highlight(sanitize(space.description), params[:q].try(:split))

- if current_site.require_space_approval? && !space.approved?
.resource-waiting-moderation-tooltip
Expand Down
8 changes: 8 additions & 0 deletions app/views/manage/spaces.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@
= link_to t('.new_space'), new_space_path, class: 'btn btn-primary btn-new-space'
= text_field_tag :spaces_filter_text, params[:q], :placeholder => t('.filter_name'), :'data-load-url' => manage_spaces_path, :'data-target' => '#space-list', :'data-filter' => '#filter-total', :class => 'resource-filter'

.search-filters
- ['approved', 'disabled'].each do |field|
.search-filter-option
= check_box_tag :"users_filter_#{field}_true", true, params[field] == 'true', 'data-attr-filter': field, class: 'resource-filter-field checkbox'
%label{ for: "users_filter_#{field}_true" }= t(".#{field}_true")
= check_box_tag :"users_filter_#{field}_false", false, params[field] == 'false', 'data-attr-filter': field, class: 'resource-filter-field checkbox'
%label{ for: "users_filter_#{field}_false" }= t(".#{field}_false")

#space-list
= render :partial => 'spaces_list', :locals => { :spaces => @spaces }
4 changes: 4 additions & 0 deletions config/locales/en/mconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,10 @@ en:
spaces:
filter_name: "Filter spaces by name"
new_space: "Create space"
approved_true: "Approved"
approved_false: "Not approved"
disabled_true: "Disabled"
disabled_false: "Enabled"
destroy_space:
destroy: Delete space
destroy_confirm: Are you sure you want to delete this space and all content associated with it? Unlike disabling the space this operation is irreversible!
Expand Down
4 changes: 4 additions & 0 deletions config/locales/pt-br/mconf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,10 @@ pt-br:
spaces:
filter_name: "Filtrar comunidades por nome"
new_space: "Nova comunidade"
approved_true: "Aprovados"
approved_false: "Não aprovados"
disabled_true: "Desabilitados"
disabled_false: "Habilitados"
destroy_space:
destroy: Remover comunidade
destroy_confirm: Tem certeza que deseja remover essa comunidade e todo conteúdo relacionado a ela? Diferente de desabilitar a comunidade essa operação é irreversível!
Expand Down
55 changes: 55 additions & 0 deletions spec/controllers/manage_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,61 @@
end
end

context "use params [:approved, :disabled] to filter the results" do
let!(:spaces) {[
FactoryGirl.create(:space, :name => 'Approved', :approved => true),
FactoryGirl.create(:space, :name => 'Not Approved', :approved => false),
FactoryGirl.create(:space, :name => 'Enabled', :disabled => false),
FactoryGirl.create(:space, :name => 'Disabled', :disabled => true)
]}
before {
spaces[1].disapprove!
get :spaces, params
}

context "no params" do
let(:params) { {} }

it { assigns(:spaces).count.should be(4) }
it { assigns(:spaces).should include(*spaces) }
end

context "params[:approved]" do
context 'is true' do
let(:params) { {approved: 'true'} }
it { assigns(:spaces).count.should be(3) }
it { assigns(:spaces).should include(spaces[0], spaces[2], spaces[3]) }
end

context 'is false' do
let(:params) { {approved: 'false'} }
it { assigns(:spaces).count.should be(1) }
it { assigns(:spaces).should include(spaces[1]) }
end
end

context "params[:disabled]" do
context 'is true' do
let(:params) { {disabled: 'true'} }
it { assigns(:spaces).count.should be(1) }
it { assigns(:spaces).should include(spaces[3]) }
end

context 'is false' do
let(:params) { {disabled: 'false'} }
it { assigns(:spaces).count.should be(3) }
it { assigns(:spaces).should include(spaces[0], spaces[1], spaces[2]) }
end
end

context "mixed params" do
let(:params) { {approved: 'true', disabled: 'false', q: 'Ena'} }

it { assigns(:spaces).count.should be(1) }
it { assigns(:spaces).should include(spaces[2]) }
end
end

context "if xhr request" do
before(:each) { xhr :get, :spaces }
it { should render_template('manage/_spaces_list') }
Expand Down
Loading