Skip to content

Commit

Permalink
refs #7 Adds new model Page. Replaces Page Modules page by Pages (Pag…
Browse files Browse the repository at this point in the history
…es & Modules) page and show here a tab pane including pages, module collections and modules.
  • Loading branch information
volontarian committed Mar 26, 2015
1 parent c98dfe0 commit 76a4097
Show file tree
Hide file tree
Showing 25 changed files with 415 additions and 50 deletions.
20 changes: 5 additions & 15 deletions app/controllers/page_module_collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,11 @@ class PageModuleCollectionsController < ApplicationController
before_filter :show_breadcrumbs, except: :index

def index
slug_stub = if params[:slug_stub].blank?
@page_module_collection_slug_stubs = PageModuleCollection.pluck(:slug_stub).uniq.sort
@page_module_collection_slug_stubs.first
else
params[:slug_stub]
end
redirect_to pages_path if params[:slug_stub].blank?

@page_module_collections = PageModuleCollection.where(slug_stub: slug_stub).paginate(page: params[:page], per_page: 10)

if params[:slug_stub].blank?
@page_module_slug_stubs = PageModule.pluck(:slug_stub).uniq.sort
@page_modules = PageModule.where(slug_stub: @page_module_slug_stubs.first).paginate(page: params[:module_page], per_page: 10)
end
@page_module_collections = PageModuleCollection.where(slug_stub: params[:slug_stub]).paginate(page: params[:page], per_page: 10)

render partial: 'page_module_collections/collection', layout: false if params[:slug_stub].present?
render partial: 'page_module_collections/collection', layout: false
end

def new
Expand All @@ -30,10 +20,10 @@ def create
@page_module_collection = PageModuleCollection.create(params[:page_module_collection])

if @page_module_collection.persisted?
@path = page_module_collections_path
@path = page_module_collections_path(slug_stub: @page_module_collection.slug_stub)

if request.xhr?
@target = '#page'
@target = "#page_module_collections_slug_stub_#{@page_module_collection.slug_stub}"
@close_modal = true
end
else
Expand Down
95 changes: 95 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
class PagesController < ApplicationController
respond_to :html, :js

before_filter :authenticate_user!, except: [:show]
before_filter :show_breadcrumbs

def index
@pages = Page.paginate(page: params[:page], per_page: 10)

if params[:page].blank?
@page_module_collection_slug_stubs = PageModuleCollection.pluck(:slug_stub).uniq.sort
slug_stub = @page_module_collection_slug_stubs.first
@page_module_collections = PageModuleCollection.where(slug_stub: slug_stub).paginate(page: params[:page], per_page: 10)
@page_module_slug_stubs = PageModule.pluck(:slug_stub).uniq.sort
@page_modules = PageModule.where(slug_stub: @page_module_slug_stubs.first).paginate(page: params[:module_page], per_page: 10)
else
render partial: 'pages/collection', layout: false
end
end

def new
@page = Page.new(params[:page])
end

def create
@page = Page.create(params[:page])

if @page.persisted?
@path = pages_path(page: 1)

if request.xhr?
@target = '#pages_container'
@close_modal = true
end
else
@template = :new

if request.xhr?
@target = '.modal-content'
@target_needs_modal_layout = false
end
end

render_or_redirect_by_request_type
end

def show
@page = Page.friendly.find(params[:id])
end

def edit
@page = Page.friendly.find(params[:id])
end

def update
@page = Page.friendly.find(params[:id])

if @page.update_attributes(params[:page])
flash[:notice] = t('general.form.successfully_updated')

if request.xhr?
@template_format = 'js'
else
@path = edit_page_path(@page_module)
end
else
@template = :edit
@target = '.modal-content' if request.xhr?
end

render_or_redirect_by_request_type
end

def destroy
@page = Page.friendly.find(params[:id]).destroy

if @page.persisted?
flash[:alert] = I18n.t('general.form.destroy_failed')
else
flash[:notice] = I18n.t('general.form.destroyed')
end

redirect_to pages_path unless request.xhr?
end

def resource
@page
end

protected

def show_breadcrumbs
@show_breadcrumbs = true if user_signed_in?
end
end
4 changes: 4 additions & 0 deletions app/helpers/home_page/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ def attribute_translation(attribute, current_resource = nil)
default: t("attributes.#{attribute}")
)
end

def liquidize(content, arguments = {})
RedCloth.new(Liquid::Template.parse(content).render(arguments)).to_html
end
end
end
31 changes: 31 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Page < ActiveRecord::Base
serialize :data, Hash

validates :title, presence: true, uniqueness: true
validates :content, presence: true

validate :valid_liquid_syntax

attr_accessible :title, :content, :data

extend FriendlyId

friendly_id :title, use: :slugged

private

def valid_liquid_syntax
Liquid::Template.parse(content)
rescue Liquid::SyntaxError => e
errors.add(
:content,
I18n.t(
'activerecord.errors.models.page_module.attributes.content.liquid_syntax_invalid', message: e.message
)
)
end

def should_generate_new_friendly_id?
title_changed?
end
end
18 changes: 18 additions & 0 deletions app/views/pages/_collection.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% if @pages.none? %>
<p><%= t('pages.index.empty_collection') %></p>
<% else %>
<table class='table table-striped'>
<thead>
<tr class='<%= cycle('odd', 'even') %>'>
<th style='width: 200px'><%= t('general.attributes.title') %></th>
<th style='width: 200px'><%= t('general.attributes.slug') %></th>
<th></th>
</tr>
</thead>
<tbody>
<%= render partial: 'pages/page', collection: @pages %>
</tbody>
</table>

<%= will_paginate @pages, renderer: BootstrapPagination::Rails %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/pages/_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= devise_error_messages! %>

<%= f.input :title %>
<%= f.input :content %>
25 changes: 25 additions & 0 deletions app/views/pages/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<% content_for :modal_footer do %>
<button type="button" class="btn btn-default" data-dismiss="modal"><%= t('general.close') %></button>
<button type="submit" class="btn btn-primary"><%= t('general.submit') %></button>
<% end %>

<%= simple_form_for(
resource, url: resource.new_record? ? pages_path : page_path(resource), wrapper: :horizontal_form,
method: resource.new_record? ? :post : :put, remote: request.xhr?, html: { class: 'form-horizontal', autocomplete: 'off' }
) do |f| %>
<% if request.xhr? %>
<% content_for :modal_body do %>
<%= render partial: 'pages/fields', locals: { f: f } %>
<% end %>

<%= render partial: 'shared/layouts/modal' %>
<% else %>
<%= render partial: 'pages/fields', locals: { f: f } %>

<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-primary"><%= t('general.submit') %></button>
</div>
</div>
<% end %>
<% end %>
35 changes: 35 additions & 0 deletions app/views/pages/_page.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<tr class="<%= cycle('odd', 'even') %>" id="pages_row_<%= page.id %>">
<td>
<%= link_to page.title, page_path(page) %>
</td>
<td>
<%= page.slug %>
</td>
<td>
<div class="dropdown">
<button
class="btn btn-default dropdown-toggle" type="button" id="page_<%= page.id %>_actions"
data-toggle="dropdown" aria-expanded="true">
<%= t('general.actions') %>
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" aria-labelledby="page_<%= page.id %>_actions">
<li role="presentation">
<%= link_to(
t('general.destroy'), page_path(page),
id: "page_#{page.id}", method: :delete, data: { confirm: t('general.questions.are_you_sure') }, remote: true
) %>
</li>
<li role="presentation">
&nbsp;&nbsp;
<button
type="button" class="btn btn-default" data-toggle="modal" data-target="#modal"
data-url="<%= edit_page_path(page) %>" data-title="<%= t('pages.edit.title') %>" data-only-update-body="false"
>
<span class="glyphicon glyphicon-pencil"></span> <%= t('general.edit') %>
</button>
</li>
</ul>
</div>
</td>
</tr>
39 changes: 39 additions & 0 deletions app/views/pages/_tab.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<p>
<% if @page_module_slug_stubs.none? %>
<div class="alert alert-info" role="alert">
<%= t('page_modules.index.empty_collection') %>
</div>
<% else %>
<div role="tabpanel">
<ul class="nav nav-tabs" role="tablist">
<% @page_module_slug_stubs.each_with_index do |slug_stub, index| %>
<li role="presentation"<% if index == 0 %> class="active"<% end %>>
<a
href="<%= page_modules_path(slug_stub: slug_stub) %>"
data-target="#page_modules_slug_stub_<%= slug_stub %>" aria-controls="page_modules_slug_stub_<%= slug_stub %>"
role="tab" data-toggle="tabajax"
>
<%= slug_stub.titleize %>
</a>
</li>
<% end %>
</ul>
<div class="tab-content">
<% @page_module_slug_stubs.each_with_index do |slug_stub, index| %>
<div role="tabpanel" class="tab-pane active" id="page_modules_slug_stub_<%= slug_stub %>">
<%= render partial: 'page_modules/collection' if index == 0 %>
</div>
<% end %>
</div>
</div>
<% end %>
</p>

<p>
<button
type="button" class="btn btn-default" data-toggle="modal" data-target="#modal" data-url="<%= new_page_module_path %>"
data-title="<%= t('page_modules.new.title') %>" data-only-update-body="false"
>
<span class="glyphicon glyphicon-plus"></span> <%= t('page_modules.new.short_title') %>
</button>
</p>
9 changes: 9 additions & 0 deletions app/views/pages/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% message = flash[:notice] || flash[:alert] %>
<% flash.delete(:notice); flash.delete(:alert) %>
<% alert = message.present? ? "alert('#{message}');" : '' %>

<% unless @page.persisted? %>
$("#pages_row_<%= @page.id %>").remove();
<% end %>

<%= raw alert %>
3 changes: 3 additions & 0 deletions app/views/pages/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% content_for :title do %><%= t('pages.edit.title') %><% end %>

<%= render partial: 'pages/form' %>
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
<% content_for :title do %><%= t('page_modules.index.title') %><% end %>
<% content_for :title do %><%= t('pages.index.long_title') %><% end %>

<div role="tabpanel">
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active">
<a href="#pages_tab" aria-controls="pages_tab" role="tab" data-toggle="tab">
<%= t('pages.index.title') %>
</a>
</li>
<li role="presentation">
<a href="#page_module_collections_tab" aria-controls="page_module_collections_tab" role="tab" data-toggle="tab">
<%= t('page_module_collections.index.short_title') %>
<%= t('page_module_collections.index.medium_title') %>
</a>
</li>
<li role="presentation">
<a href="#page_modules_tab" aria-controls="page_modules_tab" role="tab" data-toggle="tab">
<%= t('page_modules.index.short_title') %>
<%= t('page_modules.index.title') %>
</a>
</li>
</ul>
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="page_module_collections_tab">
<div role="tabpanel" class="tab-pane active" id="pages_tab">
<div id="pages_container">
<% if @pages.none? %>
<p>
<div class="alert alert-info" role="alert">
<%= t('pages.index.empty_collection') %>
</div>
</p>
<% else %>
<%= render partial: 'pages/collection' %>
<% end %>
</div>

<p>
<button
type="button" class="btn btn-default" data-toggle="modal" data-target="#modal" data-url="<%= new_page_path %>"
data-title="<%= t('pages.new.title') %>" data-only-update-body="false"
>
<span class="glyphicon glyphicon-plus"></span> <%= t('pages.new.title') %>
</button>
</p>
</div>
<div role="tabpanel" class="tab-pane" id="page_module_collections_tab">
<p>
<% if @page_module_collection_slug_stubs.none? %>
<div class="alert alert-info" role="alert">
Expand Down Expand Up @@ -63,5 +90,5 @@
</div>

<% content_for :javascript_includes do %>
<%= javascript_include_tag 'home_page/page_module_collections/index' %>
<%= javascript_include_tag 'home_page/pages/index' %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/pages/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% content_for :title do %><%= t('pages.new.title') %><% end %>

<%= render partial: 'pages/form' %>
3 changes: 3 additions & 0 deletions app/views/pages/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% content_for :title do %><%= @page.title %><% end %>

<%= raw liquidize(@page.content) %>
Loading

0 comments on commit 76a4097

Please sign in to comment.