Skip to content

Instantly share code, notes, and snippets.

@bplexico
bplexico / category_list.rb
Created July 15, 2013 21:11
calls to Topic
def add_uncategorized
# Support for uncategorized topics
uncategorized_topics = Topio.uncategorized_topics # <----- call to Topic
if uncategorized_topics.present?
totals = Topic.totals # <----- call to Topic
...
@bplexico
bplexico / topic.rb
Created July 15, 2013 21:07
new methods in Topic
def self.uncategorized_topics
listable_topics
.visible
.where(category_id: nil)
.topic_list_order
.limit(SiteSetting.category_featured_topics)
end
def self.totals
exec_sql("SELECT SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 WEEK') THEN 1 ELSE 0 END) as topics_week,
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:57
add_categorized with methods extracted
def add_uncategorized
# Support for uncategorized topics
uncategorized_topics = get_uncategorized_topics
if uncategorized_topics.present?
totals = get_topic_totals
uncategorized = get_uncategorized_category
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:55
extract insert and add topics methods
def insert_uncategorized_category(uncategorized)
insert_at = nil
@categories.each_with_index do |c, idx|
if (uncategorized.topics_week || 0) > (c.topics_week || 0)
insert_at = idx
break
end
end
@categories.insert(insert_at || @categories.size, uncategorized)
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:50
after refactoring assignments
def add_uncategorized
# Support for uncategorized topics
uncategorized_topics = get_uncategorized_topics
if uncategorized_topics.present?
totals = get_topic_totals
uncategorized = get_uncategorized_category
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:48
add get_uncategorized_category
uncategorized = get_uncategorized_category
def get_uncategorized_category
Category.new({name: SiteSetting.uncategorized_name,
slug: Slug.for(SiteSetting.uncategorized_name),
color: SiteSetting.uncategorized_color,
text_color: SiteSetting.uncategorized_text_color,
featured_topics: uncategorized_topics}.merge(totals))
end
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:47
add get_topic_totals
totals = get_topic_totals
def get_topic_totals
Topic.exec_sql("SELECT SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 WEEK') THEN 1 ELSE 0 END) as topics_week,
SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 MONTH') THEN 1 ELSE 0 END) as topics_month,
SUM(CASE WHEN created_at >= (CURRENT_TIMESTAMP - INTERVAL '1 YEAR') THEN 1 ELSE 0 END) as topics_year,
COUNT(*) AS topic_count
FROM topics
WHERE topics.visible
AND topics.deleted_at IS NULL
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:45
add_get_uncategorized_topics
uncategorized_topics = get_uncategorized_topics
def get_uncategorized_topics
Topic
.listable_topics
.visible
.where(category_id: nil)
.topic_list_order
.limit(SiteSetting.category_featured_topics)
end
@bplexico
bplexico / category_list.rb
Created July 15, 2013 20:35
add_uncategorized_starting_point
def add_uncategorized
# Support for uncategorized topics
uncategorized_topics = Topic
.listable_topics
.visible
.where(category_id: nil)
.topic_list_order
.limit(SiteSetting.category_featured_topics)
if uncategorized_topics.present?
@bplexico
bplexico / gist:5745181
Created June 9, 2013 20:58
final SearchObserver
class SearchObserver < ActiveRecord::Observer
observe :topic, :post, :user, :category
def after_save(obj)
obj.update_search_data_index
end
end