-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pages: Create controller, model, migration, basic RSpecs (refs #286)
- Loading branch information
Showing
11 changed files
with
211 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
class PagesController < ApplicationController | ||
before_action :set_page, only: %i[show edit update destroy] | ||
|
||
def index | ||
@pages = if admin? | ||
Page.all | ||
else | ||
Page.published | ||
end | ||
end | ||
|
||
def new | ||
@page = Page.new | ||
end | ||
|
||
def edit; end | ||
|
||
def create | ||
@page = Page.new(page_params) | ||
|
||
respond_to do |format| | ||
if @page.save | ||
format.html { redirect_to page_url(@page), notice: 'Page was successfully created.' } | ||
format.json { render :index, status: :created, location: @page } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @page.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /people/1 or /people/1.json | ||
def update | ||
respond_to do |format| | ||
if @page.update(page_params) | ||
format.html { redirect_to page_url(@page), notice: 'Page was successfully updated.' } | ||
format.json { render :index, status: :ok, location: @page } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @page.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
if @page.destroy | ||
respond_to do |format| | ||
format.html { redirect_to page_url(@page), notice: 'Page was successfully destroyed.' } | ||
format.json { head :no_content } | ||
end | ||
else | ||
respond_to do |format| | ||
format.html { redirect_to page_url(@page), notice: 'Page could not be destroyed, since its connected to 1 or more annotations.' } | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_page | ||
@page = Page.find(params[:id]) | ||
end | ||
|
||
def page_params | ||
params.require(:page).permit(:title, :is_published, :is_in_menu, :ptype, :teasertext, :fulltext, :footertext) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# frozen_string_literal: true | ||
|
||
module PagesHelper | ||
def ptype_for_select | ||
ptypes = %w[help faq imprint privacy] | ||
ptypes.each_with_object({}) { |e, m| m[e.capitalize] = e } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class Page < ApplicationRecord | ||
validates :title, presence: true | ||
|
||
scope :sorted, -> { order(title: :asc) } | ||
scope :published, -> { where(is_published: true) } | ||
scope :in_menu, -> { where(is_in_menu: true) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreatePages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :pages do |t| | ||
t.boolean :is_published, default: false | ||
t.boolean :in_menu, default: false | ||
t.string :ptype | ||
t.string :title | ||
t.text :teasertext | ||
t.text :fulltext | ||
t.text :footertext | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.