Skip to content

Commit

Permalink
Session now includes ActiveSupport::Configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rvalyi committed Apr 27, 2014
1 parent 045f69e commit 156ac65
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/ooor/naming.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def param_key(context={})
def const_get(model_key)
scope = self.scope_prefix ? Object.const_get(self.scope_prefix) : Object
klass_name = session.class_name_from_model_key(model_key)
if scope.const_defined?(klass_name) && Ooor.session_handler.connection_spec(scope.const_get(klass_name).session.config) == Ooor.session_handler.connection_spec(session.config)
if scope.const_defined?(klass_name) && Ooor.session_handler.noweb_session_spec(scope.const_get(klass_name).session.config) == Ooor.session_handler.noweb_session_spec(session.config)
scope.const_get(klass_name)
else
session.define_openerp_model(model: model_key, scope_prefix: self.scope_prefix)
Expand Down
20 changes: 14 additions & 6 deletions lib/ooor/session.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
require 'ooor/services'
require 'active_support/configurable'

module Ooor
class Session
include ActiveSupport::Configurable
include Transport

attr_accessor :web_session, :config, :id, :models
attr_accessor :web_session, :id, :models

def common(); @common_service ||= CommonService.new(self); end
def db(); @db_service ||= DbService.new(self); end
def object(); @object_service ||= ObjectService.new(self); end
def report(); @report_service ||= ReportService.new(self); end

def initialize(config, web_session, id)
@config = _config(config)
Object.const_set(@config[:scope_prefix], Module.new) if @config[:scope_prefix]
set_config(_config(config))
Object.const_set(config[:scope_prefix], Module.new) if config[:scope_prefix]
@models = {}
@local_context = {}
@web_session = web_session || {}
@id = id || web_session[:session_id]
end

def set_config(configuration)
configuration.each do |k, v|
config.send "#{k}=", v
end
end

# a part of the config that will be mixed in the context of each session
def connection_session
HashWithIndifferentAccess.new(@config[:connection_session] || {})
HashWithIndifferentAccess.new(config[:connection_session] || {})
end

def [](key)
Expand Down Expand Up @@ -69,7 +77,7 @@ def const_get(model_key, lang=nil);

def load_models(model_names=config[:models], reload=config[:reload])
helper_paths.each do |dir|
Dir[dir].each { |file| require file }
::Dir[dir].each { |file| require file }
end
search_domain = model_names ? [['model', 'in', model_names]] : []
models_records = read_model_data(search_domain)
Expand Down Expand Up @@ -143,7 +151,7 @@ def define_openerp_model(options) #TODO param to tell if we define constants or
def logger; Ooor.logger; end

def helper_paths
[File.dirname(__FILE__) + '/helpers/*', *@config[:helper_paths]]
[File.dirname(__FILE__) + '/helpers/*', *config[:helper_paths]]
end

def class_name_from_model_key(model_key)
Expand Down
5 changes: 1 addition & 4 deletions lib/ooor/session_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ module Ooor
autoload :SecureRandom, 'securerandom'
# The SessionHandler allows to retrieve a session with its loaded proxies to OpenERP
class SessionHandler
def connection_spec(config)
HashWithIndifferentAccess.new(config.slice(:url, :database, :username, :password, :scope_prefix, :helper_paths)) #TODO should really password be part of it?
end

def noweb_session_spec(config)
HashWithIndifferentAccess.new(config.slice(:url, :database, :username)).map{|k, v| v}.join('-')
"#{config[:url]}-#{config[:database]}-#{config[:username]}"
end

def retrieve_session(config, id=nil, web_session={})
Expand Down

0 comments on commit 156ac65

Please sign in to comment.