From 57917f850cd17c8e6bdbc56ae1054f425a0bb72f Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 28 Aug 2015 15:59:38 +0200 Subject: [PATCH 1/4] do not use js-classes for stying --- app/assets/stylesheets/participacion.scss | 2 +- app/views/shared/_locale_switcher.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/stylesheets/participacion.scss b/app/assets/stylesheets/participacion.scss index b195899d3..55f79bfa0 100644 --- a/app/assets/stylesheets/participacion.scss +++ b/app/assets/stylesheets/participacion.scss @@ -589,7 +589,7 @@ form.locale-form { select { background-image: image-url("language_select.png"); - &.js-locale-switcher { + &.locale-switcher { background-color: transparent; border: 0; color: white; diff --git a/app/views/shared/_locale_switcher.html.erb b/app/views/shared/_locale_switcher.html.erb index b60290296..2ac1c851f 100644 --- a/app/views/shared/_locale_switcher.html.erb +++ b/app/views/shared/_locale_switcher.html.erb @@ -1,7 +1,7 @@
<%= t("layouts.header.locale") %>
- <%= available_locale_options_for_select %>
From 934512ad7a222d29bf71f32e0157a0bea9c0d96c Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 28 Aug 2015 17:02:06 +0200 Subject: [PATCH 2/4] made the locale switcher use less js, more ruby & html --- app/assets/javascripts/application.js | 2 +- .../javascripts/locale_switcher.js.coffee | 19 ------------------- .../javascripts/location_changer.js.coffee | 8 ++++++++ app/helpers/application_helper.rb | 9 --------- app/views/shared/_locale_switcher.html.erb | 9 +++++++-- spec/features/localization_spec.rb | 2 +- 6 files changed, 17 insertions(+), 32 deletions(-) delete mode 100644 app/assets/javascripts/locale_switcher.js.coffee create mode 100644 app/assets/javascripts/location_changer.js.coffee diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 6e5b1df3d..cf1d2eba4 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -31,7 +31,7 @@ var initialize_modules = function() { App.Votes.initialize(); App.Tags.initialize(); App.Stats.initialize(); - App.LocaleSwitcher.initialize(); + App.LocationChanger.initialize(); App.DebatesOrderSelector.initialize(); }; diff --git a/app/assets/javascripts/locale_switcher.js.coffee b/app/assets/javascripts/locale_switcher.js.coffee deleted file mode 100644 index 2bd88b8ef..000000000 --- a/app/assets/javascripts/locale_switcher.js.coffee +++ /dev/null @@ -1,19 +0,0 @@ -App.LocaleSwitcher = - - href_with_params: (query_params) -> - loc = window.location - - loc.protocol + "//" + loc.hostname + - (if loc.port then ':' + loc.port else '') + - loc.pathname + - loc.hash + - '?' + $.param(query_params) - - initialize: -> - $('.js-locale-switcher').on 'change', -> - query_params = window.getQueryParameters() - query_params['locale'] = $(this).val() - window.location.assign(App.LocaleSwitcher.href_with_params(query_params)) - - - diff --git a/app/assets/javascripts/location_changer.js.coffee b/app/assets/javascripts/location_changer.js.coffee new file mode 100644 index 000000000..54693633d --- /dev/null +++ b/app/assets/javascripts/location_changer.js.coffee @@ -0,0 +1,8 @@ +App.LocationChanger = + + initialize: -> + $('.js-location-changer').on 'change', -> + window.location.assign($(this).val()) + + + diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 11e4cae8c..69cb06d45 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -14,13 +14,4 @@ module ApplicationHelper home_page? ? '' : 'results' end - def available_locale_options_for_select - options_for_select(available_locales_array, I18n.locale) - end - - private - def available_locales_array - I18n.available_locales.map { |loc| [I18n.t('locale', locale: loc), loc] } - end - end diff --git a/app/views/shared/_locale_switcher.html.erb b/app/views/shared/_locale_switcher.html.erb index 2ac1c851f..f76a71b9c 100644 --- a/app/views/shared/_locale_switcher.html.erb +++ b/app/views/shared/_locale_switcher.html.erb @@ -1,8 +1,13 @@
<%= t("layouts.header.locale") %>
- + <% I18n.available_locales.map do |loc| %> + + <% end %>
diff --git a/spec/features/localization_spec.rb b/spec/features/localization_spec.rb index 04993cb69..b4961f454 100644 --- a/spec/features/localization_spec.rb +++ b/spec/features/localization_spec.rb @@ -12,7 +12,7 @@ feature 'Localization' do scenario 'Available locales appear in the locale switcher' do visit '/' - within('.js-locale-switcher') do + within('.locale-form .js-location-changer') do expect(page).to have_content 'EspaƱol' expect(page).to have_content 'English' end From 088144dcd19e51e05631ba340427c4a4bc4007ba Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 28 Aug 2015 17:17:21 +0200 Subject: [PATCH 3/4] Refactor debates order select using js-location-changer --- app/assets/javascripts/application.js | 2 -- .../debates_order_selector.js.coffee | 17 ----------------- app/assets/javascripts/qp.js | 12 ------------ app/helpers/debates_helper.rb | 12 ------------ app/views/debates/_order_selector.erb | 5 ----- app/views/debates/index.html.erb | 11 ++++++++++- 6 files changed, 10 insertions(+), 49 deletions(-) delete mode 100644 app/assets/javascripts/debates_order_selector.js.coffee delete mode 100644 app/assets/javascripts/qp.js delete mode 100644 app/helpers/debates_helper.rb delete mode 100644 app/views/debates/_order_selector.erb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index cf1d2eba4..15f36ea49 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -21,7 +21,6 @@ //= require d3 //= require c3 //= require c3ext -//= require qp //= require app //= require_tree . @@ -32,7 +31,6 @@ var initialize_modules = function() { App.Tags.initialize(); App.Stats.initialize(); App.LocationChanger.initialize(); - App.DebatesOrderSelector.initialize(); }; $(function(){ diff --git a/app/assets/javascripts/debates_order_selector.js.coffee b/app/assets/javascripts/debates_order_selector.js.coffee deleted file mode 100644 index 94002f5ac..000000000 --- a/app/assets/javascripts/debates_order_selector.js.coffee +++ /dev/null @@ -1,17 +0,0 @@ -App.DebatesOrderSelector = - - href_with_params: (query_params) -> - loc = window.location - - loc.protocol + "//" + loc.hostname + - (if loc.port then ':' + loc.port else '') + - loc.pathname + - loc.hash + - '?' + $.param(query_params) - - initialize: -> - $('.js-order-selector').on 'change', -> - query_params = window.getQueryParameters() - query_params['order'] = $(this).val() - window.location.assign(App.DebatesOrderSelector.href_with_params(query_params)) - diff --git a/app/assets/javascripts/qp.js b/app/assets/javascripts/qp.js deleted file mode 100644 index 5e2b4f02d..000000000 --- a/app/assets/javascripts/qp.js +++ /dev/null @@ -1,12 +0,0 @@ -/* - * getQueryParameters.js - * Copyright (c) 2014 Nicholas Ortenzio - * The MIT License (MIT) - * - */ - -window.getQueryParameters = function(str) { - str = (str || document.location.search).replace(/(^\?)/,''); - if(!str) { return {}; } - return str.split("&").reduce(function(o,n){n=n.split('=');o[n[0]]=n[1];return o},{}); -}; diff --git a/app/helpers/debates_helper.rb b/app/helpers/debates_helper.rb deleted file mode 100644 index c594a8fa6..000000000 --- a/app/helpers/debates_helper.rb +++ /dev/null @@ -1,12 +0,0 @@ -module DebatesHelper - def available_options_for_order_selector(valid_orders, current_order) - options_for_select(available_order_filters_array(valid_orders), current_order) - end - - private - - def available_order_filters_array(orders) - orders.map { |f| [t("debates.index.orders.#{f}"), f] } - end - -end diff --git a/app/views/debates/_order_selector.erb b/app/views/debates/_order_selector.erb deleted file mode 100644 index de18cfc45..000000000 --- a/app/views/debates/_order_selector.erb +++ /dev/null @@ -1,5 +0,0 @@ -
- -
diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index 13600160c..ebe932ec3 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -4,7 +4,16 @@

<%= t("debates.index.showing") %>

- <%= render 'order_selector' %> +
+ +
From a7929c607d5d2d5c9777eff9ae25e3e33e32aa84 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 28 Aug 2015 20:07:45 +0200 Subject: [PATCH 4/4] adds a helper to simplify the way selects and filters are rendered --- app/helpers/application_helper.rb | 6 ++++++ app/views/admin/comments/index.html.erb | 2 +- app/views/admin/debates/index.html.erb | 2 +- app/views/admin/organizations/index.html.erb | 2 +- app/views/admin/users/index.html.erb | 2 +- app/views/debates/index.html.erb | 4 ++-- app/views/moderation/comments/index.html.erb | 2 +- app/views/moderation/debates/index.html.erb | 2 +- app/views/shared/_locale_switcher.html.erb | 2 +- 9 files changed, 15 insertions(+), 9 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 69cb06d45..77afc1317 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -14,4 +14,10 @@ module ApplicationHelper home_page? ? '' : 'results' end + # if current path is /debates current_path_with_query_params(foo: 'bar') returns /debates?foo=bar + # notice: if query_params have a param which also exist in current path, it "overrides" (query_params is merged last) + def current_path_with_query_params(query_parameters) + url_for(request.query_parameters.merge(query_parameters)) + end + end diff --git a/app/views/admin/comments/index.html.erb b/app/views/admin/comments/index.html.erb index 941e1e9ed..87178c66b 100644 --- a/app/views/admin/comments/index.html.erb +++ b/app/views/admin/comments/index.html.erb @@ -8,7 +8,7 @@
<%= t("admin.comments.index.filters.#{filter}") %>
<% else %>
<%= link_to t("admin.comments.index.filters.#{filter}"), - admin_comments_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/admin/debates/index.html.erb b/app/views/admin/debates/index.html.erb index cdeb7b016..9ac89b5e1 100644 --- a/app/views/admin/debates/index.html.erb +++ b/app/views/admin/debates/index.html.erb @@ -8,7 +8,7 @@
<%= t("admin.debates.index.filters.#{filter}") %>
<% else %>
<%= link_to t("admin.debates.index.filters.#{filter}"), - admin_debates_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/admin/organizations/index.html.erb b/app/views/admin/organizations/index.html.erb index df003e6f8..8569d0c47 100644 --- a/app/views/admin/organizations/index.html.erb +++ b/app/views/admin/organizations/index.html.erb @@ -21,7 +21,7 @@
<%= t("admin.organizations.index.filters.#{filter}") %>
<% else %>
<%= link_to t("admin.organizations.index.filters.#{filter}"), - admin_organizations_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 8f728295d..b6f3f1cfe 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -8,7 +8,7 @@
<%= t("admin.users.index.filters.#{filter}") %>
<% else %>
<%= link_to t("admin.users.index.filters.#{filter}"), - admin_users_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index 71266aeb2..701110af3 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -16,7 +16,7 @@ <% @valid_orders.each do |order| %> <% end %> diff --git a/app/views/moderation/comments/index.html.erb b/app/views/moderation/comments/index.html.erb index f9c95e9be..da2bb8251 100644 --- a/app/views/moderation/comments/index.html.erb +++ b/app/views/moderation/comments/index.html.erb @@ -8,7 +8,7 @@
<%= t("moderation.comments.index.filters.#{filter}") %>
<% else %>
<%= link_to t("moderation.comments.index.filters.#{filter}"), - moderation_comments_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/moderation/debates/index.html.erb b/app/views/moderation/debates/index.html.erb index cbd6baa5d..8fb013605 100644 --- a/app/views/moderation/debates/index.html.erb +++ b/app/views/moderation/debates/index.html.erb @@ -8,7 +8,7 @@
<%= t("moderation.debates.index.filters.#{filter}") %>
<% else %>
<%= link_to t("moderation.debates.index.filters.#{filter}"), - moderation_debates_path(filter: filter) %>
+ current_path_with_query_params(filter: filter) %> <% end %> <% end %> diff --git a/app/views/shared/_locale_switcher.html.erb b/app/views/shared/_locale_switcher.html.erb index f76a71b9c..763acc4b9 100644 --- a/app/views/shared/_locale_switcher.html.erb +++ b/app/views/shared/_locale_switcher.html.erb @@ -4,7 +4,7 @@