Created
May 23, 2018 15:30
-
-
Save BubuntuClu/4f09ab4b8d7117cedad10e770e21f909 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
` #v1 | |
def materials_collection | |
elems = _context.materials.map do |value, title| | |
{ title: title, value: value} | |
end | |
sort_array(elems, _context.materials.size, 'materials') | |
end | |
def styles_collection | |
elems = _main_style_tag_category.tags.published.map do |tag| | |
{ title: tag.title, value: tag.slug } | |
end | |
sort_array(elems, _main_style_tag_category.tags.published.size, 'styles') | |
end | |
def colors_collection | |
elems = _color_aliases.map do |hex, (color_en, color_t)| | |
{ | |
url: seo_pretty_url(color: color_t), | |
hex: hex, | |
value: color_t, | |
title: h.t("activerecord.attributes.search_color.colors.#{color_en}") | |
} | |
end | |
sort_array(elems, _color_aliases.size, 'color') | |
end | |
def filter_elems | |
filters = [:materials, :color, :styles] | |
ElemFilter.where(filter: filters).group_by(&:filter) | |
end | |
def sort_array(array, max_size, filter_key) | |
elems = filter_elems[filter_key].index_by(&:key) | |
array.sort_by do |elem| | |
position = | |
elems[elem[:value]]&.position || max_size | |
[position, elem[:value]] | |
end | |
end | |
#v2 | |
def materials_collection | |
sort_array(elems, _context.materials.size, 'materials') | |
materials_elems = filter_elems['materials'].index_by(&:key) | |
_context.materials.map do |value, title| | |
{ title: title, value: value, order: materials_elems[value]&.position || _context.materials.size } | |
end.sort_by { |e| e[:order] } | |
end | |
def styles_collection | |
styles_elems = filter_elems['styles'].index_by(&:key) | |
_main_style_tag_category.tags.published.map do |tag| | |
{ title: tag.title, value: tag.slug, order: styles_elems[tag.slug]&.position || _main_style_tag_category.tags.published.size } | |
end.sort_by { |e| e[:order] } | |
end | |
def colors_collection | |
colors_elems = filter_elems['color'].index_by(&:key) | |
_color_aliases.map do |hex, (color_en, color_t)| | |
{ | |
url: seo_pretty_url(color: color_t), | |
hex: hex, | |
value: color_t, | |
title: h.t("activerecord.attributes.search_color.colors.#{color_en}"), | |
order: colors_elems[color_t]&.position || _color_aliases.size | |
} | |
end.sort_by { |e| e[:order] } | |
end | |
def filter_elems | |
filters = [:materials, :color, :styles] | |
ElemFilter.where(filter: filters).group_by(&:filter) | |
end | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment