From f92a7ade5571ecacef93fcd711e20e74eb57514c Mon Sep 17 00:00:00 2001 From: decabeza Date: Wed, 6 Dec 2017 13:22:19 +0100 Subject: [PATCH 01/12] adds partial for related content --- app/assets/stylesheets/layout.scss | 54 +++++++++++++++++++++ app/views/proposals/show.html.erb | 2 + app/views/shared/_related_content.html.erb | 55 ++++++++++++++++++++++ config/locales/en/general.yml | 10 +++- config/locales/es/general.yml | 8 ++++ 5 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 app/views/shared/_related_content.html.erb diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index a2ddea800..29173bb8a 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -20,6 +20,7 @@ // 18. Banners // 19. Recommended Section Home // 20. Documents +// 21. Related content // // 01. Global styles @@ -2382,3 +2383,56 @@ table { background: #fafafa; border-bottom: 1px solid #eee; } + +// 21. Related content +// ------------------- + +.related-content { + border-top: 1px solid $border; + + h2 { + font-size: rem-calc(24); + + span { + color: #4f4f4f; + font-weight: normal; + } + } +} + +.add-related-content { + display: block; + + @include breakpoint(medium) { + float: right; + } +} + +.related-content-list { + list-style-type: none; + margin-left: 0; + + li { + border-bottom: 1px solid $border; + padding: $line-height / 4; + + &:first-child { + border-top: 1px solid $border; + } + } + + h3 { + font-size: $base-font-size; + font-weight: normal; + } + + span { + color: #4f4f4f; + font-size: rem-calc(12); + text-transform: uppercase; + } + + .flag { + margin-top: $line-height / 2; + } +} diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index ade0972d8..4a559cfd8 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -106,6 +106,8 @@ <%= render 'shared/geozone', geozonable: @proposal %> + <%= render 'shared/related_content', relationable: @proposal %> +
<%= render 'proposals/actions', proposal: @proposal %>
diff --git a/app/views/shared/_related_content.html.erb b/app/views/shared/_related_content.html.erb new file mode 100644 index 000000000..abe21e0f2 --- /dev/null +++ b/app/views/shared/_related_content.html.erb @@ -0,0 +1,55 @@ + diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 6e87e6983..2062e8d3a 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -811,4 +811,12 @@ en: user_permission_votes: Participate on final voting invisible_captcha: sentence_for_humans: "If you are human, ignore this field" - timestamp_error_message: "Sorry, that was too quick! Please resubmit." \ No newline at end of file + timestamp_error_message: "Sorry, that was too quick! Please resubmit." + related_content: + title: "Related content" + add: "Add related content" + label: "Link to related content" + placeholder: "%{url}" + help: "Yuo can add any link inside of %{org}." + submit: "Add" + error: "Link not valid. Remember to start with %{url}." diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 0d0b4b849..25e73edfc 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -809,3 +809,11 @@ es: invisible_captcha: sentence_for_humans: "Si eres humano, por favor ignora este campo" timestamp_error_message: "Eso ha sido demasiado rápido. Por favor, reenvía el formulario." + related_content: + title: "Contenido relacionado" + add: "Añadir contenido relacionado" + label: "Enlace a contenido relacionado" + placeholder: "%{url}" + help: "Puedes introducir cualquier enlace que esté dentro de %{org}." + submit: "Añadir" + error: "Enlace no válido. Recuerda que debe empezar por %{url}." \ No newline at end of file From ed8e5de094199b07069c31f86972c6667b9aa95d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Wed, 13 Dec 2017 11:29:12 +0100 Subject: [PATCH 02/12] Added translations --- config/locales/en/general.yml | 6 +++++- config/locales/es/general.yml | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 2062e8d3a..aad6031a8 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -817,6 +817,10 @@ en: add: "Add related content" label: "Link to related content" placeholder: "%{url}" - help: "Yuo can add any link inside of %{org}." + help: "You can add any link inside of %{org}." submit: "Add" error: "Link not valid. Remember to start with %{url}." + success: "You added a new related content" + content_title: + proposal: "Proposal" + debate: "Debate" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 25e73edfc..15a29856f 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -816,4 +816,8 @@ es: placeholder: "%{url}" help: "Puedes introducir cualquier enlace que esté dentro de %{org}." submit: "Añadir" - error: "Enlace no válido. Recuerda que debe empezar por %{url}." \ No newline at end of file + error: "Enlace no válido. Recuerda que debe empezar por %{url}." + success: "Has añadido un nuevo contenido relacionado" + content_title: + proposal: "Propuesta" + debate: "Debate" From 4f442792cccfc5447c13dc7fe2567c19a9d51815 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Wed, 13 Dec 2017 13:32:00 +0100 Subject: [PATCH 03/12] Added working related contents list Also separated related contents into partials. --- app/views/proposals/show.html.erb | 2 +- .../relationable/_related_content.html.erb | 15 +++++ app/views/relationable/_related_list.html.erb | 16 ++++++ app/views/shared/_related_content.html.erb | 55 ------------------- 4 files changed, 32 insertions(+), 56 deletions(-) create mode 100644 app/views/relationable/_related_content.html.erb create mode 100644 app/views/relationable/_related_list.html.erb delete mode 100644 app/views/shared/_related_content.html.erb diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb index 4a559cfd8..bbe186d2e 100644 --- a/app/views/proposals/show.html.erb +++ b/app/views/proposals/show.html.erb @@ -106,7 +106,7 @@ <%= render 'shared/geozone', geozonable: @proposal %> - <%= render 'shared/related_content', relationable: @proposal %> + <%= render 'relationable/related_content', relationable: @proposal %>
<%= render 'proposals/actions', proposal: @proposal %> diff --git a/app/views/relationable/_related_content.html.erb b/app/views/relationable/_related_content.html.erb new file mode 100644 index 000000000..e780d3cf7 --- /dev/null +++ b/app/views/relationable/_related_content.html.erb @@ -0,0 +1,15 @@ + diff --git a/app/views/relationable/_related_list.html.erb b/app/views/relationable/_related_list.html.erb new file mode 100644 index 000000000..ffeacc58e --- /dev/null +++ b/app/views/relationable/_related_list.html.erb @@ -0,0 +1,16 @@ + + + diff --git a/app/views/shared/_related_content.html.erb b/app/views/shared/_related_content.html.erb deleted file mode 100644 index abe21e0f2..000000000 --- a/app/views/shared/_related_content.html.erb +++ /dev/null @@ -1,55 +0,0 @@ - From d6fd83915d0d06d918989544e3dcf725720fd0be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Wed, 13 Dec 2017 13:32:18 +0100 Subject: [PATCH 04/12] Added tests for related contents list --- spec/features/proposals_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 192e7949f..e9c5dbd6d 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -123,6 +123,22 @@ feature 'Proposals' do visit proposal_path(proposal) expect(page).not_to have_content "Access the community" end + + scenario 'related contents are listed' do + proposal1 = create(:proposal) + proposal2 = create(:proposal) + related_content = create(:related_content, parent_relationable: proposal1, child_relationable: proposal2) + + visit proposal_path(proposal1) + within("#related-content-list") do + expect(page).to have_content(proposal2.title) + end + + visit proposal_path(proposal2) + within("#related-content-list") do + expect(page).to have_content(proposal1.title) + end + end end context "Embedded video" do From 586b1ae2015a16b7c380a1713ec78d12db016432 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 14:12:26 +0100 Subject: [PATCH 05/12] Added related content form --- app/views/relationable/_form.html.erb | 22 +++++++++++++++++++ .../relationable/_related_content.html.erb | 9 +++++--- 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 app/views/relationable/_form.html.erb diff --git a/app/views/relationable/_form.html.erb b/app/views/relationable/_form.html.erb new file mode 100644 index 000000000..e5d83bdfb --- /dev/null +++ b/app/views/relationable/_form.html.erb @@ -0,0 +1,22 @@ +<%= form_tag related_contents_path, method: :post, id: "related_content", class: "hide", "data-toggler": ".hide" do %> + + + + +
+
+ <%= text_field_tag :url, '', + "aria-describedby": "related_content_help_text", + placeholder: t("related_content.placeholder", url: setting['url']) %> + + <%= hidden_field_tag :relationable_klass, relationable.class.name %> + <%= hidden_field_tag :relationable_id, relationable.id %> +
+ +
+ <%= submit_tag t("related_content.submit"), class: "button" %> +
+
+ <% end %> diff --git a/app/views/relationable/_related_content.html.erb b/app/views/relationable/_related_content.html.erb index e780d3cf7..19435fec2 100644 --- a/app/views/relationable/_related_content.html.erb +++ b/app/views/relationable/_related_content.html.erb @@ -4,12 +4,15 @@

<%= t("related_content.title") %> (<%= relationable.relationed_contents.count %>)

- - - <%= t("related_content.add") %> + +
+ <%= render 'relationable/form', relationable: relationable %> + <%= render 'relationable/related_list', relationable: relationable %> From 5c7b5eed53a39fa1a99e592987ffac31d535cca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 14:17:58 +0100 Subject: [PATCH 06/12] Added RelatedContentsController --- .../related_contents_controller.rb | 43 +++++++++++++++++++ config/routes.rb | 2 + 2 files changed, 45 insertions(+) create mode 100644 app/controllers/related_contents_controller.rb diff --git a/app/controllers/related_contents_controller.rb b/app/controllers/related_contents_controller.rb new file mode 100644 index 000000000..d39ddc608 --- /dev/null +++ b/app/controllers/related_contents_controller.rb @@ -0,0 +1,43 @@ +class RelatedContentsController < ApplicationController + RELATIONABLE_MODELS = %w{proposals debates} + VALID_URL = /#{Setting['url']}\/.*\/.*/ + + skip_authorization_check + + def create + if relationable_object && related_object + @relationable.relate_content(@related) + + flash[:success] = t('related_content.success') + else + flash[:error] = t('related_content.error', url: Setting['url']) + end + + redirect_to @relationable + end + + private + + def valid_url? + params[:url].match(VALID_URL) + end + + def relationable_object + @relationable = (params[:relationable_klass].singularize.camelize.constantize).find_by_id(params[:relationable_id]) + end + + def related_object + begin + if valid_url? + url = params[:url] + + related_klass = url.match(/\/(#{RELATIONABLE_MODELS.join("|")})\//)[0].gsub("/", "") + related_id = url.match(/\/[0-9]+/)[0].gsub("/", "") + + @related = (related_klass.singularize.camelize.constantize).find_by_id(related_id) + end + rescue + nil + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 31d811293..429e5a862 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -462,6 +462,8 @@ Rails.application.routes.draw do root to: "dashboard#index" end + resources :related_contents, only: [:create] + # GraphQL get '/graphql', to: 'graphql#query' post '/graphql', to: 'graphql#query' From 7629fd2499073245828c7582a3b69b08967eacf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 14:18:44 +0100 Subject: [PATCH 07/12] Added specs for related content form --- spec/features/proposals_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index e9c5dbd6d..868c5f95c 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -139,6 +139,32 @@ feature 'Proposals' do expect(page).to have_content(proposal1.title) end end + + scenario 'related contents can be added' do + proposal1 = create(:proposal) + proposal2 = create(:proposal) + + visit proposal_path(proposal1) + + expect(page).to have_selector('#related_content', visible: false) + click_on("Add related content") + expect(page).to have_selector('#related_content', visible: true) + + within("#related_content") do + fill_in 'url', with: "#{Setting['url']}/proposals/#{proposal2.to_param}" + click_button "Add" + end + + within("#related-content-list") do + expect(page).to have_content(proposal2.title) + end + + visit proposal_path(proposal2) + + within("#related-content-list") do + expect(page).to have_content(proposal1.title) + end + end end context "Embedded video" do From 45a0b73e318ab7c656d231ebb1eed18203176a74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 18:26:33 +0100 Subject: [PATCH 08/12] Added translations --- config/locales/en/general.yml | 2 +- config/locales/es/general.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index aad6031a8..3391bc3fd 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -817,7 +817,7 @@ en: add: "Add related content" label: "Link to related content" placeholder: "%{url}" - help: "You can add any link inside of %{org}." + help: "You can add links of %{models} inside of %{org}." submit: "Add" error: "Link not valid. Remember to start with %{url}." success: "You added a new related content" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 15a29856f..db290316c 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -814,7 +814,7 @@ es: add: "Añadir contenido relacionado" label: "Enlace a contenido relacionado" placeholder: "%{url}" - help: "Puedes introducir cualquier enlace que esté dentro de %{org}." + help: "Puedes introducir cualquier enlace de %{models} que esté dentro de %{org}." submit: "Añadir" error: "Enlace no válido. Recuerda que debe empezar por %{url}." success: "Has añadido un nuevo contenido relacionado" From 88fc7408633b3d273c98874c7c74296ff2796039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 18:26:52 +0100 Subject: [PATCH 09/12] Moved `RELATIONABLE_MODELS` from controller to model --- app/controllers/related_contents_controller.rb | 3 +-- app/models/related_content.rb | 1 + app/views/relationable/_form.html.erb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/related_contents_controller.rb b/app/controllers/related_contents_controller.rb index d39ddc608..06bfaadc7 100644 --- a/app/controllers/related_contents_controller.rb +++ b/app/controllers/related_contents_controller.rb @@ -1,5 +1,4 @@ class RelatedContentsController < ApplicationController - RELATIONABLE_MODELS = %w{proposals debates} VALID_URL = /#{Setting['url']}\/.*\/.*/ skip_authorization_check @@ -31,7 +30,7 @@ class RelatedContentsController < ApplicationController if valid_url? url = params[:url] - related_klass = url.match(/\/(#{RELATIONABLE_MODELS.join("|")})\//)[0].gsub("/", "") + related_klass = url.match(/\/(#{RelatedContent::RELATIONABLE_MODELS.join("|")})\//)[0].gsub("/", "") related_id = url.match(/\/[0-9]+/)[0].gsub("/", "") @related = (related_klass.singularize.camelize.constantize).find_by_id(related_id) diff --git a/app/models/related_content.rb b/app/models/related_content.rb index ab72aefe3..fa41a57ad 100644 --- a/app/models/related_content.rb +++ b/app/models/related_content.rb @@ -1,5 +1,6 @@ class RelatedContent < ActiveRecord::Base RELATED_CONTENTS_REPORT_THRESHOLD = Setting['related_contents_report_threshold'].to_i + RELATIONABLE_MODELS = %w{proposals debates} belongs_to :parent_relationable, polymorphic: true belongs_to :child_relationable, polymorphic: true diff --git a/app/views/relationable/_form.html.erb b/app/views/relationable/_form.html.erb index e5d83bdfb..930c839a2 100644 --- a/app/views/relationable/_form.html.erb +++ b/app/views/relationable/_form.html.erb @@ -2,7 +2,7 @@
From 1830a7bb04c513fe76f45a539272f49861ccf074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 10:43:35 +0100 Subject: [PATCH 10/12] Added pagination for relationed content list --- app/controllers/proposals_controller.rb | 2 ++ app/views/relationable/_related_list.html.erb | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb index e86caf922..cbe45c124 100644 --- a/app/controllers/proposals_controller.rb +++ b/app/controllers/proposals_controller.rb @@ -19,6 +19,8 @@ class ProposalsController < ApplicationController def show super @notifications = @proposal.notifications + @related_contents = Kaminari.paginate_array(@proposal.relationed_contents).page(params[:page]).per(5) + redirect_to proposal_path(@proposal), status: :moved_permanently if request.path != proposal_path(@proposal) end diff --git a/app/views/relationable/_related_list.html.erb b/app/views/relationable/_related_list.html.erb index ffeacc58e..08fc931df 100644 --- a/app/views/relationable/_related_list.html.erb +++ b/app/views/relationable/_related_list.html.erb @@ -1,5 +1,5 @@ - +<%= paginate @related_contents %> From 784e576f4296b621a1b91463c0d0b3ef6994d4c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mar=C3=ADa=20Checa?= Date: Thu, 14 Dec 2017 18:27:02 +0100 Subject: [PATCH 11/12] Added new tests --- spec/features/proposals_spec.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/spec/features/proposals_spec.rb b/spec/features/proposals_spec.rb index 868c5f95c..9f64abd40 100644 --- a/spec/features/proposals_spec.rb +++ b/spec/features/proposals_spec.rb @@ -143,6 +143,7 @@ feature 'Proposals' do scenario 'related contents can be added' do proposal1 = create(:proposal) proposal2 = create(:proposal) + debate1 = create(:debate) visit proposal_path(proposal1) @@ -164,6 +165,30 @@ feature 'Proposals' do within("#related-content-list") do expect(page).to have_content(proposal1.title) end + + within("#related_content") do + fill_in 'url', with: "#{Setting['url']}/debates/#{debate1.to_param}" + click_button "Add" + end + + within("#related-content-list") do + expect(page).to have_content(debate1.title) + end + end + + scenario 'if related content URL is invalid returns error' do + proposal1 = create(:proposal) + + visit proposal_path(proposal1) + + click_on("Add related content") + + within("#related_content") do + fill_in 'url', with: "http://invalidurl.com" + click_button "Add" + end + + expect(page).to have_content("Link not valid. Remember to start with #{Setting[:url]}.") end end From ebb3c075b007fca997c367481bf2962acbc641c2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 15 Dec 2017 09:58:12 +0100 Subject: [PATCH 12/12] fixes proposal's management specs --- app/controllers/management/proposals_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/management/proposals_controller.rb b/app/controllers/management/proposals_controller.rb index 54616c05b..2101996af 100644 --- a/app/controllers/management/proposals_controller.rb +++ b/app/controllers/management/proposals_controller.rb @@ -14,6 +14,8 @@ class Management::ProposalsController < Management::BaseController def show super @notifications = @proposal.notifications + @related_contents = Kaminari.paginate_array(@proposal.relationed_contents).page(params[:page]).per(5) + redirect_to management_proposal_path(@proposal), status: :moved_permanently if request.path != management_proposal_path(@proposal) end