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 committed by Connor Turland
parent 7c28070978
commit 2369c9ce5e
3 changed files with 19 additions and 13 deletions

View file

@ -1,2 +1,18 @@
module ApplicationHelper 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 end

View file

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

View file

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