<commit_before># frozen_string_literal: true

class ListItemsFrom < Liquid::Tag
  include GobiertoCms::PageHelper

  def initialize(tag_name, params, tokens)
    super

    @options = { date: true, intro_text: true, limit: 4, order: "DESC" }
    split_params = params.split("|")
    @collection_slug = split_params.first.strip

    if split_params.length > 1
      parsed_options = Hash[split_params.last.split(",").map{|e| e.split(":").map(&:strip) }].symbolize_keys
      parsed_options[:date] = parsed_options[:date] == "true" if parsed_options.has_key?(:date)
      parsed_options[:intro_text] = parsed_options[:intro_text] == "true" if parsed_options.has_key?(:intro_text)
      parsed_options[:limit] = parsed_options[:limit].to_i if parsed_options.has_key?(:limit)
      @options.merge!(parsed_options)
    end
  end

  def render(context)
    current_site = context.environments.first["current_site"]
    pages = fetch_pages(current_site)

    return "" if pages.empty?
    render_pages(pages)
  rescue ActiveRecord::RecordNotFound
    return ""
  end

  def render_pages(pages)
    html = [ %Q{<div class="list_items_from_collection">} ]
    pages.each do |page|
      page = GobiertoCms::PageDecorator.new(page)
      collection_item_text = [ %Q{<div class="collection_item">} ]
      if page.main_image
        collection_item_text << %Q{ <img src="#{page.main_image}"> }
      end
      if @options[:date]
        collection_item_text << %Q{ <span class="date">#{I18n.l(page.published_on, format: "%d %b %y")}</span> }
      end
      collection_item_text << %Q{ <h2>#{page.title}</h2> }
      if @options[:intro_text]
        collection_item_text << %Q{ <p class="description">#{page.summary}</p> }
      end
      collection_item_text << "</div>"
      html << helpers.link_to(collection_item_text.join.html_safe, gobierto_cms_page_or_news_path(page))
    end
    html << "</div>"
    html.join.html_safe
  end

  private

  def fetch_pages(current_site)
    collection = current_site.collections.find_by!(slug: @collection_slug)
    current_site.pages.where(id: collection.pages_in_collection).active.limit(@options[:limit]).order("updated_at #{@options[:order]}")
  end

  def helpers
    ActionController::Base.helpers
  end
end

Liquid::Template.register_tag("list_items_from", ListItemsFrom)<commit_msg>Fix page ordering in liquid tab<commit_after># frozen_string_literal: true

class ListItemsFrom < Liquid::Tag
  include GobiertoCms::PageHelper

  def initialize(tag_name, params, tokens)
    super

    @options = { date: true, intro_text: true, limit: 4, order: "DESC" }
    split_params = params.split("|")
    @collection_slug = split_params.first.strip

    if split_params.length > 1
      parsed_options = Hash[split_params.last.split(",").map{|e| e.split(":").map(&:strip) }].symbolize_keys
      parsed_options[:date] = parsed_options[:date] == "true" if parsed_options.has_key?(:date)
      parsed_options[:intro_text] = parsed_options[:intro_text] == "true" if parsed_options.has_key?(:intro_text)
      parsed_options[:limit] = parsed_options[:limit].to_i if parsed_options.has_key?(:limit)
      @options.merge!(parsed_options)
    end
  end

  def render(context)
    current_site = context.environments.first["current_site"]
    pages = fetch_pages(current_site)

    return "" if pages.empty?
    render_pages(pages)
  rescue ActiveRecord::RecordNotFound
    return ""
  end

  def render_pages(pages)
    html = [ %Q{<div class="list_items_from_collection">} ]
    pages.each do |page|
      page = GobiertoCms::PageDecorator.new(page)
      collection_item_text = [ %Q{<div class="collection_item">} ]
      if page.main_image
        collection_item_text << %Q{ <img src="#{page.main_image}"> }
      end
      if @options[:date]
        collection_item_text << %Q{ <span class="date">#{I18n.l(page.published_on, format: "%d %b %y")}</span> }
      end
      collection_item_text << %Q{ <h2>#{page.title}</h2> }
      if @options[:intro_text]
        collection_item_text << %Q{ <p class="description">#{page.summary}</p> }
      end
      collection_item_text << "</div>"
      html << helpers.link_to(collection_item_text.join.html_safe, gobierto_cms_page_or_news_path(page))
    end
    html << "</div>"
    html.join.html_safe
  end

  private

  def fetch_pages(current_site)
    collection = current_site.collections.find_by!(slug: @collection_slug)
    current_site.pages.where(id: collection.pages_in_collection).active.limit(@options[:limit]).order("published_on #{@options[:order]}")
  end

  def helpers
    ActionController::Base.helpers
  end
end

Liquid::Template.register_tag("list_items_from", ListItemsFrom)
{'fraction_non_alphanumeric': 0.13979416809605488, 'fraction_numerical': 0.002572898799313894, 'mean_word_length': 4.194877505567929, 'pattern_counts': {'":': 2, '<': 55, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1554111', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1550, 'n_words': 268}