From a8ad51a3c6739ae8f158798dd0b260ff1bc8cbc8 Mon Sep 17 00:00:00 2001 From: isubas Date: Fri, 18 Jan 2019 17:05:00 +0300 Subject: [PATCH 1/7] =?UTF-8?q?Link=20helper'=C4=B1n=C4=B1=20iyile=C5=9Fti?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit link_to_* metodlarının kısmen link_to gibi de kullanılabilmesini sağla #669 --- app/helpers/link_helper.rb | 113 ++++++++++-------- app/views/account/addresses/index.html.erb | 2 +- app/views/account/identities/index.html.erb | 2 +- app/views/admin/cities/show.html.erb | 2 +- app/views/admin/countries/show.html.erb | 2 +- .../calendars/index.html.erb | 2 +- .../committee/agenda_types/index.html.erb | 2 +- app/views/committee/agendas/index.html.erb | 2 +- app/views/committee/decisions/show.html.erb | 2 +- app/views/committee/meetings/index.html.erb | 2 +- app/views/committee/meetings/show.html.erb | 6 +- .../_evaluation_types.html.erb | 2 +- .../available_courses/_groups.html.erb | 2 +- .../available_courses/index.html.erb | 2 +- .../course_group_types/index.html.erb | 2 +- .../course_groups/index.html.erb | 2 +- .../course_types/index.html.erb | 2 +- .../course_management/courses/index.html.erb | 4 +- .../curriculums/_semesters.html.erb | 4 +- .../curriculums/index.html.erb | 4 +- .../registration_documents/index.html.erb | 2 +- app/views/home/index.html.erb | 2 +- app/views/layouts/builders/_index.html.erb | 6 +- .../references/academic_terms/index.html.erb | 2 +- app/views/references/terms/index.html.erb | 2 +- app/views/users/_addresses.html.erb | 2 +- app/views/users/_identities.html.erb | 2 +- app/views/users/index.html.erb | 4 +- config/locales/defaults/en.yml | 2 +- config/locales/defaults/tr.yml | 2 +- test/helpers/link_helper_test.rb | 26 ++-- 31 files changed, 115 insertions(+), 98 deletions(-) diff --git a/app/helpers/link_helper.rb b/app/helpers/link_helper.rb index 85af1c2b8..6d63f8ce9 100644 --- a/app/helpers/link_helper.rb +++ b/app/helpers/link_helper.rb @@ -1,62 +1,79 @@ # frozen_string_literal: true module LinkHelper - def link_to_back(path = nil, text = t('action_group.back')) - link_to( - fa_icon('arrow-left', text: text), - path, - class: 'btn btn-secondary btn-sm' - ) - end - - def link_to_destroy(path = nil, text = t('action_group.destroy')) - link_to( - fa_icon('trash', text: text), - path, - method: :delete, - data: { confirm: t('are_you_sure') }, - class: 'btn btn-outline-danger btn-sm' - ) - end - - def link_to_edit(path = nil, text = t('action_group.edit')) - link_to( - fa_icon('pencil', text: text), - path, - class: 'btn btn-outline-success btn-sm' - ) - end + LINKS = { + back: { + icon: 'arrow-left', + text: I18n.t('action_group.back'), + options: { + class: 'btn btn-secondary btn-sm' + } + }, + destroy: { + icon: 'trash', + text: I18n.t('action_group.destroy'), + options: { + class: 'btn btn-outline-danger btn-sm', + method: :delete, + data: { confirm: I18n.t('are_you_sure') } + } + }, + edit: { + icon: 'pencil', + text: I18n.t('action_group.edit'), + options: { + class: 'btn btn-outline-success btn-sm' + } + }, + new: { + icon: 'plus', + text: I18n.t('action_group.add'), + options: { + class: 'btn btn-outline-primary btn-sm', + id: 'add-button' + } + }, + show: { + icon: 'eye', + text: I18n.t('action_group.show'), + options: { + class: 'btn btn-outline-info btn-sm' + } + }, + update: { + icon: 'pencil-square-o', + text: I18n.t('action_group.update'), + options: { + class: 'btn btn-outline-info btn-sm' + } + }, + file: { + icon: 'file-word-o', + text: I18n.t('action_group.file'), + options: { + class: 'btn btn-secondary btn-sm' + } + } + }.freeze - def link_to_new(path = nil, text = t('action_group.add')) - link_to( - fa_icon('plus', text: text), - path, - class: 'btn btn-outline-primary btn-sm', - id: 'add-button' - ) + LINKS.each do |action, configuration| + define_method("link_to_#{action}") do |*args| + link_builder(args, configuration) + end end - def link_to_show(path = nil, text = t('action_group.show')) - link_to( - fa_icon('eye', text: text), - path, - class: 'btn btn-outline-info btn-sm' - ) - end + private - def link_to_update(path = nil, text = t('action_group.update')) + def link_builder(args, configuration) + text, path = split_args_for_link_to(args) link_to( - fa_icon('pencil-square-o', text: text), + fa_icon(configuration[:icon], text: text || configuration[:text]), path, - class: 'btn btn-outline-info btn-sm' + configuration.fetch(:options, {}) ) end - def link_to_file(path = nil, text = t('action_group.file')) - link_to( - fa_icon('file-word-o', text: text), - path, - class: 'btn btn-secondary btn-sm' - ) + def split_args_for_link_to(args) + args.length == 1 ? [nil, *args] : args end end diff --git a/app/views/account/addresses/index.html.erb b/app/views/account/addresses/index.html.erb index 1bd97a7e1..c36a21af6 100644 --- a/app/views/account/addresses/index.html.erb +++ b/app/views/account/addresses/index.html.erb @@ -1,6 +1,6 @@
<%= link_to_back user_path(@user) %> - <%= link_to_new(new_user_address_path(@user), t('.new_address')) unless @user.addresses.informal.present? %> + <%= link_to_new(t('.new_address'), new_user_address_path(@user)) unless @user.addresses.informal.present? %> <%= link_to (@addresses.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_from_mernis_user_addresses_path, class: "btn btn-outline-primary btn-sm" %>
diff --git a/app/views/account/identities/index.html.erb b/app/views/account/identities/index.html.erb index 5ce0b2d62..5b3edefbc 100644 --- a/app/views/account/identities/index.html.erb +++ b/app/views/account/identities/index.html.erb @@ -1,6 +1,6 @@
<%= link_to_back user_path(@user) %> - <%= link_to_new(new_user_identity_path(@user), t('.new_identity')) unless @user.identities.informal.present? %> + <%= link_to_new(t('.new_identity'), new_user_identity_path(@user)) unless @user.identities.informal.present? %> <%= link_to (@identities.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_from_mernis_user_identities_path, class: "btn btn-outline-primary btn-sm" %>
diff --git a/app/views/admin/cities/show.html.erb b/app/views/admin/cities/show.html.erb index 42113db6e..13f83c1d7 100644 --- a/app/views/admin/cities/show.html.erb +++ b/app/views/admin/cities/show.html.erb @@ -29,7 +29,7 @@
<%= fa_icon 'street-view', text: t('.card_header') %>
- <%= link_to_new [:new, :admin, @country, @city, :district], t('.new_district_link') %> + <%= link_to_new t('.new_district_link'), [:new, :admin, @country, @city, :district] %>
diff --git a/app/views/admin/countries/show.html.erb b/app/views/admin/countries/show.html.erb index 9d8a5326c..3c3d7b5ab 100644 --- a/app/views/admin/countries/show.html.erb +++ b/app/views/admin/countries/show.html.erb @@ -45,7 +45,7 @@
<%= fa_icon 'street-view', text: t('.card_header') %>
- <%= link_to_new [:new, :admin, @country, 'city'], t('.new_city_link') %> + <%= link_to_new t('.new_city_link'), [:new, :admin, @country, 'city'] %>
diff --git a/app/views/calendar_management/calendars/index.html.erb b/app/views/calendar_management/calendars/index.html.erb index c46242193..14252cfd7 100644 --- a/app/views/calendar_management/calendars/index.html.erb +++ b/app/views/calendar_management/calendars/index.html.erb @@ -4,7 +4,7 @@
<%= fa_icon 'street-view', text: t('.card_header') %>
- <%= link_to_new [:new, :calendar_management, :calendar], t('.new_calendar_link') %> + <%= link_to_new t('.new_calendar_link'), [:new, :calendar_management, :calendar] %>
diff --git a/app/views/committee/agenda_types/index.html.erb b/app/views/committee/agenda_types/index.html.erb index 645f70abc..45b0f16c5 100644 --- a/app/views/committee/agenda_types/index.html.erb +++ b/app/views/committee/agenda_types/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_agenda_type_path, t('.new_agenda_type_link') %> + <%= link_to_new t('.new_agenda_type_link'), new_agenda_type_path %>
diff --git a/app/views/committee/agendas/index.html.erb b/app/views/committee/agendas/index.html.erb index e14ab6f94..ec1fbe45b 100644 --- a/app/views/committee/agendas/index.html.erb +++ b/app/views/committee/agendas/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_committee_agenda_path(@committee), t('.new_agenda_link') %> + <%= link_to_new t('.new_agenda_link'), new_committee_agenda_path(@committee) %> <%= link_to(fa_icon('archive', text: t('.meetings')), committee_meetings_path(@committee), diff --git a/app/views/committee/decisions/show.html.erb b/app/views/committee/decisions/show.html.erb index 25fb5c3cd..bd50ce0e2 100644 --- a/app/views/committee/decisions/show.html.erb +++ b/app/views/committee/decisions/show.html.erb @@ -39,7 +39,7 @@
diff --git a/app/views/committee/meetings/index.html.erb b/app/views/committee/meetings/index.html.erb index ce8ac4ac6..86f5298cc 100644 --- a/app/views/committee/meetings/index.html.erb +++ b/app/views/committee/meetings/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_committee_meeting_path(@committee), t('.new_committee_meeting_link') %> + <%= link_to_new t('.new_committee_meeting_link'), new_committee_meeting_path(@committee) %> <%= link_to(fa_icon('tasks', text: t('.agendas')), committee_agendas_path(@committee), class: 'btn btn-dark btn-sm') %> diff --git a/app/views/committee/meetings/show.html.erb b/app/views/committee/meetings/show.html.erb index 8fdea1c26..7ccb215d1 100644 --- a/app/views/committee/meetings/show.html.erb +++ b/app/views/committee/meetings/show.html.erb @@ -56,10 +56,10 @@ <%= agenda.decision.try(:decision_no) %> <% if agenda.decision.present? %> - <%= link_to_show(committee_meeting_agenda_decision_path(@committee, agenda, agenda.decision), t('.show_decision')) %> - <%= link_to_edit(edit_committee_meeting_agenda_decision_path(@committee, agenda), t('.update_decision')) %> + <%= link_to_show(t('.show_decision'), committee_meeting_agenda_decision_path(@committee, agenda, agenda.decision)) %> + <%= link_to_edit(t('.update_decision'), edit_committee_meeting_agenda_decision_path(@committee, agenda)) %> <% else %> - <%= link_to_new(new_committee_meeting_agenda_decision_path(@committee, agenda), t('.create_decision')) %> + <%= link_to_new(t('.create_decision'), new_committee_meeting_agenda_decision_path(@committee, agenda)) %> <% end %> diff --git a/app/views/course_management/available_courses/_evaluation_types.html.erb b/app/views/course_management/available_courses/_evaluation_types.html.erb index 4ecbf5dff..53281ebc1 100644 --- a/app/views/course_management/available_courses/_evaluation_types.html.erb +++ b/app/views/course_management/available_courses/_evaluation_types.html.erb @@ -3,7 +3,7 @@
<%= fa_icon 'list-alt' %><%= t('.card_header') %> - <%= link_to_new(new_available_course_evaluation_type_path(@available_course), t('.create_evaluation_type')) %> + <%= link_to_new(t('.create_evaluation_type'), new_available_course_evaluation_type_path(@available_course)) %>
diff --git a/app/views/course_management/available_courses/_groups.html.erb b/app/views/course_management/available_courses/_groups.html.erb index 4538a65eb..fce2a95e4 100644 --- a/app/views/course_management/available_courses/_groups.html.erb +++ b/app/views/course_management/available_courses/_groups.html.erb @@ -3,7 +3,7 @@
<%= fa_icon 'cubes' %><%= t('.available_course_groups') %> - <%= link_to_new(new_available_course_available_course_group_path(@available_course), t('.create_available_course_group')) %> + <%= link_to_new(t('.create_available_course_group'), new_available_course_available_course_group_path(@available_course)) %>
diff --git a/app/views/course_management/available_courses/index.html.erb b/app/views/course_management/available_courses/index.html.erb index 4cd20fa9b..21394d227 100644 --- a/app/views/course_management/available_courses/index.html.erb +++ b/app/views/course_management/available_courses/index.html.erb @@ -6,7 +6,7 @@
<%= fa_icon 'align-justify', text: t('.available_courses') %> - <%= link_to_new new_available_course_path, t('.add_new_available_course') %> + <%= link_to_new t('.add_new_available_course'), new_available_course_path %>
diff --git a/app/views/course_management/course_group_types/index.html.erb b/app/views/course_management/course_group_types/index.html.erb index e4449eb80..a659f4157 100644 --- a/app/views/course_management/course_group_types/index.html.erb +++ b/app/views/course_management/course_group_types/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_course_group_type_path, t('.new_course_group_type_link') %> + <%= link_to_new t('.new_course_group_type_link'), new_course_group_type_path %>
diff --git a/app/views/course_management/course_groups/index.html.erb b/app/views/course_management/course_groups/index.html.erb index f028dea94..6e24a0f48 100644 --- a/app/views/course_management/course_groups/index.html.erb +++ b/app/views/course_management/course_groups/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_course_group_path, t('.new_course_group_link') %> + <%= link_to_new t('.new_course_group_link'), new_course_group_path %>
<%= render 'search' %> diff --git a/app/views/course_management/course_types/index.html.erb b/app/views/course_management/course_types/index.html.erb index 232458c80..0a8c23ca6 100644 --- a/app/views/course_management/course_types/index.html.erb +++ b/app/views/course_management/course_types/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_course_type_path, t('.new_course_type_link') %> + <%= link_to_new t('.new_course_type_link'), new_course_type_path %>
diff --git a/app/views/course_management/courses/index.html.erb b/app/views/course_management/courses/index.html.erb index 776fc30fb..c218cfd03 100644 --- a/app/views/course_management/courses/index.html.erb +++ b/app/views/course_management/courses/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_course_path, t('.add_new_course') %> + <%= link_to_new t('.add_new_course'), new_course_path %>
<%= render 'search' %> @@ -65,4 +65,4 @@
-
\ No newline at end of file +
diff --git a/app/views/course_management/curriculums/_semesters.html.erb b/app/views/course_management/curriculums/_semesters.html.erb index 2aef4fb6d..3abcd5a28 100644 --- a/app/views/course_management/curriculums/_semesters.html.erb +++ b/app/views/course_management/curriculums/_semesters.html.erb @@ -5,8 +5,8 @@
<%= fa_icon 'book' %><%= "#{semester.sequence}. #{t('semester')} (#{enum_t(semester, :term)})" %>
- <%= link_to_new new_curriculum_semester_curriculum_course_path(semester), t('.add_course') %> - <%= link_to_new new_curriculum_semester_curriculum_course_group_path(semester), t('.add_course_group') %> + <%= link_to_new t('.add_course'), new_curriculum_semester_curriculum_course_path(semester) %> + <%= link_to_new t('.add_course_group'), new_curriculum_semester_curriculum_course_group_path(semester) %>
diff --git a/app/views/course_management/curriculums/index.html.erb b/app/views/course_management/curriculums/index.html.erb index 576f74d9c..f27cff908 100644 --- a/app/views/course_management/curriculums/index.html.erb +++ b/app/views/course_management/curriculums/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_curriculum_path, t('.add_new_curriculum') %> + <%= link_to_new t('.add_new_curriculum'), new_curriculum_path %>
<%= render 'search' %>
@@ -45,4 +45,4 @@
-
\ No newline at end of file + diff --git a/app/views/first_registration/registration_documents/index.html.erb b/app/views/first_registration/registration_documents/index.html.erb index ad17090cd..3c35bf497 100644 --- a/app/views/first_registration/registration_documents/index.html.erb +++ b/app/views/first_registration/registration_documents/index.html.erb @@ -4,7 +4,7 @@
<%= fa_icon 'align-justify', text: t('.card_header') %>
- <%= link_to_new [:new, :first_registration, :registration_document], t('.new_registration_document_link') %> + <%= link_to_new t('.new_registration_document_link'), [:new, :first_registration, :registration_document] %>
diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index d2431a4ab..fbbdebd0e 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,3 +1,3 @@ <%= render 'home/templates/admin' %> - \ No newline at end of file + diff --git a/app/views/layouts/builders/_index.html.erb b/app/views/layouts/builders/_index.html.erb index d10785a39..cac403c37 100644 --- a/app/views/layouts/builders/_index.html.erb +++ b/app/views/layouts/builders/_index.html.erb @@ -4,9 +4,9 @@
<%= fa_icon 'align-justify', text: card_header %>
- <%= link_to_new([ - :new, namespace.to_sym, klass], - t("#{namespace}.#{klass.pluralize}.index.new_#{klass}_link") + <%= link_to_new( + t("#{namespace}.#{klass.pluralize}.index.new_#{klass}_link"), + [:new, namespace.to_sym, klass] ) %>
diff --git a/app/views/references/academic_terms/index.html.erb b/app/views/references/academic_terms/index.html.erb index e5f91fb07..a627dffb1 100644 --- a/app/views/references/academic_terms/index.html.erb +++ b/app/views/references/academic_terms/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new(new_academic_term_path, t('.new_academic_term_link')) %> + <%= link_to_new(t('.new_academic_term_link'), new_academic_term_path) %>
diff --git a/app/views/references/terms/index.html.erb b/app/views/references/terms/index.html.erb index 6e16a318e..98a3a4b03 100644 --- a/app/views/references/terms/index.html.erb +++ b/app/views/references/terms/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new new_term_path, t('.new_term_link') %> + <%= link_to_new t('.new_term_link'), new_term_path %>
diff --git a/app/views/users/_addresses.html.erb b/app/views/users/_addresses.html.erb index 8c903e720..cf6ef3ab2 100644 --- a/app/views/users/_addresses.html.erb +++ b/app/views/users/_addresses.html.erb @@ -2,7 +2,7 @@
- <%= link_to_new(new_user_address_path(@user), t('.new_address')) unless @user.addresses.informal.present? %> + <%= link_to_new(t('.new_address'), new_user_address_path(@user)) unless @user.addresses.informal.present? %> <%= link_to (@user.addresses.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_address_from_mernis_user_path, class: "btn btn-outline-primary btn-sm" %>
diff --git a/app/views/users/_identities.html.erb b/app/views/users/_identities.html.erb index b985e0504..611926319 100644 --- a/app/views/users/_identities.html.erb +++ b/app/views/users/_identities.html.erb @@ -2,7 +2,7 @@
- <%= link_to_new(new_user_identity_path(@user), t('.new_identity')) unless @user.identities.informal.present? %> + <%= link_to_new(t('.new_identity'), new_user_identity_path(@user)) unless @user.identities.informal.present? %> <%= link_to (@user.identities.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_identity_from_mernis_user_path, class: "btn btn-outline-primary btn-sm" %>
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 0a438c477..45a8b230a 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new(new_user_path, t('.new_user_link')) %> + <%= link_to_new(t('.new_user_link'), new_user_path) %>
@@ -48,4 +48,4 @@
-
\ No newline at end of file +
diff --git a/config/locales/defaults/en.yml b/config/locales/defaults/en.yml index 7e2350fee..1028a69ff 100644 --- a/config/locales/defaults/en.yml +++ b/config/locales/defaults/en.yml @@ -28,4 +28,4 @@ en: cocoon: defaults: add: 'Add' - remove: 'Remove' \ No newline at end of file + remove: 'Remove' diff --git a/config/locales/defaults/tr.yml b/config/locales/defaults/tr.yml index cc62e7028..300bc8360 100644 --- a/config/locales/defaults/tr.yml +++ b/config/locales/defaults/tr.yml @@ -28,4 +28,4 @@ tr: cocoon: defaults: add: 'Ekle' - remove: 'Sil' \ No newline at end of file + remove: 'Sil' diff --git a/test/helpers/link_helper_test.rb b/test/helpers/link_helper_test.rb index b324279ca..60365cb3b 100644 --- a/test/helpers/link_helper_test.rb +++ b/test/helpers/link_helper_test.rb @@ -5,44 +5,44 @@ class LinkHelperTest < ActionView::TestCase include FontAwesome::Rails::IconHelper - test '#link_to_back' do + test 'link_to_back method' do link = <<-HTML.squish Back HTML - assert_equal link_to_back('#', 'Back'), link + assert_equal link_to_back('Back', '#'), link end - test '#link_to_destroy' do + test 'link_to_destroy method' do link = <<-HTML.squish - Test Destroy HTML - assert_equal link_to_destroy('#', 'Test Destroy'), link + assert_equal link_to_destroy('Test Destroy', '#'), link end - test '#link_to_edit' do + test 'link_to_edit method' do link = <<-HTML.squish Test Edit HTML - assert_equal link_to_edit('#', 'Test Edit'), link + assert_equal link_to_edit('Test Edit', '#'), link end - test '#link_to_new' do + test 'link_to_new method' do link = <<-HTML.squish Test New HTML - assert_equal link_to_new('#', 'Test New'), link + assert_equal link_to_new('Test New', '#'), link end - test '#link_to_show' do + test 'link_to_show' do link = <<-HTML.squish Test Show HTML - assert_equal link_to_show('#', 'Test Show'), link + assert_equal link_to_show('Test Show', '#'), link end test '#link_to_update' do @@ -50,6 +50,6 @@ class LinkHelperTest < ActionView::TestCase Test Update HTML - assert_equal link_to_update('#', 'Test Update'), link + assert_equal link_to_update('Test Update', '#'), link end end From 4c1b713286e1a4c47c4ca0da70ed3e95fbff52bd Mon Sep 17 00:00:00 2001 From: isubas Date: Fri, 18 Jan 2019 18:59:43 +0300 Subject: [PATCH 2/7] . --- app/views/units/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/units/index.html.erb b/app/views/units/index.html.erb index fa93b14ea..8dd87e6d8 100644 --- a/app/views/units/index.html.erb +++ b/app/views/units/index.html.erb @@ -1,5 +1,5 @@
- <%= link_to_new(new_unit_path, t('.new_unit_link')) %> + <%= link_to_new(t('.new_unit_link'), new_unit_path) %>
From 25c125334e235fa1c09be9f570f3ec5d2b8bc034 Mon Sep 17 00:00:00 2001 From: isubas Date: Fri, 18 Jan 2019 19:02:36 +0300 Subject: [PATCH 3/7] =?UTF-8?q?A=C3=A7=C4=B1lan=20ders=20ekleme=20formunda?= =?UTF-8?q?=20aktif=20d=C3=B6nem=20derslerini=20listele?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../curriculums_controller.rb | 8 ++++---- app/decorators/curriculum_decorator.rb | 16 ++++++++++++++++ .../available_courses/_form.html.erb | 18 ++++++++++++++---- .../available_courses/_js.html.erb | 14 +++++++++----- config/routes/course_management.rb | 2 +- .../curriculums_controller_test.rb | 11 +++++++++++ test/decorators/curriculum_decorator_test.rb | 17 +++++++++++++++++ test/fixtures/courses.yml | 2 +- test/fixtures/curriculum_courses.yml | 6 ++++++ test/models/curriculum_semester_test.rb | 2 +- 10 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 app/decorators/curriculum_decorator.rb create mode 100644 test/decorators/curriculum_decorator_test.rb diff --git a/app/controllers/course_management/curriculums_controller.rb b/app/controllers/course_management/curriculums_controller.rb index ef79810c2..4553a6c94 100644 --- a/app/controllers/course_management/curriculums_controller.rb +++ b/app/controllers/course_management/curriculums_controller.rb @@ -3,7 +3,7 @@ module CourseManagement class CurriculumsController < ApplicationController include PagyBackendWithHelpers - before_action :set_curriculum, only: %i[show edit update destroy courses] + before_action :set_curriculum, only: %i[show edit update destroy openable_courses] def index curriculums = Curriculum.includes(:unit) @@ -44,9 +44,9 @@ def destroy redirect_with(message) end - def courses - @courses = @curriculum.courses - render json: @courses + def openable_courses + @curriculum = CurriculumDecorator.new(@curriculum) + render json: @curriculum.openable_courses_for_active_term end private diff --git a/app/decorators/curriculum_decorator.rb b/app/decorators/curriculum_decorator.rb new file mode 100644 index 000000000..840da3112 --- /dev/null +++ b/app/decorators/curriculum_decorator.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class CurriculumDecorator < SimpleDelegator + def openable_courses_for_active_term(appends: nil) + term = AcademicTerm.active.last.try(:term) + + return [] unless term + + courses = semesters.where(term: term) + .includes(:courses) + .where.not(courses: { id: available_courses.pluck(:course_id) }) + .order('courses.name') + .map(&:courses) + [*appends, courses].flatten + end +end diff --git a/app/views/course_management/available_courses/_form.html.erb b/app/views/course_management/available_courses/_form.html.erb index 73982b4f5..f5292f1b9 100644 --- a/app/views/course_management/available_courses/_form.html.erb +++ b/app/views/course_management/available_courses/_form.html.erb @@ -20,16 +20,26 @@ <% end %>
- <%= f.association :unit, collection: Unit.active.coursable.order(:name), label_method: :names_depth_cache %> + <%= f.association :unit, + collection: Unit.active.coursable.order(:name), + label_method: :names_depth_cache %>
- <%= f.association :curriculum, collection: f.object.unit.present? ? f.object.unit.managed_curriculums : [] %> + <%= f.association :curriculum, + collection: [*f.object.unit.try(:managed_curriculums)] %>
- <%= f.association :coordinator, collection: f.object.unit.present? ? f.object.unit.subtree_employees : [], label_method: lambda { |c| full_name(c) } %> + <%= f.association :coordinator, + collection: [*f.object.unit.try(:subtree_employees)], + label_method: lambda { |c| full_name(c) } %>
- <%= f.association :course, collection: f.object.curriculum.present? ? f.object.curriculum.courses : [] %> + <% if f.object.curriculum %> + <% courses = CurriculumDecorator.new(f.object.curriculum) + .openable_courses_for_active_term(appends: f.object.course) %> + <% end %> + <%= f.association :course, + collection: [*courses] %>
<%= f.button :submit, class: 'btn btn-outline-success btn-sm' %> diff --git a/app/views/course_management/available_courses/_js.html.erb b/app/views/course_management/available_courses/_js.html.erb index 6713876d3..72df1546f 100644 --- a/app/views/course_management/available_courses/_js.html.erb +++ b/app/views/course_management/available_courses/_js.html.erb @@ -1,7 +1,13 @@