[yast-commit] <rest-service> master : language: migrate to BaseModel and various fix
ref: refs/heads/master commit 83f51c45ac623be6971a491b3e03a9b7605e91e2 Author: Josef Reidinger <jreidinger@suse.cz> Date: Fri Dec 18 11:40:39 2009 +0100 language: migrate to BaseModel and various fix migrate language to BaseModel remove onliners views check permissions before checking argument to have same behavior as others clean code temporaly remove available languages caching (need measure before dirtying code) unify naming of current language --- .../app/controllers/language_controller.rb | 16 +++-- plugins/language/app/models/language.rb | 66 +++++--------------- plugins/language/app/views/language/show.html.erb | 6 +- plugins/language/app/views/language/show.json.erb | 1 - plugins/language/app/views/language/show.xml.erb | 1 - .../test/functional/language_controller_test.rb | 53 +++++----------- plugins/language/test/test_helper.rb | 1 + plugins/language/test/unit/language_test.rb | 21 +----- 8 files changed, 47 insertions(+), 118 deletions(-) diff --git a/plugins/language/app/controllers/language_controller.rb b/plugins/language/app/controllers/language_controller.rb index cc47f32..938aca2 100644 --- a/plugins/language/app/controllers/language_controller.rb +++ b/plugins/language/app/controllers/language_controller.rb @@ -12,20 +12,16 @@ class LanguageController < ApplicationController # Actualizes language settings. Requires write permissions for language YaPI. def update + yapi_perm_check "language.write" if params.has_key?(:language) - yapi_perm_check "language.write" - @language = Language.new - @language.language = params[:language][:current] - @language.utf8 = params[:language][:utf8] - @language.rootlocale = params[:language][:rootlocale] + @language = Language.new params[:language] @language.save - else logger.warn("No argument to update") raise InvalidParameters.new :language => "Missing" end - render :show + show end # See update @@ -38,6 +34,12 @@ class LanguageController < ApplicationController yapi_perm_check "language.read" @language = Language.find + + respond_to do |format| + format.html { render :show } + format.xml { render :xml => systemtime.to_xml( :dasherize => false ) } + format.json { render :json => systemtime.to_json( :dasherize => false ) } + end end end diff --git a/plugins/language/app/models/language.rb b/plugins/language/app/models/language.rb index 5121530..08eef42 100644 --- a/plugins/language/app/models/language.rb +++ b/plugins/language/app/models/language.rb @@ -3,57 +3,49 @@ # Provides set and gets resources from YaPI language module. # Main goal is handle YaPI specific calls and data formats. Provides cleaned # and well defined data. -class Language - - # cache available languages as it is change only rarely - @@available = {} +class Language < BaseModel::Base # current language - attr_accessor :language + attr_accessor :current # utf8 settings ("true" or "false") attr_accessor :utf8 # root locale settings ("true" or "false" or "ctype") # see yast-country documentation attr_accessor :rootlocale + # available languages on target machine. + # + # It is a array of hashs. Each hash contain keys id ( of language, + # value for language variable) and name (which is localized language + # name in UTF8. + attr_reader :available private # Creates argument for dbus call which specify what data is requested. - # Available languages is cached so request it only if it is necessary. # return:: hash with requested keys def self.create_read_question #:doc: - ret = { + { "current" => "true", "utf8" => "true", - "rootlang" => "true" + "rootlang" => "true", + "languages" => "true" } - if @@available.nil? || @@available.empty? - ret["languages"]= "true" - end - return ret end public # Parses response from dbus YaPI call # response:: response from dbus def parse_response(response) #:doc: - @language = response["current"] + @current = response["current"] @utf8 = response["utf8"] @rootlocale = response["rootlang"] if response["languages"] - @@available = response["languages"] + @available = response["languages"].collect { |k,v| { :id => k, :name => v[0] } } # v array => [native UTF8, native ascii, UTF8 extension, english name] end end - # Getter for available static field - def available - @@available - end - # fills language instance with data from YaPI. - # - # +warn+: Doesn't take any parameters. - def Language.find + def Language.find(*args) ret = Language.new ret.parse_response YastService.Call("YaPI::LANGUAGE::Read",create_read_question) return ret @@ -62,37 +54,9 @@ class Language # Saves data from model to system via YaPI def save settings = {} - settings["current"] = @language if @language #set only if value is passed + settings["current"] = @current if @current #set only if value is passed settings["utf8"] = @utf8 if @utf8 #set only if value is passed settings["rootlang"] = @rootlocale if @rootlocale #set only if value is passed YastService.Call("YaPI::LANGUAGE::Write",settings) end - - def initialize - end - - def to_xml( options = {} ) - xml = options[:builder] ||= Builder::XmlMarkup.new(options) - xml.instruct! unless options[:skip_instruct] - - xml.language do - xml.tag!(:current, @language ) - xml.tag!(:utf8, @utf8) - xml.tag!(:rootlocale, @rootlocale ) - xml.available({:type => "array"}) do - @@available.each do |k,v| - xml.language do - xml.tag!(:id, k) - xml.tag!(:name, v[0]) # [native UTF8, native ascii, UTF8 extension, english name] - end - end - end - end - end - - def to_json( options = {} ) - hash = Hash.from_xml(to_xml()) - return hash.to_json - end - end diff --git a/plugins/language/app/views/language/show.html.erb b/plugins/language/app/views/language/show.html.erb index 8e37a72..6300ffb 100644 --- a/plugins/language/app/views/language/show.html.erb +++ b/plugins/language/app/views/language/show.html.erb @@ -1,10 +1,10 @@ -Locale (user): <%= @language.language -%><br/> +Locale (user): <%= @language.current -%><br/> Locale (root): <%= @language.rootlocale -%><br/> UTF-8?: <%= @language.utf8 -%><br/> <p/> Available: <ul> -<% @language.available.each do |k,v| -%> - <li><%= "#{k}: #{v.inspect}" -%></li> +<% @language.available.each do |v| -%> + <li><%= "#{v[:id]}: #{v[:name]}" -%></li> <% end -%> </ul> diff --git a/plugins/language/app/views/language/show.json.erb b/plugins/language/app/views/language/show.json.erb deleted file mode 100644 index 391a4e0..0000000 --- a/plugins/language/app/views/language/show.json.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @language.to_json -%> \ No newline at end of file diff --git a/plugins/language/app/views/language/show.xml.erb b/plugins/language/app/views/language/show.xml.erb deleted file mode 100644 index 8fc40f8..0000000 --- a/plugins/language/app/views/language/show.xml.erb +++ /dev/null @@ -1 +0,0 @@ -<%= @language.to_xml -%> \ No newline at end of file diff --git a/plugins/language/test/functional/language_controller_test.rb b/plugins/language/test/functional/language_controller_test.rb index 160043d..370fc29 100644 --- a/plugins/language/test/functional/language_controller_test.rb +++ b/plugins/language/test/functional/language_controller_test.rb @@ -5,6 +5,7 @@ require File.expand_path( File.join("test","plugin_basic_tests"), RailsParent.pa class LanguageControllerTest < ActionController::TestCase fixtures :accounts + include PluginBasicTests def setup # @model_class = Language @@ -12,50 +13,26 @@ class LanguageControllerTest < ActionController::TestCase @request = ActionController::TestRequest.new # http://railsforum.com/viewtopic.php?id=1719 @request.session[:account_id] = 1 # defined in fixtures - @data = DATA - - Language.stubs(:available).returns( - { 'rootlang' => 'ctype', - 'languages' => { - 'en_US' => [ - 'English (US)', - 'English (US)', - '.UTF-8', - '', - 'English (US)' ], - 'fr_FR' => [ - 'Français', - 'Francais', - '.UTF-8', - '@euro', - 'French' ], - 'de_DE' => [ - 'Deutsch', - 'Deutsch', - '.UTF-8', - '@euro', - 'German' ] - }, - 'current' => 'en_US', - 'utf8' => 'true' - } ) - - @lang = Language.new - @lang.language = "de_DE" - @lang.rootlocale = "false" - @lang.utf8 = "false" - - Language.stubs(:find).returns(@lang) - + lang = Language.new(DATA[:language]) + lang.instance_variable_set(:'@available',AVAILABLE) #set available, but it doesn't has accessor, so it is little tricky + Language.stubs(:find).returns(lang) end - include PluginBasicTests DATA = {:language => { :current => "cs_CZ", :utf8 => "true", - :rootlocale => "false" + :rootlocale => "false", }} + AVAILABLE = [ + { :id => 'en_US', + :name => 'English (US)'}, + { :id => 'fr_FR', + :name => 'Français' }, + { :id => 'de_DE', + :name => 'Deutsch', + } + ] def test_update Language.any_instance.stubs(:save) @@ -73,7 +50,7 @@ class LanguageControllerTest < ActionController::TestCase assert_response :success lang = assigns(:language) assert lang - assert_equal lang.language, DATA[:language][:current] + assert_equal lang.current, DATA[:language][:current] assert_equal lang.utf8, DATA[:language][:utf8] assert_equal lang.rootlocale, DATA[:language][:rootlocale] end diff --git a/plugins/language/test/test_helper.rb b/plugins/language/test/test_helper.rb index 54d2979..c0b0373 100644 --- a/plugins/language/test/test_helper.rb +++ b/plugins/language/test/test_helper.rb @@ -5,6 +5,7 @@ require File.join(File.dirname(__FILE__), '..', 'config', 'rails_parent') require File.expand_path( File.join("config","environment"), RailsParent.parent ) # then enable testing, this will get the routing right require 'test_help' +require 'mocha' class ActiveSupport::TestCase # Transactional fixtures accelerate your tests by wrapping each test method diff --git a/plugins/language/test/unit/language_test.rb b/plugins/language/test/unit/language_test.rb index 22d92ae..ca23c04 100644 --- a/plugins/language/test/unit/language_test.rb +++ b/plugins/language/test/unit/language_test.rb @@ -30,23 +30,12 @@ class LanguageTest < ActiveSupport::TestCase "languages" => "true" } - ARGS_PARTIAL = { - "current"=> "true", - "utf8"=> "true", - "rootlang"=> "true" - } - RESPONSE_FULL = { "current" => "en_US", "utf8" => "true", "rootlang" => "ctype", "languages" => LANGUAGES } - RESPONSE_PARTIAL = { - "current" => "en_US", - "utf8" => "true", - "rootlang" => "ctype" } - ARGS_WRITE = { "current" => "de_DE", "utf8" => "false", @@ -54,7 +43,6 @@ class LanguageTest < ActiveSupport::TestCase def setup YastService.stubs(:Call).with("YaPI::LANGUAGE::Read",ARGS_FULL).returns(RESPONSE_FULL) - YastService.stubs(:Call).with("YaPI::LANGUAGE::Read",ARGS_PARTIAL).returns(RESPONSE_PARTIAL) YastService.stubs(:Call).with("YaPI::LANGUAGE::Write",ARGS_WRITE).returns(true) end @@ -62,15 +50,14 @@ class LanguageTest < ActiveSupport::TestCase lang = Language.find # pp Language.available - assert_equal("en_US", lang.language) + assert_equal("en_US", lang.current) assert_equal("ctype", lang.rootlocale) assert_equal("true", lang.utf8) - assert_equal(LANGUAGES,lang.available) end def test_setter lang = Language.find - lang.language = "de_DE" + lang.current = "de_DE" lang.rootlocale = "false" lang.utf8 = "false" lang.save @@ -78,7 +65,7 @@ class LanguageTest < ActiveSupport::TestCase def test_xml lang = Language.find - lang.language = "de_DE" + lang.current = "de_DE" lang.rootlocale = "false" lang.utf8 = "false" @@ -98,7 +85,7 @@ class LanguageTest < ActiveSupport::TestCase def test_json lang = Language.find - lang.language = "de_DE" + lang.current = "de_DE" lang.rootlocale = "false" lang.utf8 = "false" -- To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org For additional commands, e-mail: yast-commit+help@opensuse.org
participants (1)
-
Josef Reidinger