Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

Commit

Permalink
Limits number of visible ideas (6 on board and 12 on cards)
Browse files Browse the repository at this point in the history
  • Loading branch information
mezis committed Jun 23, 2013
1 parent 01cad4d commit a1b403d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
43 changes: 40 additions & 3 deletions app/controllers/ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,22 @@ class IdeasController < ApplicationController
VALID_VIEWS = %w(cards board)
DEFAULT_VIEW = VALID_VIEWS.first

LIMIT_RANGE = 5..100
DEFAULT_LIMIT = { 'board' => 6, 'cards' => 12 }

def index
@angle = set_angle_from_params
@order = set_order_from_params_and_angle
@filter = set_filter_from_params_and_angle
@category = set_category_from_params_and_angle
@view = set_view_from_params
@limit = set_limit_from_params(@view)
@state = set_state_from_params


@ideas = current_account.ideas.
send(:"#{@angle}_by", current_user).
send(:"by_#{@order}")
# account, angle and filter
@ideas = current_account.ideas
@ideas = @ideas.send(:"#{@angle}_by", current_user)
@ideas = @ideas.send(:"#{@filter}_by", current_user) unless @filter == 'all'

if @category == 'none'
Expand All @@ -38,6 +44,22 @@ def index
@ideas = @ideas.where(category: @category)
end

@ideas = @ideas.with_state(@state.to_sym) unless @state == 'all'

# counts before ordering and pagination
@idea_counts = @ideas.counts_per_state if @view == 'board'

# order
@ideas = @ideas.send(:"by_#{@order}")

# pagination
case @view
when 'board'
@ideas = @ideas.limit_per_state(limit:@limit)
else
@ideas = @ideas.paginate(page:1, per_page:@limit)
end

# eager-load participants
@ideas.map(&:participants)
end
Expand Down Expand Up @@ -208,6 +230,21 @@ def set_category_from_params_and_angle
end
end

def set_limit_from_params(view)
@limit = begin
limit = params[:limit].to_i rescue 0
LIMIT_RANGE.include?(limit) ? limit : DEFAULT_LIMIT[view]
end
end

VALID_STATES = Idea.state_machine.states.map(&:name).map(&:to_s)
def set_state_from_params
@state = begin
state = params.fetch(:state, 'all')
VALID_STATES.include?(state) ? state : 'all'
end
end

def map_state_names
# map state names to values
return unless state = params[:idea].andand[:state]
Expand Down
11 changes: 11 additions & 0 deletions app/models/idea.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ class Idea < ActiveRecord::Base
backed_ideas_ids.any? ? where('ideas.id NOT IN (?)', backed_ideas_ids) : scoped
}

def self.limit_per_state(limit:10)
ids = values_of(:id, :state).group_by(&:last).values.map { |id_states| id_states.take(limit).map(&:first) }.flatten
where id:ids
end

def self.counts_per_state
Hash[
group(:state).count.map { |state_value,count| [state_name(state_value), count] }
]
end

# Other helpers

def participants
Expand Down
7 changes: 7 additions & 0 deletions app/views/ideas/_angle_copy.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,10 @@
.ht-margin-icon
%i.icon-info-sign
!= ideas_copy_for_angle(@angle)
- if @ideas.respond_to?(:total_entries) && @ideas.size < @ideas.total_entries
.ht-with-margin-icon
.ht-margin-icon
%i.icon-info-sign
= _("We're only showing you %{count} of a total %{total_count} ideas. Don't worry, the others haven't been lost—It's just they're probably not that important if they haven't bubbled up the list.") % { count:@ideas.size, total_count:@ideas.total_entries }
%br/
= _("Remember that you can find your %{own_ideas} and your %{bookmarked_ideas} easily.").html_safe % { own_ideas:(link_to _('own ideas'), ideas_path(view:'cards', filter:'author')), bookmarked_ideas:link_to(_('bookmarked ideas'), ideas_path(view:'cards', angle:'followed')) }
6 changes: 6 additions & 0 deletions app/views/ideas/_board.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,9 @@
- grouped_ideas[state_name].andand.each do |idea|
.af-rtable-cell
= render 'idea_mini', idea:idea, title_link:true
- hidden_count = @idea_counts.fetch(state_name, 0) - grouped_ideas.fetch(state_name, []).length
- if hidden_count > 0
.af-rtable-cell
%div
.ht-separator-text
%p= _('+%{count} hidden') % { count:hidden_count }
4 changes: 2 additions & 2 deletions app/views/ideas/_cards.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
-# Give me an array of +@ideas+
.row
.span3
= render 'angle_copy'
.span9
= render 'no_ideas_banner'
- @ideas.each do |idea|
= render 'idea', idea:idea, collapsed:true, title_link:true, muted:true
.span3
= render 'angle_copy'

0 comments on commit a1b403d

Please sign in to comment.