From 2695e19e2fc2bcffb8200b81e1f190c98ed6ff90 Mon Sep 17 00:00:00 2001 From: decabeza Date: Tue, 29 Jan 2019 17:54:02 +0100 Subject: [PATCH 1/6] Fix hound warnings --- app/helpers/admin_helper.rb | 3 ++- app/models/site_customization/page.rb | 12 +++++----- app/models/widget/card.rb | 2 +- .../admin/legislation/processes_spec.rb | 22 +++++++++---------- spec/helpers/legislation_helper_spec.rb | 2 +- spec/models/legislation/process_spec.rb | 3 ++- 6 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 06e8c3eb7..239c2a616 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -54,7 +54,8 @@ module AdminHelper end def menu_customization? - ["pages", "banners", "information_texts"].include?(controller_name) || menu_homepage? || menu_pages? + ["pages", "banners", "information_texts"].include?(controller_name) || + menu_homepage? || menu_pages? end def menu_homepage? diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index 9d3c6b37e..a4bb213ad 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -1,6 +1,6 @@ class SiteCustomization::Page < ActiveRecord::Base - VALID_STATUSES = %w(draft published) - has_many :cards, class_name: 'Widget::Card', foreign_key: 'site_customization_page_id' + VALID_STATUSES = %w[draft published] + has_many :cards, class_name: "Widget::Card", foreign_key: "site_customization_page_id" translates :title, touch: true translates :subtitle, touch: true @@ -13,9 +13,11 @@ class SiteCustomization::Page < ActiveRecord::Base format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format } validates :status, presence: true, inclusion: { in: VALID_STATUSES } - scope :published, -> { where(status: 'published').order('id DESC') } - scope :with_more_info_flag, -> { where(status: 'published', more_info_flag: true).order('id ASC') } - scope :with_same_locale, -> { joins(:translations).where("site_customization_page_translations.locale": I18n.locale) } + scope :published, -> { where(status: "published").order("id DESC") } + scope :with_more_info_flag, -> { where(status: "published", + more_info_flag: true).order("id ASC") } + scope :with_same_locale, -> { joins(:translations) + .where("site_customization_page_translations.locale": I18n.locale) } def url "/#{slug}" diff --git a/app/models/widget/card.rb b/app/models/widget/card.rb index bc7da86d2..5ec0d4944 100644 --- a/app/models/widget/card.rb +++ b/app/models/widget/card.rb @@ -1,6 +1,6 @@ class Widget::Card < ActiveRecord::Base include Imageable - belongs_to :page, class_name: 'SiteCustomization::Page', foreign_key: 'site_customization_page_id' + belongs_to :page, class_name: "SiteCustomization::Page", foreign_key: "site_customization_page_id" # table_name must be set before calls to 'translates' self.table_name = "widget_cards" diff --git a/spec/features/admin/legislation/processes_spec.rb b/spec/features/admin/legislation/processes_spec.rb index e56bca81b..b5842c235 100644 --- a/spec/features/admin/legislation/processes_spec.rb +++ b/spec/features/admin/legislation/processes_spec.rb @@ -134,23 +134,23 @@ feature 'Admin legislation processes' do scenario "Create a legislation process with an image", :js do visit new_admin_legislation_process_path() - fill_in 'Process Title', with: 'An example legislation process' - fill_in 'Summary', with: 'Summary of the process' + fill_in "Process Title", with: "An example legislation process" + fill_in "Summary", with: "Summary of the process" base_date = Date.current - fill_in 'legislation_process[start_date]', with: base_date.strftime("%d/%m/%Y") - fill_in 'legislation_process[end_date]', with: (base_date + 5.days).strftime("%d/%m/%Y") - imageable_attach_new_file(create(:image), Rails.root.join('spec/fixtures/files/clippy.jpg')) + fill_in "legislation_process[start_date]", with: base_date.strftime("%d/%m/%Y") + fill_in "legislation_process[end_date]", with: (base_date + 5.days).strftime("%d/%m/%Y") + imageable_attach_new_file(create(:image), Rails.root.join("spec/fixtures/files/clippy.jpg")) - click_button 'Create process' + click_button "Create process" - expect(page).to have_content 'An example legislation process' - expect(page).to have_content 'Process created successfully' + expect(page).to have_content "An example legislation process" + expect(page).to have_content "Process created successfully" - click_link 'Click to visit' + click_link "Click to visit" - expect(page).to have_content 'An example legislation process' - expect(page).not_to have_content 'Summary of the process' + expect(page).to have_content "An example legislation process" + expect(page).not_to have_content "Summary of the process" expect(page).to have_css("img[alt='#{Legislation::Process.last.title}']") end end diff --git a/spec/helpers/legislation_helper_spec.rb b/spec/helpers/legislation_helper_spec.rb index 9d88aa8c8..627155123 100644 --- a/spec/helpers/legislation_helper_spec.rb +++ b/spec/helpers/legislation_helper_spec.rb @@ -1,4 +1,4 @@ -require 'rails_helper' +require "rails_helper" describe LegislationHelper do let(:process) { build(:legislation_process) } diff --git a/spec/models/legislation/process_spec.rb b/spec/models/legislation/process_spec.rb index c2b1c8214..274487cd6 100644 --- a/spec/models/legislation/process_spec.rb +++ b/spec/models/legislation/process_spec.rb @@ -193,7 +193,8 @@ describe Legislation::Process do expect { process1 = create(:legislation_process, background_color: "#123ghi", font_color: "#fff") process2 = create(:legislation_process, background_color: "#ffffffff", font_color: "#123") - }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Background color is invalid") + }.to raise_error(ActiveRecord::RecordInvalid, + "Validation failed: Background color is invalid") end end From 5893ed7587854631f7a4a85c438d13b1a37c5010 Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 30 Jan 2019 13:15:42 +0100 Subject: [PATCH 2/6] Create helper for legislation process header with custom colors --- app/helpers/legislation_helper.rb | 7 +++++++ app/views/legislation/processes/_header.html.erb | 4 +--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/helpers/legislation_helper.rb b/app/helpers/legislation_helper.rb index a39e9784d..d695e0400 100644 --- a/app/helpers/legislation_helper.rb +++ b/app/helpers/legislation_helper.rb @@ -41,4 +41,11 @@ module LegislationHelper def banner_color? @process.background_color.present? && @process.font_color.present? end + + def css_for_process_header + if banner_color? + "background:" + @process.background_color + ";color:" + @process.font_color + ";" + end + end + end diff --git a/app/views/legislation/processes/_header.html.erb b/app/views/legislation/processes/_header.html.erb index 607fb9cbf..67de094a9 100644 --- a/app/views/legislation/processes/_header.html.erb +++ b/app/views/legislation/processes/_header.html.erb @@ -1,6 +1,4 @@ -
- style="background:<%= process.background_color %>; color:<%= process.font_color %>;" - <% end %>> +
From 865dca85bfb191f6e3b016696891084f760c5f2b Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 30 Jan 2019 13:16:35 +0100 Subject: [PATCH 3/6] Improve layout of admin legislation process form --- .../legislation/processes/_form.html.erb | 29 +++++++++++++------ config/locales/en/admin.yml | 2 +- config/locales/es/admin.yml | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/app/views/admin/legislation/processes/_form.html.erb b/app/views/admin/legislation/processes/_form.html.erb index 1cfb699ee..86c419b07 100644 --- a/app/views/admin/legislation/processes/_form.html.erb +++ b/app/views/admin/legislation/processes/_form.html.erb @@ -213,16 +213,27 @@

<%= t("admin.legislation.processes.form.banner_title") %>

-
-
- <%= f.label :sections, t("admin.legislation.processes.form.banner_background_color") %> - <%= color_field(:process, :background_color, id: 'banner_background_color_picker') %> - <%= f.text_field :background_color, label: false, id: 'banner_background_color' %> +
+ <%= f.label :sections, t("admin.legislation.processes.form.banner_background_color") %> +
+
+ <%= color_field(:process, :background_color) %> +
+
+ <%= f.text_field :background_color, label: false %> +
-
- <%= f.label :sections, t("admin.legislation.processes.form.banner_font_color") %> - <%= color_field(:process, :font_color, id: 'banner_font_color_picker') %> - <%= f.text_field :font_color, label: false, id: 'banner_font_color' %> +
+ +
+ <%= f.label :sections, t("admin.legislation.processes.form.banner_font_color") %> +
+
+ <%= color_field(:process, :font_color) %> +
+
+ <%= f.text_field :font_color, label: false %> +
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 819a2037e..d1a9fe98c 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -438,7 +438,7 @@ en: homepage: Description homepage_description: Here you can explain the content of the process homepage_enabled: Homepage enabled - banner_title: Banner colors + banner_title: Header colors banner_background_color: Background colour banner_font_color: Font colour index: diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 50576eec7..3b82eee84 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -439,7 +439,7 @@ es: homepage: Descripción homepage_description: Aquí puedes explicar el contenido del proceso homepage_enabled: Homepage activada - banner_title: Colores del banner + banner_title: Colores del encabezado banner_background_color: Color de fondo banner_font_color: Color del texto index: From 41f9ef167d3251562dbbead09edaf7fd58fb12c6 Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 30 Jan 2019 13:17:04 +0100 Subject: [PATCH 4/6] Refactor hexadecimal color validation --- app/models/legislation/process.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/models/legislation/process.rb b/app/models/legislation/process.rb index 974546ff4..ffb1fc5e2 100644 --- a/app/models/legislation/process.rb +++ b/app/models/legislation/process.rb @@ -22,6 +22,8 @@ class Legislation::Process < ActiveRecord::Base PHASES_AND_PUBLICATIONS = %i[homepage_phase draft_phase debate_phase allegations_phase proposals_phase draft_publication result_publication].freeze + CSS_HEX_COLOR = /\A#?(?:[A-F0-9]{3}){1,2}\z/i + has_many :draft_versions, -> { order(:id) }, class_name: 'Legislation::DraftVersion', foreign_key: 'legislation_process_id', dependent: :destroy @@ -44,8 +46,8 @@ class Legislation::Process < ActiveRecord::Base validates :allegations_end_date, presence: true, if: :allegations_start_date? validates :proposals_phase_end_date, presence: true, if: :proposals_phase_start_date? validate :valid_date_ranges - validates :background_color, format: { allow_blank: true, with: /\A#?(?:[A-F0-9]{3}){1,2}\z/i } - validates :font_color, format: { allow_blank: true, with: /\A#?(?:[A-F0-9]{3}){1,2}\z/i } + validates :background_color, format: { allow_blank: true, with: CSS_HEX_COLOR } + validates :font_color, format: { allow_blank: true, with: CSS_HEX_COLOR } scope :open, -> { where("start_date <= ? and end_date >= ?", Date.current, Date.current) .order('id DESC') } From cdb66ce23c101cb7d368b984b71f24af8d643a3d Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 30 Jan 2019 13:17:35 +0100 Subject: [PATCH 5/6] Refactor header process colors specs --- spec/models/legislation/process_spec.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/spec/models/legislation/process_spec.rb b/spec/models/legislation/process_spec.rb index 274487cd6..503b4395e 100644 --- a/spec/models/legislation/process_spec.rb +++ b/spec/models/legislation/process_spec.rb @@ -177,8 +177,8 @@ describe Legislation::Process do end end - describe "banner colors" do - it "valid banner colors" do + describe "Header colors" do + it "valid format colors" do process1 = create(:legislation_process, background_color: "123", font_color: "#fff") process2 = create(:legislation_process, background_color: "#fff", font_color: "123") process3 = create(:legislation_process, background_color: "", font_color: "") @@ -189,12 +189,16 @@ describe Legislation::Process do expect(process4).to be_valid end - it "invalid banner colors" do + it "invalid format colors" do expect { - process1 = create(:legislation_process, background_color: "#123ghi", font_color: "#fff") - process2 = create(:legislation_process, background_color: "#ffffffff", font_color: "#123") + process = create(:legislation_process, background_color: "#123ghi", font_color: "#fff") }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Background color is invalid") + + expect { + process = create(:legislation_process, background_color: "#fff", font_color: "ggg") + }.to raise_error(ActiveRecord::RecordInvalid, + "Validation failed: Font color is invalid") end end From 483182ceed0e27229cbc6a0cbfec21dfb79efa63 Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 30 Jan 2019 18:03:58 +0100 Subject: [PATCH 6/6] Refactor scopes on site customization page model --- app/models/site_customization/page.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/models/site_customization/page.rb b/app/models/site_customization/page.rb index a4bb213ad..6dd8f8d2f 100644 --- a/app/models/site_customization/page.rb +++ b/app/models/site_customization/page.rb @@ -13,11 +13,12 @@ class SiteCustomization::Page < ActiveRecord::Base format: { with: /\A[0-9a-zA-Z\-_]*\Z/, message: :slug_format } validates :status, presence: true, inclusion: { in: VALID_STATUSES } - scope :published, -> { where(status: "published").order("id DESC") } - scope :with_more_info_flag, -> { where(status: "published", - more_info_flag: true).order("id ASC") } - scope :with_same_locale, -> { joins(:translations) - .where("site_customization_page_translations.locale": I18n.locale) } + scope :published, -> { where(status: "published").sort_desc } + scope :sort_asc, -> { order("id ASC") } + scope :sort_desc, -> { order("id DESC") } + scope :with_more_info_flag, -> { where(status: "published", more_info_flag: true).sort_asc } + scope :with_same_locale, -> { joins(:translations).locale } + scope :locale, -> { where("site_customization_page_translations.locale": I18n.locale) } def url "/#{slug}"