[yast-commit] <web-client> jr-yastmodel : fix collection name problem by hack

ref: refs/heads/jr-yastmodel commit d39e19b0d6921aca4f817372df1717c4c55a3485 Author: Josef Reidinger <jreidinger@suse.cz> Date: Mon Dec 21 11:07:43 2009 +0100 fix collection name problem by hack --- webclient/lib/yast_model/base.rb | 83 ++++++++++++++++++-------------- webclient/test/unit/yast_model_test.rb | 13 +++-- 2 files changed, 54 insertions(+), 42 deletions(-) diff --git a/webclient/lib/yast_model/base.rb b/webclient/lib/yast_model/base.rb index 6e640d5..98af103 100644 --- a/webclient/lib/yast_model/base.rb +++ b/webclient/lib/yast_model/base.rb @@ -1,46 +1,55 @@ module YastModel class Base < ActiveResource::Base + self.logger = Rails.logger #avoid problem with nil logger - def self.model_interface(i) - @@interface = i.to_sym - end + class << self + def model_interface(i) + @@interface = i.to_sym + end - def self.site - ret = super - if ret.nil? || ret != URI.parse(YaST::ServiceResource::Session.site) || - password != YaST::ServiceResource::Session.auth_token - set_site - end - return ret - end + alias_method :site_orig, :site + def site + ret = site_orig + if ret.nil? || ret != URI.parse(YaST::ServiceResource::Session.site) || + password != YaST::ServiceResource::Session.auth_token + set_site + end + return site_orig + end - def self.set_site - self.site = YaST::ServiceResource::Session.site - self.password = YaST::ServiceResource::Session.auth_token - Resource.site = "#{self.site}/" #resource has constant prefix to allow introspect -#FIXME not thread save - Rails.logger.debug "read interface to #{@interface.to_s}" - resource = Resource.find(:all).find { |r| r.interface.to_sym == @@interface.to_sym } -#TODO throw exception if not find - prefix, sep, collection_name = resource.href.rpartition('/') - prefix += '/' - prefix = Pathname.new prefix - end + def set_site + site = YaST::ServiceResource::Session.site + self.site = site + self.password = YaST::ServiceResource::Session.auth_token + YastModel::Resource.site = "#{site}/" #resource has constant prefix to allow introspect + #FIXME not thread save + Rails.logger.debug "read interface to #{@@interface.to_s}" + resource = YastModel::Resource.find(:all).find { |r| r.interface.to_sym == @@interface.to_sym } + #TODO throw exception if not find + p, sep, @@collection_name = resource.href.rpartition('/') + p += '/' + self.prefix = p + end -#fix ARs broken singleton - def self.find_one(options) - case from = options[:from] - when Symbol - instantiate_record(get(from, options[:params])) - when String - path = "#{from}#{query_string(options[:params])}" - instantiate_record(connection.get(path, headers)) - else - prefix_options, query_options = split_options(options[:params]) - path = collection_path(prefix_options, query_options) - instantiate_record( (connection.get(path, headers)), prefix_options ) - end - end +#redefine collection name as it is little broken see http://lists.opensuse.org/opensuse-ruby/2009-12/msg00008.html + def collection_name + @@collection_name + end + #fix ARs broken singleton + def find_one(options) + case from = options[:from] + when Symbol + instantiate_record(get(from, options[:params])) + when String + path = "#{from}#{query_string(options[:params])}" + instantiate_record(connection.get(path, headers)) + else + prefix_options, query_options = split_options(options[:params]) + path = self.collection_path(prefix_options, query_options) + instantiate_record( (connection.get(path, headers)), prefix_options ) + end + end + end end end diff --git a/webclient/test/unit/yast_model_test.rb b/webclient/test/unit/yast_model_test.rb index 03cb86f..d013678 100644 --- a/webclient/test/unit/yast_model_test.rb +++ b/webclient/test/unit/yast_model_test.rb @@ -8,8 +8,7 @@ PERMISSION_RESPONSE= <<EOF <permission> <granted type="boolean">true</granted> <id>org.opensuse.yast.modules.yapi.ntp.synchronize</id> - <:w - /permission> + </permission> </permissions> EOF @@ -41,16 +40,20 @@ def setup YaST::ServiceResource::Session.auth_token = "1234" end -class Test < YastModel::Base +class TestModel < YastModel::Base model_interface :'org.opensuse.yast.modules.test' end def test_model - assert Test.site.to_s.include?('localhost'), "site doesn't include localhost : #{Test.site}" + assert TestModel.site.to_s.include?('localhost'), "site doesn't include localhost : #{TestModel.site}" + puts YastModel::Base.collection_name + puts TestModel.collection_name + assert_equal "test",TestModel.collection_name + assert_equal "/",TestModel.prefix end def test_find - test = Test.find :one + test = TestModel.find :one assert "test",test.arg1 end -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
Josef Reidinger