<commit_before>class PageController < BaseController

  helpers do
    def slug
      Slug.slugify(params[:slug]) if params[:slug]
    end
  end

  get '/' do
    @page = Page.order(:id).first
    redirect "new" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  get '/list' do
    @page_groups = Page.order(:title_char, :title).to_hash_groups(:title_char)
    haml :index
  end

  get '/latest' do
    @page_groups = Page.order(:updated_on).reverse.
      select_append(Sequel.lit("DATE(updated_on)")).
      to_hash_groups(:date)
    haml :latest
  end

  get '/search' do
    @pages = Page.search(params[:q])
    @q = params[:q]

    case @pages.count
    when 0
      flash[:notice] = "Din sökning gav inga träffar"
      redirect request.referrer
    when 1
      flash[:confirm] = "Din sökning gav bara denna sida som träff"
      redirect @pages.first.slug
    else
      flash.now[:confirm] = "Din sökning gav #{@pages.count} träffar"
      haml :search
    end
  end

  post '/new*' do
    page = Page.create(title: params[:title], content: params[:content],description: params[:description], author: current_user)
    redirect "#{page.slug}"
  end

  get '/new' do
    @page = Page.new
    haml :new
  end

  get '/new/:slug' do
    @page = Page.new(title: slug)
    flash.now[:notice] = "Det finns ingen sida för #{slug}, du får skapa den!"
    haml :new
  end

  get '/:slug/edit' do
    @page = Page.find(slug: slug)
    haml :edit
  end

  get '/:slug' do
    @page = Page.find(slug: slug)
    redirect "new/#{slug}" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  get '/:slug/:revision' do |_, revision|
    @page = Revision.where(slug: slug, revision: revision).first
    redirect "#{slug}" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  post '/:slug' do
    page = Page.find(slug: slug)
    page.revise!
    page.update(title: params[:title], content: params[:content], description: params[:description], comment: params[:comment], author: current_user)

    redirect "#{page.slug}"
  end
end<commit_msg>Use Page#set_fields to assign attributes<commit_after>class PageController < BaseController

  helpers do
    def slug
      Slug.slugify(params[:slug]) if params[:slug]
    end
  end

  get '/' do
    @page = Page.order(:id).first
    redirect "new" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  get '/list' do
    @page_groups = Page.order(:title_char, :title).to_hash_groups(:title_char)
    haml :index
  end

  get '/latest' do
    @page_groups = Page.order(:updated_on).reverse.
      select_append(Sequel.lit("DATE(updated_on)")).
      to_hash_groups(:date)
    haml :latest
  end

  get '/search' do
    @pages = Page.search(params[:q])
    @q = params[:q]

    case @pages.count
    when 0
      flash[:notice] = "Din sökning gav inga träffar"
      redirect request.referrer
    when 1
      flash[:confirm] = "Din sökning gav bara denna sida som träff"
      redirect @pages.first.slug
    else
      flash.now[:confirm] = "Din sökning gav #{@pages.count} träffar"
      haml :search
    end
  end

  post '/new*' do
    page = Page.new
    page.set_fields(params, %i(title content description comment))
    page.author = current_user
    page.save
    redirect "#{page.slug}"
  end

  get '/new' do
    @page = Page.new
    haml :new
  end

  get '/new/:slug' do
    @page = Page.new(title: slug)
    flash.now[:notice] = "Det finns ingen sida för #{slug}, du får skapa den!"
    haml :new
  end

  get '/:slug/edit' do
    @page = Page.find(slug: slug)
    haml :edit
  end

  get '/:slug' do
    @page = Page.find(slug: slug)
    redirect "new/#{slug}" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  get '/:slug/:revision' do |_, revision|
    @page = Revision.where(slug: slug, revision: revision).first
    redirect "#{slug}" unless @page
    etag @page.sha1 unless logged_in?
    haml :show
  end

  post '/:slug' do
    page = Page.find(slug: slug)
    page.revise!
    page.set_fields(params, %i(title content description comment))
    page.author = current_user
    page.save

    redirect "#{page.slug}"
  end
end
{'fraction_non_alphanumeric': 0.12905534351145037, 'fraction_numerical': 0.002385496183206107, 'mean_word_length': 2.8257299270072993, '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': '2995639', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1489, 'n_words': 399}