-
Notifications
You must be signed in to change notification settings - Fork 0
/
angularjs-rails-resource.min.js
7 lines (7 loc) · 15.5 KB
/
angularjs-rails-resource.min.js
1
2
3
4
5
6
7
/**
* A resource factory inspired by $resource from AngularJS
* @version v1.1.1 - 2014-04-12
* @link https://github.com/FineLinePrototyping/angularjs-rails-resource.git
* @author
*/
!function(){angular.module("rails",["ng"])}(),function(){angular.module("rails").factory("RailsInflector",function(){function a(a){return angular.isString(a)?a.replace(/_[\w\d]/g,function(a,b,c){return 0===b?a:c.charAt(b+1).toUpperCase()}):a}function b(a){return angular.isString(a)?a.replace(/[A-Z]/g,function(a,b){return 0===b?a:"_"+a.toLowerCase()}):a}function c(a){return a+"s"}return{camelize:a,underscore:b,pluralize:c}})}(),function(a){angular.module("rails").factory("RailsResourceInjector",["$injector",function(b){function c(c){return c?angular.isString(c)?b.get(c):c:a}function d(d){return d?b.instantiate(c(d)):a}function e(b){return angular.isObject(b)?b:b?d(b):a}return{createService:d,getService:e,getDependency:c}}])}(),function(){angular.module("rails").factory("railsUrlBuilder",["$interpolate",function(a){return function(b){var c,d=b.url,e=b.idAttribute;return angular.isFunction(d)||angular.isUndefined(d)?d:(-1===d.indexOf(a.startSymbol())&&(d=d+"/"+a.startSymbol()+e+a.endSymbol()),c=a(d),function(a){return d=c(a),"/"===d.charAt(d.length-1)&&(d=d.substr(0,d.length-1)),d})}}])}(),function(a){angular.module("rails").provider("railsSerializer",function(){var b={underscore:a,camelize:a,pluralize:a,exclusionMatchers:[]};this.underscore=function(a){return b.underscore=a,this},this.camelize=function(a){return b.camelize=a,this},this.pluralize=function(a){return b.pluralize=a,this},this.exclusionMatchers=function(a){return b.exclusionMatchers=a,this},this.$get=["$injector","RailsInflector","RailsResourceInjector",function(c,d,e){function f(c,d){function f(){angular.isFunction(c)&&(d=c,c={}),this.exclusions={},this.inclusions={},this.serializeMappings={},this.deserializeMappings={},this.customSerializedAttributes={},this.preservedAttributes={},this.customSerializers={},this.nestedResources={},this.options=angular.extend({excludeByDefault:!1},b,c||{}),d&&d.call(this,this)}return f.prototype.exclude=function(){var a=this.exclusions;return angular.forEach(arguments,function(b){a[b]=!1}),this},f.prototype.only=function(){var a=this.inclusions;return this.options.excludeByDefault=!0,angular.forEach(arguments,function(b){a[b]=!0}),this},f.prototype.nestedAttribute=function(){var a=this;return angular.forEach(arguments,function(b){a.rename(b,b+"_attributes")}),this},f.prototype.resource=function(a,b,c){return this.nestedResources[a]=b,c&&this.serializeWith(a,c),this},f.prototype.rename=function(b,c,d){return this.serializeMappings[b]=c,(d||d===a)&&(this.deserializeMappings[c]=b),this},f.prototype.add=function(a,b){return this.customSerializedAttributes[a]=b,this},f.prototype.preserve=function(a){return this.preservedAttributes[a]=!0,this},f.prototype.serializeWith=function(a,b){return this.customSerializers[a]=b,this},f.prototype.isExcludedFromSerialization=function(b){if(this.options.excludeByDefault&&!this.inclusions.hasOwnProperty(b)||this.exclusions.hasOwnProperty(b))return!0;if(this.options.exclusionMatchers){var c=!1;return angular.forEach(this.options.exclusionMatchers,function(d){angular.isString(d)?c=c||0===b.indexOf(d):angular.isFunction(d)?c=c||d.call(a,b):d instanceof RegExp&&(c=c||d.test(b))}),c}return!1},f.prototype.getSerializedAttributeName=function(b){var c=this.serializeMappings[b]||b,d=this.isExcludedFromSerialization(c),e=this.isExcludedFromSerialization(b);if(this.options.excludeByDefault){if(d&&e)return a}else if(d||e)return a;return this.underscore(c)},f.prototype.isExcludedFromDeserialization=function(){return!1},f.prototype.getDeserializedAttributeName=function(b){var c=this.camelize(b);return c=this.deserializeMappings[b]||this.deserializeMappings[c]||c,this.isExcludedFromDeserialization(b)||this.isExcludedFromDeserialization(c)?a:c},f.prototype.getNestedResource=function(a){return e.getDependency(this.nestedResources[a])},f.prototype.getAttributeSerializer=function(b){var c=this.getNestedResource(b),d=this.customSerializers[b];return d?e.createService(d):c?c.config.serializer:a},f.prototype.serializeValue=function(a){var b=a,c=this;if(angular.isArray(a))b=[],angular.forEach(a,function(a){b.push(c.serializeValue(a))});else if(angular.isObject(a)){if(angular.isDate(a))return a;b={},angular.forEach(a,function(a,d){angular.isFunction(a)||c.serializeAttribute(b,d,a)})}return b},f.prototype.serializeAttribute=function(b,c,d){var e=this.getAttributeSerializer(c),f=this.getSerializedAttributeName(c);f!==a&&(b[f]=e?e.serialize(d):this.serializeValue(d))},f.prototype.serialize=function(a){var b=this.serializeValue(a),c=this;return angular.isObject(b)&&angular.forEach(this.customSerializedAttributes,function(d,e){angular.isFunction(d)&&(d=d.call(a,a)),c.serializeAttribute(b,e,d)}),b},f.prototype.deserializeValue=function(a,b){var c=a,d=this;if(angular.isArray(a))c=[],angular.forEach(a,function(a){c.push(d.deserializeValue(a,b))});else if(angular.isObject(a)){if(angular.isDate(a))return a;c={},b&&(c=new b.config.resourceConstructor),angular.forEach(a,function(a,b){d.deserializeAttribute(c,b,a)})}return c},f.prototype.deserializeAttribute=function(b,c,d){var e,f,g=this.getDeserializedAttributeName(c);g!==a&&(e=this.getAttributeSerializer(g),f=this.getNestedResource(g),b[g]=this.preservedAttributes[g]?d:e?e.deserialize(d,f):this.deserializeValue(d,f))},f.prototype.deserialize=function(a,b){return this.deserializeValue(a,b)},f.prototype.pluralize=function(a){return this.options.pluralize?this.options.pluralize(a):a},f.prototype.underscore=function(a){return this.options.underscore?this.options.underscore(a):a},f.prototype.camelize=function(a){return this.options.camelize?this.options.camelize(a):a},f}return b.underscore=b.underscore||d.underscore,b.camelize=b.camelize||d.camelize,b.pluralize=b.pluralize||d.pluralize,f.defaultOptions=b,f}]})}(),function(a){angular.module("rails").factory("railsRootWrapper",function(){return{wrap:function(a,b){var c={};return c[angular.isArray(a)?b.config.pluralName:b.config.name]=a,c},unwrap:function(a,b){return a.data&&a.data.hasOwnProperty(b.config.name)?a.data=a.data[b.config.name]:a.data&&a.data.hasOwnProperty(b.config.pluralName)&&(a.data=a.data[b.config.pluralName]),a}}}),angular.module("rails").provider("RailsResource",function(){var b={rootWrapping:!0,updateMethod:"put",httpConfig:{},defaultParams:a,underscoreParams:!0,fullResponse:!1,extensions:[]};this.rootWrapping=function(a){return b.rootWrapping=a,this},this.updateMethod=function(a){return b.updateMethod=a,this},this.httpConfig=function(a){return b.httpConfig=a,this},this.defaultParams=function(a){return b.defaultParams=a,this},this.underscoreParams=function(a){return b.underscoreParams=a,this},this.fullResponse=function(a){return b.fullResponse=a,this},this.extensions=function(){return b.extensions=[],angular.forEach(arguments,function(a){b.extensions=b.extensions.concat(a)}),this},this.$get=["$http","$q","railsUrlBuilder","railsSerializer","railsRootWrapper","RailsResourceInjector",function(a,c,d,e,f,g){function h(a){if(a){var b=this.constructor.deserialize({data:a});this.constructor.config.rootWrapping&&(b=f.unwrap(b,this.constructor)),angular.extend(this,b.data)}}function i(a,b){return b&&("/"!==b[0]&&(a+="/"),a+=b),a}function j(a,b){for(var c,d=0,e=a.length;e>d;d++)c=a[d],angular.isString(c)&&(c=a[d]=g.getDependency(c)),b(c)}function k(a,b,c,d){var e=["included","extended,","configure"];a.$mixins||(a.$mixins=[]),angular.isString(c)&&(c=g.getDependency(c)),c&&-1===a.$mixins.indexOf(c)&&(angular.forEach(c,function(a,c){-1===e.indexOf(c)&&(b[c]=a)}),a.$mixins.push(c),angular.isFunction(d)&&d(a,c))}function l(a){var b=[];return angular.forEach(a,function(a){a="RailsResource"+a.charAt(0).toUpperCase()+a.slice(1)+"Mixin",b.push(g.getDependency(a))}),b}function m(a,b){return angular.isUndefined(a)?b:a}function n(a,b,c){return function(d){return(a||angular.identity)(d,b,c)}}function o(a,b,d){return function(e){return a?a(e,b,d):c.reject(e)}}return h.extendTo=function(a){function b(){this.constructor=a}return angular.forEach(this,function(b,c){a[c]=b}),angular.isArray(this.$modules)&&(a.$modules=this.$modules.slice(0)),b.prototype=this.prototype,a.prototype=new b,a.__super__=this.prototype,a},h.extend=function(){return angular.forEach(arguments,function(a){k(this,this,a,function(a,b){angular.isFunction(b.extended)&&b.extended(a)})},this),this},h.include=function(){return angular.forEach(arguments,function(a){k(this,this.prototype,a,function(a,b){angular.isFunction(b.included)&&b.included(a)})},this),this},h.configure=function(a){a=a||{},this.config&&(a=angular.extend({},this.config,a)),this.config={},this.config.idAttribute=a.idAttribute||"id",this.config.url=a.url,this.config.rootWrapping=m(a.rootWrapping,b.rootWrapping),this.config.httpConfig=a.httpConfig||b.httpConfig,this.config.httpConfig.headers=angular.extend({Accept:"application/json","Content-Type":"application/json"},this.config.httpConfig.headers||{}),this.config.defaultParams=a.defaultParams||b.defaultParams,this.config.underscoreParams=m(a.underscoreParams,b.underscoreParams),this.config.updateMethod=(a.updateMethod||b.updateMethod).toLowerCase(),this.config.fullResponse=m(a.fullResponse,b.fullResponse),this.config.requestTransformers=a.requestTransformers?a.requestTransformers.slice(0):[],this.config.responseInterceptors=a.responseInterceptors?a.responseInterceptors.slice(0):[],this.config.afterResponseInterceptors=a.afterResponseInterceptors?a.afterResponseInterceptors.slice(0):[],this.config.interceptors=a.interceptors?a.interceptors.slice(0):[],this.config.serializer=g.getService(a.serializer||e()),this.config.name=this.config.serializer.underscore(a.name),this.config.name&&(this.config.pluralName=this.config.serializer.underscore(a.pluralName||this.config.serializer.pluralize(this.config.name))),this.config.urlBuilder=d(this.config),this.config.resourceConstructor=this,this.extend.apply(this,l((a.extensions||[]).concat(b.extensions))),angular.forEach(this.$mixins,function(b){angular.isFunction(b.configure)&&b.configure(this.config,a)},this)},h.setUrl=function(a){this.configure({url:a})},h.buildUrl=function(a){return this.config.urlBuilder(a)},h.addInterceptor=function(a){this.config.interceptors.push(a)},h.intercept=function(a,b){var c={};b=g.getDependency(b),c[a]=function(a,c,d){return b(a,c,d)||a},this.addInterceptor(c)},h.interceptBeforeRequest=function(a){this.intercept("beforeRequest",a)},h.interceptBeforeRequestWrapping=function(a){this.intercept("beforeRequestWrapping",a)},h.interceptRequest=function(a){this.intercept("request",a)},h.interceptBeforeResponse=function(a){this.intercept("beforeResponse",a)},h.interceptBeforeResponseDeserialize=function(a){this.intercept("beforeResponseDeserialize",a)},h.interceptResponse=function(a){this.intercept("response",a)},h.interceptAfterResponse=function(a){this.intercept("afterResponse",a)},h.beforeResponse=function(a){a=g.getDependency(a),this.interceptResponse(function(b,c,d){return a(b.data,c.config.resourceConstructor,d),b})},h.afterResponse=function(a){a=g.getDependency(a),this.interceptAfterResponse(function(b,c,d){return a(b,c.config.resourceConstructor,d),b})},h.beforeRequest=function(a){a=g.getDependency(a),this.interceptBeforeRequestWrapping(function(b,c){return b.data=a(b.data,c.config.resourceConstructor)||b.data,b})},h.serialize=function(a){return a.data&&(a.data=this.config.serializer.serialize(a.data)),a},h.deserialize=function(a){return a.data=this.config.serializer.deserialize(a.data,this.config.resourceConstructor),a},h.callResponseInterceptors=function(a,b){var c=this.config;return j(c.responseInterceptors,function(d){a.resource=c.resourceConstructor,a.context=b,a=d(a)}),a},h.callAfterResponseInterceptors=function(a){var b=this.config;return j(b.afterResponseInterceptors,function(c){a.resource=b.resourceConstructor,a=c(a)}),a},h.runInterceptorPhase=function(a,b,c){var d=this.config,e=[];for(j(d.interceptors,function(b){(b[a]||b[a+"Error"])&&e.push(b[a],b[a+"Error"])});e.length;){var f=e.shift(),g=e.shift();c=c.then(n(f,d.resourceConstructor,b),o(g,d.resourceConstructor,b))}return c},h.$http=function(b,d,e){var g=angular.extend(angular.copy(this.config),e||{}),h=g.resourceConstructor,i=c.when(b);return i=this.runInterceptorPhase("beforeRequest",d,i).then(function(a){return a=h.serialize(a),j(g.requestTransformers,function(b){a.data=b(a.data,g.resourceConstructor)}),a}),i=this.runInterceptorPhase("beforeRequestWrapping",d,i),g.rootWrapping&&(i=i.then(function(a){return a.data=f.wrap(a.data,g.resourceConstructor),a})),i=this.runInterceptorPhase("request",d,i).then(function(b){return a(b)}),i=this.runInterceptorPhase("beforeResponse",d,i),i=this.runInterceptorPhase("beforeResponse",d,i).then(function(a){return a.originalData=a.data,a}),g.rootWrapping&&(i=i.then(function(a){return f.unwrap(a,g.resourceConstructor)})),i=this.runInterceptorPhase("beforeResponseDeserialize",d,i).then(function(a){return h.deserialize(a)}),i=this.callResponseInterceptors(i,d),i=this.runInterceptorPhase("response",d,i).then(function(a){return d&&a.hasOwnProperty("data")&&angular.isObject(a.data)&&angular.extend(d,a.data),g.fullResponse?a:d||a.data}),i=this.callAfterResponseInterceptors(i,d),i=this.runInterceptorPhase("afterResponse",d,i),i.resource=g.resourceConstructor,i.context=d,i},h.processParameters=function(a){var b={};return angular.isObject(a)&&this.config.underscoreParams?(angular.forEach(a,function(a,c){b[this.config.serializer.underscore(c)]=a},this),b):a},h.getParameters=function(a){var b;return this.config.defaultParams&&(b=angular.copy(this.config.defaultParams)),angular.isObject(a)&&(b=angular.extend(b||{},a)),this.processParameters(b)},h.getHttpConfig=function(a){var b=this.getParameters(a);return b?angular.extend({params:b},this.config.httpConfig):angular.copy(this.config.httpConfig)},h.$url=h.resourceUrl=function(a,b){return angular.isObject(a)||(a={id:a}),i(this.buildUrl(a||{}),b)},h.$get=function(a,b){return this.$http(angular.extend({method:"get",url:a},this.getHttpConfig(b)))},h.query=function(a,b){return this.$get(this.resourceUrl(b),a)},h.get=function(a,b){return this.$get(this.resourceUrl(a),b)},h.prototype.$url=function(a){return i(this.constructor.resourceUrl(this),a)},h.prototype.$http=function(a,b){return this.constructor.$http(a,this,b)},angular.forEach(["post","put","patch"],function(a){h["$"+a]=function(b,c){return c=angular.copy(c),this.$http(angular.extend({method:a,url:b,data:c},this.getHttpConfig()))},h.prototype["$"+a]=function(b){var c=angular.copy(this,{});return this.constructor.$http(angular.extend({method:a,url:b,data:c},this.constructor.getHttpConfig()),this)}}),h.prototype.create=function(){return this.$post(this.$url(),this)},h.prototype.update=function(){return this["$"+this.constructor.config.updateMethod](this.$url(),this)},h.prototype.get=function(){return this.constructor.$http(angular.extend({method:"GET",url:this.$url()},this.constructor.getHttpConfig()),this)},h.prototype.isNew=function(){var a=this.constructor.config.idAttribute;return angular.isUndefined(this[a])||null===this[a]},h.prototype.save=function(){return this.isNew()?this.create():this.update()},h.$delete=function(a,b){return this.$http(angular.extend({method:"delete",url:a},this.getHttpConfig(b)))},h.prototype.$delete=function(a,b){return this.constructor.$http(angular.extend({method:"delete",url:a},this.constructor.getHttpConfig(b)),this)},h.prototype.remove=h.prototype["delete"]=function(){return this.$delete(this.$url())},h}]}),angular.module("rails").factory("railsResourceFactory",["RailsResource",function(a){return function(b){function c(){c.__super__.constructor.apply(this,arguments)}return a.extendTo(c),c.configure(b),c}}])}();