select metacodes with one query instead of n queries, move logic to application_helper

This commit is contained in:
Devin Howard 2015-09-19 20:17:56 +08:00
parent 62035ed015
commit 086acb09af
3 changed files with 19 additions and 13 deletions

View file

@ -1,2 +1,18 @@
module ApplicationHelper
def get_metacodeset
@m = user.settings.metacodes
set = @m[0].include?("metacodeset") ? MetacodeSet.find(@m[0].sub("metacodeset-","").to_i) : false
return set
end
def user_metacodes
@m = user.settings.metacodes
set = get_metacodeset
if set
@metacodes = set.metacodes
else
@metacodes = Metacode.where(id: @m).to_a
end
@metacodes.sort! {|m1,m2| m2.name.downcase <=> m1.name.downcase }.rotate!(-1)
end
end

View file

@ -13,5 +13,4 @@ class Metacode < ActiveRecord::Base
return true if self.metacode_sets.include? metacode_set
return false
end
end
end

View file

@ -1,17 +1,8 @@
<%= form_for Topic.new, url: topics_url, remote: true do |form| %>
<div class="openMetacodeSwitcher openLightbox" data-open="switchMetacodes"></div>
<div id="metacodeImg">
<% @m = user.settings.metacodes %>
<% set = @m[0].include?("metacodeset") ? MetacodeSet.find(@m[0].sub("metacodeset-","").to_i) : false %>
<% if set %>
<% @metacodes = set.metacodes %>
<% else %>
<% @metacodes = [] %>
<% @m.each do |m| %>
<% @metacodes.push(Metacode.find(m.to_i)) %>
<% end %>
<% end %>
<% @metacodes.sort! {|m1,m2| m2.name.downcase <=> m1.name.downcase }.rotate!(-1) %>
<% @metacodes = user_metacodes() %>
<% set = get_metacodeset() %>
<% @metacodes.each do |metacode| %>
<img class="cloudcarousel" width="40" height="40" src="<%= metacode.icon %>" alt="<%= metacode.name %>" title="<%= metacode.name %>" data-id="<%= metacode.id %>" />
<% end %>