<commit_before># frozen_string_literal: true

#  News Articles
#
#  Actions
#
#    create:          Create article from data in "new" form
#    destroy::        Destroy article
#    edit::           Display form for editing article
#    index::          List all articles
#    index_article::  List selected (based on last search) articles
#    new::            Display form for new article
#    show::           Show one article
#    update::         Update article from "edit" form
#
#
#  Callbacks and Methods
#
#    ignore_request_unless_permitted:: Unless user permitted to perform request,
#                       then index_articles
#    permitted?         boolean: permitted to create/update/destroy Articles
#
class ArticlesController < ApplicationController
  before_action :login_required, except: [
    :index,
    :index_article,
    :list_articles, # aliased
    :show,
    :show_article # aliased
  ]
  before_action :store_location
  before_action :ignore_request_unless_permitted, except: [
    :index,
    :index_article,
    :list_articles, # aliased
    :show,
    :show_article # aliased
  ]

  ############ - Actions to Display data (index, show, etc.)

  # List selected articles, based on current Query.
  def index_articles
    query = find_or_create_query(:Article, by: params[:by])
    show_selected_articles(
      query,
      id: params[:id].to_s,
      always_index: true
    )
  end

  # List all articles
  def index
    query = create_query(
      :Article,
      :all,
      by: :created_at
    )
    show_selected_articles(query)
  end

  alias_method :list_articles, :index

  def show_article_sorts
    [
      ["created_at",  :sort_by_created_at.t],
      ["updated_at",  :sort_by_updated_at.t],
      ["user",        :sort_by_user.t],
      ["title",       :sort_by_title.t]
    ]
  end

  # Display one Article
  def show
    return false unless (@article = find_or_goto_index(Article, params[:id]))

    @canonical_url = "#{MO.http_domain}/articles/#{@article.id}"
  end

  alias_method :show_article, :show

  ############ Actions to Display forms -- (new, edit, etc.)
  def new
    @article = Article.new
  end

  alias_method :create_article, :new

  # Edit existing article
  def edit
    pass_query_params
    @article = find_or_goto_index(Article, params[:id])
  end

  alias_method :edit_article, :edit

  ############ Actions to Modify data: (create, update, destroy, etc.)
  def create
    return if flash_missing_title?

    @article = Article.new(
      title: params[:article][:title],
      body: params[:article][:body],
      user_id: @user.id
    )
    @article.save
    redirect_to article_path(@article.id)
  end

  def update
    pass_query_params
    @article = Article.find(params[:id])
    return if flash_missing_title?

    @article.title = params[:article][:title]
    @article.body = params[:article][:body]

    if @article.changed?
      raise(:runtime_unable_to_save_changes.t) unless @article.save

      flash_notice(:runtime_edit_article_success.t(id: @article.id))
    else
      flash_warning(:runtime_no_changes.t)
    end
    redirect_to article_path(@article.id)
  end

  alias_method :save_edits, :update

  # Destroy one article
  def destroy
    pass_query_params
    if (@article = Article.find(params[:id])) && @article.destroy
      flash_notice(:runtime_destroyed_id.t(type: Article, value: params[:id]))
    end
    redirect_to action: :index_article<commit_msg>ArticlesController trivial format changes<commit_after># frozen_string_literal: true

#  News Articles
#
#  Actions
#
#    create:          Create article from data in "new" form
#    destroy::        Destroy article
#    edit::           Display form for editing article
#    index::          List all articles
#    index_article::  List selected (based on last search) articles
#    new::            Display form for new article
#    show::           Show one article
#    update::         Update article from "edit" form
#
#
#  Callbacks and Methods
#
#    ignore_request_unless_permitted:: Unless user permitted to perform request,
#                       then index_articles
#    permitted?         boolean: permitted to create/update/destroy Articles
#
class ArticlesController < ApplicationController
  before_action :login_required, except: [
    :index,
    :index_article,
    :list_articles, # aliased
    :show,
    :show_article # aliased
  ]
  before_action :store_location
  before_action :ignore_request_unless_permitted, except: [
    :index,
    :index_article,
    :list_articles, # aliased
    :show,
    :show_article # aliased
  ]

  ############ - Actions to Display data (index, show, etc.)

  # List selected articles, based on current Query.
  def index_articles
    query = find_or_create_query(:Article, by: params[:by])
    show_selected_articles(
      query,
      id: params[:id].to_s,
      always_index: true
    )
  end

  # List all articles
  def index
    query = create_query(
      :Article,
      :all,
      by: :created_at
    )
    show_selected_articles(query)
  end

  alias_method :list_articles, :index

  def show_article_sorts
    [
      ["created_at",  :sort_by_created_at.t],
      ["updated_at",  :sort_by_updated_at.t],
      ["user",        :sort_by_user.t],
      ["title",       :sort_by_title.t]
    ]
  end

  # Display one Article
  def show
    return false unless (@article = find_or_goto_index(Article, params[:id]))

    @canonical_url = "#{MO.http_domain}/articles/#{@article.id}"
  end

  alias_method :show_article, :show

  ############ Actions to Display forms -- (new, edit, etc.)

  def new
    @article = Article.new
  end

  alias_method :create_article, :new

  # Edit existing article
  def edit
    pass_query_params
    @article = find_or_goto_index(Article, params[:id])
  end

  alias_method :edit_article, :edit

  ############ Actions to Modify data: (create, update, destroy, etc.)

  def create
    return if flash_missing_title?

    @article = Article.new(
      title: params[:article][:title],
      body: params[:article][:body],
      user_id: @user.id
    )
    @article.save
    redirect_to article_path(@article.id)
  end

  def update
    pass_query_params
    @article = Article.find(params[:id])
    return if flash_missing_title?

    @article.title = params[:article][:title]
    @article.body = params[:article][:body]

    if @article.changed?
      raise(:runtime_unable_to_save_changes.t) unless @article.save

      flash_notice(:runtime_edit_article_success.t(id: @article.id))
    else
      flash_warning(:runtime_no_changes.t)
    end
    redirect_to article_path(@article.id)
  end

  alias_method :save_edits, :update

  def destroy
    pass_query_params
    if (@article = Article.find(params[:id])) && @article.destroy
      flash_notice(:runtime_destroyed_id.t(type: Article, value: params[:id]))
    end
    redirect_to action: :index_article
{'fraction_non_alphanumeric': 0.10877752027809966, 'fraction_numerical': 0.0, 'mean_word_length': 3.038011695906433, 'pattern_counts': {'":': 0, '<': 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': '5985958', 'n_tokens_mistral': 2195, 'n_tokens_neox': 2098, 'n_words': 527}