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

updated ngUpload + [ongoing] fixing adding/updating training

This commit is contained in:
Sylvain 2016-06-22 17:15:19 +02:00
parent 7deefd0e57
commit 7e1b05d339
13 changed files with 155 additions and 60 deletions

View File

@ -1,35 +1,79 @@
'use strict'
### COMMON CODE ###
##
# Provides a set of common callback methods to the $scope parameter. These methods are used
# in the various trainings' admin controllers.
#
# Provides :
# - $scope.submited(content)
# - $scope.fileinputClass(v)
#
# Requires :
# - $state (Ui-Router) [ 'app.admin.trainings' ]
##
class TrainingsController
constructor: ($scope, $state) ->
##
# For use with ngUpload (https://github.com/twilson63/ngUpload).
# Intended to be the callback when the upload is done: any raised error will be stacked in the
# $scope.alerts array. If everything goes fine, the user is redirected to the trainings list.
# @param content {Object} JSON - The upload's result
##
$scope.submited = (content) ->
if !content.id?
$scope.alerts = []
angular.forEach content, (v, k)->
angular.forEach v, (err)->
$scope.alerts.push
msg: k+': '+err
type: 'danger'
else
$state.go('app.admin.trainings')
##
# Changes the current user's view, redirecting him to the machines list
##
$scope.cancel = ->
$state.go('app.admin.trainings')
##
# For use with 'ng-class', returns the CSS class name for the uploads previews.
# The preview may show a placeholder or the content of the file depending on the upload state.
# @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
##
$scope.fileinputClass = (v)->
if v
'fileinput-exists'
else
'fileinput-new'
##
# Controller used in the training creation page (admin)
##
Application.Controllers.controller "NewTrainingController", [ '$scope', 'machinesPromise', ($scope, machinesPromise) ->
### PUBLIC SCOPE ###
Application.Controllers.controller "NewTrainingController", [ '$scope', '$state', 'machinesPromise', 'CSRF'
, ($scope, $state, machinesPromise, CSRF) ->
CSRF.setMetaTags()
## Form action on the following URL
$scope.method = 'post'
## API URL where the form will be posted
$scope.actionUrl = '/api/trainings'
$scope.actionUrl = '/api/trainings/'
## list of machines
$scope.machines = machinesPromise
##
# For use with 'ng-class', returns the CSS class name for the uploads previews.
# The preview may show a placeholder or the content of the file depending on the upload state.
# @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
##
$scope.fileinputClass = (v)->
if v
'fileinput-exists'
else
'fileinput-new'
## Using the TrainingsController
new TrainingsController($scope, $state)
]
@ -37,17 +81,15 @@ Application.Controllers.controller "NewTrainingController", [ '$scope', 'machine
##
# Controller used in the training edition page (admin)
##
Application.Controllers.controller "EditTrainingController", [ '$scope', '$stateParams', 'trainingPromise', 'machinesPromise', ($scope, $stateParams, trainingPromise, machinesPromise) ->
### PUBLIC SCOPE ###
Application.Controllers.controller "EditTrainingController", [ '$scope', '$state', '$stateParams', 'trainingPromise', 'machinesPromise', 'CSRF'
, ($scope, $state, $stateParams, trainingPromise, machinesPromise, CSRF) ->
CSRF.setMetaTags()
## Form action on the following URL
$scope.method = 'put'
## API URL where the form will be posted
$scope.actionUrl = '/api/trainings' + $stateParams.id
$scope.actionUrl = '/api/trainings/' + $stateParams.id
## Details of the training to edit (id in URL)
$scope.training = trainingPromise
@ -55,16 +97,8 @@ Application.Controllers.controller "EditTrainingController", [ '$scope', '$state
## list of machines
$scope.machines = machinesPromise
##
# For use with 'ng-class', returns the CSS class name for the uploads previews.
# The preview may show a placeholder or the content of the file depending on the upload state.
# @param v {*} any attribute, will be tested for truthiness (see JS evaluation rules)
##
$scope.fileinputClass = (v)->
if v
'fileinput-exists'
else
'fileinput-new'
## Using the TrainingsController
new TrainingsController($scope, $state)
]

View File

@ -1,4 +1,11 @@
<form role="form" name="trainingForm" class="form-horizontal" novalidate action="{{ actionUrl }}" ng-upload="submited(content)" upload-options-enable-rails-csrf="true" unsaved-warning-form>
<form role="form"
name="trainingForm"
class="form-horizontal"
ng-attr-action="{{ actionUrl }}"
ng-upload="whatthefuck(content)"
upload-options-enable-rails-csrf="true"
unsaved-warning-form
novalidate>
<input name="_method" type="hidden" ng-value="method">
@ -10,7 +17,12 @@
<div class="form-group m-b-lg" ng-class="{'has-error': trainingForm['training[name]'].$dirty && trainingForm['training[name]'].$invalid}">
<label for="name" class="col-sm-2 control-label">{{ 'name' | translate }} *</label>
<div class="col-sm-4">
<input ng-model="training.name" type="text" name="training[name]" class="form-control" id="training_name" placeholder="{{'name' | translate}}" required>
<input ng-model="training.name"
type="text" name="training[name]"
class="form-control"
id="training_name"
placeholder="{{'name' | translate}}"
required>
<span class="help-block" ng-show="trainingForm['training[name]'].$dirty && trainingForm['training[name]'].$error.required" translate>{{ 'name_is_required' }}</span>
</div>
</div>
@ -46,18 +58,22 @@
<div class="form-group m-b-xl" ng-class="{'has-error': trainingForm['training[description]'].$dirty && trainingForm['training[description]'].$invalid}">
<label for="description" class="col-sm-2 control-label">{{ 'description' | translate }} *</label>
<div class="col-sm-10">
<textarea ng-model="training.description" class="form-control" rows="12" id="training_description" placeholder="" name="training[description]" required></textarea>
<textarea ng-model="training.description"
class="form-control"
rows="12"
id="training_description"
placeholder=""
name="training[description]"
required>
</textarea>
<span class="help-block" ng-show="trainingForm['training[description]'].$dirty && trainingForm['training[description]'].$error.required" translate>{{ 'description_is_required' }}</span>
</div>
</div>
<div class="form-group m-b-lg" ng-class="{'has-error': trainingForm['training[machine_ids]'].$dirty && trainingForm['training[machine_ids]'].$invalid}">
<label for="name" class="col-sm-2 control-label">{{ 'associated_machines' | translate }}</label>
<label for="training_machines" class="col-sm-2 control-label">{{ 'associated_machines' | translate }}</label>
<div class="col-sm-4">
<input type="hidden"
value="{{training.machine_ids}}"
name="training[machine_ids][]">
<ui-select multiple ng-model="training.machine_ids" name="training[machine_ids][]" class="form-control">
<ui-select multiple ng-model="training.machine_ids" class="form-control" id="training_machines">
<ui-select-match>
<span ng-bind="$item.name"></span>
<input type="hidden" name="training[machine_ids][]" value="{{$item.id}}" />
@ -70,10 +86,11 @@
</div>
<div class="form-group m-b-lg" ng-class="{'has-error': trainingForm['training[nb_total_places]'].$dirty && trainingForm['training[nb_total_places]'].$invalid}">
<label for="name" class="col-sm-2 control-label">{{ 'number_of_tickets' | translate }}</label>
<label for="training_nb_total_places" class="col-sm-2 control-label">{{ 'number_of_tickets' | translate }}</label>
<div class="col-sm-4">
<input ng-model="training.nb_total_places"
type="number"
min="0"
name="training[nb_total_places]"
class="form-control"
id="training_nb_total_places">
@ -83,7 +100,10 @@
</div> <!-- ./panel-body -->
<div class="panel-footer no-padder">
<input type="submit" value="{{ 'validate_your_training' | translate }}" class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c" ng-disabled="trainingForm.$invalid"/>
<input type="submit"
value="{{ 'validate_your_training' | translate }}"
class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c"
ng-disabled="trainingForm.$invalid"/>
</div>
</section>
</form>

View File

@ -1,4 +1,11 @@
<form role="form" name="machineForm" class="form-horizontal" novalidate action="{{ actionUrl }}" ng-upload="submited(content)" upload-options-enable-rails-csrf="true" unsaved-warning-form>
<form role="form"
name="machineForm"
class="form-horizontal"
action="{{ actionUrl }}"
ng-upload="submited(content)"
upload-options-enable-rails-csrf="true"
unsaved-warning-form
novalidate>
<input name="_method" type="hidden" ng-value="method">
@ -10,7 +17,13 @@
<div class="form-group m-b-lg" ng-class="{'has-error': machineForm['machine[name]'].$dirty && machineForm['machine[name]'].$invalid}">
<label for="name" class="col-sm-2 control-label">{{ 'name' | translate }} *</label>
<div class="col-sm-4">
<input ng-model="machine.name" type="text" name="machine[name]" class="form-control" id="machine_name" placeholder="{{'name' | translate}}" required>
<input ng-model="machine.name"
type="text"
name="machine[name]"
class="form-control"
id="machine_name"
placeholder="{{'name' | translate}}"
required>
<span class="help-block" ng-show="machineForm['machine[name]'].$dirty && machineForm['machine[name]'].$error.required" translate>{{ 'name_is_required' }}</span>
</div>
</div>
@ -46,7 +59,14 @@
<div class="form-group m-b-xl" ng-class="{'has-error': machineForm['machine[description]'].$dirty && machineForm['machine[description]'].$invalid}">
<label for="description" class="col-sm-2 control-label">{{ 'description' | translate }} *</label>
<div class="col-sm-10">
<textarea ng-model="machine.description" class="form-control" rows="12" id="machine_description" placeholder="" name="machine[description]" required></textarea>
<textarea ng-model="machine.description"
class="form-control"
rows="12"
id="machine_description"
placeholder=""
name="machine[description]"
required>
</textarea>
<span class="help-block" ng-show="machineForm['machine[description]'].$dirty && machineForm['machine[description]'].$error.required" translate>{{ 'description_is_required' }}</span>
</div>
</div>
@ -54,7 +74,14 @@
<div class="form-group m-b-xl" ng-class="{'has-error': machineForm['machine[spec]'].$dirty && machineForm['machine[spec]'].$invalid}">
<label for="spec" class="col-sm-2 control-label">{{ 'technical_specifications' | translate }} *</label>
<div class="col-sm-10">
<textarea ng-model="machine.spec" class="form-control" rows="12" id="machine_spec" placeholder="" name="machine[spec]" required></textarea>
<textarea ng-model="machine.spec"
class="form-control"
rows="12"
id="machine_spec"
placeholder=""
name="machine[spec]"
required>
</textarea>
<span class="help-block" ng-show="machineForm['machine[spec]'].$dirty && machineForm['machine[spec]'].$error.required" translate>{{ 'technical_specifications_are_required' }}</span>
</div>
</div>
@ -83,7 +110,10 @@
</div> <!-- ./panel-body -->
<div class="panel-footer no-padder">
<input type="submit" value="{{ 'validate_your_machine' | translate }}" class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c" ng-disabled="machineForm.$invalid"/>
<input type="submit"
value="{{ 'validate_your_machine' | translate }}"
class="r-b btn-valid btn btn-warning btn-block p-lg btn-lg text-u-c"
ng-disabled="machineForm.$invalid"/>
</div>
</section>
</form>

View File

@ -57,6 +57,6 @@ class API::TrainingsController < API::ApiController
end
def training_params
params.require(:training).permit(:id, :name, :description, :machine_ids, :plan_ids, :nb_total_places, machine_ids: [], plan_ids: [])
params.require(:training).permit(:id, :name, :description, :machine_ids, :plan_ids, :nb_total_places, training_image_attributes: [:attachment], machine_ids: [], plan_ids: [])
end
end

View File

@ -2,6 +2,9 @@ class Training < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_one :training_image, as: :viewable, dependent: :destroy
accepts_nested_attributes_for :training_image, allow_destroy: true
has_and_belongs_to_many :machines, join_table: :trainings_machines
has_many :trainings_availabilities
@ -23,7 +26,7 @@ class Training < ActiveRecord::Base
after_update :update_statistic_subtype, if: :name_changed?
after_destroy :remove_statistic_subtype
validates :description, length: { maximum: 255 }
validates_presence_of :description
def amount_by_group(group)
trainings_pricings.where(group_id: group).first

View File

@ -0,0 +1,4 @@
class TrainingImage < Asset
mount_uploader :attachment, MachineImageUploader
end

View File

@ -1,4 +1,5 @@
json.extract! @training, :id, :name, :machine_ids, :nb_total_places
json.extract! @training, :id, :name, :description, :machine_ids, :nb_total_places
json.training_image @training.training_image.attachment.large.url if @training.training_image
json.availabilities @training.availabilities.order('start_at DESC') do |a|
json.id a.id
json.start_at a.start_at.iso8601

View File

@ -25,7 +25,7 @@
"tag": "0.14.3",
"commit": "306d1a30b4a8e8144741bb9c0126331ac884126a"
},
"_source": "git://github.com/angular-ui/bootstrap-bower.git",
"_target": ">=0.13.1",
"_source": "https://github.com/angular-ui/bootstrap-bower.git",
"_target": "~0.14.3",
"_originalSource": "angular-bootstrap"
}

View File

@ -1,6 +1,6 @@
{
"name": "ngUpload",
"version": "0.5.17",
"version": "0.5.18",
"main": "ng-upload.js",
"ignore": [
"node_modules",
@ -20,13 +20,13 @@
"angular": ">=1.0.4"
},
"homepage": "https://github.com/twilson63/ngUpload",
"_release": "0.5.17",
"_release": "0.5.18",
"_resolution": {
"type": "version",
"tag": "v0.5.17",
"commit": "df9f3edfdbcd1ca6d3f365ff85e32de229df3af1"
"tag": "v0.5.18",
"commit": "da7fe2bb94eb6adb2cd26ab4f6f979aa020baf9c"
},
"_source": "git://github.com/twilson63/ngUpload.git",
"_source": "https://github.com/twilson63/ngUpload.git",
"_target": ">=0.5.11",
"_originalSource": "ngUpload"
}

View File

@ -1,6 +1,6 @@
{
"name": "ngUpload",
"version": "0.5.17",
"version": "0.5.18",
"main": "ng-upload.js",
"ignore": [
"node_modules",

View File

@ -146,6 +146,9 @@ angular.module('ngUpload', [])
}
// perform check before submit file
if (options.beforeSubmit && options.beforeSubmit(scope, {}) === false) {
if(!scope.$$phase){
scope.$apply();
}
$event.preventDefault();
return false;
}

View File

@ -1 +1 @@
angular.module("ngUpload",[]).directive("uploadSubmit",["$parse",function(){function n(t,e){t=angular.element(t);var a=t.parent();return e=e.toLowerCase(),a&&a[0].tagName.toLowerCase()===e?a:a?n(a,e):null}return{restrict:"AC",link:function(t,e){e.bind("click",function(t){if(t&&(t.preventDefault(),t.stopPropagation()),!e.attr("disabled")){var a=n(e,"form");a.triggerHandler("submit"),a[0].submit()}})}}}]).directive("ngUpload",["$log","$parse","$document",function(n,t,e){function a(n){var t,a=e.find("head");return angular.forEach(a.find("meta"),function(e){e.getAttribute("name")===n&&(t=e)}),angular.element(t)}var r=1;return{restrict:"AC",link:function(e,o,i){function l(n){e.$isUploading=n}function u(){s.unbind("load"),e.$$phase?l(!1):e.$apply(function(){l(!1)});try{var t,a=(s[0].contentDocument||s[0].contentWindow.document).body;try{t=angular.fromJson(a.innerText||a.textContent),e.$$phase?p(e,{content:t}):e.$apply(function(){p(e,{content:t})})}catch(r){t=a.innerHTML;var o="ng-upload: Response is not valid JSON";n.warn(o),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}catch(o){n.warn("ng-upload: Server error"),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}r++;var d={},p=i.ngUpload?t(i.ngUpload):null,f=i.errorCatcher?t(i.errorCatcher):null,c=i.ngUploadLoading?t(i.ngUploadLoading):null;i.hasOwnProperty("uploadOptionsConvertHidden")&&(d.convertHidden="false"!=i.uploadOptionsConvertHidden),i.hasOwnProperty("uploadOptionsEnableRailsCsrf")&&(d.enableRailsCsrf="false"!=i.uploadOptionsEnableRailsCsrf),i.hasOwnProperty("uploadOptionsBeforeSubmit")&&(d.beforeSubmit=t(i.uploadOptionsBeforeSubmit)),o.attr({target:"upload-iframe-"+r,method:"post",enctype:"multipart/form-data",encoding:"multipart/form-data"});var s=angular.element('<iframe name="upload-iframe-'+r+'" '+'border="0" width="0" height="0" '+'style="width:0px;height:0px;border:none;display:none">');if(d.enableRailsCsrf){var m=angular.element("<input />");m.attr("class","upload-csrf-token"),m.attr("type","hidden"),m.attr("name",a("csrf-param").attr("content")),m.val(a("csrf-token").attr("content")),o.append(m)}o.after(s),l(!1),o.bind("submit",function(n){var t=e[i.name];return t&&t.$invalid?(n.preventDefault(),!1):d.beforeSubmit&&d.beforeSubmit(e,{})===!1?(n.preventDefault(),!1):(s.bind("load",u),d.convertHidden&&angular.forEach(o.find("input"),function(n){var t=angular.element(n);t.attr("ng-model")&&t.attr("type")&&"hidden"==t.attr("type")&&t.attr("value",e.$eval(t.attr("ng-model")))}),e.$$phase?(c&&c(e),l(!0)):e.$apply(function(){c&&c(e),l(!0)}),void 0)})}}}]);
angular.module("ngUpload",[]).directive("uploadSubmit",["$parse",function(){function n(t,e){t=angular.element(t);var a=t.parent();return e=e.toLowerCase(),a&&a[0].tagName.toLowerCase()===e?a:a?n(a,e):null}return{restrict:"AC",link:function(t,e){e.bind("click",function(t){if(t&&(t.preventDefault(),t.stopPropagation()),!e.attr("disabled")){var a=n(e,"form");a.triggerHandler("submit"),a[0].submit()}})}}}]).directive("ngUpload",["$log","$parse","$document",function(n,t,e){function a(n){var t,a=e.find("head");return angular.forEach(a.find("meta"),function(e){e.getAttribute("name")===n&&(t=e)}),angular.element(t)}var r=1;return{restrict:"AC",link:function(e,o,i){function l(n){e.$isUploading=n}function p(){c.unbind("load"),e.$$phase?l(!1):e.$apply(function(){l(!1)});try{var t,a=(c[0].contentDocument||c[0].contentWindow.document).body;try{t=angular.fromJson(a.innerText||a.textContent),e.$$phase?d(e,{content:t}):e.$apply(function(){d(e,{content:t})})}catch(r){t=a.innerHTML;var o="ng-upload: Response is not valid JSON";n.warn(o),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}catch(o){n.warn("ng-upload: Server error"),f&&(e.$$phase?f(e,{error:o}):e.$apply(function(){f(e,{error:o})}))}}r++;var u={},d=i.ngUpload?t(i.ngUpload):null,f=i.errorCatcher?t(i.errorCatcher):null,s=i.ngUploadLoading?t(i.ngUploadLoading):null;i.hasOwnProperty("uploadOptionsConvertHidden")&&(u.convertHidden="false"!=i.uploadOptionsConvertHidden),i.hasOwnProperty("uploadOptionsEnableRailsCsrf")&&(u.enableRailsCsrf="false"!=i.uploadOptionsEnableRailsCsrf),i.hasOwnProperty("uploadOptionsBeforeSubmit")&&(u.beforeSubmit=t(i.uploadOptionsBeforeSubmit)),o.attr({target:"upload-iframe-"+r,method:"post",enctype:"multipart/form-data",encoding:"multipart/form-data"});var c=angular.element('<iframe name="upload-iframe-'+r+'" '+'border="0" width="0" height="0" '+'style="width:0px;height:0px;border:none;display:none">');if(u.enableRailsCsrf){var m=angular.element("<input />");m.attr("class","upload-csrf-token"),m.attr("type","hidden"),m.attr("name",a("csrf-param").attr("content")),m.val(a("csrf-token").attr("content")),o.append(m)}o.after(c),l(!1),o.bind("submit",function(n){var t=e[i.name];return t&&t.$invalid?(n.preventDefault(),!1):u.beforeSubmit&&u.beforeSubmit(e,{})===!1?(e.$$phase||e.$apply(),n.preventDefault(),!1):(c.bind("load",p),u.convertHidden&&angular.forEach(o.find("input"),function(n){var t=angular.element(n);t.attr("ng-model")&&t.attr("type")&&"hidden"==t.attr("type")&&t.attr("value",e.$eval(t.attr("ng-model")))}),e.$$phase?(s&&s(e),l(!0)):e.$apply(function(){s&&s(e),l(!0)}),void 0)})}}}]);

View File

@ -133,7 +133,7 @@ angular.module('app', ['ngUpload'])
* Working in IE
In order, for ngUpload to respond correctly for IE, your server needs to return the response back as `html/text` not `application/json`
In order, for ngUpload to respond correctly for IE, your server needs to return the response back as `text/html` not `application/json`
## Directive Options