mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-17 06:52:27 +01:00
[feature] manage event themes
This commit is contained in:
parent
9adc219edb
commit
51cce7bbc3
@ -136,8 +136,8 @@ class EventsController
|
||||
##
|
||||
# Controller used in the events listing page (admin view)
|
||||
##
|
||||
Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'Event', 'Category', 'eventsPromise', 'categoriesPromise'
|
||||
, ($scope, $state, Event, Category, eventsPromise, categoriesPromise) ->
|
||||
Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'Event', 'Category', 'EventThemes', 'eventsPromise', 'categoriesPromise', 'themesPromise'
|
||||
, ($scope, $state, Event, Category, EventThemes, eventsPromise, categoriesPromise, themesPromise) ->
|
||||
|
||||
|
||||
|
||||
@ -152,9 +152,18 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state",
|
||||
## Current virtual page
|
||||
$scope.page = 2
|
||||
|
||||
## Temporary datastore for creating new elements
|
||||
$scope.inserted =
|
||||
category: null
|
||||
theme: null
|
||||
age_range: null
|
||||
|
||||
## List of categories for the events
|
||||
$scope.categories = categoriesPromise
|
||||
|
||||
## List of events themes
|
||||
$scope.themes = themesPromise
|
||||
|
||||
##
|
||||
# Adds a bucket of events to the bottom of the page, grouped by month
|
||||
##
|
||||
@ -166,49 +175,53 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state",
|
||||
|
||||
|
||||
##
|
||||
# Saves a new categoty / Update an existing one to the server (form validation callback)
|
||||
# @param data {Object} category name
|
||||
# @param [data] {number} category id, in case of update
|
||||
# Saves a new element / Update an existing one to the server (form validation callback)
|
||||
# @param model {string} model name
|
||||
# @param data {Object} element name
|
||||
# @param [id] {number} element id, in case of update
|
||||
##
|
||||
$scope.saveCategory = (data, id) ->
|
||||
$scope.saveElement = (model, data, id) ->
|
||||
if id?
|
||||
Category.update {id: id}, data
|
||||
getModel(model)[0].update {id: id}, data
|
||||
else
|
||||
Category.save data, (resp)->
|
||||
$scope.categories[$scope.categories.length-1].id = resp.id
|
||||
getModel(model)[0].save data, (resp)->
|
||||
getModel(model)[1][getModel(model)[1].length-1].id = resp.id
|
||||
|
||||
|
||||
|
||||
##
|
||||
# Deletes the category at the specified index
|
||||
# @param index {number} category index in the $scope.categories array
|
||||
# Deletes the element at the specified index
|
||||
# @param model {string} model name
|
||||
# @param index {number} element index in the $scope[model] array
|
||||
##
|
||||
$scope.removeCategory = (index) ->
|
||||
Category.delete $scope.categories[index]
|
||||
$scope.categories.splice(index, 1)
|
||||
$scope.removeElement = (model, index) ->
|
||||
getModel(model)[0].delete getModel(model)[1][index]
|
||||
getModel(model)[1].splice(index, 1)
|
||||
|
||||
|
||||
|
||||
##
|
||||
# Creates a new empty entry in the $scope.categories array
|
||||
# Creates a new empty entry in the $scope[model] array
|
||||
# @param model {string} model name
|
||||
##
|
||||
$scope.addCategory = ->
|
||||
$scope.inserted =
|
||||
$scope.addElement = (model) ->
|
||||
$scope.inserted[model] =
|
||||
name: ''
|
||||
$scope.categories.push($scope.inserted)
|
||||
getModel(model)[1].push($scope.inserted[model])
|
||||
|
||||
|
||||
|
||||
##
|
||||
# Removes the newly inserted but not saved category / Cancel the current category modification
|
||||
# Removes the newly inserted but not saved element / Cancel the current element modification
|
||||
# @param model {string} model name
|
||||
# @param rowform {Object} see http://vitalets.github.io/angular-xeditable/
|
||||
# @param index {number} category index in the $scope.categories array
|
||||
# @param index {number} element index in the $scope[model] array
|
||||
##
|
||||
$scope.cancelCategory = (rowform, index) ->
|
||||
if $scope.categories[index].id?
|
||||
$scope.cancelElement = (model, rowform, index) ->
|
||||
if getModel(model)[1][index].id?
|
||||
rowform.$cancel()
|
||||
else
|
||||
$scope.categories.splice(index, 1)
|
||||
getModel(model)[1].splice(index, 1)
|
||||
|
||||
|
||||
|
||||
@ -233,6 +246,17 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state",
|
||||
else
|
||||
$scope.paginateActive = false
|
||||
|
||||
##
|
||||
# Return the model and the datastore matching the given name
|
||||
# @param name {string} 'category', 'theme' or 'age_range'
|
||||
# @return {[Object, Array]} model and datastore
|
||||
##
|
||||
getModel = (name) ->
|
||||
switch name
|
||||
when 'category' then [Category, $scope.categories]
|
||||
when 'theme' then [EventThemes, $scope.themes]
|
||||
#when 'age_range' then [AgeRange, $scope.ageRanges]
|
||||
else [null, []]
|
||||
|
||||
|
||||
# init the controller (call at the end !)
|
||||
|
@ -537,6 +537,9 @@ angular.module('application.router', ['ui.router']).
|
||||
categoriesPromise: ['Category', (Category) ->
|
||||
Category.query().$promise
|
||||
]
|
||||
themesPromise: ['EventThemes', (EventThemes) ->
|
||||
EventThemes.query().$promise
|
||||
]
|
||||
translations: [ 'Translations', (Translations) ->
|
||||
Translations.query('app.admin.events').$promise
|
||||
]
|
||||
|
8
app/assets/javascripts/services/event_themes.coffee
Normal file
8
app/assets/javascripts/services/event_themes.coffee
Normal file
@ -0,0 +1,8 @@
|
||||
'use strict'
|
||||
|
||||
Application.Services.factory 'EventThemes', ["$resource", ($resource)->
|
||||
$resource "/api/event_themes/:id",
|
||||
{id: "@id"},
|
||||
update:
|
||||
method: 'PUT'
|
||||
]
|
@ -1,6 +1,6 @@
|
||||
<div class="m-t">
|
||||
<h3 translate>{{ 'categories' }}</h3>
|
||||
<button type="button" class="btn btn-warning m-b m-t" ng-click="addCategory()" translate>{{ 'add_a_category' }}</button>
|
||||
<button type="button" class="btn btn-warning m-b m-t" ng-click="addElement('category')" translate>{{ 'add_a_category' }}</button>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
@ -17,11 +17,11 @@
|
||||
</td>
|
||||
<td>
|
||||
<!-- form -->
|
||||
<form editable-form name="rowform" onbeforesave="saveCategory($data, category.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted == category">
|
||||
<form editable-form name="rowform" onbeforesave="saveElement('category', $data, category.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted.category == category">
|
||||
<button type="submit" ng-disabled="rowform.$waiting" class="btn btn-warning">
|
||||
<i class="fa fa-check"></i>
|
||||
</button>
|
||||
<button type="button" ng-disabled="rowform.$waiting" ng-click="cancelCategory(rowform, $index)" class="btn btn-default">
|
||||
<button type="button" ng-disabled="rowform.$waiting" ng-click="cancelElement('category', rowform, $index)" class="btn btn-default">
|
||||
<i class="fa fa-times"></i>
|
||||
</button>
|
||||
</form>
|
||||
@ -29,7 +29,7 @@
|
||||
<button class="btn btn-default" ng-click="rowform.$show()">
|
||||
<i class="fa fa-edit"></i> <span class="hidden-xs hidden-sm" translate>{{ 'edit' }}</span>
|
||||
</button>
|
||||
<button class="btn btn-danger" ng-click="removeCategory($index)">
|
||||
<button class="btn btn-danger" ng-click="removeElement('category', $index)">
|
||||
<i class="fa fa-trash-o"></i>
|
||||
</button>
|
||||
</div>
|
||||
@ -38,4 +38,43 @@
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
<h3 translate>{{ 'themes' }}</h3>
|
||||
<button type="button" class="btn btn-warning m-b m-t" ng-click="addElement('theme')" translate>{{ 'add_a_theme' }}</button>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width:80%" translate>{{ 'name' }}</th>
|
||||
<th style="width:20%"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr ng-repeat="theme in themes">
|
||||
<td>
|
||||
<span editable-text="theme.name" e-cols="100" e-name="name" e-form="rowform" e-required>
|
||||
{{ theme.name }}
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
<!-- form -->
|
||||
<form editable-form name="rowform" onbeforesave="saveElement('theme', $data, category.id)" ng-show="rowform.$visible" class="form-buttons form-inline" shown="inserted.theme == theme">
|
||||
<button type="submit" ng-disabled="rowform.$waiting" class="btn btn-warning">
|
||||
<i class="fa fa-check"></i>
|
||||
</button>
|
||||
<button type="button" ng-disabled="rowform.$waiting" ng-click="cancelElement('theme', rowform, $index)" class="btn btn-default">
|
||||
<i class="fa fa-times"></i>
|
||||
</button>
|
||||
</form>
|
||||
<div class="buttons" ng-show="!rowform.$visible">
|
||||
<button class="btn btn-default" ng-click="rowform.$show()">
|
||||
<i class="fa fa-edit"></i> <span class="hidden-xs hidden-sm" translate>{{ 'edit' }}</span>
|
||||
</button>
|
||||
<button class="btn btn-danger" ng-click="removeElement('theme', $index)">
|
||||
<i class="fa fa-trash-o"></i>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
@ -1,4 +1,5 @@
|
||||
class API::CategoriesController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
before_action :set_category, only: [:show, :update, :destroy]
|
||||
|
||||
def index
|
||||
|
47
app/controllers/api/event_themes_controller.rb
Normal file
47
app/controllers/api/event_themes_controller.rb
Normal file
@ -0,0 +1,47 @@
|
||||
class API::EventThemesController < API::ApiController
|
||||
before_action :authenticate_user!
|
||||
before_action :set_event_theme, only: [:show, :update, :destroy]
|
||||
|
||||
def index
|
||||
authorize EventTheme
|
||||
@event_themes = EventTheme.all
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
def create
|
||||
authorize EventTheme
|
||||
@event_theme = EventTheme.new(event_theme_params)
|
||||
if @event_theme.save
|
||||
render :show, status: :created, location: @event_theme
|
||||
else
|
||||
render json: @event_theme.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def update
|
||||
authorize EventTheme
|
||||
if @event_theme.update(event_theme_params)
|
||||
render :show, status: :ok, location: @event_theme
|
||||
else
|
||||
render json: @event_theme.errors, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
authorize EventTheme
|
||||
@event_theme.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
def set_event_theme
|
||||
@event_theme = EventTheme.find(params[:id])
|
||||
end
|
||||
|
||||
def event_theme_params
|
||||
params.require(:event_theme).permit(:name)
|
||||
end
|
||||
end
|
7
app/policies/event_theme_policy.rb
Normal file
7
app/policies/event_theme_policy.rb
Normal file
@ -0,0 +1,7 @@
|
||||
class EventThemePolicy < ApplicationPolicy
|
||||
%w(index create update destroy show).each do |action|
|
||||
define_method "#{action}?" do
|
||||
user.is_admin?
|
||||
end
|
||||
end
|
||||
end
|
3
app/views/api/event_themes/index.json.jbuilder
Normal file
3
app/views/api/event_themes/index.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
||||
json.array!(@event_themes) do |theme|
|
||||
json.extract! theme, :id, :name
|
||||
end
|
1
app/views/api/event_themes/show.json.jbuilder
Normal file
1
app/views/api/event_themes/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.extract! @event_theme, :id, :name
|
@ -81,6 +81,7 @@ en:
|
||||
view_reservations: "View reservations"
|
||||
categories: "Categories"
|
||||
add_a_category: "Add a category"
|
||||
add_a_theme: "Add a theme"
|
||||
|
||||
events_new:
|
||||
# add a new event
|
||||
|
@ -81,6 +81,7 @@ fr:
|
||||
view_reservations: "Consulter les réservations"
|
||||
categories: "Catégories"
|
||||
add_a_category: "Ajouter une catégorie"
|
||||
add_a_theme: "Ajouter une thématique"
|
||||
|
||||
events_new:
|
||||
# ajouter un nouveau atelier/stage
|
||||
|
@ -88,6 +88,7 @@ Rails.application.routes.draw do
|
||||
resources :trainings
|
||||
resources :credits
|
||||
resources :categories
|
||||
resources :event_themes
|
||||
resources :statistics, only: [:index]
|
||||
resources :custom_assets, only: [:show, :create, :update]
|
||||
resources :tags
|
||||
|
Loading…
x
Reference in New Issue
Block a user