Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/slug for page #116

Open
wants to merge 3 commits into
base: feature/sites
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ def default_url_options
{}
end


def respond_error(message, status = nil)
status ||= 400
render json: { message: message }, status: status
end

private

def http_auth_for_staging
Expand All @@ -21,4 +27,8 @@ def http_auth_for_staging
username == "myapp" && password == "myapp"
end
end




end
87 changes: 87 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# == Schema Information
#
# Table name: pages
#
# id :integer not null, primary key
# title :string
# body :text
# slug :string
# site_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class PagesController < ApplicationController
before_action :set_site
before_action :set_pages
before_action :set_page, :only => [:update, :destroy, :show]

def show

end

def create
@page = @site.pages.build(page_params)

if @page.save
flash[:notice] = "success to create"
else
flash[:alert] = "failed to create"
@url = site_pages_path(@site)
@action = "post"
@submit_name = "Create"
end
redirect_to site_path(@site)
# respond_to do |format|
# format.js
# end

end

def update
if @page.update(page_params)

flash[:notice] = "success to update"

else
@url = site_page_path(@site, @page)
@action = "patch"
@submit_name = "Update"
flash[:alert] = "failed to update"

end
redirect_to site_path(@site)
end

def destroy
@page.destroy
flash[:notice] = "success to delete"
redirect_to site_path(@site)

# respond_to do |format|
# format.js
# end

end

private

def page_params
params.require(:page).permit(:title, :body, :slug)
end

def set_pages
@pages = @site.pages
end

def set_page
@page = Page.find_by(slug: params[:slug])

respond_error("not find page", 404) unless @page

end

def set_site
@site = Site.find(params[:site_id])
end
end
60 changes: 59 additions & 1 deletion app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,74 @@
#

class SitesController < ApplicationController
before_action :find_site
before_action :find_site, only: [:show, :edit, :update, :destroy]

def index
@sites = Site.all
end

def show
@pages = @site.pages
if params[:id] && params[:page_id]
@page = Page.find(params[:page_id])
@url = site_page_path(@site, @page.title)
@action = "patch"
@submit_name = "Update"
else
@page = @site.pages.build
@url = site_pages_path(@site)
@action = "post"
@submit_name = "Create"
end
end

def new
@site = Site.new
end

def create
@site = Site.new(site_params)

if @site.save
flash[:notice] = "success to create"
redirect_to site_path(@site)
else
flash[:alert] = "failed to create"
render :new
end
end

def edit

end

def update

if @site.update(site_params)
flash[:notice] = "success to update"
redirect_to site_path(@site)
else
flash[:alert] = "failed to update"
render :edit
end

end

def destroy
@site.destroy
flash[:notice] = "success to delete"
redirect_to sites_path
end


private

def find_site
@site = Site.find(params[:id])
end

def site_params
params.require(:site).permit(:name, :host, :data)
end

end
23 changes: 23 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# == Schema Information
#
# Table name: pages
#
# id :integer not null, primary key
# title :string
# body :text
# slug :string
# site_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#

class Page < ActiveRecord::Base
validates :title, presence: true
validates :slug, presence: true, uniqueness: true

belongs_to :site

def to_param
slug
end
end
2 changes: 2 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Site < ActiveRecord::Base

before_validation :generate_subdomain

has_many :pages, dependent: :destroy

private

def generate_subdomain
Expand Down
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ html
= javascript_include_tag 'application', 'data-turbolinks-track' => true
= csrf_meta_tags
body
- if flash[:notice].present?
div style="color: #0f0"
= flash[:notice]
- if flash[:alert].present?
div style="color: #f00"
= flash[:alert]
= yield
12 changes: 12 additions & 0 deletions app/views/pages/show.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
p
= @page.title
p
= @page.slug
p
= @page.body
p
= link_to "edit", site_path(@site, page_id: @page.id)
p
= link_to "destroy", site_page_path(@site, @page.slug), method: :delete
p
= link_to "back", site_path(@site)
15 changes: 15 additions & 0 deletions app/views/sites/_form.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- if @site.errors.any?
ul
- @site.errors.full_messages.each do |msg|
li = msg

p
= f.label :name, "Name"
= f.text_field :name
p
= f.label :host, "host"
= f.text_field :host

p
= f.label :data, "data"
= f.text_area :data
6 changes: 5 additions & 1 deletion app/views/sites/edit.html.slim
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
= @site.name
h2 = "Edit " + @site.name
= form_for @site, url: site_path(@site), method: :patch do |f|
= render partial: "form", locals: {f: f}

= f.submit "Update"
22 changes: 22 additions & 0 deletions app/views/sites/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
= link_to "new", new_site_path

table
thead
tr
td = "id"
td = "name"
td = "host"
td = "data"
td = "subdomain"
td
td
tbody
- @sites.each do |site|
tr
td = site.id
td = link_to site.name, site_path(site)
td = site.host
td = site.data
td = site.subdomain
td = link_to "edit", edit_site_path(site)
td = link_to "destroy", site_path(site), method: :delete
5 changes: 5 additions & 0 deletions app/views/sites/new.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
h2 New site
= form_for @site, url: sites_path do |f|
= render partial: "form", locals: {f: f}

= f.submit "create"
46 changes: 45 additions & 1 deletion app/views/sites/show.html.slim
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
= @site.name
p
= @site.name
p
= @site.host
p
= @site.data
p
= @site.subdomain
p
= link_to "edit", edit_site_path(@site)
p
= link_to "destroy", site_path(@site), method: :delete

= form_for @page, url: @url, method: @action do |f|
div
= f.label :title, "title:"
= f.text_field :title
div
= f.label :slug, "slug:"
= f.text_field :slug
div
= f.label :body, "body:"
= f.text_area :body

= f.submit @submit_name

table
thead
tr
td title
td slug
td body
td
td
tbody
- @pages.each do |page|
- unless page.new_record?
tr
td = link_to page.title, site_page_path(@site, page.slug)
td = page.slug
td = page.body
td = link_to "edit", site_path(@site, page_id: page.id)
td = link_to "destroy", site_page_path(@site, page.slug), method: :delete


6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
end
end

resources :sites, only: [:show, :edit]
resources :sites do
resources :pages, param: :slug
end


end
13 changes: 13 additions & 0 deletions db/migrate/20161206071038_create_pages.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :title
t.text :body
t.string :slug
t.integer :site_id

t.timestamps null: false
end
add_index :pages, :site_id
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160221104754) do
ActiveRecord::Schema.define(version: 20161206071038) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -44,6 +44,17 @@
add_index "categories", ["name"], name: "index_categories_on_name", using: :btree
add_index "categories", ["sort"], name: "index_categories_on_sort", using: :btree

create_table "pages", force: :cascade do |t|
t.string "title"
t.text "body"
t.string "slug"
t.integer "site_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "pages", ["site_id"], name: "index_pages_on_site_id", using: :btree

create_table "sites", force: :cascade do |t|
t.string "name"
t.string "host"
Expand Down
Loading