From 6d6da8affc5461bda3ee46695258f1b298f435e7 Mon Sep 17 00:00:00 2001 From: iagirre Date: Tue, 9 Jan 2018 17:30:11 +0100 Subject: [PATCH 01/10] Add form fields to set the colours and the sections for the banner Add migrations to store the information in the database --- app/assets/javascripts/banners.js.coffee | 17 ++++++++++ app/controllers/admin/banners_controller.rb | 7 ++++- app/models/banner.rb | 2 ++ app/models/banner/section.rb | 5 +++ app/views/admin/banners/_form.html.erb | 31 ++++++++++++++++--- app/views/admin/banners/index.html.erb | 8 ++--- config/locales/en/admin.yml | 9 ++++++ config/locales/es/admin.yml | 9 ++++++ .../20180109081115_add_settings_to_banners.rb | 6 ++++ .../20180109101656_create_banner_sections.rb | 9 ++++++ db/schema.rb | 15 +++++++-- 11 files changed, 106 insertions(+), 12 deletions(-) create mode 100644 app/models/banner/section.rb create mode 100644 db/migrate/20180109081115_add_settings_to_banners.rb create mode 100644 db/migrate/20180109101656_create_banner_sections.rb diff --git a/app/assets/javascripts/banners.js.coffee b/app/assets/javascripts/banners.js.coffee index 66d3a8df7..5f0c770f1 100644 --- a/app/assets/javascripts/banners.js.coffee +++ b/app/assets/javascripts/banners.js.coffee @@ -7,6 +7,14 @@ App.Banners = $(selector).removeClass($(selector).attr("class"), true) .addClass(style, true) + update_background_color: (selector, text_selector, background_color) -> + $(selector).css('background-color', background_color); + $(text_selector).val(background_color); + + update_font_color: (selector, text_selector, font_color) -> + $(selector).css('color', font_color); + $(text_selector).val(font_color); + initialize: -> $('[data-js-banner-title]').on change: -> @@ -23,3 +31,12 @@ App.Banners = $("#banner_image").on change: -> App.Banners.update_style("#js-banner-image", $(this).val()) + + $("#banner_background_color_picker").on + change: -> + App.Banners.update_background_color("#js-banner-style", "#banner_background_color", $(this).val()); + + $("#banner_font_color_picker").on + change: -> + App.Banners.update_font_color("#js-banner-title", "#banner_font_color", $(this).val()); + App.Banners.update_font_color("#js-banner-description", "#banner_font_color", $(this).val()); diff --git a/app/controllers/admin/banners_controller.rb b/app/controllers/admin/banners_controller.rb index 281fc9360..1c0d1528c 100644 --- a/app/controllers/admin/banners_controller.rb +++ b/app/controllers/admin/banners_controller.rb @@ -4,6 +4,7 @@ class Admin::BannersController < Admin::BaseController before_action :banner_styles, only: [:edit, :new, :create, :update] before_action :banner_imgs, only: [:edit, :new, :create, :update] + before_action :banner_sections, only: [:edit, :new, :create, :update] respond_to :html, :js @@ -39,7 +40,8 @@ class Admin::BannersController < Admin::BaseController def banner_params attributes = [:title, :description, :target_url, :style, :image, - :post_started_at, :post_ended_at] + :post_started_at, :post_ended_at, :sections, + :background_color, :font_color] params.require(:banner).permit(*attributes) end @@ -55,4 +57,7 @@ class Admin::BannersController < Admin::BaseController end end + def banner_sections + @banner_sections = Banner::Section.sections.map {|banner_section| [banner_section[1], banner_section[0]]} # name, value + end end diff --git a/app/models/banner.rb b/app/models/banner.rb index 8e816d446..afa6e5115 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -12,6 +12,8 @@ class Banner < ActiveRecord::Base validates :post_started_at, presence: true validates :post_ended_at, presence: true + has_many :sections + scope :with_active, -> { where("post_started_at <= ?", Time.current).where("post_ended_at >= ?", Time.current) } scope :with_inactive, -> { where("post_started_at > ? or post_ended_at < ?", Time.current, Time.current) } diff --git a/app/models/banner/section.rb b/app/models/banner/section.rb new file mode 100644 index 000000000..ac1861a1e --- /dev/null +++ b/app/models/banner/section.rb @@ -0,0 +1,5 @@ +class Banner::Section < ActiveRecord::Base + enum sections: { homepage: 0, debates: 1, proposals: 2, budgets: 3, more_info: 4 } + + belongs_to :banner +end diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 9c8d20612..852ef551c 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -60,17 +60,40 @@ +
+
+ <%= f.label :sections, t("admin.banners.banner.sections_label") %> + <% @banner_sections.each do |section| %> + <%= f.check_box :sections, {multiple: true, label: false}, section[0], nil %> + <%= t("admin.banners.banner.sections.#{section[1]}") %>
+ <% end %> +
+
+ +
+
+ <%= f.label :sections, t("admin.banners.banner.background_color") %> + <%= color_field(:banner, :background_color, id: 'banner_background_color_picker') %> + <%= f.text_field :background_color, label: false %> +
+
+ <%= f.label :sections, t("admin.banners.banner.font_color") %> + <%= color_field(:banner, :font_color, id: 'banner_font_color_picker') %> + <%= f.text_field :font_color, label: false %> +
+
+
<%= f.submit(class: "button expanded", value: t("admin.banners.edit.form.submit_button")) %>
-
+
<%= link_to @banner.target_url do %> -

<%= @banner.title %>

-

<%= @banner.description %>

+

<%= @banner.title %>

+

<%= @banner.description %>

<% end %>
-<% end %> \ No newline at end of file +<% end %> diff --git a/app/views/admin/banners/index.html.erb b/app/views/admin/banners/index.html.erb index 11f2ef6ab..352816bfc 100644 --- a/app/views/admin/banners/index.html.erb +++ b/app/views/admin/banners/index.html.erb @@ -37,11 +37,11 @@ -
+
<%= link_to banner.target_url do %> -

<%= banner.title %>

-

<%= banner.description %>

+

<%= banner.title %>

+

<%= banner.description %>

<% end %>
@@ -51,4 +51,4 @@ <% end %> -<%= paginate @banners %> \ No newline at end of file +<%= paginate @banners %> diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 67273c327..c999fcbc6 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -33,6 +33,15 @@ en: image: Image post_started_at: Post started at post_ended_at: Post ended at + sections_label: Seccions where it will appear + sections: + homepage: Homepage + debates: Debates + proposals: Proposals + budgets: Participatory budgeting + more_info: More info page + background_color: Background colour + font_color: Font colour edit: editing: Edit banner form: diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 8e3c546f9..371fab679 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -33,6 +33,15 @@ es: image: Imagen post_started_at: Inicio de publicación post_ended_at: Fin de publicación + sections_label: Secciones en las que aparece + sections: + homepage: Homepage + debates: Debates + proposals: Propuestas + budgets: Presupuestos participativos + more_info: Página de más información + background_color: Color de fondo + font_color: Color del texto edit: editing: Editar el banner form: diff --git a/db/migrate/20180109081115_add_settings_to_banners.rb b/db/migrate/20180109081115_add_settings_to_banners.rb new file mode 100644 index 000000000..5092501cc --- /dev/null +++ b/db/migrate/20180109081115_add_settings_to_banners.rb @@ -0,0 +1,6 @@ +class AddSettingsToBanners < ActiveRecord::Migration + def change + add_column :banners, :background_color, :text + add_column :banners, :font_color, :text + end +end diff --git a/db/migrate/20180109101656_create_banner_sections.rb b/db/migrate/20180109101656_create_banner_sections.rb new file mode 100644 index 000000000..e65bbd91a --- /dev/null +++ b/db/migrate/20180109101656_create_banner_sections.rb @@ -0,0 +1,9 @@ +class CreateBannerSections < ActiveRecord::Migration + def change + create_table :banner_sections do |t| + t.integer :banner_id + t.integer :section + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 916342063..bb5863510 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -63,8 +63,15 @@ ActiveRecord::Schema.define(version: 20180519132610) do add_index "annotations", ["legacy_legislation_id"], name: "index_annotations_on_legacy_legislation_id", using: :btree add_index "annotations", ["user_id"], name: "index_annotations_on_user_id", using: :btree + create_table "banner_sections", force: :cascade do |t| + t.integer "banner_id" + t.integer "section" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "banners", force: :cascade do |t| - t.string "title", limit: 80 + t.string "title", limit: 80 t.string "description" t.string "target_url" t.string "style" @@ -72,8 +79,10 @@ ActiveRecord::Schema.define(version: 20180519132610) do t.date "post_started_at" t.date "post_ended_at" t.datetime "hidden_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "background_color" + t.text "font_color" end add_index "banners", ["hidden_at"], name: "index_banners_on_hidden_at", using: :btree From 3690cae458c065879515abac50c44c7bfb545f88 Mon Sep 17 00:00:00 2001 From: iagirre Date: Mon, 15 Jan 2018 11:25:14 +0100 Subject: [PATCH 02/10] Apply banner style to the new banners Banner sections can be saved (one banner can appear in several sections) If the hex color is changed in the textfield, the color of the color picker changes. --- app/assets/javascripts/banners.js.coffee | 9 ++++++++ app/assets/stylesheets/layout.scss | 3 ++- app/controllers/admin/banners_controller.rb | 7 ++++--- app/models/banner.rb | 4 +--- app/models/banner/section.rb | 3 +-- app/models/web_section.rb | 4 ++++ app/views/admin/banners/_form.html.erb | 21 +++++-------------- app/views/admin/banners/index.html.erb | 2 +- .../20180109101656_create_banner_sections.rb | 2 +- .../20180115075542_create_web_sections.rb | 8 +++++++ db/schema.rb | 13 +++++++++--- lib/tasks/web_sections.rake | 10 +++++++++ 12 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 app/models/web_section.rb create mode 100644 db/migrate/20180115075542_create_web_sections.rb create mode 100644 lib/tasks/web_sections.rake diff --git a/app/assets/javascripts/banners.js.coffee b/app/assets/javascripts/banners.js.coffee index 5f0c770f1..e7748c148 100644 --- a/app/assets/javascripts/banners.js.coffee +++ b/app/assets/javascripts/banners.js.coffee @@ -36,7 +36,16 @@ App.Banners = change: -> App.Banners.update_background_color("#js-banner-style", "#banner_background_color", $(this).val()); + $("#banner_background_color").on + change: -> + App.Banners.update_background_color("#js-banner-style", "#banner_background_color_picker", $(this).val()); + $("#banner_font_color_picker").on change: -> App.Banners.update_font_color("#js-banner-title", "#banner_font_color", $(this).val()); App.Banners.update_font_color("#js-banner-description", "#banner_font_color", $(this).val()); + + $("#banner_font_color").on + change: -> + App.Banners.update_font_color("#js-banner-title", "#banner_font_color_picker", $(this).val()); + App.Banners.update_font_color("#js-banner-description", "#banner_font_color_picker", $(this).val()); diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 8ef6ebb7f..e7e917be8 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -2226,7 +2226,8 @@ table { .banner-style-one, .banner-style-two, -.banner-style-three { +.banner-style-three, +.banner { margin: 0; margin-bottom: $line-height; diff --git a/app/controllers/admin/banners_controller.rb b/app/controllers/admin/banners_controller.rb index 1c0d1528c..802110063 100644 --- a/app/controllers/admin/banners_controller.rb +++ b/app/controllers/admin/banners_controller.rb @@ -40,8 +40,9 @@ class Admin::BannersController < Admin::BaseController def banner_params attributes = [:title, :description, :target_url, :style, :image, - :post_started_at, :post_ended_at, :sections, - :background_color, :font_color] + :post_started_at, :post_ended_at, + :background_color, :font_color, + web_section_ids: []] params.require(:banner).permit(*attributes) end @@ -58,6 +59,6 @@ class Admin::BannersController < Admin::BaseController end def banner_sections - @banner_sections = Banner::Section.sections.map {|banner_section| [banner_section[1], banner_section[0]]} # name, value + @banner_sections = WebSection.all end end diff --git a/app/models/banner.rb b/app/models/banner.rb index afa6e5115..ad37fdc72 100644 --- a/app/models/banner.rb +++ b/app/models/banner.rb @@ -7,15 +7,13 @@ class Banner < ActiveRecord::Base length: { minimum: 2 } validates :description, presence: true validates :target_url, presence: true - validates :style, presence: true - validates :image, presence: true validates :post_started_at, presence: true validates :post_ended_at, presence: true has_many :sections + has_many :web_sections, through: :sections scope :with_active, -> { where("post_started_at <= ?", Time.current).where("post_ended_at >= ?", Time.current) } scope :with_inactive, -> { where("post_started_at > ? or post_ended_at < ?", Time.current, Time.current) } - end diff --git a/app/models/banner/section.rb b/app/models/banner/section.rb index ac1861a1e..493093731 100644 --- a/app/models/banner/section.rb +++ b/app/models/banner/section.rb @@ -1,5 +1,4 @@ class Banner::Section < ActiveRecord::Base - enum sections: { homepage: 0, debates: 1, proposals: 2, budgets: 3, more_info: 4 } - belongs_to :banner + belongs_to :web_section end diff --git a/app/models/web_section.rb b/app/models/web_section.rb new file mode 100644 index 000000000..ffeca96f2 --- /dev/null +++ b/app/models/web_section.rb @@ -0,0 +1,4 @@ +class WebSection < ActiveRecord::Base + has_many :sections + has_many :banners, through: :sections +end diff --git a/app/views/admin/banners/_form.html.erb b/app/views/admin/banners/_form.html.erb index 852ef551c..1db97e71a 100644 --- a/app/views/admin/banners/_form.html.erb +++ b/app/views/admin/banners/_form.html.erb @@ -3,18 +3,8 @@ <%= render 'errors' %>
-
- <%= f.label :style, t("admin.banners.banner.style") %> - <%= f.select :style, options_for_select(@banner_styles, @banner.style),:include_blank => '-', - label: false,placeholder: t("admin.banners.banner.style") %> -
-
- <%= f.label :image, t("admin.banners.banner.image") %> - <%= f.select :image, options_for_select(@banner_imgs, @banner.image),:include_blank => '-', - label: false, placeholder: t("admin.banners.banner.image") %> -
<% date_started_at = @banner.post_started_at.present? ? I18n.localize(@banner.post_started_at) : "" %> -
+
<%= f.label :post_started_at, t("admin.banners.banner.post_started_at") %> <%= f.text_field :post_started_at, label: false, @@ -24,7 +14,7 @@ id: "post_started_at" %>
<% date_ended_at = @banner.post_ended_at.present? ? I18n.localize(@banner.post_ended_at) : ""%> -
+
<%= f.label :post_ended_at, t("admin.banners.banner.post_ended_at") %> <%= f.text_field :post_ended_at, label: false, @@ -63,9 +53,8 @@
<%= f.label :sections, t("admin.banners.banner.sections_label") %> - <% @banner_sections.each do |section| %> - <%= f.check_box :sections, {multiple: true, label: false}, section[0], nil %> - <%= t("admin.banners.banner.sections.#{section[1]}") %>
+ <%= f.collection_check_boxes(:web_section_ids, @banner_sections, :id, :name) do |b| %> + <%= b.check_box %> <%= t("admin.banners.banner.sections.#{b.text}") %>
<% end %>
@@ -88,7 +77,7 @@ <%= f.submit(class: "button expanded", value: t("admin.banners.edit.form.submit_button")) %>
-
+