abyme 0.5.0 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
data/abyme.gemspec CHANGED
@@ -38,9 +38,11 @@ Gem::Specification.new do |spec|
38
38
 
39
39
  # Dummy app
40
40
  spec.add_development_dependency "sqlite3"
41
- spec.add_development_dependency 'rails'
42
- spec.add_development_dependency 'pry-rails'
41
+ spec.add_development_dependency 'rails', "~> 6.0.3.6"
42
+ spec.add_development_dependency 'pry-byebug'
43
+ spec.add_development_dependency 'byebug'
43
44
  spec.add_development_dependency 'web-console'
45
+ spec.add_development_dependency 'simple_form'
44
46
 
45
47
  spec.add_development_dependency 'puma'
46
48
  spec.add_development_dependency 'simplecov'
data/dist/abyme.js ADDED
@@ -0,0 +1,2 @@
1
+ function e(t,n){return(e=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(t,n)}function t(e,n,i){if(!e.s){if(i instanceof r){if(!i.s)return void(i.o=t.bind(null,e,n));1&n&&(n=i.s),i=i.v}if(i&&i.then)return void i.then(t.bind(null,e,n),t.bind(null,e,2));e.s=n,e.v=i;var o=e.o;o&&o(e)}}var n=function(n){var o,a;function s(){return n.apply(this,arguments)||this}a=n,(o=s).prototype=Object.create(a.prototype),o.prototype.constructor=o,e(o,a);var c,u=s.prototype;return u.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},u.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},u.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},u.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},u.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},u.abymeBeforeAdd=function(e){},u.abymeAfterAdd=function(e){},u.abymeBeforeRemove=function(e){},u.abymeAfterRemove=function(e){},u.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},u.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},u.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},u.add_default_associations=function(){try{var e=this,n=0,o=function(e,n,o){for(var a;;){var s=e();if(i(s)&&(s=s.v),!s)return c;if(s.then){a=0;break}var c=o();if(c&&c.then){if(!i(c)){a=1;break}c=c.s}}var u=new r,f=t.bind(null,u,2);return(0===a?s.then(h):1===a?c.then(d):(void 0).then(function(){(s=e())?s.then?s.then(h).then(void 0,f):h(s):t(u,1,c)})).then(void 0,f),u;function d(n){c=n;do{if(!(s=e())||i(s)&&!s.v)return void t(u,1,c);if(s.then)return void s.then(h).then(void 0,f);i(c=o())&&(c=c.v)}while(!c||!c.then);c.then(d).then(void 0,f)}function h(e){e?(c=o())&&c.then?c.then(d).then(void 0,f):d(c):t(u,1,c)}}(function(){return n<e.count},0,function(){return e.add_association(),n++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(o&&o.then?o.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},u.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(c=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(s.prototype,c),s}(require("stimulus").Controller),r=function(){function e(){}return e.prototype.then=function(n,r){var i=new e,o=this.s;if(o){var a=1&o?n:r;if(a){try{t(i,1,a(this.v))}catch(e){t(i,2,e)}return i}return this}return this.o=function(e){try{var o=e.v;1&e.s?t(i,1,n?n(o):o):r?t(i,1,r(o)):t(i,2,o)}catch(e){t(i,2,e)}},i},e}();function i(e){return e instanceof r&&1&e.s}n.targets=["template","associations","fields","newFields"],exports.AbymeController=n;
2
+ //# sourceMappingURL=abyme.js.map
data/dist/abyme.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"4FAuCO,SAASA,EAAQC,EAAMC,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,EACN,EAAV+E,EAAMnF,EACTJ,EAAQqE,EAAQ,EAAGc,EAAcA,EAAYhF,GAASA,GAC5CiF,EACVpF,EAAQqE,EAAQ,EAAGe,EAAWjF,IAE9BH,EAAQqE,EAAQ,EAAGlE,GAEnB,MAAOmF,GACRtF,EAAQqE,EAAQ,EAAGiB,KAGdjB,KAhC0B,GAgE5B,WAAwBmB,GAC9B,OAAOA,gBAA0C,EAAbA,EAASpF,IA3DrCqF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import{Controller as e}from"stimulus";class t extends e{connect(){console.log("Abyme Connected"),this.count&&this.add_default_associations()}get count(){return this.element.dataset.minCount||0}get position(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}add_association(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;const t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")}remove_association(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")}create_event(e,t=null){const a=new CustomEvent(`abyme:${e}`,{detail:{controller:this,content:t}});this.element.dispatchEvent(a),this.dispatch(a,e)}dispatch(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)}abymeBeforeAdd(e){}abymeAfterAdd(e){}abymeBeforeRemove(e){}abymeAfterRemove(e){}build_html(){let e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){const t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e}mark_for_destroy(e){let t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")}limit_check(){return this.newFieldsTargets.filter(e=>!e.classList.contains("abyme--marked-for-destroy")).length>=parseInt(this.element.dataset.limit)}async add_default_associations(){let e=0;for(;e<this.count;)this.add_association(),e++,await this.sleep(1)}sleep(e){return new Promise(t=>setTimeout(t,e))}}t.targets=["template","associations","fields","newFields"];export{t as AbymeController};
2
+ //# sourceMappingURL=abyme.modern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.modern.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["Controller","connect","console","log","this","count","add_default_associations","element","dataset","minCount","position","associationsTarget","abymePosition","add_association","event","preventDefault","limit","limit_check","create_event","html","build_html","insertAdjacentHTML","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","value","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","[object Object]","i","sleep","ms","Promise","resolve","setTimeout","targets"],"mappings":"sDAE6BA,EAO3BC,UACEC,QAAQC,IAAI,mBAERC,KAAKC,OAIPD,KAAKE,2BAMAD,YACP,YAAYE,QAAQC,QAAQC,UAAY,EAM9BC,eACV,MAAyD,aAA7CC,mBAAmBH,QAAQI,cAA0B,YAAc,aAsBjFC,gBAAgBC,GAKd,GAJIA,GACFA,EAAMC,iBAGJX,KAAKG,QAAQC,QAAQQ,OAASZ,KAAKa,cAErC,OADAb,KAAKc,aAAa,oBAIpB,MAAMC,EAAOf,KA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IAAIC,EAAI,EACR,KAAOA,EAAIrD,KAAKC,OACdD,KAAKS,kBACL4C,eACWC,MAAM,GAIrBA,MAAMC,GACJ,WAAWC,QAAQC,GAAWC,WAAWD,EAASF,OAxL7CI,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import{Controller as e}from"stimulus";function t(e,n){return(t=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,n)}function n(e,t,r){if(!e.s){if(r instanceof i){if(!r.s)return void(r.o=n.bind(null,e,t));1&t&&(t=r.s),r=r.v}if(r&&r.then)return void r.then(n.bind(null,e,t),n.bind(null,e,2));e.s=t,e.v=r;var o=e.o;o&&o(e)}}var r=function(e){var r,a;function s(){return e.apply(this,arguments)||this}a=e,(r=s).prototype=Object.create(a.prototype),r.prototype.constructor=r,t(r,a);var c,f=s.prototype;return f.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},f.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},f.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},f.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},f.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},f.abymeBeforeAdd=function(e){},f.abymeAfterAdd=function(e){},f.abymeBeforeRemove=function(e){},f.abymeAfterRemove=function(e){},f.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},f.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},f.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},f.add_default_associations=function(){try{var e=this,t=0,r=function(e,t,r){for(var a;;){var s=e();if(o(s)&&(s=s.v),!s)return c;if(s.then){a=0;break}var c=r();if(c&&c.then){if(!o(c)){a=1;break}c=c.s}}var f=new i,u=n.bind(null,f,2);return(0===a?s.then(h):1===a?c.then(d):(void 0).then(function(){(s=e())?s.then?s.then(h).then(void 0,u):h(s):n(f,1,c)})).then(void 0,u),f;function d(t){c=t;do{if(!(s=e())||o(s)&&!s.v)return void n(f,1,c);if(s.then)return void s.then(h).then(void 0,u);o(c=r())&&(c=c.v)}while(!c||!c.then);c.then(d).then(void 0,u)}function h(e){e?(c=r())&&c.then?c.then(d).then(void 0,u):d(c):n(f,1,c)}}(function(){return t<e.count},0,function(){return e.add_association(),t++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(r&&r.then?r.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},f.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(c=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}(s.prototype,c),s}(e),i=function(){function e(){}return e.prototype.then=function(t,r){var i=new e,o=this.s;if(o){var a=1&o?t:r;if(a){try{n(i,1,a(this.v))}catch(e){n(i,2,e)}return i}return this}return this.o=function(e){try{var o=e.v;1&e.s?n(i,1,t?t(o):o):r?n(i,1,r(o)):n(i,2,o)}catch(e){n(i,2,e)}},i},e}();function o(e){return e instanceof i&&1&e.s}r.targets=["template","associations","fields","newFields"];export{r as AbymeController};
2
+ //# sourceMappingURL=abyme.module.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.module.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","Controller","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"kIAuCO,SAASA,EAAQC,EAAMC,EAAOC,GACpC,IAAKF,EAAKG,EAAG,CACZ,GAAID,aAAiBE,EAAO,CAC3B,IAAIF,EAAMC,EAOT,YADAD,EAAMG,EAAIN,EAAQO,KAAK,KAAMN,EAAMC,IALvB,EAARA,IACHA,EAAQC,EAAMC,GAEfD,EAAQA,EAAMK,EAMhB,GAAIL,GAASA,EAAMM,KAElB,YADAN,EAAMM,KAAKT,EAAQO,KAAK,K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,gBAA0C,EAAbA,EAASrF,IA3DrCsF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
data/dist/abyme.umd.js ADDED
@@ -0,0 +1,2 @@
1
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("stimulus")):"function"==typeof define&&define.amd?define(["exports","stimulus"],t):t((e||self).abyme={},e.Stimulus)}(this,function(e,t){function n(e,t){return(n=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function i(e,t,n){if(!e.s){if(n instanceof o){if(!n.s)return void(n.o=i.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(i.bind(null,e,t),i.bind(null,e,2));e.s=t,e.v=n;var r=e.o;r&&r(e)}}var r=function(e){var t,r;function s(){return e.apply(this,arguments)||this}r=e,(t=s).prototype=Object.create(r.prototype),t.prototype.constructor=t,n(t,r);var f,u=s.prototype;return u.connect=function(){console.log("Abyme Connected"),this.count&&this.add_default_associations()},u.add_association=function(e){if(e&&e.preventDefault(),this.element.dataset.limit&&this.limit_check())return this.create_event("limit-reached"),!1;var t=this.build_html();this.create_event("before-add"),this.associationsTarget.insertAdjacentHTML(this.position,t),this.create_event("after-add")},u.remove_association=function(e){e.preventDefault(),this.create_event("before-remove"),this.mark_for_destroy(e),this.create_event("after-remove")},u.create_event=function(e,t){void 0===t&&(t=null);var n=new CustomEvent("abyme:"+e,{detail:{controller:this,content:t}});this.element.dispatchEvent(n),this.dispatch(n,e)},u.dispatch=function(e,t){"before-add"===t&&this.abymeBeforeAdd&&this.abymeBeforeAdd(e),"after-add"===t&&this.abymeAfterAdd&&this.abymeAfterAdd(e),"before-remove"===t&&this.abymeBeforeRemove&&this.abymeBeforeAdd(e),"after-remove"===t&&this.abymeAfterRemove&&this.abymeAfterRemove(e)},u.abymeBeforeAdd=function(e){},u.abymeAfterAdd=function(e){},u.abymeBeforeRemove=function(e){},u.abymeAfterRemove=function(e){},u.build_html=function(){var e=this.templateTarget.innerHTML.replace(/NEW_RECORD/g,(new Date).getTime());if(e.match(/<template[\s\S]+<\/template>/)){var t=e.match(/<template[\s\S]+<\/template>/)[0].replace(/(\[\d{12,}\])(\[[^\[\]]+\]"){1}/g,"[NEW_RECORD]$2");e=e.replace(/<template[\s\S]+<\/template>/g,t)}return e},u.mark_for_destroy=function(e){var t=e.target.closest(".abyme--fields");t.querySelector("input[name*='_destroy']").value=1,t.style.display="none",t.classList.add("abyme--marked-for-destroy")},u.limit_check=function(){return this.newFieldsTargets.filter(function(e){return!e.classList.contains("abyme--marked-for-destroy")}).length>=parseInt(this.element.dataset.limit)},u.add_default_associations=function(){try{var e=this,t=0,n=function(e,t,n){for(var r;;){var s=e();if(a(s)&&(s=s.v),!s)return f;if(s.then){r=0;break}var f=n();if(f&&f.then){if(!a(f)){r=1;break}f=f.s}}var u=new o,c=i.bind(null,u,2);return(0===r?s.then(l):1===r?f.then(d):(void 0).then(function(){(s=e())?s.then?s.then(l).then(void 0,c):l(s):i(u,1,f)})).then(void 0,c),u;function d(t){f=t;do{if(!(s=e())||a(s)&&!s.v)return void i(u,1,f);if(s.then)return void s.then(l).then(void 0,c);a(f=n())&&(f=f.v)}while(!f||!f.then);f.then(d).then(void 0,c)}function l(e){e?(f=n())&&f.then?f.then(d).then(void 0,c):d(f):i(u,1,f)}}(function(){return t<e.count},0,function(){return e.add_association(),t++,Promise.resolve(e.sleep(1)).then(function(){})});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},u.sleep=function(e){return new Promise(function(t){return setTimeout(t,e)})},(f=[{key:"count",get:function(){return this.element.dataset.minCount||0}},{key:"position",get:function(){return"end"===this.associationsTarget.dataset.abymePosition?"beforeend":"afterbegin"}}])&&function(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}(s.prototype,f),s}(t.Controller),o=function(){function e(){}return e.prototype.then=function(t,n){var r=new e,o=this.s;if(o){var a=1&o?t:n;if(a){try{i(r,1,a(this.v))}catch(e){i(r,2,e)}return r}return this}return this.o=function(e){try{var o=e.v;1&e.s?i(r,1,t?t(o):o):n?i(r,1,n(o)):i(r,2,o)}catch(e){i(r,2,e)}},r},e}();function a(e){return e instanceof o&&1&e.s}r.targets=["template","associations","fields","newFields"],e.AbymeController=r});
2
+ //# sourceMappingURL=abyme.umd.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abyme.umd.js","sources":["../src/abyme_controller.js"],"sourcesContent":["import { Controller } from 'stimulus';\n\nexport default class extends Controller {\n // static targets = ['template', 'associations', 'fields', 'newFields'];\n // Some applications don't compile correctly with the usual static syntax. \n // Thus implementing targets with standard getters below\n\n static targets = ['template', 'associations', 'fields', 'newFields']\n\n connect() {\n console.log(\"Abyme Connected\")\n\n if (this.count) {\n // If data-count is present,\n // add n default fields on page load\n\n this.add_default_associations();\n }\n }\n\n // return the value of the data-count attribute\n\n get count() {\n return this.element.dataset.minCount || 0;\n }\n \n // return the value of the data-position attribute\n // if there is no position specified set end as default\n\n get position() {\n return this.associationsTarget.dataset.abymePosition === 'end' ? 'beforeend' : 'afterbegin';\n }\n\n // ADD_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#add_association\n // <button> element by default\n\n // if a data-count is present the add_association\n // will be call without an event so we have to check\n // this case\n\n // check for limit reached \n // dispatch an event if the limit is reached\n\n // - call the function build_html that take care\n // for building the correct html to be inserted in the DOM\n // - dispatch an event before insert\n // - insert html into the dom\n // - dispatch an event after insert\n\n add_association(event) {\n if (event) {\n event.preventDefault();\n }\n\n if (this.element.dataset.limit && this.limit_check()) {\n this.create_event('limit-reached')\n return false\n }\n\n const html = this.build_html();\n this.create_event('before-add');\n this.associationsTarget.insertAdjacentHTML(this.position, html);\n this.create_event('after-add');\n }\n\n // REMOVE_ASSOCIATION\n\n // this function is call whenever a click occurs\n // on the element with the click->abyme#remove_association\n // <button> element by default\n\n // - call the function mark_for_destroy that takes care\n // of marking the element for destruction and hiding it\n // - dispatch an event before mark & hide\n // - mark for descrution + hide the element\n // - dispatch an event after mark and hide\n\n remove_association(event) {\n event.preventDefault();\n\n this.create_event('before-remove');\n this.mark_for_destroy(event);\n this.create_event('after-remove');\n }\n\n // LIFECYCLE EVENTS RELATED\n\n // CREATE_EVENT\n\n // take a stage (String) => before-add, after-add...\n // create a new custom event \n // and dispatch at at the controller level\n\n create_event(stage, html = null) {\n const event = new CustomEvent(`abyme:${stage}`, { detail: {controller: this, content: html} });\n this.element.dispatchEvent(event);\n // WIP\n this.dispatch(event, stage);\n }\n\n // WIP : Trying to integrate event handling through controller inheritance\n dispatch(event, stage) {\n if (stage === 'before-add' && this.abymeBeforeAdd) this.abymeBeforeAdd(event);\n if (stage === 'after-add' && this.abymeAfterAdd) this.abymeAfterAdd(event);\n if (stage === 'before-remove' && this.abymeBeforeRemove) this.abymeBeforeAdd(event);\n if (stage === 'after-remove' && this.abymeAfterRemove) this.abymeAfterRemove(event);\n }\n\n abymeBeforeAdd(event) {\n }\n\n abymeAfterAdd(event) {\n }\n\n abymeBeforeRemove(event) {\n }\n\n abymeAfterRemove(event) {\n }\n\n // BUILD HTML\n\n // takes the html template and substitutes the sub-string\n // NEW_RECORD for a generated timestamp\n // then if there is a sub template in the html (multiple nested level)\n // set all the sub timestamps back as NEW_RECORD\n // finally returns the html \n\n build_html() {\n let html = this.templateTarget.innerHTML.replace(\n /NEW_RECORD/g,\n new Date().getTime()\n );\n \n if (html.match(/<template[\\s\\S]+<\\/template>/)) {\n const template = html\n .match(/<template[\\s\\S]+<\\/template>/)[0]\n .replace(/(\\[\\d{12,}\\])(\\[[^\\[\\]]+\\]\"){1}/g, `[NEW_RECORD]$2`);\n \n html = html.replace(/<template[\\s\\S]+<\\/template>/g, template);\n }\n\n return html;\n }\n \n // MARK_FOR_DESTROY\n\n // mark association for destruction\n // get the closest abyme--fields from the remove_association button\n // set the _destroy input value as 1\n // hide the element\n // add the class of abyme--marked-for-destroy to the element\n\n mark_for_destroy(event) {\n let item = event.target.closest('.abyme--fields');\n item.querySelector(\"input[name*='_destroy']\").value = 1;\n item.style.display = 'none';\n item.classList.add('abyme--marked-for-destroy')\n }\n\n\n // LIMIT_CHECK\n\n // Check if associations limit is reached\n // based on newFieldsTargets only\n // persisted fields are ignored\n\n limit_check() {\n return (this.newFieldsTargets\n .filter(item => !item.classList.contains('abyme--marked-for-destroy'))).length \n >= parseInt(this.element.dataset.limit)\n }\n\n // ADD_DEFAULT_ASSOCIATION\n\n // Add n default blank associations at page load\n // call sleep function to ensure uniqueness of timestamp\n\n async add_default_associations() {\n let i = 0\n while (i < this.count) {\n this.add_association()\n i++\n await this.sleep(1);\n }\n }\n\n sleep(ms) {\n return new Promise(resolve => setTimeout(resolve, ms));\n }\n}\n"],"names":["_settle","pact","state","value","s","_Pact","o","bind","v","then","observer","connect","console","log","this","count","add_default_associations","add_association","event","preventDefault","element","dataset","limit","limit_check","create_event","html","build_html","associationsTarget","insertAdjacentHTML","position","remove_association","mark_for_destroy","stage","CustomEvent","detail","controller","content","dispatchEvent","dispatch","abymeBeforeAdd","abymeAfterAdd","abymeBeforeRemove","abymeAfterRemove","templateTarget","innerHTML","replace","Date","getTime","match","template","item","target","closest","querySelector","style","display","classList","add","newFieldsTargets","filter","contains","length","parseInt","i","test","update","body","shouldContinue","_isSettledPact","result","reject","_resumeAfterTest","_resumeAfterBody","updateValue","_this2","sleep","ms","Promise","resolve","setTimeout","minCount","abymePosition","Controller","prototype","onFulfilled","onRejected","callback","e","_this","thenable","targets"],"mappings":"uWAuCO,SAASA,EAAQC,EAAMC,EAAOC,GACpC,IAAKF,EAAKG,EAAG,CACZ,GAAID,aAAiBE,EAAO,CAC3B,IAAIF,EAAMC,EAOT,YADAD,EAAMG,EAAIN,EAAQO,KAAK,KAAMN,EAAMC,IALvB,EAARA,IACHA,EAAQC,EAAMC,GAEfD,EAAQA,EAAMK,EAMhB,GAAIL,GAASA,EAAMM,KAElB,YADAN,EA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E5B,WAAwBoB,GAC9B,OAAOA,gBAA0C,EAAbA,EAASrF,IA3DrCsF,QAAU,CAAC,WAAY,eAAgB,SAAU"}
@@ -0,0 +1,2 @@
1
+ import '@babel/polyfill'
2
+ import 'mutationobserver-shim'
@@ -2,11 +2,11 @@ module Abyme
2
2
  class AbymeBuilder < ActionView::Base
3
3
  include ActionView
4
4
 
5
- # If a block is given to the #abymize helper
6
- # it will instanciate a new AbymeBuilder
5
+ # If a block is given to the #abymize helper
6
+ # it will instanciate a new AbymeBuilder
7
7
  # and pass to it the association name (Symbol)
8
8
  # the form object, lookup_context optionaly a partial path
9
- # then yield itself to the block
9
+ # then yield itself to the block
10
10
 
11
11
  def initialize(association:, form:, context:, partial:, &block)
12
12
  @association = association
@@ -14,14 +14,14 @@ module Abyme
14
14
  @context = context
15
15
  @lookup_context = context.lookup_context
16
16
  @partial = partial
17
- yield(self) if block_given?
17
+ yield(self) if block
18
18
  end
19
19
 
20
20
  # RECORDS
21
21
 
22
22
  # calls the #persisted_records_for helper method
23
23
  # passing association, form and options to it
24
-
24
+
25
25
  def records(options = {})
26
26
  persisted_records_for(@association, @form, options) do |fields_for_association|
27
27
  render_association_partial(@association, fields_for_association, @partial, @context)
@@ -32,12 +32,10 @@ module Abyme
32
32
 
33
33
  # calls the #new_records_for helper method
34
34
  # passing association, form and options to it
35
-
36
35
  def new_records(options = {}, &block)
37
36
  new_records_for(@association, @form, options) do |fields_for_association|
38
37
  render_association_partial(@association, fields_for_association, @partial, @context)
39
38
  end
40
39
  end
41
-
42
40
  end
43
- end
41
+ end
@@ -4,6 +4,8 @@ module Abyme
4
4
  def abyme_for(association, options = {}, &block)
5
5
  @template.abyme_for(association, self, options, &block)
6
6
  end
7
+
8
+ alias :abymize :abyme_for
7
9
  end
8
10
  end
9
11
  end
data/lib/abyme/model.rb CHANGED
@@ -1,50 +1,52 @@
1
1
  module Abyme
2
2
  module Model
3
3
  module ClassMethods
4
- def abymize(association, permit: nil, reject: nil, **options)
4
+ def abymize(association, permit: nil, reject: nil, class_name: nil, **options)
5
5
  default_options = {reject_if: :all_blank, allow_destroy: true}
6
6
  nested_attributes_options = default_options.merge(options)
7
7
  accepts_nested_attributes_for association, nested_attributes_options
8
8
  # Save allow_destroy value for this model/association for later
9
9
  save_destroy_option(association, nested_attributes_options[:allow_destroy])
10
- Abyme::Model.permit_attributes(self.name, association, permit || reject, permit.present?) if permit.present? || reject.present?
10
+ Abyme::Model.permit_attributes(name, association, permit || reject, permit.present?, class_name) if permit.present? || reject.present?
11
11
  end
12
12
 
13
+ alias_method :abyme_for, :abymize
14
+
13
15
  def abyme_attributes
14
- Abyme::Model.instance_variable_get(:@permitted_attributes)[self.name]
16
+ Abyme::Model.instance_variable_get(:@permitted_attributes)[name]
15
17
  end
16
18
 
17
19
  private
18
20
 
19
21
  def save_destroy_option(association, value)
20
- Abyme::Model.instance_variable_get(:@allow_destroy)[self.name][association] = value
22
+ Abyme::Model.instance_variable_get(:@allow_destroy)[name][association] = value
21
23
  end
22
24
  end
23
25
 
24
26
  @permitted_attributes ||= {}
25
- @allow_destroy ||= {}
27
+ @allow_destroy ||= {}
26
28
 
27
29
  attr_accessor :allow_destroy
28
- attr_reader :permitted_attributes
30
+ attr_reader :permitted_attributes
29
31
 
30
- def self.permit_attributes(class_name, association, attributes, permit)
31
- @permitted_attributes[class_name]["#{association}_attributes".to_sym] = AttributesBuilder.new(class_name, association, attributes, permit)
32
- .build_attributes
32
+ def self.permit_attributes(class_name, association, attributes, permit, association_class_name = nil)
33
+ @permitted_attributes[class_name]["#{association}_attributes".to_sym] = AttributesBuilder.new(class_name, association, attributes, permit, association_class_name)
34
+ .build_attributes
33
35
  end
34
36
 
35
37
  def self.included(klass)
36
38
  @permitted_attributes[klass.name] ||= {}
37
- @allow_destroy[klass.name] ||= {}
39
+ @allow_destroy[klass.name] ||= {}
38
40
  klass.extend ClassMethods
39
41
  end
40
42
 
41
43
  class AttributesBuilder
42
- def initialize(model, association, attributes, permit = true)
44
+ def initialize(model, association, attributes, permit = true, association_class_name = nil)
43
45
  @model = model
44
46
  @association = association
45
47
  @attributes_list = attributes
46
48
  @permit = permit
47
- @association_class = @association.to_s.classify.constantize
49
+ @association_class = association_class_name&.safe_constantize || @association.to_s.classify.constantize
48
50
  end
49
51
 
50
52
  def build_attributes
@@ -53,7 +55,7 @@ module Abyme
53
55
  if @permit && @attributes_list == :all_attributes
54
56
  authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
55
57
  elsif @permit
56
- @attributes_list << nested_attributes unless (nested_attributes.blank? || @attributes_list.include?(nested_attributes))
58
+ @attributes_list << nested_attributes unless nested_attributes.blank? || @attributes_list.include?(nested_attributes)
57
59
  authorized_attributes += @attributes_list
58
60
  else
59
61
  authorized_attributes = build_all_attributes(authorized_attributes, nested_attributes)
@@ -72,7 +74,7 @@ module Abyme
72
74
 
73
75
  def build_all_attributes(authorized_attributes, nested_attributes)
74
76
  authorized_attributes += add_all_attributes
75
- authorized_attributes << nested_attributes unless (nested_attributes.blank? || authorized_attributes.include?(nested_attributes))
77
+ authorized_attributes << nested_attributes unless nested_attributes.blank? || authorized_attributes.include?(nested_attributes)
76
78
  authorized_attributes
77
79
  end
78
80
 
@@ -83,4 +85,4 @@ module Abyme
83
85
  end
84
86
  end
85
87
  end
86
- end
88
+ end
data/lib/abyme/version.rb CHANGED
@@ -2,8 +2,8 @@
2
2
  module Abyme
3
3
  module VERSION
4
4
  MAJOR = 0
5
- MINOR = 5
6
- PATCH = 0
5
+ MINOR = 6
6
+ PATCH = 3
7
7
 
8
8
  STRING = [MAJOR, MINOR, PATCH].join(".")
9
9
  end
@@ -2,7 +2,6 @@ require_relative "abyme_builder"
2
2
 
3
3
  module Abyme
4
4
  module ViewHelpers
5
-
6
5
  # ABYME_FOR
7
6
 
8
7
  # this helper will generate the top level wrapper markup
@@ -23,7 +22,7 @@ module Abyme
23
22
  # set the default number of blank fields to display
24
23
 
25
24
  # - partial (String)
26
- # to customize the partial path by default #abyme_for will expect
25
+ # to customize the partial path by default #abyme_for will expect
27
26
  # a partial to bbe present in views/abyme
28
27
 
29
28
  # - Exemple
@@ -39,21 +38,25 @@ module Abyme
39
38
  # </div>
40
39
 
41
40
  def abyme_for(association, form, options = {}, &block)
42
- content_tag(:div, data: { controller: 'abyme', limit: options[:limit], min_count: options[:min_count] }, id: "abyme--#{association}") do
43
- if block_given?
44
- yield(Abyme::AbymeBuilder.new(
45
- association: association, form: form, context: self, partial: options[:partial]
41
+ content_tag(:div, data: {controller: "abyme", limit: options[:limit], min_count: options[:min_count]}, id: "abyme--#{association}") do
42
+ if block
43
+ yield(
44
+ Abyme::AbymeBuilder.new(
45
+ association: association, form: form, context: self, partial: options[:partial]
46
46
  )
47
47
  )
48
48
  else
49
- model = association.to_s.singularize.classify.constantize
49
+ # model = association.to_s.singularize.classify.constantize
50
+ model = association.to_s.singularize
50
51
  concat(persisted_records_for(association, form, options))
51
- concat(new_records_for(association, form, options))
52
+ concat(new_records_for(association, form, options))
52
53
  concat(add_associated_record(content: options[:button_text] || "Add #{model}"))
53
54
  end
54
55
  end
55
56
  end
56
57
 
58
+ alias_method :abymize, :abyme_for
59
+
57
60
  # NEW_RECORDS_FOR
58
61
 
59
62
  # this helper is call by the AbymeBuilder #new_records instance method
@@ -70,9 +73,9 @@ module Abyme
70
73
  # will output this html
71
74
 
72
75
  # <div data-target="abyme.associations" data-association="tasks" data-abyme-position="end">
73
- # <template class="abyme--task_template" data-target="abyme.template">
76
+ # <template class="abyme--task_template" data-target="abyme.template">
74
77
  # <div data-target="abyme.fields abyme.newFields" class="abyme--fields task-fields">
75
- # ... partial html goes here
78
+ # ... partial html goes here
76
79
  # </div>
77
80
  # </template>
78
81
  # ... new rendered fields goes here
@@ -80,41 +83,41 @@ module Abyme
80
83
 
81
84
  # == Options
82
85
  # - position (:start, :end)
83
- # allows you to specify whether new fields added dynamically
84
- # should go at the top or at the bottom
86
+ # allows you to specify whether new fields added dynamically
87
+ # should go at the top or at the bottom
85
88
  # :end is the default value
86
89
 
87
90
  # - partial (String)
88
- # to customize the partial path by default #abyme_for will expect
91
+ # to customize the partial path by default #abyme_for will expect
89
92
  # a partial to bbe present in views/abyme
90
93
 
91
94
  # - fields_html (Hash)
92
95
  # allows you to pass any html attributes to each fields wrapper
93
96
 
94
97
  # - wrapper_html (Hash)
95
- # allows you to pass any html attributes to the the html element
98
+ # allows you to pass any html attributes to the the html element
96
99
  # wrapping all the fields
97
100
 
98
101
  def new_records_for(association, form, options = {}, &block)
99
102
  options[:wrapper_html] ||= {}
100
103
 
101
- wrapper_default = {
102
- data: {
103
- abyme_target: 'associations',
104
- association: association,
105
- abyme_position: options[:position] || :end
106
- }
104
+ wrapper_default = {
105
+ data: {
106
+ abyme_target: "associations",
107
+ association: association,
108
+ abyme_position: options[:position] || :end
109
+ }
107
110
  }
108
111
 
109
- fields_default = { data: { target: 'abyme.fields abyme.newFields' } }
112
+ fields_default = {data: {target: "abyme.fields abyme.newFields"}}
110
113
 
111
114
  content_tag(:div, build_attributes(wrapper_default, options[:wrapper_html])) do
112
- content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: { abyme_target: 'template' }) do
113
- form.fields_for association, association.to_s.classify.constantize.new, child_index: 'NEW_RECORD' do |f|
115
+ content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: {abyme_target: "template"}) do
116
+ form.fields_for association, association.to_s.classify.constantize.new, child_index: "NEW_RECORD" do |f|
114
117
  content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
115
118
  # Here, if a block is passed, we're passing the association fields to it, rather than the form itself
116
119
  # block_given? ? yield(f) : render(options[:partial] || "abyme/#{association.to_s.singularize}_fields", f: f)
117
- block_given? ? yield(f) : render_association_partial(association, f, options[:partial])
120
+ block ? yield(f) : render_association_partial(association, f, options[:partial])
118
121
  end
119
122
  end
120
123
  end
@@ -152,21 +155,23 @@ module Abyme
152
155
  # ex: order: { created_at: :desc }
153
156
 
154
157
  # - partial (String)
155
- # to customize the partial path by default #abyme_for will expect
158
+ # to customize the partial path by default #abyme_for will expect
156
159
  # a partial to bbe present in views/abyme
157
160
 
158
161
  # - fields_html (Hash)
159
162
  # allows you to pass any html attributes to each fields wrapper
160
163
 
161
164
  # - wrapper_html (Hash)
162
- # allows you to pass any html attributes to the the html element
165
+ # allows you to pass any html attributes to the the html element
163
166
  # wrapping all the fields
164
-
167
+
165
168
  def persisted_records_for(association, form, options = {})
166
169
  records = options[:collection] || form.object.send(association)
170
+ # return if records.empty?
171
+
167
172
  options[:wrapper_html] ||= {}
168
- fields_default = { data: { abyme_target: 'fields' } }
169
-
173
+ fields_default = {data: {abyme_target: "fields"}}
174
+
170
175
  if options[:order].present?
171
176
  records = records.order(options[:order])
172
177
  # by calling the order method on the AR collection
@@ -174,8 +179,8 @@ module Abyme
174
179
  # so we have to get them back with the 2 lines below
175
180
  invalids = form.object.send(association).reject(&:persisted?)
176
181
  records = records.to_a.concat(invalids) if invalids.any?
177
- end
178
-
182
+ end
183
+
179
184
  content_tag(:div, options[:wrapper_html]) do
180
185
  form.fields_for(association, records) do |f|
181
186
  content_tag(:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
@@ -187,22 +192,25 @@ module Abyme
187
192
 
188
193
  # ADD & REMOVE ASSOCIATION
189
194
 
190
- # these helpers will call the #create_button method
195
+ # these helpers will call the #create_button method
191
196
  # to generate the buttons for add and remove associations
192
197
  # with the right action and a default content text for each button
193
-
198
+
194
199
  def add_associated_record(options = {}, &block)
195
- action = 'click->abyme#add_association'
196
- options[:content] ||= 'Add Association'
200
+ action = "click->abyme#add_association"
201
+ options[:content] ||= "Add Association"
197
202
  create_button(action, options, &block)
198
203
  end
199
-
204
+
200
205
  def remove_associated_record(options = {}, &block)
201
- action = 'click->abyme#remove_association'
202
- options[:content] ||= 'Remove Association'
206
+ action = "click->abyme#remove_association"
207
+ options[:content] ||= "Remove Association"
203
208
  create_button(action, options, &block)
204
209
  end
205
210
 
211
+ alias_method :add_association, :add_associated_record
212
+ alias_method :remove_association, :remove_associated_record
213
+
206
214
  private
207
215
 
208
216
  # CREATE_BUTTON
@@ -220,17 +228,17 @@ module Abyme
220
228
 
221
229
  # - html (Hash)
222
230
  # to pass any html attributes you want.
223
-
231
+
224
232
  def create_button(action, options, &block)
225
233
  options[:html] ||= {}
226
234
  options[:tag] ||= :button
227
-
228
- if block_given?
229
- content_tag(options[:tag], { data: { action: action } }.merge(options[:html])) do
235
+
236
+ if block
237
+ content_tag(options[:tag], {data: {action: action}}.merge(options[:html])) do
230
238
  capture(&block)
231
239
  end
232
240
  else
233
- content_tag(options[:tag], options[:content], { data: { action: action } }.merge(options[:html]))
241
+ content_tag(options[:tag], options[:content], {data: {action: action}}.merge(options[:html]))
234
242
  end
235
243
  end
236
244
 
@@ -241,7 +249,7 @@ module Abyme
241
249
 
242
250
  def basic_fields_markup(html, association = nil)
243
251
  if html && html[:class]
244
- html[:class] = "abyme--fields #{association.to_s.singularize}-fields #{html[:class]}"
252
+ html[:class] = "abyme--fields #{association.to_s.singularize}-fields #{html[:class]}"
245
253
  else
246
254
  html ||= {}
247
255
  html[:class] = "abyme--fields #{association.to_s.singularize}-fields"
@@ -269,13 +277,12 @@ module Abyme
269
277
 
270
278
  # RENDER PARTIAL
271
279
 
272
- # renders a partial based on the passed path, or will expect a partial to be found in the views/abyme directory.
280
+ # renders a partial based on the passed path, or will expect a partial to be found in the views/abyme directory.
273
281
 
274
282
  def render_association_partial(association, form, partial = nil, context = nil)
275
- partial_path = partial ||"abyme/#{association.to_s.singularize}_fields"
283
+ partial_path = partial || "abyme/#{association.to_s.singularize}_fields"
276
284
  context ||= self
277
285
  context.render(partial: partial_path, locals: {f: form})
278
286
  end
279
-
280
287
  end
281
- end
288
+ end
@@ -0,0 +1,16 @@
1
+ Description:
2
+ Injects the call to the `abyme_attributes` in the strong params definition of the targeted controller.
3
+ Also works with namespaced controllers.
4
+
5
+ Example:
6
+ Before :
7
+ def project_params
8
+ params.require(:project).permit(:title, :description)
9
+ end
10
+
11
+ rails generate abyme:controller Projects
12
+
13
+ After:
14
+ def project_params
15
+ params.require(:project).permit(abyme_attributes, :title, :description)
16
+ end