Class: AbstractResource

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PrintEngine
Defined in:
app/models/abstract_resource.rb

Direct Known Subclasses

AncestryAbstractResource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_userObject

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(options={}, hash=nil)
  hash ||= arrange(options)

  arr = []
  hash.each do |node, children|
    arr << node
    arr += arrange_array(options, children) unless children.nil?
  end
  arr
end

.default_label_templateObject



78
79
80
# File 'app/models/abstract_resource.rb', line 78

def self.default_label_template
  'label.html.haml'
end

.default_list_templateObject



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


109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/abstract_resource.rb', line 109

def self.print_list(options={})
  params = {}
  params[:print_job] = {}
  params[:print_job][:download] = options[:download] || false
  params[:print_job][:print_driver] = options[:print_driver] || :pdf
  params[:print_job][:print_format] = options[:print_format] || 'list'
  params[:print_job][:paper] = options[:paper] || "A4"
  params[:print_job][:view_template_path] = options[:template] || self.default_list_template
  params[:printer_name] = options[:printer_name]
  params[:user] = options[:user]
  self.print options[: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

#activateObject



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

#deactivateObject



169
170
171
# File 'app/models/abstract_resource.rb', line 169

def deactivate
  update_attributes active: false
end

#default_record_templateObject

# 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_parentsObject



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

def print_label options={}

params = {}
params[:print_job] = {}
params[:print_job][:view_template_path] = options[:template] || default_label_template
params[:print_job][:print_driver] = options[:print_driver] || :cab
params[:print_job][:print_format] = options[:print_format] || 'label'
params[:print_job][:paper] = options[:paper] || "Pakkelabel"
params[:printer_name] = options[:printer_name]
params[:user] = options[: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(options={})
  params = {}
  params[:context] = options[:context]
  options = options[:params]
  params[:print_job] = {}
  params[:print_job][:download] = options[:download] || false
  params[:print_job][:print_driver] = options[:print_driver] || :pdf
  params[:print_job][:print_format] = options[:print_format] || 'sheet'
  params[:print_job][:paper] = options[:paper] || "A4"
  params[:print_job][:view_template_path] = options[:template] || (options[:paper]!="A4" ? self.send("default_#{options[:paper]}_template") : default_record_template)
  params[:printer_name] = options[:printer_name]
  params[:user] = options[:user]
  pj = self.class.print [self], params
end

#resource_nameObject



54
55
56
# File 'app/models/abstract_resource.rb', line 54

def resource_name
 self.class.to_s.underscore.pluralize
end