From 72f5e6efe9ea18711967dfb8c16432211f4fb3be Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Wed, 1 Jan 2025 06:13:54 +0300 Subject: [PATCH] Output active blocks list --- .../user_blocks/active_lists_controller.rb | 5 ++++- .../api/user_blocks/_user_block.json.jbuilder | 20 +++++++++---------- .../api/user_blocks/_user_block.xml.builder | 3 ++- .../active_lists/show.json.jbuilder | 5 +++++ .../user_blocks/active_lists/show.xml.builder | 5 +++++ app/views/api/user_blocks/show.json.jbuilder | 4 +++- .../active_lists_controller_test.rb | 9 +++++++++ 7 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 app/views/api/user_blocks/active_lists/show.json.jbuilder create mode 100644 app/views/api/user_blocks/active_lists/show.xml.builder diff --git a/app/controllers/api/user_blocks/active_lists_controller.rb b/app/controllers/api/user_blocks/active_lists_controller.rb index 7c464190a3..6dca5eaa36 100644 --- a/app/controllers/api/user_blocks/active_lists_controller.rb +++ b/app/controllers/api/user_blocks/active_lists_controller.rb @@ -8,7 +8,10 @@ class ActiveListsController < ApiController before_action :set_request_formats - def show; end + def show + @user_blocks = current_user.blocks.active.order(:id => :desc) + @skip_reason = true + end private diff --git a/app/views/api/user_blocks/_user_block.json.jbuilder b/app/views/api/user_blocks/_user_block.json.jbuilder index 3288dd6ada..2e65b52dab 100644 --- a/app/views/api/user_blocks/_user_block.json.jbuilder +++ b/app/views/api/user_blocks/_user_block.json.jbuilder @@ -1,13 +1,11 @@ -json.user_block do - json.id user_block.id - json.created_at user_block.created_at.xmlschema - json.updated_at user_block.updated_at.xmlschema - json.ends_at user_block.ends_at.xmlschema - json.needs_view user_block.needs_view +json.id user_block.id +json.created_at user_block.created_at.xmlschema +json.updated_at user_block.updated_at.xmlschema +json.ends_at user_block.ends_at.xmlschema +json.needs_view user_block.needs_view - json.user :uid => user_block.user_id, :user => user_block.user.display_name - json.creator :uid => user_block.creator_id, :user => user_block.creator.display_name - json.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker +json.user :uid => user_block.user_id, :user => user_block.user.display_name +json.creator :uid => user_block.creator_id, :user => user_block.creator.display_name +json.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker - json.reason user_block.reason -end +json.reason user_block.reason unless @skip_reason diff --git a/app/views/api/user_blocks/_user_block.xml.builder b/app/views/api/user_blocks/_user_block.xml.builder index a41dc56d7f..288e896636 100644 --- a/app/views/api/user_blocks/_user_block.xml.builder +++ b/app/views/api/user_blocks/_user_block.xml.builder @@ -10,5 +10,6 @@ xml.user_block(attrs) do xml.user :uid => user_block.user_id, :user => user_block.user.display_name xml.creator :uid => user_block.creator_id, :user => user_block.creator.display_name xml.revoker :uid => user_block.revoker_id, :user => user_block.revoker.display_name if user_block.revoker - xml.reason user_block.reason + + xml.reason user_block.reason unless @skip_reason end diff --git a/app/views/api/user_blocks/active_lists/show.json.jbuilder b/app/views/api/user_blocks/active_lists/show.json.jbuilder new file mode 100644 index 0000000000..aaf1c21195 --- /dev/null +++ b/app/views/api/user_blocks/active_lists/show.json.jbuilder @@ -0,0 +1,5 @@ +json.partial! "api/root_attributes" + +json.user_blocks do + json.array! @user_blocks, :partial => "api/user_blocks/user_block", :as => :user_block +end diff --git a/app/views/api/user_blocks/active_lists/show.xml.builder b/app/views/api/user_blocks/active_lists/show.xml.builder new file mode 100644 index 0000000000..907392646a --- /dev/null +++ b/app/views/api/user_blocks/active_lists/show.xml.builder @@ -0,0 +1,5 @@ +xml.instruct! + +xml.osm(OSM::API.new.xml_root_attributes) do |osm| + osm << (render(:partial => "api/user_blocks/user_block", :collection => @user_blocks) || "") +end diff --git a/app/views/api/user_blocks/show.json.jbuilder b/app/views/api/user_blocks/show.json.jbuilder index 6cfc0ded50..a90b7cd195 100644 --- a/app/views/api/user_blocks/show.json.jbuilder +++ b/app/views/api/user_blocks/show.json.jbuilder @@ -1,3 +1,5 @@ json.partial! "api/root_attributes" -json.partial! @user_block +json.user_block do + json.partial! @user_block +end diff --git a/test/controllers/api/user_blocks/active_lists_controller_test.rb b/test/controllers/api/user_blocks/active_lists_controller_test.rb index efca9b6c63..6856dca8d2 100644 --- a/test/controllers/api/user_blocks/active_lists_controller_test.rb +++ b/test/controllers/api/user_blocks/active_lists_controller_test.rb @@ -50,6 +50,10 @@ def test_show get api_user_blocks_active_list_path, :headers => user_auth_header assert_response :success + assert_dom "user_block", :count => 2 do |dom_blocks| + assert_dom dom_blocks[0], "> @id", block1.id.to_s + assert_dom dom_blocks[1], "> @id", block0.id.to_s + end end def test_show_json @@ -63,6 +67,11 @@ def test_show_json get api_user_blocks_active_list_path(:format => "json"), :headers => user_auth_header assert_response :success + js = ActiveSupport::JSON.decode(@response.body) + assert_not_nil js + assert_equal 2, js["user_blocks"].count + assert_equal block1.id, js["user_blocks"][0]["id"] + assert_equal block0.id, js["user_blocks"][1]["id"] end end end