1

What I want to achieve: dynamically create category sorter using isotope.js from my Gallery.tag

To do that I need to

  1. loop out a lowercase unique tags for my html .class
  2. loop out the tag (using globalize 3 for translation) to display in the <h2>

I cannot use @galleries for both cases because upon switch language it will output chinese character to my .class which breaks the sorter. Therefore I created two arrays and combine them together with zip and the looping it with each.

I try to sort them by alphabetic order, but my corresponding Chinese tags are not following my English order.

galleries/index.html.erb

<% @uniq_test = [] %>
    <% @galleries_order.each do |gallery| %>
        <% next if @uniq_test.include?(gallery.tag) %>
    <% @uniq_test << gallery.tag %>
<% end %>
<% @sorters = @sorters.map(&:downcase).sort! %>
<% @uniq_test = @uniq_test.map(&:downcase).sort! %>
<% @uniq_sorters = @uniq_test.zip(@sorters) %>
<div class="main">
    <div class="gallery-select-wrapper">
        <div class="sort-gallery-buttons animated slideInLeft text-center">
            <h2 id="recent"class="recent"><%= t"galleries.sorter.recent"%></h2>
    <% @uniq_sorters.each do |uniq, sorter| %>
        <% if sorter != nil %>
            <% str = "<h2 class='" + sorter + "'" + "id='"+ sorter + "'>"%>
            <%= str.html_safe + uniq + "</h2>".html_safe %>
        <% end %>
    <% end %>
        </div>
    </div>
</div>

controllers/galleries.rb

def index
    @galleries = Gallery.all.order("created_at DESC")
    @galleries_order = Gallery.all.order("title ASC")
    @sorters = Gallery.uniq.pluck(:tag)
    gon.tags = Gallery.uniq.pluck(:tag)
end

en categories are [country, theme, project, war] zh categories are [主題,國家, 戰爭, 項目] <-- current (in en = theme, country, war, project) my categories are [國家, 主題, 項目, 戰爭] <-- goal (same as en)

in a nutshell I want chinese translation to follow my english alphabetic order.

enter image description here

enter image description here

Rex
  • 238
  • 2
  • 13

1 Answers1

0

Solved it! In short, to sort something with globalize3 ::translation models, the best way for my case is to sort by :id or in my case :created_at instead of by title which I have to deal with finding a way to sort Chinese Characters! Since I don't particularly care if the terms are in alphabetic order, as long as they are in sync I'm happy.

As easy as that, I was overthinking it definitely!

galleries/index.html

<% @array = [] %>
<% @test.each do |test| %>
    <% next if @array.include?(test.tag) %>
    <% @array << test.tag %>
<% end %>

<% @array_en = [] %>
<% @test.each do |test| %>
    <% next if @array_en.include?(test.tag_en) %>
    <% @array_en << test.tag_en %>
<% end %>

<% @combined_array = @array.zip(@array_en) %>

<div class="main">
    <div class="gallery-select-wrapper">
        <div class="sort-gallery-buttons animated slideInLeft text-center">
            <h2 id="recent"class="recent"><%= t"galleries.sorter.recent"%></h2>
    <% @combined_array.each do |uniq, sorter| %>
        <% if sorter != nil %>
            <% str = "<h2 class='" + sorter + "'" + "id='"+ sorter + "'>"%>
            <%= str.html_safe + uniq + "</h2>".html_safe %>
        <% end %>
    <% end %>
        </div>
    </div>
</div>

controllers/galleries.rb

@test = Gallery.all.sort { |p1, p2| p1.created_at <=> p2.created_at }
Rex
  • 238
  • 2
  • 13