Class: Ooor::Session

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Transport
Defined in:
lib/ooor/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Transport

#base_jsonrpc2_url, #base_url, #get_client

Constructor Details

#initialize(connection, web_session, id) ⇒ Session

Returns a new instance of Session.



14
15
16
17
18
19
# File 'lib/ooor/session.rb', line 14

def initialize(connection, web_session, id)
  super(connection)
  @connection = connection
  @web_session = web_session || {}
  @id = id || web_session[:session_id]
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



7
8
9
# File 'lib/ooor/session.rb', line 7

def connection
  @connection
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/ooor/session.rb', line 7

def id
  @id
end

#web_sessionObject

Returns the value of attribute web_session.



7
8
9
# File 'lib/ooor/session.rb', line 7

def web_session
  @web_session
end

Instance Method Details

#[](model_key) ⇒ Object

TODO invert: define method here and use []



53
54
55
# File 'lib/ooor/session.rb', line 53

def [](key)
  @session[key]
end

#[]=(key, value) ⇒ Object



25
26
27
# File 'lib/ooor/session.rb', line 25

def []=(key, value)
  @session[key] = value
end

#commonObject



9
# File 'lib/ooor/session.rb', line 9

def common(); @common_service ||= CommonService.new(self); end

#const_get(model_key, lang = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ooor/session.rb', line 38

def const_get(model_key, lang=nil);
  if config[:aliases]
    if lang && alias_data = config[:aliases][lang]
      openerp_model = alias_data[model_key] || model_key
    elsif alias_data = config[:aliases][connection_session['lang'] || :en_US]
      openerp_model = alias_data[model_key] || model_key
    else
      openerp_model = model_key
    end
  else
    openerp_model = model_key
  end
  define_openerp_model(model: openerp_model, scope_prefix: config[:scope_prefix], generate_constants: config[:generate_constants])
end

#dbObject



10
# File 'lib/ooor/session.rb', line 10

def db(); @db_service ||= DbService.new(self); end

#define_openerp_model(options) ⇒ Object

TODO param to tell if we define constants or not



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ooor/session.rb', line 93

def define_openerp_model(options) #TODO param to tell if we define constants or not
  if !models[options[:model]] || options[:reload]# || !scope.const_defined?(model_class_name)
    scope_prefix = options[:scope_prefix]
    scope = scope_prefix ? Object.const_get(scope_prefix) : Object
    model_class_name = class_name_from_model_key(options[:model])
    logger.debug "registering #{model_class_name}"
    klass = Class.new(Base)
    set_model_template!(klass, options)
    klass.name = model_class_name
    klass.scope_prefix = scope_prefix
    klass.connection = self
    if options[:generate_constants] && (options[:reload] || !scope.const_defined?(model_class_name))
      scope.const_set(model_class_name, klass)
    end
    (Ooor.extensions[options[:model]] || []).each do |block|
      klass.class_eval(&block)
    end
    models[options[:model]] = klass
  end
  models[options[:model]]
end

#global_login(options) ⇒ Object



29
30
31
32
# File 'lib/ooor/session.rb', line 29

def (options)
  config.merge!(options)
  load_models(config[:models], options[:reload])
end

#load_models(model_names = , reload = ) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ooor/session.rb', line 57

def load_models(model_names=config[:models], reload=config[:reload])
  helper_paths.each do |dir|
    Dir[dir].each { |file| require file }
  end
  domain = model_names ? [['model', 'in', model_names]] : []
  search_domain = domain - [1]
  model_ids = object.object_service(:execute, "ir.model", :search, search_domain, 0, false, false, {}, false)
  models_records = object.object_service(:execute, "ir.model", :read, model_ids, ['model', 'name']) #TODO use search_read
  models_records.each do |opts|
    options = HashWithIndifferentAccess.new(opts.merge(scope_prefix: config[:scope_prefix], reload: reload, generate_constants: config[:generate_constants]))
    define_openerp_model(options)
  end
end

#loggerObject



117
# File 'lib/ooor/session.rb', line 117

def logger; Ooor.logger; end

#modelsObject



115
# File 'lib/ooor/session.rb', line 115

def models; @models ||= {}; end

#objectObject



11
# File 'lib/ooor/session.rb', line 11

def object(); @object_service ||= ObjectService.new(self); end

#reportObject



12
# File 'lib/ooor/session.rb', line 12

def report(); @report_service ||= ReportService.new(self); end

#session_context(context = {}) ⇒ Object



34
35
36
# File 'lib/ooor/session.rb', line 34

def session_context(context={})
  connection_session.merge(web_session.slice('lang', 'tz')).merge(context) # not just lang and tz?
end

#set_model_template!(klass, options) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/ooor/session.rb', line 71

def set_model_template!(klass, options)
  template = Ooor.model_registry.get_template(config, options[:model])
  if template
    klass.t = template
    klass.one2many_associations.keys.each do |meth|
      klass.define_nested_attributes_method(meth)
    end
  else
    template = Ooor::ModelTemplate.new
    template.openerp_model = options[:model]
    template.openerp_id = options[:id]
    template.description = options[:name]
    template.state = options[:state]
    template.many2one_associations = {}
    template.one2many_associations = {}
    template.many2many_associations = {}
    template.polymorphic_m2o_associations = {}
    template.associations_keys = []
    klass.t = template
  end
end