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
-
.filter(lot, filter, options = {}) ⇒ Object
filter the lot on filter.
- .filter_proc(lot, fld, k, v) ⇒ Object
- .logit(log_type, msg) ⇒ 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
-
#detach(parent) ⇒ Object
# remove the child from an association of children.
-
#lock_record?(unlock = false) ⇒ Boolean
will lock a record - provided the record responds to :record_lock field and will unlock the record - provided an argument TRUE is provided.
- #logit(log_type, msg) ⇒ Object
-
#resource_name ⇒ Object
depreciated ———— 12/3/2016.
Instance Attribute Details
#current_user ⇒ Object
Returns the value of attribute current_user.
5 6 7 |
# File 'app/models/abstract_resource.rb', line 5 def current_user @current_user end |
Class Method Details
.filter(lot, filter, options = {}) ⇒ Object
filter the lot on filter
filter => { updated_at: [ ‘<’, ‘11-11-2011’], material: { eq: ‘alu%’}}
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/models/abstract_resource.rb', line 45 def self.filter(lot, filter, ={}) filter.clone.each do |fld,v| if v.is_a? Array k = v.shift lot = filter_proc lot, fld, k, v elsif v.is_a? Hash v.each do |k,val| lot = filter_proc lot, fld, k, val end elsif v.is_a? String lot = lot.where( "? = ?", fld, v ) else raise "filter valueset '#{v}' not recognised?!" end end lot end |
.filter_proc(lot, fld, k, v) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'app/models/abstract_resource.rb', line 63 def self.filter_proc lot, fld, k, v tbl = self.arel_table case k.to_sym when :lt, :lteq, :eq, :gteq, :gt, :matches raise "Exactly one value allowed in filter [lt|eq|gt][|eq]!" if [v].flatten.size > 1 lot = lot.where tbl[fld].send k.to_sym, [v].flatten.shift when :not_eq_any, :not_eq_all, :eq_any, :eq_all, :gteq_any, :gteq_all, :gt_any, :gt_all, :lt_any, :lt_all, :lteq_any, :lteq_all raise "At least one value required in filter [|not][lt|eq|gt][_any|_all]!" if [v].flatten.size < 1 lot = lot.where tbl[fld].send k, [v].flatten when :between, :not_between raise "Exactly two values allowed in filter [not_]between!" if [v].flatten.size != 2 lot = lot.where tbl[fld].send k, [v].flatten when :in, :in_any, :in_all, :not_in, :not_in_any, :not_in_all lot = lot.where tbl[fld].send k, [v].flatten when :matches_any, :matches_all, :does_not_match, :does_not_match_any, :does_not_match_all lot = lot.where tbl[fld].send k, [v].flatten when :matches_regexp, :does_not_match_regexp raise "filter [does_not_]match[es]_regexp is not supported" when :when, :concat raise "filter [when|concat] is not supported" else raise "filter key '#{k}' not recognised?!" end lot end |
.logit(log_type, msg) ⇒ Object
7 8 9 |
# File 'app/models/abstract_resource.rb', line 7 def self.logit( log_type, msg ) Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}") end |
.search(lot, query, fields = "id") ⇒ Object
this generic search needs a bit more work but it is a ok first draft
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/abstract_resource.rb', line 19 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
158 159 160 |
# File 'app/models/abstract_resource.rb', line 158 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
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/models/abstract_resource.rb', line 119 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
162 163 164 |
# File 'app/models/abstract_resource.rb', line 162 def deactivate update_attributes active: false end |
#detach(parent) ⇒ Object
# remove the child from an association of children
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/models/abstract_resource.rb', line 138 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 |
#lock_record?(unlock = false) ⇒ Boolean
will lock a record - provided the record responds to :record_lock field and will unlock the record - provided an argument TRUE is provided
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'app/models/abstract_resource.rb', line 170 def lock_record? unlock=false return false unless respond_to? :record_lock reload return false unless [ 0, current_user.id ].include? record_lock.to_i case unlock when true,"true" begin self.with_lock do update_attributes record_lock: 0 true end rescue e case e when ActiveRecord::StaleObjectError return false else raise e end end else begin self.with_lock do update_attributes record_lock: current_user.id record_lock.to_i == current_user.id end rescue e case e when ActiveRecord::StaleObjectError return false else raise e end end end end |
#logit(log_type, msg) ⇒ Object
11 12 13 |
# File 'app/models/abstract_resource.rb', line 11 def logit( log_type, msg ) Rails.logger.send(log_type, "[OXEN] #{Time.now} [#{log_type.to_s}] #{msg}") end |
#resource_name ⇒ Object
depreciated ———— 12/3/2016
ancestry related methods - find them on AncestryAbstractResource def self.arrange_array(options={}, hash=nil)
hash ||= arrange()
arr = []
hash.each do |node, children|
arr << node
arr += arrange_array(, children) unless children.nil?
end
arr
end
def possible_parents
prtns = self.arrange_array(:order => 'name')
return new_record? ? prtns : prtns - subtree
end
108 109 110 |
# File 'app/models/abstract_resource.rb', line 108 def resource_name self.class.to_s.underscore.pluralize end |