From 79b97eb113b117d788c9eaa6fa08e1f6cf56eeaa Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 29 Jun 2016 11:21:21 +0200 Subject: [PATCH] [feature] manage event's age range --- .../controllers/admin/events.coffee | 11 +++-- app/assets/javascripts/router.coffee.erb | 7 ++- .../javascripts/services/age_range.coffee | 8 ++++ ...event_themes.coffee => event_theme.coffee} | 2 +- .../templates/admin/events/filters.html.erb | 41 +++++++++++++++- app/controllers/api/age_ranges_controller.rb | 47 +++++++++++++++++++ app/policies/age_range_policy.rb | 7 +++ app/views/api/age_ranges/index.json.jbuilder | 3 ++ app/views/api/age_ranges/show.json.jbuilder | 1 + config/locales/app.admin.en.yml | 2 + config/locales/app.admin.fr.yml | 2 + config/routes.rb | 1 + ...649_rename_range_to_name_from_age_range.rb | 3 ++ db/schema.rb | 4 +- 14 files changed, 129 insertions(+), 10 deletions(-) create mode 100644 app/assets/javascripts/services/age_range.coffee rename app/assets/javascripts/services/{event_themes.coffee => event_theme.coffee} (58%) create mode 100644 app/controllers/api/age_ranges_controller.rb create mode 100644 app/policies/age_range_policy.rb create mode 100644 app/views/api/age_ranges/index.json.jbuilder create mode 100644 app/views/api/age_ranges/show.json.jbuilder create mode 100644 db/migrate/20160629091649_rename_range_to_name_from_age_range.rb diff --git a/app/assets/javascripts/controllers/admin/events.coffee b/app/assets/javascripts/controllers/admin/events.coffee index 6f5a3dd5c..f9ff1a531 100644 --- a/app/assets/javascripts/controllers/admin/events.coffee +++ b/app/assets/javascripts/controllers/admin/events.coffee @@ -136,8 +136,8 @@ class EventsController ## # Controller used in the events listing page (admin view) ## -Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'Event', 'Category', 'EventThemes', 'eventsPromise', 'categoriesPromise', 'themesPromise' -, ($scope, $state, Event, Category, EventThemes, eventsPromise, categoriesPromise, themesPromise) -> +Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'Event', 'Category', 'EventTheme', 'AgeRange', 'eventsPromise', 'categoriesPromise', 'themesPromise', 'ageRangesPromise' +, ($scope, $state, Event, Category, EventTheme, AgeRange, eventsPromise, categoriesPromise, themesPromise, ageRangesPromise) -> @@ -164,6 +164,9 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state", ## List of events themes $scope.themes = themesPromise + ## List of age ranges + $scope.ageRanges = ageRangesPromise + ## # Adds a bucket of events to the bottom of the page, grouped by month ## @@ -254,8 +257,8 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state", getModel = (name) -> switch name when 'category' then [Category, $scope.categories] - when 'theme' then [EventThemes, $scope.themes] - #when 'age_range' then [AgeRange, $scope.ageRanges] + when 'theme' then [EventTheme, $scope.themes] + when 'age_range' then [AgeRange, $scope.ageRanges] else [null, []] diff --git a/app/assets/javascripts/router.coffee.erb b/app/assets/javascripts/router.coffee.erb index 47ed39c94..4f525dfb3 100644 --- a/app/assets/javascripts/router.coffee.erb +++ b/app/assets/javascripts/router.coffee.erb @@ -537,8 +537,11 @@ angular.module('application.router', ['ui.router']). categoriesPromise: ['Category', (Category) -> Category.query().$promise ] - themesPromise: ['EventThemes', (EventThemes) -> - EventThemes.query().$promise + themesPromise: ['EventTheme', (EventTheme) -> + EventTheme.query().$promise + ] + ageRangesPromise: ['AgeRange', (AgeRange) -> + AgeRange.query().$promise ] translations: [ 'Translations', (Translations) -> Translations.query('app.admin.events').$promise diff --git a/app/assets/javascripts/services/age_range.coffee b/app/assets/javascripts/services/age_range.coffee new file mode 100644 index 000000000..e60d5b9d8 --- /dev/null +++ b/app/assets/javascripts/services/age_range.coffee @@ -0,0 +1,8 @@ +'use strict' + +Application.Services.factory 'AgeRange', ["$resource", ($resource)-> + $resource "/api/age_ranges/:id", + {id: "@id"}, + update: + method: 'PUT' +] diff --git a/app/assets/javascripts/services/event_themes.coffee b/app/assets/javascripts/services/event_theme.coffee similarity index 58% rename from app/assets/javascripts/services/event_themes.coffee rename to app/assets/javascripts/services/event_theme.coffee index 8b65329fd..934a54f25 100644 --- a/app/assets/javascripts/services/event_themes.coffee +++ b/app/assets/javascripts/services/event_theme.coffee @@ -1,6 +1,6 @@ 'use strict' -Application.Services.factory 'EventThemes', ["$resource", ($resource)-> +Application.Services.factory 'EventTheme', ["$resource", ($resource)-> $resource "/api/event_themes/:id", {id: "@id"}, update: diff --git a/app/assets/templates/admin/events/filters.html.erb b/app/assets/templates/admin/events/filters.html.erb index fd3442981..b9a8d1a30 100644 --- a/app/assets/templates/admin/events/filters.html.erb +++ b/app/assets/templates/admin/events/filters.html.erb @@ -56,7 +56,7 @@ -
+ @@ -77,4 +77,43 @@ +

{{ 'age_ranges' }}

+ + + + + + + + + + + + + + +
{{ 'name' }}
+ + {{ range.name }} + + + + + + + +
+ + +
+
+ \ No newline at end of file diff --git a/app/controllers/api/age_ranges_controller.rb b/app/controllers/api/age_ranges_controller.rb new file mode 100644 index 000000000..3b4f1bc56 --- /dev/null +++ b/app/controllers/api/age_ranges_controller.rb @@ -0,0 +1,47 @@ +class API::AgeRangesController < API::ApiController + before_action :authenticate_user! + before_action :set_age_range, only: [:show, :update, :destroy] + + def index + authorize AgeRange + @age_ranges = AgeRange.all + end + + def show + end + + def create + authorize AgeRange + @age_range = AgeRange.new(age_range_params) + if @age_range.save + render :show, status: :created, location: @age_range + else + render json: @age_range.errors, status: :unprocessable_entity + end + end + + + def update + authorize AgeRange + if @age_range.update(age_range_params) + render :show, status: :ok, location: @age_range + else + render json: @age_range.errors, status: :unprocessable_entity + end + end + + def destroy + authorize AgeRange + @age_range.destroy + head :no_content + end + + private + def set_age_range + @age_range = AgeRange.find(params[:id]) + end + + def age_range_params + params.require(:age_range).permit(:name) + end +end diff --git a/app/policies/age_range_policy.rb b/app/policies/age_range_policy.rb new file mode 100644 index 000000000..77438111d --- /dev/null +++ b/app/policies/age_range_policy.rb @@ -0,0 +1,7 @@ +class AgeRangePolicy < ApplicationPolicy + %w(index create update destroy show).each do |action| + define_method "#{action}?" do + user.is_admin? + end + end +end diff --git a/app/views/api/age_ranges/index.json.jbuilder b/app/views/api/age_ranges/index.json.jbuilder new file mode 100644 index 000000000..31531860e --- /dev/null +++ b/app/views/api/age_ranges/index.json.jbuilder @@ -0,0 +1,3 @@ +json.array!(@age_ranges) do |ar| + json.extract! ar, :id, :name +end diff --git a/app/views/api/age_ranges/show.json.jbuilder b/app/views/api/age_ranges/show.json.jbuilder new file mode 100644 index 000000000..5f77a2b46 --- /dev/null +++ b/app/views/api/age_ranges/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @age_range, :id, :name \ No newline at end of file diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index 62c3803b1..0a554e427 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -82,6 +82,8 @@ en: categories: "Categories" add_a_category: "Add a category" add_a_theme: "Add a theme" + age_ranges: "Age ranges" + add_a_range: "Add a range" events_new: # add a new event diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index d9b254843..0ecda2b38 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -82,6 +82,8 @@ fr: categories: "Catégories" add_a_category: "Ajouter une catégorie" add_a_theme: "Ajouter une thématique" + age_ranges: "Tranches d'âge" + add_a_range: "Ajouter une tranche" events_new: # ajouter un nouveau atelier/stage diff --git a/config/routes.rb b/config/routes.rb index 100065e5a..4e539ecd6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -89,6 +89,7 @@ Rails.application.routes.draw do resources :credits resources :categories resources :event_themes + resources :age_ranges resources :statistics, only: [:index] resources :custom_assets, only: [:show, :create, :update] resources :tags diff --git a/db/migrate/20160629091649_rename_range_to_name_from_age_range.rb b/db/migrate/20160629091649_rename_range_to_name_from_age_range.rb new file mode 100644 index 000000000..3158b5a63 --- /dev/null +++ b/db/migrate/20160629091649_rename_range_to_name_from_age_range.rb @@ -0,0 +1,3 @@ +class RenameRangeToNameFromAgeRange < ActiveRecord::Migration + rename_column :age_ranges, :range, :name +end diff --git a/db/schema.rb b/db/schema.rb index 025cfa02e..f574480e5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160628134303) do +ActiveRecord::Schema.define(version: 20160629091649) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -45,7 +45,7 @@ ActiveRecord::Schema.define(version: 20160628134303) do end create_table "age_ranges", force: :cascade do |t| - t.string "range" + t.string "name" t.datetime "created_at", null: false t.datetime "updated_at", null: false end