abstracted 0.0.6 → 0.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (224) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/Gemfile.lock +11 -8
  4. data/app/assets/javascripts/abstract_resources.js +29 -25
  5. data/app/assets/javascripts/crud.js.coffee +174 -29
  6. data/app/assets/javascripts/initializers.js.coffee +403 -58
  7. data/app/assets/javascripts/pagescrolling.js.coffee +92 -0
  8. data/app/assets/stylesheets/abstract_resources.css +5 -0
  9. data/app/assets/stylesheets/scaffold.css +4 -2
  10. data/app/assets/stylesheets/selectize.css.scss +0 -0
  11. data/app/controllers/abstract_resources_controller.rb +473 -134
  12. data/app/controllers/application_controller.rb +22 -7
  13. data/app/helpers/abstract_resources_helper.rb +515 -9
  14. data/app/helpers/fab_button_helper.rb +82 -0
  15. data/app/models/abstract_resource.rb +141 -89
  16. data/app/models/ancestry_abstract_resource.rb +27 -0
  17. data/app/models/concerns/roleable.rb +1 -1
  18. data/app/policies/abstract_resource_policy.rb +25 -0
  19. data/app/views/abstract_resources/_title.html.haml +2 -0
  20. data/app/views/abstract_resources/activate.html.haml +5 -0
  21. data/app/views/abstract_resources/attach.html.haml +6 -0
  22. data/app/views/abstract_resources/deactivate.html.haml +5 -0
  23. data/app/views/abstract_resources/defer.html.haml +5 -0
  24. data/app/views/abstract_resources/destroy.js.haml +2 -1
  25. data/app/views/abstract_resources/detach.html.haml +5 -0
  26. data/app/views/abstract_resources/edit.html.haml +4 -9
  27. data/app/views/abstract_resources/error.html.haml +2 -0
  28. data/app/views/abstract_resources/error.js.haml +2 -0
  29. data/app/views/abstract_resources/index.html.haml +27 -0
  30. data/app/views/abstract_resources/index.js.haml +2 -0
  31. data/app/views/abstract_resources/new.html.haml +4 -10
  32. data/app/views/abstract_resources/prefer.html.haml +5 -0
  33. data/app/views/abstract_resources/print.html.haml +2 -0
  34. data/app/views/abstract_resources/print.js.haml +2 -0
  35. data/app/views/abstract_resources/show.html.haml +5 -11
  36. data/app/views/layouts/_messages.html.haml +1 -0
  37. data/app/views/layouts/_navigation.html.haml +13 -0
  38. data/bower.json +34 -0
  39. data/config/initializers/exception.rb +19 -0
  40. data/config/initializers/pundit.rb +22 -0
  41. data/config/routes.rb +24 -0
  42. data/lib/abstracted.rb +3 -0
  43. data/lib/abstracted/version.rb +1 -1
  44. data/lib/abstracted_responder.rb +1 -0
  45. data/lib/exceptions.rb +61 -0
  46. data/vendor/assets/components/jquery/.bower.json +38 -0
  47. data/vendor/assets/components/jquery/MIT-LICENSE.txt +21 -0
  48. data/vendor/assets/components/jquery/bower.json +28 -0
  49. data/vendor/assets/components/jquery/dist/jquery.js +9210 -0
  50. data/vendor/assets/components/jquery/dist/jquery.min.js +5 -0
  51. data/vendor/assets/components/jquery/dist/jquery.min.map +1 -0
  52. data/vendor/assets/components/jquery/src/ajax.js +786 -0
  53. data/vendor/assets/components/jquery/src/ajax/jsonp.js +89 -0
  54. data/vendor/assets/components/jquery/src/ajax/load.js +75 -0
  55. data/vendor/assets/components/jquery/src/ajax/parseJSON.js +13 -0
  56. data/vendor/assets/components/jquery/src/ajax/parseXML.js +28 -0
  57. data/vendor/assets/components/jquery/src/ajax/script.js +64 -0
  58. data/vendor/assets/components/jquery/src/ajax/var/nonce.js +5 -0
  59. data/vendor/assets/components/jquery/src/ajax/var/rquery.js +3 -0
  60. data/vendor/assets/components/jquery/src/ajax/xhr.js +136 -0
  61. data/vendor/assets/components/jquery/src/attributes.js +11 -0
  62. data/vendor/assets/components/jquery/src/attributes/attr.js +141 -0
  63. data/vendor/assets/components/jquery/src/attributes/classes.js +158 -0
  64. data/vendor/assets/components/jquery/src/attributes/prop.js +94 -0
  65. data/vendor/assets/components/jquery/src/attributes/support.js +35 -0
  66. data/vendor/assets/components/jquery/src/attributes/val.js +161 -0
  67. data/vendor/assets/components/jquery/src/callbacks.js +205 -0
  68. data/vendor/assets/components/jquery/src/core.js +502 -0
  69. data/vendor/assets/components/jquery/src/core/access.js +60 -0
  70. data/vendor/assets/components/jquery/src/core/init.js +123 -0
  71. data/vendor/assets/components/jquery/src/core/parseHTML.js +39 -0
  72. data/vendor/assets/components/jquery/src/core/ready.js +97 -0
  73. data/vendor/assets/components/jquery/src/core/var/rsingleTag.js +4 -0
  74. data/vendor/assets/components/jquery/src/css.js +450 -0
  75. data/vendor/assets/components/jquery/src/css/addGetHookIf.js +22 -0
  76. data/vendor/assets/components/jquery/src/css/curCSS.js +57 -0
  77. data/vendor/assets/components/jquery/src/css/defaultDisplay.js +70 -0
  78. data/vendor/assets/components/jquery/src/css/hiddenVisibleSelectors.js +15 -0
  79. data/vendor/assets/components/jquery/src/css/support.js +96 -0
  80. data/vendor/assets/components/jquery/src/css/swap.js +28 -0
  81. data/vendor/assets/components/jquery/src/css/var/cssExpand.js +3 -0
  82. data/vendor/assets/components/jquery/src/css/var/getStyles.js +12 -0
  83. data/vendor/assets/components/jquery/src/css/var/isHidden.js +13 -0
  84. data/vendor/assets/components/jquery/src/css/var/rmargin.js +3 -0
  85. data/vendor/assets/components/jquery/src/css/var/rnumnonpx.js +5 -0
  86. data/vendor/assets/components/jquery/src/data.js +178 -0
  87. data/vendor/assets/components/jquery/src/data/Data.js +181 -0
  88. data/vendor/assets/components/jquery/src/data/accepts.js +20 -0
  89. data/vendor/assets/components/jquery/src/data/var/data_priv.js +5 -0
  90. data/vendor/assets/components/jquery/src/data/var/data_user.js +5 -0
  91. data/vendor/assets/components/jquery/src/deferred.js +149 -0
  92. data/vendor/assets/components/jquery/src/deprecated.js +13 -0
  93. data/vendor/assets/components/jquery/src/dimensions.js +50 -0
  94. data/vendor/assets/components/jquery/src/effects.js +648 -0
  95. data/vendor/assets/components/jquery/src/effects/Tween.js +114 -0
  96. data/vendor/assets/components/jquery/src/effects/animatedSelector.js +13 -0
  97. data/vendor/assets/components/jquery/src/event.js +868 -0
  98. data/vendor/assets/components/jquery/src/event/ajax.js +13 -0
  99. data/vendor/assets/components/jquery/src/event/alias.js +39 -0
  100. data/vendor/assets/components/jquery/src/event/support.js +9 -0
  101. data/vendor/assets/components/jquery/src/exports/amd.js +24 -0
  102. data/vendor/assets/components/jquery/src/exports/global.js +32 -0
  103. data/vendor/assets/components/jquery/src/intro.js +44 -0
  104. data/vendor/assets/components/jquery/src/jquery.js +37 -0
  105. data/vendor/assets/components/jquery/src/manipulation.js +580 -0
  106. data/vendor/assets/components/jquery/src/manipulation/_evalUrl.js +18 -0
  107. data/vendor/assets/components/jquery/src/manipulation/support.js +32 -0
  108. data/vendor/assets/components/jquery/src/manipulation/var/rcheckableType.js +3 -0
  109. data/vendor/assets/components/jquery/src/offset.js +207 -0
  110. data/vendor/assets/components/jquery/src/outro.js +1 -0
  111. data/vendor/assets/components/jquery/src/queue.js +142 -0
  112. data/vendor/assets/components/jquery/src/queue/delay.js +22 -0
  113. data/vendor/assets/components/jquery/src/selector-native.js +172 -0
  114. data/vendor/assets/components/jquery/src/selector-sizzle.js +14 -0
  115. data/vendor/assets/components/jquery/src/selector.js +1 -0
  116. data/vendor/assets/components/jquery/src/serialize.js +111 -0
  117. data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.js +2067 -0
  118. data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.min.js +3 -0
  119. data/vendor/assets/components/jquery/src/sizzle/dist/sizzle.min.map +1 -0
  120. data/vendor/assets/components/jquery/src/traversing.js +199 -0
  121. data/vendor/assets/components/jquery/src/traversing/findFilter.js +100 -0
  122. data/vendor/assets/components/jquery/src/traversing/var/rneedsContext.js +6 -0
  123. data/vendor/assets/components/jquery/src/var/arr.js +3 -0
  124. data/vendor/assets/components/jquery/src/var/class2type.js +4 -0
  125. data/vendor/assets/components/jquery/src/var/concat.js +5 -0
  126. data/vendor/assets/components/jquery/src/var/hasOwn.js +5 -0
  127. data/vendor/assets/components/jquery/src/var/indexOf.js +5 -0
  128. data/vendor/assets/components/jquery/src/var/pnum.js +3 -0
  129. data/vendor/assets/components/jquery/src/var/push.js +5 -0
  130. data/vendor/assets/components/jquery/src/var/rnotwhite.js +3 -0
  131. data/vendor/assets/components/jquery/src/var/slice.js +5 -0
  132. data/vendor/assets/components/jquery/src/var/strundefined.js +3 -0
  133. data/vendor/assets/components/jquery/src/var/support.js +4 -0
  134. data/vendor/assets/components/jquery/src/var/toString.js +5 -0
  135. data/vendor/assets/components/jquery/src/wrap.js +79 -0
  136. data/vendor/assets/components/microplugin/.bower.json +38 -0
  137. data/vendor/assets/components/microplugin/.gitignore +2 -0
  138. data/vendor/assets/components/microplugin/.npmignore +3 -0
  139. data/vendor/assets/components/microplugin/README.md +115 -0
  140. data/vendor/assets/components/microplugin/bower.json +22 -0
  141. data/vendor/assets/components/microplugin/package.json +15 -0
  142. data/vendor/assets/components/microplugin/src/microplugin.js +135 -0
  143. data/vendor/assets/components/selectize/.bower.json +60 -0
  144. data/vendor/assets/components/selectize/.gitignore +5 -0
  145. data/vendor/assets/components/selectize/LICENSE +202 -0
  146. data/vendor/assets/components/selectize/README.md +105 -0
  147. data/vendor/assets/components/selectize/bower.json +40 -0
  148. data/vendor/assets/components/selectize/dist/css/selectize.bootstrap2.css +487 -0
  149. data/vendor/assets/components/selectize/dist/css/selectize.bootstrap3.css +401 -0
  150. data/vendor/assets/components/selectize/dist/css/selectize.css +317 -0
  151. data/vendor/assets/components/selectize/dist/css/selectize.default.css +387 -0
  152. data/vendor/assets/components/selectize/dist/css/selectize.legacy.css +364 -0
  153. data/vendor/assets/components/selectize/dist/js/selectize.js +3058 -0
  154. data/vendor/assets/components/selectize/dist/js/selectize.min.js +3 -0
  155. data/vendor/assets/components/selectize/dist/js/standalone/selectize.js +3667 -0
  156. data/vendor/assets/components/selectize/dist/js/standalone/selectize.min.js +3 -0
  157. data/vendor/assets/components/selectize/dist/less/plugins/drag_drop.less +16 -0
  158. data/vendor/assets/components/selectize/dist/less/plugins/dropdown_header.less +20 -0
  159. data/vendor/assets/components/selectize/dist/less/plugins/optgroup_columns.less +17 -0
  160. data/vendor/assets/components/selectize/dist/less/plugins/remove_button.less +37 -0
  161. data/vendor/assets/components/selectize/dist/less/selectize.bootstrap2.less +161 -0
  162. data/vendor/assets/components/selectize/dist/less/selectize.bootstrap3.less +150 -0
  163. data/vendor/assets/components/selectize/dist/less/selectize.default.less +84 -0
  164. data/vendor/assets/components/selectize/dist/less/selectize.legacy.less +75 -0
  165. data/vendor/assets/components/selectize/dist/less/selectize.less +295 -0
  166. data/vendor/assets/components/selectize/karma.conf.js +107 -0
  167. data/vendor/assets/components/sifter/.bower.json +44 -0
  168. data/vendor/assets/components/sifter/.gitignore +3 -0
  169. data/vendor/assets/components/sifter/.npmignore +4 -0
  170. data/vendor/assets/components/sifter/README.md +149 -0
  171. data/vendor/assets/components/sifter/bower.json +23 -0
  172. data/vendor/assets/components/sifter/package.json +48 -0
  173. data/vendor/assets/components/sifter/sifter.js +471 -0
  174. data/vendor/assets/components/sifter/sifter.min.js +2 -0
  175. data/vendor/assets/components/sweetalert/.bower.json +36 -0
  176. data/vendor/assets/components/sweetalert/.editorconfig +11 -0
  177. data/vendor/assets/components/sweetalert/.gitignore +5 -0
  178. data/vendor/assets/components/sweetalert/.jshintrc +11 -0
  179. data/vendor/assets/components/sweetalert/.travis.yml +6 -0
  180. data/vendor/assets/components/sweetalert/LICENSE +22 -0
  181. data/vendor/assets/components/sweetalert/README.md +157 -0
  182. data/vendor/assets/components/sweetalert/bower.json +25 -0
  183. data/vendor/assets/components/sweetalert/dev/gulpfile-wrap-template.js +18 -0
  184. data/vendor/assets/components/sweetalert/dev/ie9.css +23 -0
  185. data/vendor/assets/components/sweetalert/dev/loader-animation.css +209 -0
  186. data/vendor/assets/components/sweetalert/dev/modules/default-params.js +26 -0
  187. data/vendor/assets/components/sweetalert/dev/modules/handle-click.js +128 -0
  188. data/vendor/assets/components/sweetalert/dev/modules/handle-dom.js +161 -0
  189. data/vendor/assets/components/sweetalert/dev/modules/handle-key.js +73 -0
  190. data/vendor/assets/components/sweetalert/dev/modules/handle-swal-dom.js +148 -0
  191. data/vendor/assets/components/sweetalert/dev/modules/injected-html.js +69 -0
  192. data/vendor/assets/components/sweetalert/dev/modules/set-params.js +221 -0
  193. data/vendor/assets/components/sweetalert/dev/modules/utils.js +71 -0
  194. data/vendor/assets/components/sweetalert/dev/sweetalert.es6.js +311 -0
  195. data/vendor/assets/components/sweetalert/dev/sweetalert.scss +648 -0
  196. data/vendor/assets/components/sweetalert/dist/sweetalert-dev.js +1281 -0
  197. data/vendor/assets/components/sweetalert/dist/sweetalert.css +932 -0
  198. data/vendor/assets/components/sweetalert/dist/sweetalert.min.js +1 -0
  199. data/vendor/assets/components/sweetalert/example/example.css +442 -0
  200. data/vendor/assets/components/sweetalert/example/example.scss +580 -0
  201. data/vendor/assets/components/sweetalert/example/images/logo_big.png +0 -0
  202. data/vendor/assets/components/sweetalert/example/images/logo_big@2x.png +0 -0
  203. data/vendor/assets/components/sweetalert/example/images/logo_small.png +0 -0
  204. data/vendor/assets/components/sweetalert/example/images/logo_small@2x.png +0 -0
  205. data/vendor/assets/components/sweetalert/example/images/te-logo-small.svg +12 -0
  206. data/vendor/assets/components/sweetalert/example/images/thumbs-up.jpg +0 -0
  207. data/vendor/assets/components/sweetalert/example/images/vs_icon.png +0 -0
  208. data/vendor/assets/components/sweetalert/example/images/vs_icon@2x.png +0 -0
  209. data/vendor/assets/components/sweetalert/gulpfile.js +97 -0
  210. data/vendor/assets/components/sweetalert/index.html +584 -0
  211. data/vendor/assets/components/sweetalert/package.json +45 -0
  212. data/vendor/assets/components/sweetalert/sweetalert.gif +0 -0
  213. data/vendor/assets/components/sweetalert/test/index.html +20 -0
  214. data/vendor/assets/components/sweetalert/test/tests.js +143 -0
  215. data/vendor/assets/components/sweetalert/themes/facebook/facebook.css +111 -0
  216. data/vendor/assets/components/sweetalert/themes/facebook/facebook.scss +146 -0
  217. data/vendor/assets/components/sweetalert/themes/google/google.css +115 -0
  218. data/vendor/assets/components/sweetalert/themes/google/google.scss +148 -0
  219. data/vendor/assets/components/sweetalert/themes/twitter/twitter.css +140 -0
  220. data/vendor/assets/components/sweetalert/themes/twitter/twitter.scss +177 -0
  221. metadata +214 -5
  222. data/abstracted-0.0.4.gem +0 -0
  223. data/abstracted.gemspec +0 -51
  224. data/app/views/abstract_resources/index.html.erb +0 -1
@@ -1,5 +1,8 @@
1
1
  require 'pundit'
2
+ require 'exceptions'
3
+ #
2
4
  class ApplicationController < ActionController::Base
5
+
3
6
  # Prevent CSRF attacks by raising an exception.
4
7
  # For APIs, you may want to use :null_session instead.
5
8
  protect_from_forgery with: :exception
@@ -9,14 +12,26 @@ class ApplicationController < ActionController::Base
9
12
  #
10
13
  include Pundit
11
14
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
12
-
13
- private
14
-
15
+ #
16
+ include Exceptions
17
+ # rescue_from Exception, with: :handle_all_errors
18
+ # rescue_from Exceptions::WhatAnError do |e|
19
+ # flash[:error] = e.message
20
+ # redirect_to root_url
21
+ # end
22
+ #
23
+ # private
24
+ #
15
25
  def user_not_authorized(exception)
16
- policy_name = exception.policy.class.to_s.underscore
17
-
18
- flash[:error] = t '.not_authorized' #t "#{policy_name}.#{exception.query}", scope: "pundit", default: :default
19
- redirect_to(request.referrer || "/pages/error", flash: flash)
26
+ # policy_name = exception.policy.class.to_s.underscore
27
+ #
28
+ flash[:error] = t '.not_authorized'
29
+ redirect_to(request.referrer || "/pages/error")
20
30
  end
21
31
 
32
+ # def handle_all_errors(exception)
33
+ # Rails.logger.error "OXEN -------------- %s" % exception.message
34
+ # redirect_to (request.referrer || "/pages/error")
35
+ # end
36
+
22
37
  end
@@ -2,25 +2,531 @@ module AbstractResourcesHelper
2
2
 
3
3
  #
4
4
  # URL Helpers
5
- def edit_resource_url(options={})
6
- options[:action] ||= :edit
7
- options[:controller] ||= @resource_class.table_name
8
- url_for options
5
+ #
6
+ # parent
7
+
8
+ #
9
+ # parent returns the parent entity
10
+ def parent
11
+ parent? ? @parent : raise("parent will return nil - which probably was not anticipated!")
9
12
  end
10
13
 
11
- def resources_url resources=@resources, options={}
12
- options[:controller] ||= @resource_class.table_name
13
- url_for options
14
+ #
15
+ # parent_class returns the class of the parent
16
+ def parent_class
17
+ parent? ? @parent.class : raise("parent_class will return Object - which probably was not anticipated!")
14
18
  end
15
19
 
20
+ #
21
+ # parent_name returns the name of the resource - that would be the empty string on nil's
22
+ def parent_name
23
+ parent? ? parent_class.to_s.pluralize.underscore : raise("parent_name will return '' - which probably was not anticipated!")
24
+ end
25
+
26
+ #
27
+ # parent_url returns the parent url - /employees/1 in the /employees/1/events
28
+ def parent_url
29
+ parent? ? ( "/" + parent_class.to_s.pluralize.underscore + "/%s" % @parent.id) : "no_route_to_parent"
30
+ end
31
+
32
+ #
33
+ # parent? will tell if a parent exists
34
+ def parent?
35
+ return true if (params[:parent] && params[:parent_id] && @parent = params[:parent].classify.constantize.find(params[:parent_id]))
36
+ !(%w{NilClass TrueClass FalseClass}.include? @parent.class.to_s)
37
+ end
38
+
39
+ #
40
+ # resource
41
+
42
+ #
43
+ # did we manage to set a resource at all?
44
+ def resource?
45
+ !(%w{NilClass TrueClass FalseClass}.include? @resource.class.to_s)
46
+ end
16
47
 
17
48
  #
18
- # Resource Helpers
49
+ # resource returns the current entity
19
50
  def resource
20
- @resource
51
+ resource? ? @resource : nil
21
52
  end
22
53
 
54
+ #
55
+ # return the name of the resource - that would be the empty string on nil's
56
+ #
57
+ def resource_name
58
+ resource? ? resource.resource_name : resource_class.to_s.underscore.pluralize
59
+ end
60
+
61
+ #
62
+ # resource_class is the class which the resource represent
63
+ def resource_class
64
+ return @resource_class if resource?
65
+ @resource_class = params[:controller].singularize.classify.constantize
66
+ rescue
67
+ raise("resource_class will return Object - which probably was not anticipated!")
68
+ end
69
+
70
+ #
71
+ # resource_url returns the current entity's url
72
+ def resource_url options={}
73
+ opt = {}
74
+ opt[:id] = options.id if options.class.ancestors.include? ActiveRecord::Base
75
+ opt.merge!(options) if options.class==Hash
76
+ opt[:action] ||= 'show'
77
+ opt[:id] ||= resource.id
78
+ opt[:controller] ||= resource_class.table_name
79
+ url_for opt
80
+ end
81
+
82
+ #
83
+ # new_resource_url returns a new entity
84
+ def new_resource_url(options={})
85
+ opt = {}
86
+ opt.merge!(options) if options.class==Hash
87
+ opt[:controller] ||= resource_class.table_name
88
+ opt[:action] = :new
89
+ url_for opt
90
+ end
91
+
92
+ #
93
+ # edit_resource_url returns the current entity's url for editing
94
+ def edit_resource_url(options={})
95
+ opt = {}
96
+ opt[:id] = options.id if options.class.ancestors.include? ActiveRecord::Base
97
+ opt.merge!(options) if options.class==Hash
98
+ opt[:action] = :edit
99
+ resource_url opt
100
+ # opt[:id] ||= @resource.id
101
+ # opt[:controller] ||= @resource_class.table_name
102
+ # url_for opt
103
+ end
104
+
105
+ #
106
+ # resources
107
+
108
+ #
109
+ # returns the current collection of entities
23
110
  def resources
24
111
  @resources
25
112
  end
113
+
114
+ #
115
+ # returns the resource_class pluralized - eg: stock_item => 'stock_items'
116
+ def resources_name
117
+ resource_name.pluralize
118
+ end
119
+
120
+ #
121
+ # resources_url returns the current collection of entities' url
122
+ def resources_url resources=@resources, options={}
123
+ #
124
+ # lnk = parent? ? (parent_url + "/#{resource_name}") : "/#{resource_name}"
125
+ # lnk += options.empty? ? "" : "?" + options.collect{ |k,v| "#{k}=#{v}" }.join("&")
126
+ opt = {}
127
+ opt.merge!(options) if options.class==Hash
128
+ opt[:controller] ||= resource_class.table_name
129
+ opt[:action] ||= :index
130
+ url_for opt
131
+ end
132
+
133
+ #
134
+ # attach and detach resources
135
+ def attach_url parent, resource
136
+ '%s/%s/%s/attach' % [url_for( parent), resource.class.to_s.downcase.pluralize, resource.id]
137
+ end
138
+
139
+ def detach_url parent, resource
140
+ '%s/%s/%s/detach' % [url_for( parent), resource.class.to_s.downcase.pluralize, resource.id]
141
+ end
142
+
143
+ #
144
+ # activate and deactivate resources
145
+ def activate_url resource
146
+ '%s/activate' % url_for(resource)
147
+ end
148
+
149
+ def deactivate_url resource
150
+ '%s/deactivate' % url_for( resource)
151
+ end
152
+
153
+ #
154
+ # prefer and defer resources - like printers
155
+ def prefer_url parent, resource
156
+ '%s/%s/%s/prefer' % [url_for( parent), resource.class.to_s.downcase.pluralize, resource.id]
157
+ end
158
+
159
+ def defer_url parent, resource
160
+ '%s/%s/%s/defer' % [url_for( parent), resource.class.to_s.downcase.pluralize, "{{id}}"]
161
+ end
162
+
163
+
164
+
165
+ #
166
+ # List Helpers
167
+ def set_attached_class children, child
168
+ return "" unless parent?
169
+ return "" if children.nil?
170
+ return "detached" if children.empty?
171
+ children.include?( child) ? "" : "detached"
172
+ end
173
+
174
+
175
+ def show_resource_active item, linkable=true
176
+ unless linkable
177
+ if item.active
178
+ content_tag :i, nil, class: "mdi-action-done small"
179
+ else
180
+ content_tag :i, nil, class: "mdi-av-pause small"
181
+ end
182
+ else
183
+ if item.active
184
+ link_to deactivate_url(item), class: 'activated green-text', title: t("deactivate_#{item.resource_name}") do
185
+ content_tag :i, 'done', class: 'material-icons'
186
+ end
187
+ else
188
+ link_to activate_url(item), class: 'deactivated red-text', title: t("activate_#{item.resource_name}") do
189
+ content_tag :i, 'pause', class: 'material-icons'
190
+ end
191
+ end
192
+ end
193
+ end
194
+
195
+
196
+ # return a link to delete a row
197
+ def show_resource_delete_icon item, url, lbl='name'
198
+ link_to item, class: 'delete_link', data: { url: url, name: "#{item.send lbl }", id: "#{item.id}", remove: "#tr-#{item.id}" } do
199
+ content_tag :i, nil, class: 'mdi-action-delete small', title: "#{t('.delete')}"
200
+ end
201
+ # = link_to account, class: 'delete_link', data: { url: '/admin/accounts', name: "#{account.name}", id: "#{account.id}", remove: "#tr-#{account.id}" } do
202
+ # %i.mdi-action-delete.small{ title: "#{t('.delete')}"}
203
+
204
+ end
205
+ def build_delete_link resource
206
+ link_to(content_tag(:i,nil,class:'icon-trash'), resource, class: 'btn btn-mini delete_item', :"data-no-turbolink"=>true)
207
+ end
208
+
209
+ # return a link to print a row
210
+ # url: ''
211
+ # list: true|false
212
+ # template: 'what_list.html.haml',
213
+ # button: true|false either a button or a link
214
+ # text: 'what to write on the link if at all'
215
+ # classes: 'btn btn-mini print_item print_items ...' jquery hooks print_item will print a specific record, print_items will print a list of records
216
+ def build_print_link resource, options={}
217
+
218
+ list = options.include?(:list) ? options.delete(:list) : true
219
+ button = options.include?(:button) ? options[:button] : false
220
+ print_options = options.include?(:print_options) ? "?" + options.delete(:print_options) : ""
221
+ url = options.include?(:url) ? options.delete(:url) : ( !list ? url_for( resource) + "/print" + print_options : url_for(resource_class.to_s.underscore.pluralize) + "/print?print_list=true&#{add_print_params}" )
222
+ classes = options.include?(:classes) ? options.delete(:classes) : ( button ? 'btn btn-mini' : '')
223
+ classes += ( list ? ' print_items' : ' print_item')
224
+ return link_to( build_button_tag(options), url, class: classes) if options.include?(:button)
225
+ link_to t(resource_class.to_s.underscore.pluralize.to_sym), url, options.merge( classes: classes)
226
+
227
+ end
228
+
229
+
230
+ def build_button_tag options={}
231
+ text = options.include?(:text) ? options.delete(:text) : nil
232
+ button = options.include?(:button) ? options.delete(:button) : false
233
+ if text
234
+ content_tag( :span) do
235
+ text
236
+ end << content_tag(:i,nil, class: button )
237
+ else
238
+ content_tag(:i,nil, class: button )
239
+ end
240
+ end
241
+
242
+
243
+ def display_base_errors resource
244
+ return '' if (resource.errors.empty?) or (resource.errors[:base].empty?)
245
+ messages = resource.errors[:base].map { |msg| content_tag(:p, msg) }.join
246
+ html = <<-HTML
247
+ <div class="alert alert-error alert-block">
248
+ <button type="button" class="close" data-dismiss="alert">&#215;</button>
249
+ #{messages}
250
+ </div>
251
+ HTML
252
+ html.html_safe
253
+ end
254
+
255
+ # set_flashes will build the necessary flash
256
+ #
257
+ # quite stolen from bootstrap_flash
258
+ # - but reengineered to avoid empty arrays
259
+ def set_flashes
260
+ flash_messages = []
261
+ flash.each do |type, message|
262
+ next if message.blank?
263
+ tp = 'danger'
264
+ tp = 'success' if [ "notice", "info" ].include? type.to_s
265
+ message = show_flash(flash, type) if flash.kind_of? Array
266
+ unless message.blank?
267
+ flash[type]=nil
268
+ text = content_tag(:div, build_close_button(tp) + message.html_safe, class: "alert fade in alert-#{tp}")
269
+ flash_messages << text
270
+ end
271
+ end
272
+ flash.clear
273
+ flash_messages.join("").html_safe
274
+ rescue => e
275
+ Rails.logger.error "setting the flashes - set_flashes - failed with #{e.message}"
276
+ ""
277
+ end
278
+
279
+ def divide_text(str, max_chars)
280
+ max_chars.map do |n|
281
+ str.lstrip!
282
+ s = str[/^.{,#{n}}(?=\b)/] || ''
283
+ str = str[s.size..-1]
284
+ s
285
+ end
286
+ end
287
+
288
+ def set_toasts
289
+ toast_messages = []
290
+ flash.each do |type, message|
291
+ message_arr = message.size < 61 ? [message] : divide_text( message, [60,60,60,60] )
292
+ message_arr.each do |message|
293
+ case type
294
+ when 'error','danger','alert'; toast_messages << "Materialize.toast('#{message}',8000, 'red darken-4');"
295
+ when 'info'; toast_messages << "Materialize.toast('#{message}',2000, 'green lighten-3');"
296
+ when 'success','notice'; toast_messages << "Materialize.toast('#{message}',2000, 'blue lighten-4');"
297
+ end
298
+ end
299
+ end
300
+ flash.clear
301
+ toast_messages.join("").html_safe
302
+ rescue => e
303
+ Rails.logger.error "setting the toasts - set_toasts - failed with #{e.message}"
304
+ ""
305
+ end
306
+
307
+ def build_close_button(type)
308
+ "<a href='#!' class='#{type} close-notice btn-floating btn-small waves-effect waves-light' aria-hidden='true' type='button' data-dismiss='alert'><i class='mdi-navigation-close'></i></a>".html_safe
309
+ # link_to "#", type: "button", "aria-hidden" => "true", class: "#{type} close-notice btn-floating btn-small waves-effect waves-light", "data-dismiss" => "alert" do
310
+ # "<i class='x mdi-navigation-close'/>".html_safe
311
+ # end
312
+ end
313
+
314
+ # = show_flash flash, key
315
+ # - flash[key]=nil
316
+ #
317
+ def show_flash flash, key
318
+ msg = flash[key].kind_of?( Array) ? flash[key].flatten.compact : [ flash[key] ].compact
319
+ msg.collect!{ |m| (content_tag(:div, id: key) do m end) unless m.blank? }
320
+ raw(msg.join)
321
+ end
322
+
323
+ # return a HTML fragtment containing the menu_items referring to this one
324
+ # items is a Hash of three elements, lbl, url and possible any children
325
+ # { item: { lbl: 'menu', url: 'menu', options: {} } }
326
+ # { item: { lbl: 'menu', url: { item: {}, item: {} }} }
327
+ def menu_item(items, level=0 )
328
+ return "" if items.nil?
329
+ str = []
330
+ level+=1
331
+ items.each do |k,item|
332
+ options = item[:options] || {}
333
+ unless item[:url].class==String
334
+ if level>1
335
+ str << "<li class='dropdown-submenu'>%s<ul class='dropdown-menu'>%s</ul></li>" % [ link_to( "#{item[:lbl]}", "#", options.merge(class: "dropdown-toggle", :"data-toggle"=>"dropdown")), menu_item(item[:url],level)]
336
+ else
337
+ str << "<li class='dropdown'>%s<ul class='dropdown-menu'>%s</ul></li>" % [ link_to( "#{item[:lbl]}<b class='caret'></b>".html_safe, "#", options.merge(class: "dropdown-toggle", :"data-toggle"=>"dropdown")), menu_item(item[:url],level)]
338
+ end
339
+ else
340
+ str << "<li>%s</li>" % link_to( item[:lbl], item[:url], options )
341
+ end
342
+ end
343
+ str.join( "")
344
+ end
345
+
346
+
347
+ # return a link - either to activate_resource_path or passify_resource_path
348
+ # used to turn on/off any resource
349
+ def build_active_link resource
350
+ lbl = resource.active ? (content_tag(:i, active_popover(true)) do end) : (content_tag(:i, active_popover(false)) do end)
351
+ link_to lbl, build_active_link_url(resource), class: "btn btn-mini active_button", remote: true, :"data-no-turbolink" => true, id: "%s_%i_actpas" % [resource.class.to_s, resource.id]
352
+ end
353
+
354
+ def active_msg active
355
+ active ? "Er aktiv nu! Tryk på denne knap for at gøre denne post passiv!" : "Er passiv nu! Tryk på denne knap for at gøre denne post aktiv!"
356
+ end
357
+
358
+ def active_popover active
359
+ if active
360
+ {
361
+ class: "icon-minus",
362
+ rel: "popover",
363
+ :"data-html" => true,
364
+ :"data-trigger" => "hover",
365
+ :"data-title" => "Posten er aktiv!",
366
+ :"data-content" => "Tryk på denne knap for at gøre denne post passiv!</br/>Når poster anvendes i andre sammenhænge vil oxenServer kun tage hensyn til aktive poster!",
367
+ }
368
+ else
369
+ {
370
+ class: "icon-plus",
371
+ rel: "popover",
372
+ :"data-html" => true,
373
+ :"data-trigger" => "hover",
374
+ :"data-title" => "Posten er passiv!",
375
+ :"data-content" => "Tryk på denne knap for at gøre denne post aktiv!</br/>Når poster anvendes i andre sammenhænge vil oxenServer kun tage hensyn til aktive poster!",
376
+ }
377
+ end
378
+ end
379
+
380
+
381
+ # return the url for the activate_link only
382
+ def build_active_link_url resource
383
+ r_url = url_for( resource)
384
+ resource.active ? r_url + "/passify" : r_url + "/activate"
385
+ end
386
+
387
+ # return a link - either to attach_resource_path or detach_resource_path
388
+ # used to attach or detach a resource to its parent
389
+ def build_attach_link children, child
390
+ attached = children.nil? ? false : children.include?( child)
391
+ lbl = attached ? t(:detach) : t(:attach)
392
+ link_to lbl, build_attach_link_url(child,attached), class: "btn btn-mini", remote: true, :"data-no-turbolink" => true, id: "%s_%i_attdet" % [child.class.to_s, child.id]
393
+ end
394
+
395
+ # return the url for the attach_detach_link only
396
+ def build_attach_link_url child, attached
397
+ path = request.path.match( /detach|attach/) ? request.path.split("/")[0..3].join("/") : request.path
398
+ r_url = path + "/%i" % child.id
399
+ attached ? r_url + "/detach" : r_url + "/attach"
400
+ end
401
+
402
+ # return a link to a new entity - possibly add route to parent
403
+ def build_add_link lbl, cls
404
+ r_url = cls.to_s.singularize.underscore
405
+ r_url = parent? ? parent.class.to_s.singularize.underscore + "_" + r_url : r_url
406
+ link_to( lbl, eval("new_%s_path" % r_url), class: 'btn btn-success')
407
+ end
408
+
409
+ # return a link - either to prefer_resource_path or defer_resource_path
410
+ # used to prefer or defer a resource by its parent
411
+ def build_prefer_link children, child
412
+ preferred = (children.include?( child) && child.preferred?( parent))
413
+ return oxt(:preferred) if preferred
414
+ link_to oxt(:prefer), build_prefer_link_url(child,preferred), class: "btn btn-mini", remote: true, :"data-no-turbolink" => true, id: "%s_%i_predet" % [child.class.to_s, child.id]
415
+ end
416
+
417
+ # return the url for the prefer_defer_link only
418
+ def build_prefer_link_url child, preferred
419
+ path = request.path.match( /defer|prefer/) ? request.path.split("/")[0..3].join("/") : request.path
420
+ r_url = path + "/%i" % child.id
421
+ preferred ? r_url + "/defer" : r_url + "/prefer"
422
+ end
423
+
424
+
425
+ # return a link to print a row
426
+ # url: ''
427
+ # list: true|false
428
+ # template: 'what_list.html.haml',
429
+ # button: true|false either a button or a link
430
+ # text: 'what to write on the link if at all'
431
+ # classes: 'btn btn-mini print_item print_items ...' jquery hooks print_item will print a specific record, print_items will print a list of records
432
+ # def build_print_link resource, options={}
433
+ #
434
+ # list = options.include?(:list) ? options.delete(:list) : true
435
+ # button = options.include?(:button) ? options[:button] : false
436
+ # print_options = options.include?(:print_options) ? "?" + options.delete(:print_options) : ""
437
+ # url = options.include?(:url) ? options.delete(:url) : ( !list ? url_for( resource) + "/print" + print_options : url_for(resource_class.to_s.underscore.pluralize) + "/print?print_list=true&#{add_print_params}" )
438
+ # classes = options.include?(:classes) ? options.delete(:classes) : ( button ? 'btn btn-mini' : '')
439
+ # classes += ( list ? ' print_items' : ' print_item')
440
+ # return link_to( build_button_tag(options), url, class: classes) if options.include?(:button)
441
+ # link_to oxt(resource_class.to_s.underscore.pluralize.to_sym), url, options.merge( classes: classes)
442
+ #
443
+ # end
444
+
445
+ # def build_button_tag options={}
446
+ # text = options.include?(:text) ? options.delete(:text) : nil
447
+ # button = options.include?(:button) ? options.delete(:button) : false
448
+ # if text
449
+ # content_tag( :span) do
450
+ # text
451
+ # end << content_tag(:i,nil, class: button )
452
+ # else
453
+ # content_tag(:i,nil, class: button )
454
+ # end
455
+ # end
456
+
457
+ def add_print_params
458
+ args = []
459
+ params.each do |k,v|
460
+ args << "#{k}=#{v}" unless %w{ ids action controller}.include? k
461
+ end
462
+ args.join("&")
463
+ end
464
+
465
+ # def add_from_to_params
466
+ # return "" if params[:from].blank?
467
+ # "&from=#{params[:from]}&until=#{params[:until]}"
468
+ # end
469
+
470
+ def build_toggle_white_collar_url
471
+ if params[:blue_collar].nil?
472
+ link_to oxt(:kun_timelønnede), collection_url(blue_collar: true), class: "btn btn-info"
473
+ else
474
+ link_to oxt(:alle_medarbejdere), collection_url, class: "btn btn-info"
475
+ end
476
+ end
477
+
478
+
479
+ # return a link to print a label for a row
480
+ # url: ''
481
+ # list: true|false
482
+ # template: 'what_list.html.haml',
483
+ # button: true|false either a button or a link
484
+ # classes: 'btn btn-mini print_item print_items ...' jquery hooks print_item will print a specific record, print_items will print a list of records
485
+ def build_label_link resource, options={}
486
+
487
+ list = options.delete(:list) || false
488
+ button = options.delete(:button) || true
489
+ url = options.delete(:url) || ( !list ? url_for( resource) + "/label" : url_for(resource_class.to_s.underscore.pluralize) + "/label?print_list=true&#{add_print_params}" )
490
+ classes = options.delete(:classes) || ( button ? 'btn btn-mini' : '')
491
+
492
+ return link_to(content_tag(:i,nil,class:'icon-tag'), url, class: classes, target: '_new') if button
493
+ link_to oxt(resource_class.to_s.underscore.pluralize.to_sym), url, options.merge( class: classes, target: '_new')
494
+
495
+ end
496
+
497
+ #
498
+ # set title of breadcrumb
499
+ #
500
+ def breadcrumb_title( str )
501
+ str.index( 'title="translation missing' ) ? str : str.mb_chars.upcase
502
+ rescue
503
+ str
504
+ end
505
+
506
+ #
507
+ # show_page_title
508
+ # does a resource_class and action based translation of title on a page
509
+ #
510
+ def show_page_title
511
+ t '%s.%s.title' % [ resource_class.table_name, params[:action] ]
512
+ end
513
+
514
+ #
515
+ # show_lbl is used on every show.html.haml to build the label
516
+ #
517
+ def show_lbl key
518
+ content_tag :b, "#{key}: "
519
+ end
520
+
521
+ #
522
+ # on imprint_certificate.html.haml
523
+ def show_product_logo product_logo
524
+ case product_logo
525
+ when /.jpg/, /.png/
526
+ tag(:img, { class: "product_logo", src: "#{Rails.root}/app/assets/images/#{product_logo}" }, false)
527
+ else
528
+ product_logo
529
+ end
530
+ end
531
+
26
532
  end