From 086acb09af5d2c634c74a53c90d3c3b5181fcf75 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Sat, 19 Sep 2015 20:17:56 +0800 Subject: [PATCH] select metacodes with one query instead of n queries, move logic to application_helper --- app/helpers/application_helper.rb | 16 ++++++++++++++++ app/models/metacode.rb | 3 +-- app/views/maps/_newtopic.html.erb | 13 ++----------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 95f6ba29..fa7876f5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/metacode.rb b/app/models/metacode.rb index 315f7800..03b0f0c0 100644 --- a/app/models/metacode.rb +++ b/app/models/metacode.rb @@ -13,5 +13,4 @@ class Metacode < ActiveRecord::Base return true if self.metacode_sets.include? metacode_set return false end - -end \ No newline at end of file +end diff --git a/app/views/maps/_newtopic.html.erb b/app/views/maps/_newtopic.html.erb index f22920bb..16b5fecb 100644 --- a/app/views/maps/_newtopic.html.erb +++ b/app/views/maps/_newtopic.html.erb @@ -1,17 +1,8 @@ <%= form_for Topic.new, url: topics_url, remote: true do |form| %>
- <% @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| %> <%= metacode.name %> <% end %>