2016-09-24 03:00:46 +00:00
|
|
|
# frozen_string_literal: true
|
2017-11-25 19:23:47 +00:00
|
|
|
|
2014-05-17 18:57:03 +00:00
|
|
|
module MetacodesHelper
|
2017-03-07 03:49:46 +00:00
|
|
|
def metacodeset
|
|
|
|
metacodes = current_user.settings.metacodes
|
|
|
|
|
|
|
|
return false unless metacodes[0].include?('metacodeset')
|
|
|
|
return 'Most' if metacodes[0].sub('metacodeset-', '') == 'Most'
|
|
|
|
return 'Recent' if metacodes[0].sub('metacodeset-', '') == 'Recent'
|
|
|
|
|
|
|
|
MetacodeSet.find(metacodes[0].sub('metacodeset-', '').to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_metacodes
|
|
|
|
@m = current_user.settings.metacodes
|
|
|
|
set = metacodeset
|
|
|
|
@metacodes = if set && set == 'Most'
|
|
|
|
Metacode.where(id: current_user.most_used_metacodes).to_a
|
|
|
|
elsif set && set == 'Recent'
|
|
|
|
Metacode.where(id: current_user.recent_metacodes).to_a
|
|
|
|
elsif set
|
|
|
|
set.metacodes.to_a
|
|
|
|
else
|
|
|
|
Metacode.where(id: @m).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
focus_code = user_metacode
|
|
|
|
if !focus_code.nil? && @metacodes.index { |m| m.id == focus_code.id }.nil?
|
|
|
|
@metacodes.push(focus_code)
|
|
|
|
end
|
|
|
|
|
|
|
|
@metacodes.sort! { |m1, m2| m2.name.downcase <=> m1.name.downcase }
|
|
|
|
|
|
|
|
if !focus_code.nil?
|
|
|
|
@metacodes.rotate!(@metacodes.index { |m| m.id == focus_code.id })
|
|
|
|
else
|
|
|
|
@metacodes.rotate!(-1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_metacode
|
|
|
|
current_user.settings.metacode_focus ? Metacode.find(current_user.settings.metacode_focus.to_i) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_most_used_metacodes
|
|
|
|
@metacodes = current_user.most_used_metacodes.map { |id| Metacode.find(id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_recent_metacodes
|
|
|
|
@metacodes = current_user.recent_metacodes.map { |id| Metacode.find(id) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def metacode_sets_json
|
|
|
|
metacode_sets = []
|
2018-03-08 22:12:24 +00:00
|
|
|
if current_user
|
|
|
|
metacode_sets << {
|
|
|
|
name: 'Recently Used',
|
|
|
|
description: 'Your recently used metacodes',
|
|
|
|
metacodes: user_recent_metacodes.map { |m| m.id }
|
|
|
|
}
|
|
|
|
metacode_sets << {
|
|
|
|
name: 'Most Used',
|
|
|
|
description: 'Your most used metacodes',
|
|
|
|
metacodes: user_most_used_metacodes.map { |m| m.id }
|
|
|
|
}
|
|
|
|
end
|
2017-03-07 03:49:46 +00:00
|
|
|
metacode_sets += MetacodeSet.order('name').all.map do |set|
|
|
|
|
{
|
2018-03-08 22:12:24 +00:00
|
|
|
id: set.id,
|
2017-03-07 03:49:46 +00:00
|
|
|
name: set.name,
|
2018-03-08 22:12:24 +00:00
|
|
|
desc: set.desc,
|
|
|
|
metacodes: set.metacodes.order('name').map { |m| m.id }
|
2017-03-07 03:49:46 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
metacode_sets << {
|
|
|
|
name: 'All',
|
2018-03-08 22:12:24 +00:00
|
|
|
desc: 'A list of all the metacodes',
|
|
|
|
metacodes: Metacode.order('name').all.map { |m| m.id }
|
2017-03-07 03:49:46 +00:00
|
|
|
}
|
|
|
|
metacode_sets.to_json
|
|
|
|
end
|
2014-05-17 18:57:03 +00:00
|
|
|
end
|