Class: AbstractResource
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- AbstractResource
- Includes:
- PrintEngine
- Defined in:
- app/models/abstract_resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_user ⇒ Object
Returns the value of attribute current_user.
Class Method Summary collapse
- .arrange_array(options = {}, hash = nil) ⇒ Object
- .default_label_template ⇒ Object
- .default_list_template ⇒ Object
- .logit(log_type, msg) ⇒ Object
- .print_list(options = {}) ⇒ Object
-
.search(lot, query, fields = "id") ⇒ Object
this generic search needs a bit more work but it is a ok first draft.
Instance Method Summary collapse
- #activate ⇒ Object
-
#attach(parent) ⇒ Object
# add the child to an association of children !! remember to implement before_save action on *able tables to meet further foreign_key conditions like account_id, etc.
- #deactivate ⇒ Object
-
#default_record_template ⇒ Object
# list_title def list_title self.respond_to?( “name”) ? self.name : “please define list_title on model (#selfself.classself.class.to_s)!” end.
-
#detach(parent) ⇒ Object
# remove the child from an association of children.
- #logit(log_type, msg) ⇒ Object
- #possible_parents ⇒ Object
-
#print_record(options = {}) ⇒ Object
def print_label options={} params = {} params = {} params[:view_template_path] = options || default_label_template params[:print_driver] = options || :cab params[:print_format] = options || ‘label’ params[:paper] = options || “Pakkelabel” params = options params = options self.class.print [self], params end.
- #resource_name ⇒ Object
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
4 5 6 |
# File 'app/models/abstract_resource.rb', line 4 def current_user @current_user end |
Class Method Details
.arrange_array(options = {}, hash = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/abstract_resource.rb', line 38 def self.arrange_array(={}, hash=nil) hash ||= arrange() arr = [] hash.each do |node, children| arr << node arr += arrange_array(, children) unless children.nil? end arr end |
.default_label_template ⇒ Object
78 79 80 |
# File 'app/models/abstract_resource.rb', line 78 def self.default_label_template 'label.html.haml' end |
.default_list_template ⇒ Object
74 75 76 |
# File 'app/models/abstract_resource.rb', line 74 def self.default_list_template 'list.html.haml' end |
.logit(log_type, msg) ⇒ Object
6 7 8 |
# File 'app/models/abstract_resource.rb', line 6 def self.logit( log_type, msg ) Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}") end |
.print_list(options = {}) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'app/models/abstract_resource.rb', line 109 def self.print_list(={}) params = {} params[:print_job] = {} params[:print_job][:download] = [:download] || false params[:print_job][:print_driver] = [:print_driver] || :pdf params[:print_job][:print_format] = [:print_format] || 'list' params[:print_job][:paper] = [:paper] || "A4" params[:print_job][:view_template_path] = [:template] || self.default_list_template params[:printer_name] = [:printer_name] params[:user] = [:user] self.print [:resources], params end |
.search(lot, query, fields = "id") ⇒ Object
this generic search needs a bit more work but it is a ok first draft
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/abstract_resource.rb', line 18 def self.search(lot, query, fields="id") fields = fields.join(",") if fields.is_a? Array population = lot.pluck fields elements = query.split(' ') constraints = {and: [], or: [], not: []} elements.each do |element| case element.slice 0 when '+'; constraints[:and] += population.collect{ |k,v| k if v =~ /#{element[1..-1]}/i }.compact.flatten when '-'; constraints[:not] += population.collect{ |k,v| k if v =~ /#{element[1..-1]}/i }.compact.flatten when '*'; constraints[:or] += population.collect{ |k,v| k if b=="#{element[1..-1]}" }.compact.flatten else; constraints[:or] += population.collect{ |k,v| k if v =~ /#{element}/i }.compact.flatten end end population = constraints[:or].empty? ? population.collect{|r| r[0]} : constraints[:or] population = population & constraints[:and] if constraints[:and].any? population -= constraints[:not].uniq if constraints[:not].any? population = [] if constraints[:or].empty? and constraints[:and].empty? lot.where id: population end |
Instance Method Details
#activate ⇒ Object
165 166 167 |
# File 'app/models/abstract_resource.rb', line 165 def activate update_attributes active: true end |
#attach(parent) ⇒ Object
# add the child to an association of children !! remember to implement before_save action on *able tables to meet further foreign_key conditions like account_id, etc
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'app/models/abstract_resource.rb', line 126 def attach parent # the ordinary *able table parent.send( self.class.to_s.underscore.pluralize) << self # case child.class.to_s # when "Event","WageEvent" # Eventable.create( event: child, eventable: self) unless child.eventables.include?( self) # when "Printer" # Printable.create( printer: child, printable: self) unless child.printables.include?( self) # else # children = eval child.class.to_s.underscore.pluralize # children << child # end rescue false end |
#deactivate ⇒ Object
169 170 171 |
# File 'app/models/abstract_resource.rb', line 169 def deactivate update_attributes active: false end |
#default_record_template ⇒ Object
# list_title def list_title
self.respond_to?( "name") ? self.name : "please define list_title on model (#{self.class.to_s})!"
end
# implement on relevant models
70 71 72 |
# File 'app/models/abstract_resource.rb', line 70 def default_record_template 'accounts/record.html.haml' end |
#detach(parent) ⇒ Object
# remove the child from an association of children
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'app/models/abstract_resource.rb', line 145 def detach parent # the ordinary *able table parent.send( self.class.to_s.underscore.pluralize).delete self # case child.class.to_s # when "Event","WageEvent" # ev = Eventable.where( event: child, eventable: self) # ev.delete_all # when "Printer" # pr = Printable.where( printer: child, printable: self) # pr.delete_all # else # children = eval child.class.to_s.downcase.pluralize # children.delete child # end rescue false end |
#logit(log_type, msg) ⇒ Object
10 11 12 |
# File 'app/models/abstract_resource.rb', line 10 def logit( log_type, msg ) Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}") end |
#possible_parents ⇒ Object
49 50 51 52 |
# File 'app/models/abstract_resource.rb', line 49 def possible_parents prtns = self.arrange_array(:order => 'name') return new_record? ? prtns : prtns - subtree end |
#print_record(options = {}) ⇒ Object
def print_label options={}
params = {}
params[:print_job] = {}
params[:print_job][:view_template_path] = [:template] || default_label_template
params[:print_job][:print_driver] = [:print_driver] || :cab
params[:print_job][:print_format] = [:print_format] || 'label'
params[:print_job][:paper] = [:paper] || "Pakkelabel"
params[:printer_name] = [:printer_name]
params[:user] = [:user]
self.class.print [self], params
end
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/models/abstract_resource.rb', line 94 def print_record(={}) params = {} params[:context] = [:context] = [:params] params[:print_job] = {} params[:print_job][:download] = [:download] || false params[:print_job][:print_driver] = [:print_driver] || :pdf params[:print_job][:print_format] = [:print_format] || 'sheet' params[:print_job][:paper] = [:paper] || "A4" params[:print_job][:view_template_path] = [:template] || ([:paper]!="A4" ? self.send("default_#{[:paper]}_template") : default_record_template) params[:printer_name] = [:printer_name] params[:user] = [:user] pj = self.class.print [self], params end |
#resource_name ⇒ Object
54 55 56 |
# File 'app/models/abstract_resource.rb', line 54 def resource_name self.class.to_s.underscore.pluralize end |