Class: Ooor::Base
- Inherits:
-
MiniActiveResource
- Object
- MiniActiveResource
- Ooor::Base
- Includes:
- Associations, FieldMethods, FinderMethods, Naming, Reflection, ReflectionOoor, Report, Serialization, TypeCasting
- Defined in:
- lib/ooor/base.rb
Constant Summary
Constants included from TypeCasting
Class Attribute Summary collapse
-
.connection ⇒ Object
template.
-
.name ⇒ Object
template.
-
.scope_prefix ⇒ Object
template.
-
.t ⇒ Object
template.
Instance Attribute Summary collapse
-
#associations ⇒ Object
********************** instance methods **********************************.
-
#ir_model_data_id ⇒ Object
********************** instance methods **********************************.
-
#loaded_associations ⇒ Object
********************** instance methods **********************************.
-
#object_session ⇒ Object
********************** instance methods **********************************.
Attributes inherited from MiniActiveResource
Class Method Summary collapse
- .all(*args) ⇒ Object
-
.create(attributes = {}, context = {}, default_get_list = false, reload = true) ⇒ Object
******************** remote communication *****************************.
- .limit(value) ⇒ Object
- .logger ⇒ Object
- .method_missing(method_symbol, *args) ⇒ Object
- .name_search(name = '', domain = [], operator = 'ilike', context = {}, limit = 100) ⇒ Object
- .object_service(service, obj, method, *args) ⇒ Object
- .offset(value) ⇒ Object
- .order(value) ⇒ Object
-
.relation(context = {}) ⇒ Object
******************** AREL Minimal implementation ***********************.
- .rpc_exec_workflow(action, *args) ⇒ Object
- .rpc_execute(method, *args) ⇒ Object
- .scoped(context = {}) ⇒ Object
-
.search(domain = [], offset = 0, limit = false, order = false, context = {}, count = false) ⇒ Object
OpenERP search method.
- .where(opts, *rest) ⇒ Object
Instance Method Summary collapse
-
#call(method, *args) ⇒ Object
Generic OpenERP rpc method call.
-
#copy(defaults = {}, context = {}) ⇒ Object
OpenERP copy method, load persisted copied Object.
-
#create(context = {}, reload = true) ⇒ Object
compatible with the Rails way but also supports OpenERP context.
-
#destroy(context = {}) ⇒ Object
compatible with the Rails way but also supports OpenERP context.
- #get_report_data(report_name, report_type = "pdf", context = {}) ⇒ Object
-
#initialize(attributes = {}, default_get_list = false, context = {}, persisted = false) ⇒ Base
constructor
takes care of reading OpenERP default field values.
-
#load(attributes, remove_root = false, persisted = false) ⇒ Object
an attribute might actually be a association too, will be determined here.
-
#on_change(on_change_method, field_name, field_value, *args) ⇒ Object
Generic OpenERP on_change method.
- #rpc_execute(method, *args) ⇒ Object
- #save(context = {}, reload = true) ⇒ Object
-
#type ⇒ Object
skips deprecated Object#type method.
-
#update(context = {}, reload = true) ⇒ Object
compatible with the Rails way but also supports OpenERP context.
- #update_attributes(attributes, context = {}, reload = true) ⇒ Object
-
#wkf_action(action, context = {}, reload = true) ⇒ Object
wrapper for OpenERP exec_workflow Business Process Management engine.
Methods included from Naming
Methods included from TypeCasting
#cast_relation, #cast_relations_to_openerp, #to_openerp_hash
Methods included from Serialization
#serializable_hash, #serialize_many2one, #serialize_x_to_many
Methods included from Associations
#load_association, #many2one_id_method, #relationnal_result, #x_to_many_ids_method
Methods included from FieldMethods
Methods inherited from MiniActiveResource
element_name, #errors, #new?, #persisted?, #reload, #to_json, #to_xml
Constructor Details
#initialize(attributes = {}, default_get_list = false, context = {}, persisted = false) ⇒ Base
takes care of reading OpenERP default field values.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ooor/base.rb', line 113 def initialize(attributes = {}, default_get_list=false, context={}, persisted=false) @attributes = {} @prefix_options = {} @ir_model_data_id = attributes.delete(:ir_model_data_id) @object_session = {} @object_session = HashWithIndifferentAccess.new(context) @persisted = persisted self.class.reload_fields_definition(false, @object_session) if default_get_list == [] load(attributes) else load_with_defaults(attributes, default_get_list) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Ooor::FieldMethods
Class Attribute Details
.connection ⇒ Object
template
29 30 31 |
# File 'lib/ooor/base.rb', line 29 def connection @connection end |
.name ⇒ Object
template
29 30 31 |
# File 'lib/ooor/base.rb', line 29 def name @name end |
.scope_prefix ⇒ Object
template
29 30 31 |
# File 'lib/ooor/base.rb', line 29 def scope_prefix @scope_prefix end |
.t ⇒ Object
template
29 30 31 |
# File 'lib/ooor/base.rb', line 29 def t @t end |
Instance Attribute Details
#associations ⇒ Object
********************** instance methods **********************************
83 84 85 |
# File 'lib/ooor/base.rb', line 83 def associations @associations end |
#ir_model_data_id ⇒ Object
********************** instance methods **********************************
83 84 85 |
# File 'lib/ooor/base.rb', line 83 def ir_model_data_id @ir_model_data_id end |
#loaded_associations ⇒ Object
********************** instance methods **********************************
83 84 85 |
# File 'lib/ooor/base.rb', line 83 def loaded_associations @loaded_associations end |
#object_session ⇒ Object
********************** instance methods **********************************
83 84 85 |
# File 'lib/ooor/base.rb', line 83 def object_session @object_session end |
Class Method Details
.all(*args) ⇒ Object
70 |
# File 'lib/ooor/base.rb', line 70 def all(*args); relation.all(*args); end |
.create(attributes = {}, context = {}, default_get_list = false, reload = true) ⇒ Object
******************** remote communication *****************************
34 35 36 |
# File 'lib/ooor/base.rb', line 34 def create(attributes = {}, context={}, default_get_list=false, reload=true) self.new(attributes, default_get_list, context).tap { |resource| resource.save(context, reload) } end |
.limit(value) ⇒ Object
71 |
# File 'lib/ooor/base.rb', line 71 def limit(value); relation.limit(value); end |
.logger ⇒ Object
75 |
# File 'lib/ooor/base.rb', line 75 def logger; Ooor.logger; end |
.method_missing(method_symbol, *args) ⇒ Object
60 61 62 63 |
# File 'lib/ooor/base.rb', line 60 def method_missing(method_symbol, *args) raise RuntimeError.new("Invalid RPC method: #{method_symbol}") if [:type!, :allowed!].index(method_symbol) self.rpc_execute(method_symbol.to_s, *args) end |
.name_search(name = '', domain = [], operator = 'ilike', context = {}, limit = 100) ⇒ Object
43 44 45 |
# File 'lib/ooor/base.rb', line 43 def name_search(name='', domain=[], operator='ilike', context={}, limit=100) rpc_execute(:name_search, name, to_openerp_domain(domain), operator, context, limit) end |
.object_service(service, obj, method, *args) ⇒ Object
55 56 57 58 |
# File 'lib/ooor/base.rb', line 55 def object_service(service, obj, method, *args) reload_fields_definition(false, connection.connection_session) cast_answer_to_ruby!(connection.object.object_service(service, obj, method, *cast_request_to_openerp(args))) end |
.offset(value) ⇒ Object
73 |
# File 'lib/ooor/base.rb', line 73 def offset(value); relation.offset(value); end |
.order(value) ⇒ Object
72 |
# File 'lib/ooor/base.rb', line 72 def order(value); relation.order(value); end |
.relation(context = {}) ⇒ Object
******************** AREL Minimal implementation ***********************
67 |
# File 'lib/ooor/base.rb', line 67 def relation(context={}); @relation ||= Relation.new(self, context); end |
.rpc_exec_workflow(action, *args) ⇒ Object
51 52 53 |
# File 'lib/ooor/base.rb', line 51 def rpc_exec_workflow(action, *args) object_service(:exec_workflow, openerp_model, action, *args) end |
.rpc_execute(method, *args) ⇒ Object
47 48 49 |
# File 'lib/ooor/base.rb', line 47 def rpc_execute(method, *args) object_service(:execute, openerp_model, method, *args) end |
.scoped(context = {}) ⇒ Object
68 |
# File 'lib/ooor/base.rb', line 68 def scoped(context={}); relation(context); end |
.search(domain = [], offset = 0, limit = false, order = false, context = {}, count = false) ⇒ Object
OpenERP search method
39 40 41 |
# File 'lib/ooor/base.rb', line 39 def search(domain=[], offset=0, limit=false, order=false, context={}, count=false) rpc_execute(:search, to_openerp_domain(domain), offset, limit, order, context, count) end |
.where(opts, *rest) ⇒ Object
69 |
# File 'lib/ooor/base.rb', line 69 def where(opts, *rest); relation.where(opts, *rest); end |
Instance Method Details
#call(method, *args) ⇒ Object
Generic OpenERP rpc method call
170 |
# File 'lib/ooor/base.rb', line 170 def call(method, *args) rpc_execute(method, *args) end |
#copy(defaults = {}, context = {}) ⇒ Object
OpenERP copy method, load persisted copied Object
165 166 167 |
# File 'lib/ooor/base.rb', line 165 def copy(defaults={}, context={}) self.class.find(rpc_execute('copy', self.id, defaults, context), context: context) end |
#create(context = {}, reload = true) ⇒ Object
compatible with the Rails way but also supports OpenERP context
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ooor/base.rb', line 136 def create(context={}, reload=true) self.id = rpc_execute('create', to_openerp_hash, context) if @ir_model_data_id IrModelData.create(model: self.class.openerp_model, 'module' => @ir_model_data_id[0], 'name' => @ir_model_data_id[1], 'res_id' => self.id) end reload_from_record!(self.class.find(self.id, context: context)) if reload @persisted = true end |
#destroy(context = {}) ⇒ Object
compatible with the Rails way but also supports OpenERP context
160 161 162 |
# File 'lib/ooor/base.rb', line 160 def destroy(context={}) rpc_execute('unlink', [self.id], context) end |
#get_report_data(report_name, report_type = "pdf", context = {}) ⇒ Object
187 188 189 |
# File 'lib/ooor/base.rb', line 187 def get_report_data(report_name, report_type="pdf", context={}) self.class.get_report_data(report_name, [self.id], report_type, context) end |
#load(attributes, remove_root = false, persisted = false) ⇒ Object
an attribute might actually be a association too, will be determined here
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ooor/base.rb', line 90 def load(attributes, remove_root=false, persisted=false)#an attribute might actually be a association too, will be determined here self.class.reload_fields_definition(false, object_session) raise ArgumentError, "expected an attributes Hash, got #{attributes.inspect}" unless attributes.is_a?(Hash) @prefix_options, attributes = (attributes) @associations ||= {} @attributes ||= {} @loaded_associations = {} attributes.each do |key, value| skey = key.to_s if self.class.associations_keys.index(skey) || value.is_a?(Array) #FIXME may miss m2o with inherits! if value.is_a?(Ooor::Base) || value.is_a?(Array) && value.all? {|i| i.is_a?(Ooor::Base)} @loaded_associations[skey] = value #we want the method to load the association through method missing else @associations[skey] = value end else @attributes[skey] = value || nil #don't bloat with false values end end self end |
#on_change(on_change_method, field_name, field_value, *args) ⇒ Object
Generic OpenERP on_change method
173 174 175 176 177 178 |
# File 'lib/ooor/base.rb', line 173 def on_change(on_change_method, field_name, field_value, *args) # NOTE: OpenERP doesn't accept context systematically in on_change events unfortunately ids = self.id ? [id] : [] result = self.class.object_service(:execute, self.class.openerp_model, on_change_method, ids, *args) load_on_change_result(result, field_name, field_value) end |
#rpc_execute(method, *args) ⇒ Object
85 86 87 88 |
# File 'lib/ooor/base.rb', line 85 def rpc_execute(method, *args) args += [self.class.connection.connection_session.merge(object_session)] unless args[-1].is_a? Hash self.class.object_service(:execute, self.class.openerp_model, method, *args) end |
#save(context = {}, reload = true) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/ooor/base.rb', line 128 def save(context={}, reload=true) new? ? create(context, reload) : update(context, reload) rescue ValidationError => e e.extract_validation_error!(errors) return false end |
#type ⇒ Object
skips deprecated Object#type method
191 |
# File 'lib/ooor/base.rb', line 191 def type() method_missing(:type) end |
#update(context = {}, reload = true) ⇒ Object
compatible with the Rails way but also supports OpenERP context
153 154 155 156 157 |
# File 'lib/ooor/base.rb', line 153 def update(context={}, reload=true) #TODO use http://apidock.com/rails/ActiveRecord/Dirty to minimize data to save back rpc_execute('write', [self.id], to_openerp_hash, context) reload_fields(context) if reload @persisted = true end |
#update_attributes(attributes, context = {}, reload = true) ⇒ Object
148 149 150 |
# File 'lib/ooor/base.rb', line 148 def update_attributes(attributes, context={}, reload=true) load(attributes, false) && save(context, reload) end |
#wkf_action(action, context = {}, reload = true) ⇒ Object
wrapper for OpenERP exec_workflow Business Process Management engine
181 182 183 184 |
# File 'lib/ooor/base.rb', line 181 def wkf_action(action, context={}, reload=true) self.class.object_service(:exec_workflow, self.class.openerp_model, action, self.id, object_session) reload_fields(context) if reload end |