<commit_before># coding: utf-8
class Api::ActionPlansController < Api::ApplicationController
  include HasOrders
  include ActionView::Helpers::SanitizeHelper

  before_action :authenticate_user!
  load_and_authorize_resource

  def index
    set_seed

    action_plans = @current_order == "recommended" ? Recommender.new(current_user).action_plans : ActionPlan.all

    @action_plans = ResourceFilter.new(params, user: current_user)
      .filter_collection(action_plans.includes(:category, :subcategory))

    respond_to do |format|
      format.json { 
        action_plans = @action_plans
          .send("sort_by_created_at")
          .page(params[:page])
          .per(15)

        render json: action_plans, meta: {
          seed: @random_seed,
          current_page: action_plans.current_page,
          next_page: action_plans.next_page,
          prev_page: action_plans.prev_page,
          total_pages: action_plans.total_pages,
          total_count: action_plans.total_count
        }
      }

      format.xls do
        send_data report(@action_plans), disposition: 'inline', filename: 'action_plans.xls'
      end
    end
  end

  def update
    @action_plan = ActionPlan.find(params[:id])
    @action_plan.assign_attributes(strong_params)
    @action_plan.save!
    render json: @action_plan
  end

  def destroy
    @action_plan = ActionPlan.find(params[:id])
    @action_plan.destroy!
    render nothing: true
  end

  def show
    render json: @action_plan
  end

  private

  def strong_params
    permitted_params = []
    permitted_params += [:approved] if can?(:approve, ActionPlan)
    permitted_params += [:scope, :district, :category_id, :subcategory_id] if can?(:manage, ActionPlan)
    params.require(:action_plan).permit(permitted_params)
  end

  def set_seed
    @random_seed = params[:random_seed] ? params[:random_seed].to_f : (rand * 2 - 1)
    ActionPlan.connection.execute "select setseed(#{@random_seed})"
  end


  def report(action_plans)
    package = Axlsx::Package.new do |p|
      p.workbook.add_worksheet(:name => "Action Plans") do |sheet|
        sheet.add_row [
          "ID",
          "Origen",
          "Aprovació",
          "Districte",
          "Categoria",
          "Subcategoria",
          "Títol",
          "Descripció",
          "URL",
          "Codi Proposta",
          "Autor",
          "Origen",
          "Districte",
          "Categoria",
          "Subcategoria",
          "Títol proposta",
          "Descripció proposta",
          "Vots",
          "Comentaris",
          "URL",
        ]
        action_plans.includes(:proposals => [:author, :category, :subcategory]).each do |action_plan|
          action_plan.proposals.each do |proposal| 
            sheet.add_row [
              action_plan.id,
              action_plan.official ? 'Ajuntament' : 'Ciutadania',
              action_plan.approved ? 'aprovat' : nil,
              action_plan.scope == 'district' ? District.find(action_plan.district).try(:name) : nil,
              action_plan.category.name[I18n.default_locale.to_s],
              action_plan.subcategory.name[I18n.default_locale.to_s],
              action_plan.title,
              strip_tags(action_plan.description),
              url_for(action_plan),
              proposal.code,
              proposal.author.try(:name),
              translate_source(proposal.source),
              proposal.scope == 'district' ? District.find(proposal.district).try(:name) : nil,<commit_msg>Add missing fields<commit_after># coding: utf-8
class Api::ActionPlansController < Api::ApplicationController
  include HasOrders
  include ActionView::Helpers::SanitizeHelper

  before_action :authenticate_user!
  load_and_authorize_resource

  def index
    set_seed

    action_plans = @current_order == "recommended" ? Recommender.new(current_user).action_plans : ActionPlan.all

    @action_plans = ResourceFilter.new(params, user: current_user)
      .filter_collection(action_plans.includes(:category, :subcategory))

    respond_to do |format|
      format.json { 
        action_plans = @action_plans
          .send("sort_by_created_at")
          .page(params[:page])
          .per(15)

        render json: action_plans, meta: {
          seed: @random_seed,
          current_page: action_plans.current_page,
          next_page: action_plans.next_page,
          prev_page: action_plans.prev_page,
          total_pages: action_plans.total_pages,
          total_count: action_plans.total_count
        }
      }

      format.xls do
        send_data report(@action_plans), disposition: 'inline', filename: 'action_plans.xls'
      end
    end
  end

  def update
    @action_plan = ActionPlan.find(params[:id])
    @action_plan.assign_attributes(strong_params)
    @action_plan.save!
    render json: @action_plan
  end

  def destroy
    @action_plan = ActionPlan.find(params[:id])
    @action_plan.destroy!
    render nothing: true
  end

  def show
    render json: @action_plan
  end

  private

  def strong_params
    permitted_params = []
    permitted_params += [:approved] if can?(:approve, ActionPlan)
    permitted_params += [:scope, :district, :category_id, :subcategory_id] if can?(:manage, ActionPlan)
    params.require(:action_plan).permit(permitted_params)
  end

  def set_seed
    @random_seed = params[:random_seed] ? params[:random_seed].to_f : (rand * 2 - 1)
    ActionPlan.connection.execute "select setseed(#{@random_seed})"
  end


  def report(action_plans)
    package = Axlsx::Package.new do |p|
      p.workbook.add_worksheet(:name => "Action Plans") do |sheet|
        sheet.add_row [
          "ID",
          "Origen",
          "Aprovació",
          "Districte",
          "Categoria",
          "Subcategoria",
          "Títol",
          "Descripció",
          "URL",
          "Codi (Proposta)",
          "Autor (Proposta)",
          "Origen (Proposta)",
          "Districte (Proposta)",
          "Categoria (Proposta)",
          "Subcategoria (Proposta)",
          "Títol (Proposta)",
          "Descripció (Proposta)",
          "Vots",
          "Comentaris",
          "URL (Proposta)",
        ]
        action_plans.includes(:proposals => [:author, :category, :subcategory]).each do |action_plan|
          action_plan.proposals.each do |proposal| 
            sheet.add_row [
              action_plan.id,
              action_plan.official ? 'Ajuntament' : 'Ciutadania',
              action_plan.approved ? 'aprovat' : nil,
              action_plan.scope == 'district' ? District.find(action_plan.district).try(:name) : nil,
              action_plan.category.name[I18n.default_locale.to_s],
              action_plan.subcategory.name[I18n.default_locale.to_s],
              action_plan.title,
              strip_tags(action_plan.description),
              url_for(action_plan),
              proposal.code,
              proposal.author.try(:name),
              translate_source(proposal.source),
              proposal.scope == 'district' ? District.find(proposal.district).try(:name) : nil,
{'fraction_non_alphanumeric': 0.11046593966860219, 'fraction_numerical': 0.002549213992352358, 'mean_word_length': 2.4943097476496785, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4639985', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2180, 'n_words': 381}