agenda-libre-ruby/app/controllers/tags_controller.rb

51 lines
1.2 KiB
Ruby
Raw Normal View History

2016-02-20 16:53:54 +01:00
# Manage event and organisation tags
2014-01-05 22:10:11 +01:00
class TagsController < InheritedResources::Base
has_scope :region, :locality, :daylimit
has_scope :period, type: :hash, using: %i[year week]
has_scope :tag, as: :id
2014-01-05 22:10:11 +01:00
def index
2016-12-17 22:54:29 +01:00
@tags = ActsAsTaggableOn::Tag.where('taggings_count > 3')
if params[:term]
# Used to autocomplete tags
@tags = @tags.select(:id, :name, 'name AS label')
.where('name LIKE ?', "#{params[:term]}%")
end
respond_to do |format|
format.html
format.json { render json: @tags }
end
2014-01-05 22:10:11 +01:00
end
def show
@events_future = apply_scopes Event.moderated.future
@events_past = apply_scopes Event.moderated.past
2016-03-27 14:34:34 +02:00
respond_to do |format|
format.html
format.json { render json: @events_future + @events_past }
end
2014-01-05 22:10:11 +01:00
end
def orgas
2016-02-20 16:53:54 +01:00
@tags = organise_tags apply_scopes(Orga.moderated)
respond_to do |format|
format.html
format.json { render json: @tags }
end
end
2016-02-20 16:53:54 +01:00
private
# Splits, groups, rejects the less used
def organise_tags(tags)
tags.where.not(tags: '').pluck(:tags).map(&:split).flatten
2016-02-20 16:53:54 +01:00
.group_by { |i| i }
.map { |k, v| [k, v.size] }
.reject { |_k, v| v <= 3 }
2016-03-27 14:34:34 +02:00
.sort { |t| -t[1] }
2016-02-20 16:53:54 +01:00
end
2014-01-05 22:10:11 +01:00
end