diff --git a/app/controllers/ideas_controller.rb b/app/controllers/ideas_controller.rb index ff78656..b89049d 100644 --- a/app/controllers/ideas_controller.rb +++ b/app/controllers/ideas_controller.rb @@ -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' @@ -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 @@ -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] diff --git a/app/models/idea.rb b/app/models/idea.rb index 2c4875d..98112d3 100644 --- a/app/models/idea.rb +++ b/app/models/idea.rb @@ -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 diff --git a/app/views/ideas/_angle_copy.html.haml b/app/views/ideas/_angle_copy.html.haml index 456a75d..c804bb5 100644 --- a/app/views/ideas/_angle_copy.html.haml +++ b/app/views/ideas/_angle_copy.html.haml @@ -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')) } \ No newline at end of file diff --git a/app/views/ideas/_board.html.haml b/app/views/ideas/_board.html.haml index 4c7fb21..48936ef 100644 --- a/app/views/ideas/_board.html.haml +++ b/app/views/ideas/_board.html.haml @@ -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 } diff --git a/app/views/ideas/_cards.html.haml b/app/views/ideas/_cards.html.haml index c9f1a26..8b473a8 100644 --- a/app/views/ideas/_cards.html.haml +++ b/app/views/ideas/_cards.html.haml @@ -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'