<commit_before># frozen_string_literal: true

class ShowPoll < Liquid::Tag
  def initialize(tag_name, poll_id, tokens)
    super
    @poll_id = if poll_id.include?("poll_id")
                 poll_id.match(/(\((.*)\))/)[2].split(": ").last
               else
                 nil
               end
  end

  def render(context)
    current_site = context.environments.second["current_site"]
    current_user = context.environments.first["current_user"]

    poll = next_poll(current_site, current_user, @poll_id)

    context.registers[:controller].dup.render(partial: "shared/polls", locals: { poll: poll })
  end

  def next_poll(current_site, current_user, poll_id = nil)
    poll = GobiertoParticipation::Poll.by_site(current_site).find(poll_id) if poll_id
    answerable_polls = GobiertoParticipation::Poll.by_site(current_site).answerable.order(ends_at: :asc)
    answerable_polls_by_user = answerable_polls.detect { |p| p.answerable_by?(current_user) } if current_user

    if current_user
      if poll && poll.answerable_by?(current_user)
        poll
      elsif answerable_polls_by_user
        answerable_polls_by_user
      end
    else
      answerable_polls.first
    end
  rescue ActiveRecord::RecordNotFound
    return ""
  end
end

Liquid::Template.register_tag("show_poll", ShowPoll)<commit_msg>Force poll unless current_user<commit_after># frozen_string_literal: true

class ShowPoll < Liquid::Tag
  def initialize(tag_name, poll_id, tokens)
    super
    @poll_id = if poll_id.include?("poll_id")
                 poll_id.match(/(\((.*)\))/)[2].split(": ").last
               else
                 nil
               end
  end

  def render(context)
    current_site = context.environments.second["current_site"]
    current_user = context.environments.first["current_user"]

    poll = next_poll(current_site, current_user, @poll_id)

    context.registers[:controller].dup.render(partial: "shared/polls", locals: { poll: poll })
  end

  def next_poll(current_site, current_user, poll_id = nil)
    poll = GobiertoParticipation::Poll.by_site(current_site).find(poll_id) if poll_id
    answerable_polls = GobiertoParticipation::Poll.by_site(current_site).answerable.order(ends_at: :asc)
    answerable_polls_by_user = answerable_polls.detect { |p| p.answerable_by?(current_user) } if current_user

    if current_user
      if poll && poll.answerable_by?(current_user)
        poll
      elsif answerable_polls_by_user
        answerable_polls_by_user
      end
    else
      if poll
        poll
      else
        answerable_polls.first
      end
    end
  rescue ActiveRecord::RecordNotFound
    return ""
  end
end

Liquid::Template.register_tag("show_poll", ShowPoll)
{'fraction_non_alphanumeric': 0.10233592880978866, 'fraction_numerical': 0.0007415647015202076, 'mean_word_length': 3.344605475040258, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 3, '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': '6266020', 'n_tokens_mistral': 885, 'n_tokens_neox': 867, 'n_words': 155}