From c6750d3a1a38081b0cfa99c00d42252d3ab39e2e Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Sun, 19 Jul 2015 11:44:44 +0300 Subject: [PATCH] Nest everything under /conferences/:id/ again Would be more straightforward to work with, even if it's a bit more verbose sometimes. --- .../management/conferences_controller.rb | 2 +- app/controllers/management/events_controller.rb | 11 +++++++++-- app/controllers/management/home_controller.rb | 5 ----- .../management/management_controller.rb | 15 +++++---------- app/helpers/application_helper.rb | 10 +++++----- app/models/conference.rb | 9 --------- .../layouts/management/_navigation.html.slim | 2 +- app/views/management/conferences/show.html.slim | 8 ++++---- app/views/management/events/edit.html.slim | 4 ++-- app/views/management/events/index.html.slim | 2 +- app/views/management/events/show.html.slim | 2 +- app/views/management/home/index.html.slim | 2 +- config/routes.rb | 6 +----- ...20150719114546_remove_slug_from_conferences.rb | 5 +++++ .../call_for_participations_controller_spec.rb | 4 ++-- 15 files changed, 38 insertions(+), 49 deletions(-) create mode 100644 db/migrate/20150719114546_remove_slug_from_conferences.rb diff --git a/app/controllers/management/conferences_controller.rb b/app/controllers/management/conferences_controller.rb index d15d3e0..ac3bec6 100644 --- a/app/controllers/management/conferences_controller.rb +++ b/app/controllers/management/conferences_controller.rb @@ -10,7 +10,7 @@ module Management @conference = Conference.new(conference_params) if @conference.save - redirect_to management_conference_path(@conference, current_conference: @conference.slug) + redirect_to [:management, @conference] else render :new end diff --git a/app/controllers/management/events_controller.rb b/app/controllers/management/events_controller.rb index 93cb141..45d15d2 100644 --- a/app/controllers/management/events_controller.rb +++ b/app/controllers/management/events_controller.rb @@ -1,21 +1,23 @@ module Management class EventsController < ManagementController - before_action :require_current_conference - def index + @conference = find_conference # TODO (2015-07-14) Scoped by conference? Why no conference_id @events = Event.all end def show + @conference = find_conference @event = Event.find(params[:id]) end def edit + @conference = find_conference @event = Event.find(params[:id]) end def update + @conference = find_conference @event = Event.find(params[:id]) if @event.update_attributes(event_params) @@ -27,6 +29,7 @@ module Management end def destroy + @conference = find_conference @event = Event.find(params[:id]) @event.destroy @@ -35,6 +38,10 @@ module Management private + def find_conference + Conference.find(params[:conference_id]) + end + def event_params params.require(:event).permit( :title, diff --git a/app/controllers/management/home_controller.rb b/app/controllers/management/home_controller.rb index 64ed2f1..0d0aee3 100644 --- a/app/controllers/management/home_controller.rb +++ b/app/controllers/management/home_controller.rb @@ -2,10 +2,5 @@ module Management class HomeController < ManagementController def index end - - def set_conference - conference = Conference.find(params[:id]) - redirect_to management_conference_path(conference, current_conference: conference.slug) - end end end diff --git a/app/controllers/management/management_controller.rb b/app/controllers/management/management_controller.rb index de3faf3..aa98b89 100644 --- a/app/controllers/management/management_controller.rb +++ b/app/controllers/management/management_controller.rb @@ -6,22 +6,17 @@ module Management private - def default_url_options(options = {}) - { current_conference: current_conference.try(:slug) }.merge(options) - end - def current_conference? current_conference.present? end helper_method :current_conference? def current_conference - @current_conference ||= - begin - if params[:current_conference] - Conference.find_by_slug(params[:current_conference]) - end - end + if not @current_conference and params[:conference_id].present? + @current_conference = Conference.find(params[:conference_id]) + end + + @current_conference end helper_method :current_conference diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 51e8e78..eb4b1ad 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,31 +1,31 @@ module ApplicationHelper - def action_buttons(record, actions = [:index, :show, :edit, :destroy]) + def action_buttons(conference, record, actions = [:index, :show, :edit, :destroy]) klass = record.class output = '' if actions.include? :index - output += link_to(icon(:list), [:management, klass], { + output += link_to(icon(:list), [:management, conference, klass], { title: t('actions.index.button', models: klass.model_name.human(count: 2)), class: 'btn btn-info' }) end if actions.include? :show - output += link_to(icon(:eye), [:management, record], { + output += link_to(icon(:eye), [:management, conference, record], { title: t('actions.view.button', model: klass.model_name.human), class: 'btn btn-info' }) end if actions.include? :edit - output += link_to(icon(:edit), [:edit, :management, record], { + output += link_to(icon(:edit), [:edit, :management, conference, record], { title: t('actions.edit.button', model: klass.model_name.human), class: 'btn btn-primary' }) end if actions.include? :destroy - output += link_to(icon(:trash), [:management, record], { + output += link_to(icon(:trash), [:management, conference, record], { method: :delete, data: {confirm: t('actions.are_you_sure')}, title: t('actions.edit.button', model: klass.model_name.human), diff --git a/app/models/conference.rb b/app/models/conference.rb index f0c7c94..af860bd 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -16,7 +16,6 @@ class Conference < ActiveRecord::Base accepts_nested_attributes_for :tracks, :halls, reject_if: :all_blank, allow_destroy: true - before_create :slugify_title after_create :create_call_for_participation def submissions_grouped_by_day @@ -29,14 +28,6 @@ class Conference < ActiveRecord::Base submissions.group_by { |s| s.confirmed_at.to_date } end - def self.find_by_slug(slug) - find_by(slug: slug) - end - - def slugify_title - self.slug = title.gsub(/\s+/, '-') - end - private def start_date_is_before_end_date diff --git a/app/views/layouts/management/_navigation.html.slim b/app/views/layouts/management/_navigation.html.slim index 631791f..e808d2f 100644 --- a/app/views/layouts/management/_navigation.html.slim +++ b/app/views/layouts/management/_navigation.html.slim @@ -18,7 +18,7 @@ nav.navbar.navbar-static-top.navbar-inverse role="navigation" = current_conference.title li class="#{'active' if controller_name == 'events'}" - = link_to management_events_path do + = link_to [:management, current_conference, :events] do => icon 'file-text', Event.model_name.human(count: 2).mb_chars.capitalize, class: 'fa-fw' li class="#{'active' if controller_name == 'sponsorship_offers'}" = link_to '#' do diff --git a/app/views/management/conferences/show.html.slim b/app/views/management/conferences/show.html.slim index e943bf7..2cfff14 100644 --- a/app/views/management/conferences/show.html.slim +++ b/app/views/management/conferences/show.html.slim @@ -17,7 +17,7 @@ div = t Event.model_name.human(count: @conference.events.count) - = link_to management_events_path + = link_to [:management, @conference, :events] do .panel-footer.text-primary span.pull-left = t 'views.conference.see_details' span.pull-right = icon 'arrow-circle-right' @@ -34,7 +34,7 @@ = @conference.events.rejected.count div = t 'event_states.rejected', count: @conference.events.rejected.count - = link_to management_events_path(status: :rejected) + = link_to [:management, @conference, :events, {status: :rejected}] .panel-footer.text-danger span.pull-left = t 'views.conference.see_details' span.pull-right = icon 'arrow-circle-right' @@ -51,7 +51,7 @@ = @conference.events.approved.count div = t 'event_states.approved', count: @conference.events.approved.count - = link_to management_events_path(status: :approved) + = link_to [:management, @conference, :events, {status: :approved}] .panel-footer.text-info span.pull-left = t 'views.conference.see_details' span.pull-right = icon 'arrow-circle-right' @@ -67,7 +67,7 @@ = @conference.events.approved.confirmed.count div = t 'event_states.confirmed', count: @conference.events.approved.confirmed.count - = link_to management_events_path(status: :approved, confirmed: true) + = link_to [:management, @conference, :events, {status: :approved, confirmed: true}] .panel-footer.text-success span.pull-left = t 'views.conference.see_details' span.pull-right = icon 'arrow-circle-right' diff --git a/app/views/management/events/edit.html.slim b/app/views/management/events/edit.html.slim index 8dc7dde..1f62ddf 100644 --- a/app/views/management/events/edit.html.slim +++ b/app/views/management/events/edit.html.slim @@ -1,11 +1,11 @@ .row .col-lg-12 - = simple_nested_form_for [:management, @event], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| + = simple_nested_form_for [:management, @conference, @event], wrapper: :horizontal_form, html: { class: 'form-horizontal' } do |f| .panel.panel-primary .panel-heading h1.panel-title = t 'views.user.info' - = link_to icon(:eye), [:management, @event], class: 'btn btn-xs btn-info pull-right' + = link_to icon(:eye), [:management, @conference, @event], class: 'btn btn-xs btn-info pull-right' .panel-body .row diff --git a/app/views/management/events/index.html.slim b/app/views/management/events/index.html.slim index 4055b98..5b20420 100644 --- a/app/views/management/events/index.html.slim +++ b/app/views/management/events/index.html.slim @@ -23,4 +23,4 @@ td.actions div.btn-group.btn-group-sm - = action_buttons(event, [:show, :edit, :destroy]) + = action_buttons(@conference, event, [:show, :edit, :destroy]) diff --git a/app/views/management/events/show.html.slim b/app/views/management/events/show.html.slim index 9c5aae2..c242cb1 100644 --- a/app/views/management/events/show.html.slim +++ b/app/views/management/events/show.html.slim @@ -22,4 +22,4 @@ .panel-footer div.btn-group.btn-group-sm - = action_buttons(@event, [:index, :edit, :destroy]) + = action_buttons(@conference, @event, [:index, :edit, :destroy]) diff --git a/app/views/management/home/index.html.slim b/app/views/management/home/index.html.slim index 54434b0..2e37f5a 100644 --- a/app/views/management/home/index.html.slim +++ b/app/views/management/home/index.html.slim @@ -8,7 +8,7 @@ span.caret< ul.dropdown-menu role="menu" - Conference.all.each do |conference| - li= link_to conference.title, management_set_conference_path(conference), method: :put + li= link_to conference.title, [:management, conference] = link_to new_management_conference_path, class: 'btn btn-lg btn-primary' => icon 'plus', t('actions.create.button', model: Conference.model_name.human), class: 'fa-fw' diff --git a/config/routes.rb b/config/routes.rb index ae1d2a8..e7e84dd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,11 +4,7 @@ Rails.application.routes.draw do namespace :management do root to: 'home#index' - put '/set_conference/:id', to: 'home#set_conference', as: :set_conference - - resources :conferences - - scope ':current_conference' do + resources :conferences do resources :events resources :volunteers resources :sponsorship_offers diff --git a/db/migrate/20150719114546_remove_slug_from_conferences.rb b/db/migrate/20150719114546_remove_slug_from_conferences.rb new file mode 100644 index 0000000..83de9ba --- /dev/null +++ b/db/migrate/20150719114546_remove_slug_from_conferences.rb @@ -0,0 +1,5 @@ +class RemoveSlugFromConferences < ActiveRecord::Migration + def change + remove_column :conferences, :slug + end +end diff --git a/spec/controllers/management/call_for_participations_controller_spec.rb b/spec/controllers/management/call_for_participations_controller_spec.rb index 9151c93..5723b6f 100644 --- a/spec/controllers/management/call_for_participations_controller_spec.rb +++ b/spec/controllers/management/call_for_participations_controller_spec.rb @@ -8,7 +8,7 @@ RSpec.describe Management::CallForParticipationsController, type: :controller do describe 'POST #create' do it 'opens the CFP of the specified conference' do expect do - post 'create', current_conference: conference.slug, format: :js + post 'create', conference_id: conference.id, format: :js end.to change { Conference.find(conference.id).call_for_participation.in_progress? }.from(false).to(true) end end @@ -18,7 +18,7 @@ RSpec.describe Management::CallForParticipationsController, type: :controller do conference.call_for_participation.open! expect do - delete 'destroy', current_conference: conference.slug, format: :js + delete 'destroy', conference_id: conference.id, format: :js end.to change { Conference.find(conference.id).call_for_participation.in_progress? }.from(true).to(false) end end