start on homegrown course admin

This commit is contained in:
Torsten Ruger 2017-06-06 22:07:44 +03:00
parent 886e88175c
commit a22794d5a2
15 changed files with 154 additions and 432 deletions

View File

@ -1,20 +1,56 @@
module Admin
class CoursesController < Admin::ApplicationController
class CoursesController < AdminController
# To customize the behavior of this controller,
# simply overwrite any of the RESTful actions. For example:
#
# def index
# super
# @resources = Course.all.paginate(10, params[:page])
# end
before_action :load_course, :only => [:show, :edit, :update, :destroy]
# Define a custom finder by overriding the `find_resource` method:
# def find_resource(param)
# Course.find_by!(slug: param)
# end
def index
@q = Course.search(params[:q])
@course_scope = @q.result(:distinct => true)
@courses = @course_scope.paginate(:page => params[:page], :per_page => 20)
end
# See https://administrate-docs.herokuapp.com/customizing_controller_actions
# for more information
def show
end
def new
@course = Course.new
render :edit
end
def edit
end
def create
@course = Course.create(params_for_course)
if @course.save
redirect_to course_path(@course), :notice => t(:create_success, :model => :course)
else
render :edit
end
end
def update
@course.update_attributes params_for_course
if @course.save
redirect_to course_path(@course), :notice => t(:update_success, :model => :course)
else
render :action => :edit
end
end
def destroy
# @course.deleted_at = Time.now
redirect_to courses_url
end
private
def load_course
@course = Course.find(params[:id])
end
def params_for_course
params.require(:course).permit!
end
end
end

View File

@ -1,92 +0,0 @@
<%#
# Collection
This partial is used on the `index` and `show` pages
to display a collection of resources in an HTML table.
## Local variables:
- `collection_presenter`:
An instance of [Administrate::Page::Collection][1].
The table presenter uses `ResourceDashboard::COLLECTION_ATTRIBUTES` to determine
the columns displayed in the table
- `resources`:
An ActiveModel::Relation collection of resources to be displayed in the table.
By default, the number of resources is limited by pagination
or by a hard limit to prevent excessive page load times
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>
<table aria-labelledby="page-title">
<thead>
<tr>
<% collection_presenter.attribute_types.each do |attr_name, attr_type| %>
<th class="cell-label
cell-label--<%= attr_type.html_class %>
cell-label--<%= collection_presenter.ordered_html_class(attr_name) %>
" scope="col">
<%= link_to(sanitized_order_params.merge(
collection_presenter.order_params_for(attr_name)
)) do %>
<%= t(
"helpers.label.#{resource_name}.#{attr_name}",
default: attr_name.to_s,
).titleize %>
<% if collection_presenter.ordered_by?(attr_name) %>
<span class="cell-label__sort-indicator cell-label__sort-indicator--<%= collection_presenter.ordered_html_class(attr_name) %>">
<%= svg_tag(
"administrate/sort_arrow.svg",
"sort_arrow",
width: "13",
height: "13"
) %>
</span>
<% end %>
<% end %>
</th>
<% end %>
<% [valid_action?(:edit), valid_action?(:destroy)].count(true).times do %>
<th scope="col"></th>
<% end %>
</tr>
</thead>
<tbody>
<% resources.each do |resource| %>
<tr class="js-table-row"
tabindex="0"
<%= %(role=link data-url=#{polymorphic_path([namespace, resource])}) if valid_action? :show -%>
>
<% collection_presenter.attributes_for(resource).each do |attribute| %>
<td class="cell-data cell-data--<%= attribute.html_class %>">
<a href="<%= polymorphic_path([namespace, resource]) -%>"
class="action-show"
>
<%= render_field attribute %>
</a>
</td>
<% end %>
<% if valid_action? :edit %>
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) %></td>
<% end %>
<% if valid_action? :destroy %>
<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "text-color-red",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) %></td>
<% end %>
</tr>
<% end %>
</tbody>
</table>

View File

@ -1,20 +0,0 @@
%table{"aria-labelledby" => "page-title"}
%thead
%tr
- collection_presenter.attribute_types.each do |attr_name, attr_type|
%th.cell-label.cell-label--
= attr_type.html_class
cell-label--#{collection_presenter.ordered_html_class(attr_name)}
" scope="col"&gt;
\#{link_to(sanitized_order_params.merge( collection_presenter.order_params_for(attr_name) )) do}
= t( |
helpers.label.#{resource_name}.#{attr_name}, |
default: attr_name.to_s, |
).titleize |
- if collection_presenter.ordered_by?(attr_name)
%span.cell-label__sort-indicator.cell-label__sort-indicator--
= collection_presenter.ordered_html_class(attr_name)
"&gt;
\#{svg_tag( administrate/sort_arrow.svg, sort_arrow, width: 13, height: 13 )}
- [valid_action?(:edit), valid_action?(:destroy)].count(true).times do
%th{:scope => "col"}

View File

@ -1,42 +0,0 @@
<%#
# Form Partial
This partial is rendered on a resource's `new` and `edit` pages,
and renders all form fields for a resource's editable attributes.
## Local variables:
- `page`:
An instance of [Administrate::Page::Form][1].
Contains helper methods to display a form,
and knows which attributes should be displayed in the resource's form.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
%>
<%= form_for([namespace, page.resource], html: { class: "form" }) do |f| %>
<% if page.resource.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(page.resource.errors.count, "error") %>
prohibited this <%= page.resource_name %> from being saved:
</h2>
<ul>
<% page.resource.errors.full_messages.each do |message| %>
<li class="flash-error"><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<% page.attributes.each do |attribute| -%>
<div class="field-unit field-unit--<%= attribute.html_class %>">
<%= render_field attribute, f: f %>
</div>
<% end -%>
<div class="form-actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -1,14 +0,0 @@
= form_for([namespace, page.resource], html: { class: "form" }) do |f|
- if page.resource.errors.any?
#error_explanation
%h2
= pluralize(page.resource.errors.count, "error")
prohibited this #{page.resource_name} from being saved:
%ul
- page.resource.errors.full_messages.each do |message|
%li.flash-error= message
- page.attributes.each do |attribute|
%div{:class => "field-unit field-unit--#{attribute.html_class}"}
= render_field attribute, f: f
.form-actions
= f.submit

View File

@ -1,36 +0,0 @@
<%#
# Edit
This view is the template for the edit page.
It displays a header, and renders the `_form` partial to do the heavy lifting.
## Local variables:
- `page`:
An instance of [Administrate::Page::Form][1].
Contains helper methods to help display a form,
and knows which attributes should be displayed in the resource's form.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
%>
<% content_for(:title) { "#{t("administrate.actions.edit")} #{page.page_title}" } %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
"#{t("administrate.actions.show")} #{page.page_title}",
[namespace, page.resource],
class: "button",
) if valid_action? :show %>
</div>
</header>
<section class="main-content__body">
<%= render "form", page: page %>
</section>

View File

@ -1,13 +1,10 @@
%body
- content_for(:title) { "#{t("administrate.actions.edit")} #{page.page_title}" }
%header.main-content__header{:role => "banner"}
%h1.main-content__page-title
= content_for(:title)
%div
= link_to( |
"#{t("administrate.actions.show")} #{page.page_title}", |
[namespace, page.resource], |
class: "button", |
) if valid_action? :show |
%section.main-content__body
= render "form", page: page
%h2.courses_edit_start
= t params["action"]
= t(:course)
= bootstrap_form_for([:admin , @course]) do |f|
.row
= f.text_field :name
.form-actions
= f.submit :class => "btn btn-success"
= link_to t(:back), courses_path, :class => "btn btn-warning"
.courses_edit_end

View File

@ -1,53 +0,0 @@
<%#
# Index
This view is the template for the index page.
It is responsible for rendering the search bar, header and pagination.
It renders the `_table` partial to display details about the resources.
## Local variables:
- `page`:
An instance of [Administrate::Page::Collection][1].
Contains helper methods to help display a table,
and knows which attributes should be displayed in the resource's table.
- `resources`:
An instance of `ActiveRecord::Relation` containing the resources
that match the user's search criteria.
By default, these resources are passed to the table partial to be displayed.
- `search_term`:
A string containing the term the user has searched for, if any.
- `show_search_bar`:
A boolean that determines if the search bar should be shown.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>
<% content_for(:title) do %>
<%= display_resource_name(page.resource_name) %>
<% end %>
<% content_for(:search) do %>
<% if show_search_bar %>
<%= render "search", search_term: search_term %>
<% end %>
<% end %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title" id="page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
"#{t("administrate.actions.new")} #{page.resource_name.titleize.downcase}",
[:new, namespace, page.resource_path],
class: "button",
) if valid_action? :new %>
</div>
</header>
<section class="main-content__body main-content__body--flush">
<%= render "collection", collection_presenter: page, resources: resources %>
<%= paginate resources %>
</section>

View File

@ -1,18 +1,38 @@
%body
- content_for(:title) do
= display_resource_name(page.resource_name)
- content_for(:search) do
- if show_search_bar
= render "search", search_term: search_term
%header.main-content__header{:role => "banner"}
%h1#page-title.main-content__page-title
= content_for(:title)
.row.courses_index_start
.col-md-9.courses
.row
.col-md-10
%h2 Courses
= paginate @courses
.col-md-2
= link_to "New course" , new_admin_course_path , :class => "btn btn-primary"
.col-md-3
.row
.col-md-9
%div
= link_to( |
"#{t("administrate.actions.new")} #{page.resource_name.titleize.downcase}", |
[:new, namespace, page.resource_path], |
class: "button", |
) if valid_action? :new |
%section.main-content__body.main-content__body--flush
= render "collection", collection_presenter: page, resources: resources
= paginate resources
%table.table.table-striped.table-bordered.table-condensed
%thead
%tr
%th=sort_link [:admin,@q], :name
%th Extra
%th Start
%th{:colspan => "2"}
%label.checkbox
= @course_scope.length.to_s + " " + t(:courses)
%tbody
- @courses.each do |course|
%tr{:class => "line-#{cycle("1","2")}"}
%td.name=course.name
%td=course.extra
%td=course.start
%td= link_to "Show" , course_path(course), :title => "Show"
%td= link_to "Edit" , edit_admin_course_path(course), :title => "Edit"
= paginate @courses
.col-md-3
= search_form_for [:admin,@q], :html => { :class => "form-horizontal" }, :method => :get do |f|
.form-group
= f.text_field :name_cont , :class => "form-control", :placeholder => "Name"
= f.submit "Filter", :id => :filter , :class => "btn btn-success"
= link_to "Cancel", admin_courses_path, :class => "btn btn-warning"
.courses_index_end

View File

@ -1,32 +0,0 @@
<%#
# New
This view is the template for the "new resource" page.
It displays a header, and then renders the `_form` partial
to do the heavy lifting.
## Local variables:
- `page`:
An instance of [Administrate::Page::Form][1].
Contains helper methods to help display a form,
and knows which attributes should be displayed in the resource's form.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
%>
<% content_for(:title) { "#{t("administrate.actions.new")} #{page.resource_name.titleize}" } %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to t("administrate.actions.back"), :back, class: "button" %>
</div>
</header>
<section class="main-content__body">
<%= render "form", page: page %>
</section>

View File

@ -1,9 +0,0 @@
%body
- content_for(:title) { "#{t("administrate.actions.new")} #{page.resource_name.titleize}" }
%header.main-content__header{:role => "banner"}
%h1.main-content__page-title
= content_for(:title)
%div
= link_to t("administrate.actions.back"), :back, class: "button"
%section.main-content__body
= render "form", page: page

View File

@ -1,49 +0,0 @@
<%#
# Show
This view is the template for the show page.
It renders the attributes of a resource,
as well as a link to its edit page.
## Local variables:
- `page`:
An instance of [Administrate::Page::Show][1].
Contains methods for accessing the resource to be displayed on the page,
as well as helpers for describing how each attribute of the resource
should be displayed.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Show
%>
<% content_for(:title) { "#{t("administrate.actions.show")} #{page.page_title}" } %>
<header class="main-content__header" role="banner">
<h1 class="main-content__page-title">
<%= content_for(:title) %>
</h1>
<div>
<%= link_to(
"#{t("administrate.actions.edit")} #{page.page_title}",
[:edit, namespace, page.resource],
class: "button",
) if valid_action? :edit %>
</div>
</header>
<section class="main-content__body">
<dl>
<% page.attributes.each do |attribute| %>
<dt class="attribute-label">
<%= t(
"helpers.label.#{resource_name}.#{attribute.name}",
default: attribute.name.titleize,
) %>
</dt>
<dd class="attribute-data attribute-data--<%=attribute.html_class%>"
><%= render_field attribute %></dd>
<% end %>
</dl>
</section>

View File

@ -1,20 +1,19 @@
%body
- content_for(:title) { "#{t("administrate.actions.show")} #{page.page_title}" }
%header.main-content__header{:role => "banner"}
%h1.main-content__page-title
= content_for(:title)
%div
= link_to( |
"#{t("administrate.actions.edit")} #{page.page_title}", |
[:edit, namespace, page.resource], |
class: "button", |
) if valid_action? :edit |
%section.main-content__body
%dl
- page.attributes.each do |attribute|
%dt.attribute-label
= t( |
"helpers.label.#{resource_name}.#{attribute.name}", |
default: attribute.name.titleize, |
) |
%dd{:class => "attribute-data attribute-data--#{attribute.html_class}"}= render_field attribute
%h2.courses_show_start
Course :
= @course.name
.col-md-12
%p
%b
= "Name: " + @course.name.to_s
%p
%b
= "Extra : " + @course.extra.to_s
%p
%b
= "Start : " + @course.start.to_s
= link_to "Edit", edit_admin_course_path(@course), :class => "btn btn-success"
= link_to "Back", admin_courses_path, :class => "btn btn-warning"
.courses_show_end

View File

@ -9,13 +9,28 @@ Rails.application.configure do
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Show full error reports.
config.consider_all_requests_local = true
# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false
config.cache_store = :null_store
end
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
@ -27,25 +42,13 @@ Rails.application.configure do
# number of complex assets.
config.assets.debug = true
config.action_mailer.smtp_settings = {
}
# ActionMailer Config
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config.action_mailer.delivery_method = :sendmail
config.action_mailer.raise_delivery_errors = true
# Send email in development mode?
config.action_mailer.perform_deliveries = true
# Asset digests allow you to set far-future HTTP expiration dates on all assets,
# yet still be able to expire them through the digest params.
config.assets.digest = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Suppress logger output for asset requests.
config.assets.quiet = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

View File

@ -0,0 +1,14 @@
describe "Admin Course" do
before(:each) do
sign_admin_in
end
it "lists courses" do
#create(:course)
visit_path admin_courses_path
end
it "shows a course" do
course = create(:course)
visit_path admin_course_path(course)
end
end