Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BubuntuClu/4f09ab4b8d7117cedad10e770e21f909 to your computer and use it in GitHub Desktop.
Save BubuntuClu/4f09ab4b8d7117cedad10e770e21f909 to your computer and use it in GitHub Desktop.
` #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