abstracted 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27795b5e4072282e297becda9681b2485fadc339
4
- data.tar.gz: a0b78bef1eb0150fa3ed0fcbd7ff1908476a8641
3
+ metadata.gz: b3eb85a893fc241b14df9db62dc8e5f7e6bfb070
4
+ data.tar.gz: a441bb37f0703fd705f4fb369ef97d008fce9713
5
5
  SHA512:
6
- metadata.gz: 6788211f7da0a881926438a8d5e3049d62697b3b5114b866ca8c43e52e141397e93a96c2b8880f1f926a3dec5d3a0fee96c63d7bd637084746f1f271ca5a7cf3
7
- data.tar.gz: c46d3691914e586368cb3b329b2af7dbf4b874dcfed79c7efe0ef69b5fecf7d4e8c2ea659ab97bfeca062a360e770998bf047f8dc3e02304d2931de8265655a5
6
+ metadata.gz: 550b2dcd201453b90e5bcacdc148f8fc77aeda8db01d249116c81cbb914e021e9409111c51c4305b475bc42e716bb26ab73bd79303405771f4d23fe77f65a09b
7
+ data.tar.gz: 1f66fc01ffda843425e566df943be521e704eca8d69fe7a0c47d0ea77e5fe98c68ca236f3611eb4ac2e3cbc9ebf3367b2b3e0247e72923032715782ae170be81
@@ -1,4 +1,7 @@
1
1
  require "abstracted_responder"
2
+ require "print_control"
3
+ require "resource_control"
4
+ require "parent_control"
2
5
 
3
6
  class AbstractResourcesController < ApplicationController
4
7
  self.responder = ::AbstractedResponder
@@ -0,0 +1,127 @@
1
+ require 'active_support/concern'
2
+
3
+ module ParentControl
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_filter :set_parents, only: [ :new, :edit, :show ]
8
+ end
9
+
10
+ def parent
11
+ @parent ||= find_parent
12
+ end
13
+
14
+
15
+ def parent_class
16
+ @parent_class ||= @parent.class
17
+ end
18
+
19
+ def parent_class= val
20
+ @parent_class = val
21
+ end
22
+
23
+ def parent?
24
+ !(%w{NilClass TrueClass FalseClass}.include? parent.class.to_s)
25
+ end
26
+
27
+ #
28
+ # parent_url returns the parent url - /employees/1
29
+ def parent_url options={}
30
+ parent? ? url_for(@parent) : ""
31
+ # parent? ? ( "/%s/%s" % [ @parent.class.table_name, @parent.id ] ) : ""
32
+ rescue Exception => e
33
+ scoop_from_error e
34
+ end
35
+
36
+
37
+ #
38
+ # build an array of the resource - particular to <SELECT>
39
+ def set_parents
40
+ @parents = []
41
+ return @parents unless resource_class.respond_to? :roots #( 'arraying') && resource? )
42
+ @parents = resource_class.arraying({ order: 'name'}, resource.possible_parents)
43
+ rescue Exception => e
44
+ scoop_from_error e
45
+ end
46
+
47
+
48
+ #
49
+ #
50
+ # /employees/1/teams
51
+ # /employees/1/teams/5
52
+ # /employees/1/teams/5/attach
53
+ # /theatres/2/contacts/5/uris.js
54
+ def find_parent path=nil, parms=nil
55
+ path ||= request.path
56
+ parms ||= params
57
+ if parms[:parent].nil? #or params[:parent_id].nil?
58
+ paths=path.split("/")
59
+ paths.pop if %w{new edit show create update delete index}.include? paths[-1]
60
+ return nil if (paths.size < 3) #or (paths.size==4 and %w{edit new}.include?( parms[:action]))
61
+ recognise_path paths.join("/")
62
+ else
63
+ parms[:parent].classify.constantize.find(parms[:parent_id])
64
+ end
65
+ end
66
+ #
67
+ # ['theatres','5','contacts','2','uris.js']
68
+ def recognise_path path
69
+ path_elements = Rails.application.routes.recognize_path path.gsub /\..*$/,'' # "/admin/users/2/printers/3/attach" => {:controller=>"printers", :action=>"attach", :user_id=>"2", :id=>"3"}
70
+ recognise_parent( recognise_resource( path_elements ) )
71
+
72
+ rescue Exception => e
73
+ nil
74
+ # return [ nil, nil, false ] if e.class.to_s == "ActionController::RoutingError"
75
+
76
+ end
77
+
78
+ # {:controller=>"printers", :action=>"attach", :user_id=>"2", :id=>"3"}
79
+ def recognise_resource elems
80
+ resource_class = elems.delete(:controller).singularize.classify.constantize
81
+ resource = resource_class.find( elems.delete(:id) )
82
+ elems
83
+ rescue Exception => e
84
+ return elems if e.class.to_s == "ActiveRecord::RecordNotFound"
85
+ resource_class = nil
86
+ elems
87
+ end
88
+
89
+ # { :action=>"attach", :user_id=>"2" }
90
+ def recognise_parent elems
91
+ elems.delete :action
92
+ arr = elems.keys.first.to_s.split("_")
93
+ return nil unless arr.include? "id"
94
+ arr.pop
95
+ arr.join("_").singularize.classify.constantize.find(elems.values.first)
96
+ rescue
97
+ nil
98
+ end
99
+
100
+ #
101
+ # # /employees/1/teams/new
102
+ # # /employees/1/teams/1/edit
103
+ # # /employees/1/teams/1/delete
104
+ def update_parenthood
105
+ if params[:parent] and params[:parent_id]
106
+ # raise "Ups - is this ready for prime time yet?"
107
+ parent = params[:parent].classify.constantize.find(params[:parent_id])
108
+ unless parent.blank?
109
+ case params[:action]
110
+ when "create"
111
+ children = eval("parent.#{resource_name}")
112
+ children << resource unless children.include? resource
113
+ # when "edit"
114
+ when "delete"
115
+ children = eval("parent.#{resource_name}")
116
+ children >> resource
117
+ end
118
+ end
119
+ end
120
+ true
121
+ rescue
122
+ false
123
+ end
124
+ #
125
+ #
126
+
127
+ end
@@ -0,0 +1,190 @@
1
+ require 'active_support/concern'
2
+ #
3
+ #
4
+ # ARGS # first value is default
5
+ #
6
+ # collation # 'list' | 'record'
7
+ # paper # 'A4' | 'label' | ...
8
+ # template # '' | 'slip' | 'quote'
9
+ # cmd # '' | 'print_label' - a particular method on the printing_class
10
+ #
11
+ # PRINTPROMPT
12
+ # print[medium] # 'display' | 'email' | 'printer' | 'download'
13
+ # print[output_type] # 'html' | 'pdf' | 'text'
14
+ # print[printer] # what printer to send output to
15
+ # print[email_to] # email address
16
+ # print[message] # body of email
17
+ # print[range] # which pages should print
18
+ # print[copies] # number of copies
19
+ #
20
+ # PRINTJOB
21
+ # id,
22
+ # account_id, # what 'system' / customer
23
+ # printer_id, # on what printer
24
+ # printed_by_id, # what id has the printed_by entity
25
+ # printed_by_type, # what entity - like user
26
+ # view_template_path, # what template
27
+ # name, # label the job
28
+ # printing_class, # what entity provides the data
29
+ # print_driver, # what 'driver' - like :pdf, :cab, :zebra, :csv, :html, etc
30
+ # print_format, # data collation - like 'record', 'list'
31
+ # state, # record the progress
32
+ # paper, # what material - like 'label32x42', 'A4', etc
33
+ # copies, # number of identical prints
34
+ # print_sql, # how to find what will be printed
35
+ # created_at,
36
+ # updated_at
37
+ #
38
+ # TEMPLATE
39
+ # id
40
+ # account_id, # what 'system' / customer
41
+ # template_key, # a key to search the template by
42
+ # template_path, # where the template is stored - like stock_items/print/zebra.html.haml
43
+ # template_content, # or what the template contains - all HTML, CSS etc.
44
+ # template_print_driver # identification of a particular driver -if necessary - like the :zebra
45
+ # template_paper # identification of a particular paper -if necessary - like the :label10x32
46
+ # created_at
47
+ # updated_at
48
+
49
+
50
+ module PrintControl
51
+ extend ActiveSupport::Concern
52
+
53
+ included do
54
+
55
+ PRINTSUCCESS = 1
56
+ NOQUEUE = -1
57
+ NOUSER = -2
58
+ PRINTRECERROR = -3
59
+ PRINTCMDERROR = -4
60
+ PRINTLISTERROR = -5
61
+ PRINTEXCEPTION = -99
62
+
63
+ end
64
+ #
65
+ # print this view - let the Class handle everything
66
+ # returning either the ID to a print_job or false (in which case something went terribly wrong)
67
+ #
68
+ # always an Ajax call - hence will always update the print_jobs link with 'yellow'-blink
69
+ # POST /printers/print.js
70
+ # params[:id] holds records to be printed
71
+ #
72
+ # from the print-dialog:
73
+ #
74
+ # from a link tag
75
+ def print
76
+ authorize resource, :print?
77
+ if resources.any?
78
+ params[:printed_by] = current_user || User.first
79
+ params[:print] ||= {}
80
+ params[:print][:collation] ||= 'list'
81
+ params[:print_job] ||= {}
82
+ # params[:print_job][:view_template] ||= current_user.account.find_print_template(params[:print][:template]) || Template.new( template_path: 'list.html.haml')
83
+ # params[:print_job][:print_driver] = params[:print][:print_driver] || params[:print_job][:view_template].template_print_driver
84
+ # params[:print_job][:paper] = params[:print_job][:view_template].template_paper || params[:print][:paper] || "A4"
85
+ params[:print_job][:paper] = params[:print][:paper] || "A4"
86
+ #
87
+ # ok so we have items to print!
88
+ status = case params[:print][:medium]
89
+ when "display" ; display_print_result and return
90
+ when "email" ; email_print_result
91
+ when "printer" ; print_print_result
92
+ when "download" ; download_print_result and return
93
+ else
94
+ flash[:error] = t('output.medium.no_medium_found');
95
+ 301
96
+ end
97
+ end
98
+ render :print, layout: false, status: status and return
99
+
100
+ rescue Exception => e
101
+ scoop_from_error e
102
+ end
103
+
104
+ #
105
+ # send HTML or PDF down the wire
106
+ #
107
+ def display_print_result
108
+ params[:print][:output_type] ||= 'html'
109
+ case params[:print][:output_type].downcase
110
+ when 'html'; render params[:print][:view_template_path], layout: 'print'
111
+ when 'pdf'; render action: :new, status: :unprocessable_entity
112
+ end
113
+ end
114
+
115
+ #
116
+ # send PDF down the wire
117
+ #
118
+ def download_print_result
119
+ params[:print][:output_type] ||= 'pdf'
120
+ end
121
+
122
+ #
123
+ # send PDF via email
124
+ #
125
+ def email_print_result
126
+ params[:print][:output_type] ||= 'pdf'
127
+ if params[:print][:email_to].blank?
128
+ flash[:error] = t('output.email.email_address_missing')
129
+ return 301
130
+ else
131
+ flash[:info] = t('output.email.email_sent')
132
+ return 200
133
+ end
134
+ end
135
+
136
+ def print_print_result
137
+ params[:print][:output_type] ||= 'pdf'
138
+ if (result = print_resources) > 0
139
+ flash[:info] = t(:resources_printed_correct)
140
+ status = 200
141
+ else
142
+ case result
143
+ when NOQUEUE; flash[:error] = t(:noqueue_created)
144
+ when NOUSER; flash[:error] = t(:no_user_present)
145
+ when PRINTRECERROR; flash[:error] = t(:printing_record_error)
146
+ when PRINTCMDERROR; flash[:error] = t(:print_command_error)
147
+ when PRINTLISTERROR; flash[:error] = t(:printing_list_error)
148
+ when PRINTEXCEPTION ; flash[:error] = t(:exception_in_print_engine)
149
+ end
150
+ status = 301
151
+ end
152
+ status
153
+ end
154
+
155
+
156
+ #
157
+ # print_resources will try to add a print_job
158
+ # return error code
159
+ def print_resources
160
+ if resource_class == PrintJob
161
+ resources.each do |res|
162
+ return NOQUEUE unless Delayed::Job.enqueue res, :queue => 'printing'
163
+ end
164
+ return PRINTSUCCESS
165
+ else
166
+ return NOUSER if params[:printed_by].nil?
167
+ case params[:print][:collation]
168
+ when 'record'
169
+ if params[:print][:cmd].blank?
170
+ resources.each do |res|
171
+ return PRINTRECERROR unless res.print_record params.merge context: self
172
+ end
173
+ else
174
+ resources.each do |res|
175
+ return PRINTCMDERROR unless res.send(params[:print][:cmd],params)
176
+ end
177
+ end
178
+
179
+ when 'list'
180
+ params[:resources] = resources
181
+ return PRINTLISTERROR unless resource_class.print_list( params )
182
+
183
+ end
184
+ end
185
+
186
+ return PRINTSUCCESS
187
+
188
+ end
189
+
190
+ end
@@ -0,0 +1,220 @@
1
+ require 'active_support/concern'
2
+
3
+ module ResourceControl
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+
8
+ before_action :set_resource #, except: :index
9
+ before_action :set_resources, only: [:index, :print]
10
+
11
+ end
12
+
13
+ def resource_params
14
+ raise 'You need to "def resource_params" on the %sController! (see: http://blog.trackets.com/2013/08/17/strong-parameters-by-example.html)' % params[:controller].capitalize
15
+ end
16
+
17
+ def set_resource
18
+ parent
19
+ resource
20
+ end
21
+
22
+ def set_resources
23
+ resources
24
+ end
25
+
26
+ def resource?
27
+ !(%w{NilClass TrueClass FalseClass}.include? @resource.class.to_s)
28
+ rescue Exception => e
29
+ scoop_from_error e
30
+ end
31
+
32
+ def resource
33
+ @resource ||= (_id.nil? ? new_resource : resource_class.find(_id) )
34
+ return @resource if @resource.nil?
35
+ @resource.current_user = (current_user || nil) if @resource.respond_to? :current_user
36
+ @resource
37
+ rescue Exception => e
38
+ scoop_from_error e
39
+ end
40
+
41
+ # def resource=val
42
+ # @resource=val
43
+ # end
44
+
45
+ def new_resource
46
+ return nil if resource_class.nil?
47
+ return resource_class.new if resource_class.ancestors.include?( ActiveRecord::Base ) and !(params.include? resource_class.to_s.underscore) #[ 'create', 'update' ].include? params[:action]
48
+ p = resource_params
49
+ p=p.compact.first if p.class==Array
50
+ return resource_class.new(p.merge(current_user: current_user)) if resource_class.ancestors.include? ActiveRecord::Base
51
+ nil
52
+ rescue Exception => e
53
+ scoop_from_error e
54
+ end
55
+
56
+ def _id
57
+ return nil if !params[:id] || params[:id]=="0"
58
+ params[:id] || params["#{resource_class.to_s.downcase}_id".to_sym]
59
+ rescue Exception => e
60
+ scoop_from_error e
61
+ end
62
+
63
+ def resource_name options={}
64
+ resource_class.table_name
65
+ rescue Exception => e
66
+ scoop_from_error e
67
+ # resource_class.to_s.underscore.pluralize
68
+ end
69
+
70
+ def resource_class
71
+ return @resource_class if resource?
72
+ @resource_class ||= params[:controller].singularize.classify.constantize
73
+ rescue Exception => e
74
+ scoop_from_error e
75
+ end
76
+
77
+ def resource_class= val
78
+ @resource_class = val
79
+ end
80
+
81
+ #
82
+ #
83
+ # return the resources collection - preferably from the cache
84
+ def resources options={}
85
+ @resources ||= find_resources options
86
+ end
87
+
88
+
89
+ #
90
+ # returns the url for the resource - like /users/1
91
+ def resource_url options={}
92
+ r=request.path
93
+ id=params[:id]
94
+ options = case params[:action]
95
+ when 'create','update','delete','destroy'; ""
96
+ else resource_options(options)
97
+ end
98
+ return "%s%s" % [r,options] if r.match "#{resource.class.to_s.tableize}\/#{id}$"
99
+ "%s%s" % [ r.split("/#{params[:action]}")[0], options]
100
+ rescue Exception => e
101
+ scoop_from_error e
102
+ end
103
+
104
+
105
+ #
106
+ # returns the url for the resources - /employees or /employees/1/events
107
+ def resources_url options={}
108
+ r=request.path
109
+ options = case params[:action]
110
+ when 'create','update','delete','destroy'; ""
111
+ else resource_options(options)
112
+ end
113
+ return "%s%s" % [r,options] if r.match "#{resource.class.to_s.tableize}$"
114
+ "%s%s%s" % [ r.split( resource.class.to_s.tableize)[0],resource.class.to_s.tableize, options]
115
+ rescue Exception => e
116
+ scoop_from_error e
117
+ end
118
+
119
+ def resource_options options
120
+ options.merge! params.except( "id", "controller", "action", "utf8", "_method", "authenticity_token" )
121
+ options = (options.empty? ? "" : "?" + options.collect{ |k,v| "#{k}=#{v}" }.join("&"))
122
+ end
123
+
124
+ #
125
+ # find the resources collection
126
+ def find_resources options
127
+
128
+ # return [] unless resource_class.ancestors.include? ActiveRecord::Base
129
+ params[:ids] ||= []
130
+ params[:ids] << params[:id] unless params[:id].nil?
131
+
132
+ if params[:ids].compact.any?
133
+ policy_scope(resource_class).where(id: params[:ids].compact.split(",").flatten)
134
+ else
135
+ # search
136
+ r = _search options
137
+ # sort
138
+ r = _sort r, options
139
+
140
+ # paginate
141
+ r = _paginate r
142
+
143
+ # (params[:format].nil? or params[:format]=='html') ? r.page(params[:page]).per(params[:perpage]) : r
144
+ end
145
+ end
146
+
147
+ #
148
+ # search - it at all
149
+ #
150
+ def _search options
151
+ if params[:q].blank? #or params[:q]=="undefined"
152
+ parent? ? parent.send(resource_name) : (resource_class.nil? ? nil : find_all_resources(options))
153
+ else
154
+ find_resources_queried options
155
+ end
156
+ end
157
+
158
+ #
159
+ # sort - it at all
160
+ #
161
+ # %th{ role:"sort", data{ field: "*barcode", direction: "DESC"} }
162
+ # $('th[role="sort"]')
163
+ def _sort r, options
164
+ r.order "%s %s" % [ (params[:s] || "id"), sorting_direction(params[:d]) ]
165
+ end
166
+
167
+ def sorting_direction val=nil
168
+ _flip(val || @sorting_direction)
169
+ end
170
+
171
+ def _flip val
172
+ @sorting_direction = (val == "ASC" ? "DESC" : "ASC")
173
+ val
174
+ rescue
175
+ @sorting_direction = "ASC"
176
+ end
177
+
178
+ #
179
+ # paginate - it at all
180
+ #
181
+ def _paginate r
182
+ return r if params[:action]=='print'
183
+ # will it obey the #page call?
184
+ # is it without format or html or scrolling or with search?
185
+ if (r.respond_to?( :page)) && (params[:format].nil? or params[:format]=='html' or params[:scrolling] or params[:s])
186
+ params[:perpage] ||= 20
187
+ params[:page] ||= 1
188
+ return r.page(params[:page]).per(params[:perpage])
189
+ else
190
+ return r
191
+ end
192
+ end
193
+
194
+ #
195
+ # find queried resources collection - implement on each controller to customize
196
+ # raise an exception
197
+ def find_all_resources options
198
+ policy_scope(resource_class)
199
+ end
200
+
201
+ #
202
+ # find queried resources collection - implement on each controller to customize
203
+ # raise an exception
204
+ def find_resources_queried options
205
+ case params[:f]
206
+ when nil
207
+ if parent?
208
+ policy_scope(resource_class).tags_included?( params[:q].split(" ") ).where( options )
209
+ else
210
+ policy_scope(resource_class).tags_included?( params[:q].split(" ") )
211
+ end
212
+ else
213
+ policy_scope(resource_class)
214
+ end
215
+ end
216
+
217
+ class_methods do
218
+ end
219
+
220
+ end
@@ -1,3 +1,3 @@
1
1
  module Abstracted
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstracted
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Walther H Diechmann
@@ -288,6 +288,9 @@ files:
288
288
  - app/controllers/.keep
289
289
  - app/controllers/abstract_resources_controller.rb
290
290
  - app/controllers/application_controller.rb
291
+ - app/controllers/concerns/parent_control.rb
292
+ - app/controllers/concerns/print_control.rb
293
+ - app/controllers/concerns/resource_control.rb
291
294
  - app/helpers/.keep
292
295
  - app/helpers/abstract_resources_helper.rb
293
296
  - app/helpers/fab_button_helper.rb