From b89416e1254486012f8073cdfef0c74a171880fa Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Thu, 8 Dec 2011 00:46:01 +0200 Subject: [PATCH 1/2] Add project name and bs_id filters for the build_lists list. Refactor and code cleanup to close #2 --- app/models/build_list.rb | 1 + app/models/build_list/filter.rb | 100 ++++++++++++------------ app/views/build_lists/_filter.html.haml | 30 +++---- config/locales/ru.yml | 2 + 4 files changed, 69 insertions(+), 64 deletions(-) diff --git a/app/models/build_list.rb b/app/models/build_list.rb index c8648d427..e8aacc4b2 100644 --- a/app/models/build_list.rb +++ b/app/models/build_list.rb @@ -76,6 +76,7 @@ class BuildList < ActiveRecord::Base where(["notified_at <= ?", end_date]) end } + scope :scoped_to_project_name, lambda {|project_name| joins(:project).where('projects.name LIKE ?', "%#{project_name}%")} serialize :additional_repos diff --git a/app/models/build_list/filter.rb b/app/models/build_list/filter.rb index f974c3672..6340dc1b7 100644 --- a/app/models/build_list/filter.rb +++ b/app/models/build_list/filter.rb @@ -1,32 +1,30 @@ class BuildList::Filter - def initialize(project, options = {}) @project = project - set_options(options) end def find - if @project.nil? - build_lists = BuildList.recent + build_lists = @project ? @project.build_lists : BuildList.scoped + + if @options[:bs_id] + build_lists = build_lists.where(:bs_id => @options[:bs_id]) else - build_lists = @project.build_lists.recent + build_lists = build_lists.for_status(@options[:status]) if @options[:status] + build_lists = build_lists.scoped_to_arch(@options[:arch_id]) if @options[:arch_id] + build_lists = build_lists.scoped_to_project_version(@options[:project_version]) if @options[:project_version] + build_lists = build_lists.scoped_to_is_circle(@options[:is_circle]) if @options[:is_circle].present? + build_lists = build_lists.scoped_to_project_name(@options[:project_name]) if @options[:project_name] + + if @options[:created_at_start] || @options[:created_at_end] + build_lists = build_lists.for_creation_date_period(@options[:created_at_start], @options[:created_at_end]) + end + if @options[:notified_at_start] || @options[:notified_at_end] + build_lists = build_lists.for_notified_date_period(@options[:notified_at_start], @options[:notified_at_end]) + end end - build_lists = build_lists.for_status(@options[:status]) if @options[:status] - build_lists = build_lists.scoped_to_arch(@options[:arch_id]) if @options[:arch_id] - build_lists = build_lists.scoped_to_project_version(@options[:project_version]) if @options[:project_version] - build_lists = build_lists.scoped_to_is_circle(@options[:is_circle]) if @options[:is_circle].present? - - if @options[:created_at_start] || @options[:created_at_end] - build_lists = build_lists.for_creation_date_period(@options[:created_at_start], @options[:created_at_end]) - end - - if @options[:notified_at_start] || @options[:notified_at_end] - build_lists = build_lists.for_notified_date_period(@options[:notified_at_start], @options[:notified_at_end]) - end - - build_lists + build_lists.recent end def respond_to?(name) @@ -39,36 +37,40 @@ class BuildList::Filter end private - def set_options(options) - @options = HashWithIndifferentAccess.new(options.reverse_merge({ - :status => nil, - :created_at_start => nil, - :created_at_end => nil, - :notified_at_start => nil, - :notified_at_end => nil, - :arch_id => nil, - :is_circle => nil, - :project_version => nil - })) - @options[:status] = @options[:status].present? ? @options[:status].to_i : nil - @options[:created_at_start] = build_date_from_params(:created_at_start, @options) - @options[:created_at_end] = build_date_from_params(:created_at_end, @options) - @options[:notified_at_start] = build_date_from_params(:notified_at_start, @options) - @options[:notified_at_end] = build_date_from_params(:notified_at_end, @options) - @options[:project_version] = @options[:project_version].present? ? @options[:project_version] : nil - @options[:arch_id] = @options[:arch_id].present? ? @options[:arch_id].to_i : nil - @options[:is_circle] = @options[:is_circle].present? ? @options[:is_circle] == "1" : nil + def set_options(options) + @options = HashWithIndifferentAccess.new(options.reverse_merge({ + :status => nil, + :created_at_start => nil, + :created_at_end => nil, + :notified_at_start => nil, + :notified_at_end => nil, + :arch_id => nil, + :is_circle => nil, + :project_version => nil, + :bs_id => nil, + :project_name => nil + })) + + @options[:status] = @options[:status].present? ? @options[:status].to_i : nil + @options[:created_at_start] = build_date_from_params(:created_at_start, @options) + @options[:created_at_end] = build_date_from_params(:created_at_end, @options) + @options[:notified_at_start] = build_date_from_params(:notified_at_start, @options) + @options[:notified_at_end] = build_date_from_params(:notified_at_end, @options) + @options[:project_version] = @options[:project_version].presence + @options[:arch_id] = @options[:arch_id].present? ? @options[:arch_id].to_i : nil + @options[:is_circle] = @options[:is_circle].present? ? @options[:is_circle] == "1" : nil + @options[:bs_id] = @options[:bs_id].presence + @options[:project_name] = @options[:project_name].presence + end + + def build_date_from_params(field_name, params) + if params["#{field_name}(1i)"].present? || params["#{field_name}(2i)"].present? || params["#{field_name}(3i)"].present? + Date.civil((params["#{field_name}(1i)"].presence || Date.today.year).to_i, + (params["#{field_name}(2i)"].presence || Date.today.mohth).to_i, + (params["#{field_name}(3i)"].presence || Date.today.day).to_i) + else + nil end - - def build_date_from_params(field_name, params) - if params["#{field_name}(1i)"].present? || params["#{field_name}(2i)"].present? || params["#{field_name}(3i)"].present? - Date.civil((params["#{field_name}(1i)"].presence || Date.today.year).to_i, - (params["#{field_name}(2i)"].presence || Date.today.mohth).to_i, - (params["#{field_name}(3i)"].presence || Date.today.day).to_i) - else - nil - end - end - + end end \ No newline at end of file diff --git a/app/views/build_lists/_filter.html.haml b/app/views/build_lists/_filter.html.haml index 08a35245e..366754f79 100644 --- a/app/views/build_lists/_filter.html.haml +++ b/app/views/build_lists/_filter.html.haml @@ -3,48 +3,48 @@ = form_for :filter, :url => @action_url, :html => { :method => :get, :class => :form } do |f| .columns.wat-cf .column.left - .group = f.label :status, t("activerecord.attributes.build_list.status"), :class => :label = f.select :status, BuildList::STATUSES.collect{|status| [BuildList.human_status(status), status]}, :include_blank => true, :selected => @filter.status - .group = f.label :arch_id, t("activerecord.attributes.build_list.arch"), :class => :label = f.select :arch_id, @arches.collect{|arch| [arch.name, arch.id]}, :include_blank => true, :selected => @filter.arch_id - .column.right - if @project_versions .group = f.label :project_version, t("activerecord.attributes.build_list.project_version"), :class => :label = f.select :project_version, @project_versions, :include_blank => true, :selected => @filter.project_version - .group = f.label :is_circle, t("activerecord.attributes.build_list.is_circle"), :class => :label = f.select :is_circle, [[t("layout.yes_"), 1], [t("layout.no_"), 0]], :include_blank => true, :selected => @filter.is_circle.present? ? (@filter.is_circle ? "1" : "0") : nil - .columns.wat-cf .column.left - .group = f.label :status, t("layout.build_lists.created_at_start"), :class => :label - = date_select("filter", :created_at_start, :include_blank => true, :selected => @filter.created_at_start) - + = f.date_select(:created_at_start, :include_blank => true, :selected => @filter.created_at_start) .group = f.label :status, t("layout.build_lists.notified_at_start"), :class => :label - = date_select("filter", :notified_at_start, :include_blank => true, :selected => @filter.notified_at_start) - + = f.date_select(:notified_at_start, :include_blank => true, :selected => @filter.notified_at_start) .column.right - .group = f.label :status, t("layout.build_lists.created_at_end"), :class => :label - = date_select("filter", :created_at_end, :include_blank => true, :selected => @filter.created_at_end) - + = f.date_select(:created_at_end, :include_blank => true, :selected => @filter.created_at_end) .group = f.label :status, t("layout.build_lists.notified_at_end"), :class => :label - = date_select("filter", :notified_at_end, :include_blank => true, :selected => @filter.notified_at_end) + = f.date_select(:notified_at_end, :include_blank => true, :selected => @filter.notified_at_end) - .grou.navform.wat-cf + .columns.wat-cf + .column.left + .group + = f.label :status, t("layout.build_lists.bs_id"), :class => :label + = f.text_field :bs_id, :class => :text_field + .column.right + .group + = f.label :status, t("layout.build_lists.project_name"), :class => :label + = f.text_field :project_name, :class => :text_field + + .group.navform.wat-cf %button.button{ :type => "submit" } = image_tag("web-app-theme/icons/tick.png", :alt => "Save") = t("layout.search") \ No newline at end of file diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 415a50305..3d3f379ad 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -279,6 +279,8 @@ ru: created_at_end: "Время постановки на сборку по:" notified_at_start: "Время последнего обновления от BS с:" notified_at_end: "Время последнего обновления от BS по:" + bs_id: 'Id в ядре' + project_name: 'Название проекта' bs_id_not_set: Id еще не присвоен items_header: Элементы сборки no_items_data: Данных нет From 0ca7c5d451f15bdc0af4dbe60884dede1a60b123 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Thu, 8 Dec 2011 20:51:25 +0200 Subject: [PATCH 2/2] Rename bs_id label and key. Stub XML RPC to raise exception and fix spec. Write specs for build_lists filters. Refs #2 --- app/views/build_lists/_filter.html.haml | 2 +- config/environments/test.rb | 2 +- config/locales/ru.yml | 2 +- lib/stub_xml_rpc.rb | 1 - .../build_lists_controller_spec.rb | 33 ++++++++++++++++--- spec/factories/build_list_factory.rb | 6 +++- 6 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app/views/build_lists/_filter.html.haml b/app/views/build_lists/_filter.html.haml index 366754f79..3b6994cfe 100644 --- a/app/views/build_lists/_filter.html.haml +++ b/app/views/build_lists/_filter.html.haml @@ -37,7 +37,7 @@ .columns.wat-cf .column.left .group - = f.label :status, t("layout.build_lists.bs_id"), :class => :label + = f.label :status, t("layout.build_lists.bs_id_search"), :class => :label = f.text_field :bs_id, :class => :text_field .column.right .group diff --git a/config/environments/test.rb b/config/environments/test.rb index 27fdd7bed..8bff1dc49 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -35,4 +35,4 @@ Rosa::Application.configure do config.active_support.deprecation = :stderr end -require 'stub_xml_rpc' +require 'stub_xml_rpc' # TODO stub XML calls through stubbers diff --git a/config/locales/ru.yml b/config/locales/ru.yml index 3d3f379ad..3819d314f 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -279,7 +279,7 @@ ru: created_at_end: "Время постановки на сборку по:" notified_at_start: "Время последнего обновления от BS с:" notified_at_end: "Время последнего обновления от BS по:" - bs_id: 'Id в ядре' + bs_id_search: 'Поиск по Id' project_name: 'Название проекта' bs_id_not_set: Id еще не присвоен items_header: Элементы сборки diff --git a/lib/stub_xml_rpc.rb b/lib/stub_xml_rpc.rb index 1a5bb33aa..8f4ab315f 100644 --- a/lib/stub_xml_rpc.rb +++ b/lib/stub_xml_rpc.rb @@ -6,7 +6,6 @@ module XMLRPC case when args.first == 'get_status' {'client_count' => 1, 'count_new_task' => 2, 'count_build_task' => 3} - # raise Timeout::Error else; 0 end end diff --git a/spec/controllers/build_lists_controller_spec.rb b/spec/controllers/build_lists_controller_spec.rb index beb60af42..a8f00c9e5 100644 --- a/spec/controllers/build_lists_controller_spec.rb +++ b/spec/controllers/build_lists_controller_spec.rb @@ -2,8 +2,6 @@ require 'spec_helper' describe BuildListsController do context 'crud' do - # let(:build_list) { Factory(:build_list) } - context 'for guest' do it 'should not be able to perform all action' do get :all @@ -24,10 +22,37 @@ describe BuildListsController do before(:each) { set_session_for Factory(:admin) } it "should be able to perform all action without exception" do + any_instance_of(XMLRPC::Client) do |xml_rpc| + stub(xml_rpc).call do |args| + raise Timeout::Error + end + end get :all - assigns[:build_server_status].should == {} # TODO stub to isolate + assigns[:build_server_status].should == {} response.should be_success - end + end + end + end + + context 'filter' do + let(:build_list1) { Factory(:build_list_core) } + let(:build_list2) { Factory(:build_list_core) } + let(:build_list3) { Factory(:build_list_core) } + before(:each) { set_session_for Factory(:admin) } + + it 'should filter by bs_id' do + get :all, :filter => {:bs_id => build_list1.bs_id, :project_name => 'fdsfdf', :any_other_field => 'do not matter'} + assigns[:build_lists].should include(build_list1) + assigns[:build_lists].should_not include(build_list2) + assigns[:build_lists].should_not include(build_list3) + end + + it 'should filter by project_name' do + # Project.where(:id => build_list2.project.id).update_all(:name => 'project_name') + get :all, :filter => {:project_name => build_list2.project.name} + assigns[:build_lists].should_not include(build_list1) + assigns[:build_lists].should include(build_list2) + assigns[:build_lists].should_not include(build_list3) end end diff --git a/spec/factories/build_list_factory.rb b/spec/factories/build_list_factory.rb index 359610e28..a4e1aa39a 100644 --- a/spec/factories/build_list_factory.rb +++ b/spec/factories/build_list_factory.rb @@ -6,4 +6,8 @@ Factory.define(:build_list) do |p| p.project_version "1.0" p.build_requires true p.update_type 'security' -end \ No newline at end of file +end + +Factory.define(:build_list_core, :parent => :build_list) do |p| + p.bs_id { Factory.next(:integer) } +end