1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

API for price categories management

This commit is contained in:
Sylvain 2016-08-24 12:30:48 +02:00
parent 9748ea596f
commit 6c3dfab0a9
23 changed files with 182 additions and 12 deletions

View File

@ -129,8 +129,8 @@ class EventsController
##
# Controller used in the events listing page (admin view)
##
Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'dialogs', 'growl', 'Event', 'Category', 'EventTheme', 'AgeRange', 'eventsPromise', 'categoriesPromise', 'themesPromise', 'ageRangesPromise', '_t'
, ($scope, $state, dialogs, growl, Event, Category, EventTheme, AgeRange, eventsPromise, categoriesPromise, themesPromise, ageRangesPromise, _t) ->
Application.Controllers.controller "AdminEventsController", ["$scope", "$state", 'dialogs', 'growl', 'Event', 'Category', 'EventTheme', 'AgeRange', 'eventsPromise', 'categoriesPromise', 'themesPromise', 'ageRangesPromise', 'priceCategoriesPromise', '_t'
, ($scope, $state, dialogs, growl, Event, Category, EventTheme, AgeRange, eventsPromise, categoriesPromise, themesPromise, ageRangesPromise, priceCategoriesPromise, _t) ->
@ -160,6 +160,9 @@ Application.Controllers.controller "AdminEventsController", ["$scope", "$state",
## List of age ranges
$scope.ageRanges = ageRangesPromise
## List of price categories for the events
$scope.priceCategories = priceCategoriesPromise
##
# Adds a bucket of events to the bottom of the page, grouped by month
##

View File

@ -653,6 +653,9 @@ angular.module('application.router', ['ui.router']).
ageRangesPromise: ['AgeRange', (AgeRange) ->
AgeRange.query().$promise
]
priceCategoriesPromise: ['PriceCategory', (PriceCategory) ->
PriceCategory.query().$promise
]
translations: [ 'Translations', (Translations) ->
Translations.query('app.admin.events').$promise
]

View File

@ -0,0 +1,8 @@
'use strict'
Application.Services.factory 'PriceCategory', ["$resource", ($resource)->
$resource "/api/price_categories/:id",
{id: "@id"},
update:
method: 'PUT'
]

View File

@ -1,3 +0,0 @@
<div class="m-t">
<h3 translate>{{ 'fares_categories' }}</h3>
</div>

View File

@ -30,8 +30,8 @@
<ng-include src="'<%= asset_path 'admin/events/filters.html' %>'"></ng-include>
</uib-tab>
<uib-tab heading="{{ 'manage_fares_categories' | translate }}">
<ng-include src="'<%= asset_path 'admin/events/fares.html' %>'"></ng-include>
<uib-tab heading="{{ 'manage_prices_categories' | translate }}">
<ng-include src="'<%= asset_path 'admin/events/prices.html' %>'"></ng-include>
</uib-tab>
</uib-tabset>
</div>

View File

@ -0,0 +1,4 @@
<div class="m-t">
<h3 translate>{{ 'prices_categories' }}</h3>
</div>

View File

@ -0,0 +1,48 @@
class PriceCategoriesController < API::ApiController
before_action :authenticate_user!
before_action :set_price_category, only: [:show, :update, :destroy]
def index
@price_categories = PriceCategory.all
end
def update
authorize PriceCategory
if @price_category.update(price_category_params)
render :show, status: :ok, location: @price_category
else
render json: @price_category.errors, status: :unprocessable_entity
end
end
def show
end
def create
authorize PriceCategory
@price_category = PriceCategory.new(price_category_params)
if @price_category.save
render :show, status: :created, location: @price_category
else
render json: @price_category.errors, status: :unprocessable_entity
end
end
def destroy
authorize PriceCategory
if @price_category.safe_destroy
head :no_content
else
render json: @price_category.errors, status: :unprocessable_entity
end
end
private
def set_price_category
@price_category = PriceCategory.find(params[:id])
end
def price_category_params
params.require(:price_category).permit(:name, :description)
end
end

View File

@ -10,6 +10,9 @@ class Event < ActiveRecord::Base
has_many :reservations, as: :reservable, dependent: :destroy
has_and_belongs_to_many :event_themes, join_table: :events_event_themes, dependent: :destroy
has_many :event_price_categories
has_many :price_categories, through: :event_price_categories
belongs_to :age_range
belongs_to :availability, dependent: :destroy

View File

@ -0,0 +1,4 @@
class EventPriceCategory < ActiveRecord::Base
belongs_to :event
belongs_to :price_category
end

View File

@ -0,0 +1,12 @@
class PriceCategory < ActiveRecord::Base
has_many :event_price_category
has_many :events, through: :event_price_categories
def safe_destroy
if event_price_category.count == 0
destroy
else
false
end
end
end

View File

@ -0,0 +1,7 @@
class GroupPolicy < ApplicationPolicy
%w(index show create update destroy).each do |action|
define_method "#{action}?" do
user.is_admin?
end
end
end

View File

@ -0,0 +1,4 @@
json.array!(@price_categories) do |category|
json.extract! category, :id, :name
json.events category.event_price_category.count
end

View File

@ -0,0 +1 @@
json.extract! @price_category, :id, :name, :description, :created_at

View File

@ -90,8 +90,8 @@ en:
at_least_one_category_is_required: "At least one category is required."
unable_to_delete_the_last_one: "Unable to delete the last one."
unable_to_delete_an_error_occured: "Unable to delete: an error occurred."
manage_fares_categories: "Manage fares' categories"
fares_categories: "Fares' categories"
manage_prices_categories: "Manage prices' categories"
prices_categories: "Prices' categories"
events_new:
# add a new event

View File

@ -90,8 +90,8 @@ fr:
at_least_one_category_is_required: "Au moins une catégorie est requise."
unable_to_delete_the_last_one: "Impossible de supprimer la dernière."
unable_to_delete_an_error_occured: "Impossible de supprimer : une erreur est survenue."
manage_fares_categories: "Gérer les catégories de tarifs"
fares_categories: "Catégories de tarifs"
manage_prices_categories: "Gérer les catégories de tarifs"
prices_categories: "Catégories de tarifs"
events_new:
# ajouter un nouvel évènement

View File

@ -118,6 +118,7 @@ Rails.application.routes.draw do
resources :open_api_clients, only: [:index, :create, :update, :destroy] do
patch :reset_token, on: :member
end
resources :price_categories
# i18n
get 'translations/:locale/:state' => 'translations#show', :constraints => { :state => /[^\/]+/ } # allow dots in URL for 'state'

View File

@ -0,0 +1,10 @@
class CreatePriceCategories < ActiveRecord::Migration
def change
create_table :price_categories do |t|
t.string :name
t.text :description
t.timestamps null: false
end
end
end

View File

@ -0,0 +1,11 @@
class CreateEventPriceCategories < ActiveRecord::Migration
def change
create_table :event_price_categories do |t|
t.belongs_to :event, index: true, foreign_key: true
t.belongs_to :price_category, index: true, foreign_key: true
t.integer :amount
t.timestamps null: false
end
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20160808113930) do
ActiveRecord::Schema.define(version: 20160824084111) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -138,6 +138,17 @@ ActiveRecord::Schema.define(version: 20160808113930) do
t.datetime "updated_at", null: false
end
create_table "event_price_categories", force: :cascade do |t|
t.integer "event_id"
t.integer "price_category_id"
t.integer "amount"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "event_price_categories", ["event_id"], name: "index_event_price_categories_on_event_id", using: :btree
add_index "event_price_categories", ["price_category_id"], name: "index_event_price_categories_on_price_category_id", using: :btree
create_table "event_themes", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
@ -366,6 +377,13 @@ ActiveRecord::Schema.define(version: 20160808113930) do
add_index "plans", ["group_id"], name: "index_plans_on_group_id", using: :btree
create_table "price_categories", force: :cascade do |t|
t.string "name"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "prices", force: :cascade do |t|
t.integer "group_id"
t.integer "plan_id"
@ -765,6 +783,8 @@ ActiveRecord::Schema.define(version: 20160808113930) do
add_foreign_key "availability_tags", "availabilities"
add_foreign_key "availability_tags", "tags"
add_foreign_key "event_price_categories", "events"
add_foreign_key "event_price_categories", "price_categories"
add_foreign_key "events", "categories"
add_foreign_key "events_event_themes", "event_themes"
add_foreign_key "events_event_themes", "events"

View File

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
event_id:
price_category_id:
amount: 1
two:
event_id:
price_category_id:
amount: 1

9
test/fixtures/price_categories.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: MyString
description: MyText
two:
name: MyString
description: MyText

View File

@ -0,0 +1,7 @@
require 'test_helper'
class EventPriceCategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class PriceCategoryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end