From 98e0a256064eeb99b17184864b876e033facde91 Mon Sep 17 00:00:00 2001 From: nasia Date: Sun, 18 Jun 2017 23:33:54 +0300 Subject: [PATCH] Modify views for booths --- app/controllers/admin/booths_controller.rb | 67 +++++++++++++++- app/models/booth.rb | 33 ++++++++ app/models/booth_request.rb | 2 + .../booths/_change_state_dropdown.html.haml | 25 ++++++ app/views/admin/booths/_form.html.haml | 43 ++++------- app/views/admin/booths/edit.html.haml | 4 - app/views/admin/booths/index.html.haml | 76 ++++++++++++------- app/views/admin/booths/new.html.haml | 2 +- app/views/admin/booths/show.html.haml | 42 +++++----- config/routes.rb | 14 +++- 10 files changed, 220 insertions(+), 88 deletions(-) create mode 100644 app/views/admin/booths/_change_state_dropdown.html.haml diff --git a/app/controllers/admin/booths_controller.rb b/app/controllers/admin/booths_controller.rb index 65f6f9f6..c946e274 100644 --- a/app/controllers/admin/booths_controller.rb +++ b/app/controllers/admin/booths_controller.rb @@ -3,19 +3,80 @@ module Admin load_and_authorize_resource :conference, find_by: :short_title load_and_authorize_resource :booth, through: :conference + def index @booths = @conference.booths end def show; end - def new; end + def new + @booth = Booth.new(conference: @conference) + end - def create; end + def create + @booth = @conference.booths.build(booth_params) - def update; end + if @booth.save + redirect_to admin_conference_booths_path, + notice: "Booth successfully created." + else + flash[:error] = "Creating booth failed. #{@booth.errors.full_messages.join('. ')}." + render :new + end + end + + def edit; end + + def update + @booth = @conference.booths + @booth.assign_attributes(booth_params) + + + if @booth.update_attributes(booth_params) + redirect_to admin_conference_booths_path, + notice: "Successfully updated booth for #{@booth.title}." + else + flash[:error] = "An error prohibited the Booth for #{@booth.title} "\ + "#{@booth.errors.full_messages.join('. ')}." + render :edit + end + end def destroy; end + def accept + update_state(:accept, 'Booth accepted!') + end + + def to_accept + update_state(:to_accept, 'Booth to accept') + end + + def reject + update_state(:rejected, 'Booth rejected') + end + + def reset + update_state(:reset, 'Booth is submitted') + end + + private + + def update_state(transition, notice) + alert = @booth.update_state(transition, notice) + + if alert.blank? + flash[:notice] = notice + redirect_back_or_to(admin_conference_booths_path(conference_id: @conference.short_title)) && return + else + flash[:error] = alert + return redirect_back_or_to(admin_conference_booths_path(conference_id: @conference.short_title)) && return + end + end + + def booth_params + params.require(:booth).permit(:title, :description, :reasoning, :state, :logo_link, :conference_id) + end end end diff --git a/app/models/booth.rb b/app/models/booth.rb index 35ea1a42..f68d31d7 100644 --- a/app/models/booth.rb +++ b/app/models/booth.rb @@ -1,4 +1,6 @@ class Booth < ActiveRecord::Base + include ActiveRecord::Transitions + belongs_to :conference has_many :booth_requests has_many :users, through: :booth_requests @@ -13,4 +15,35 @@ class Booth < ActiveRecord::Base :logo_link, :conference_id, presence: true + + state_machine initial: :submitted do + state :submitted + state :withdrawn + state :to_accept + state :accepted + state :rejected + + event :restart do + transitions to: :submitted, from: [:withdrawn] + end + event :withdrawn do + transitions to: :withdrawn, from: [:submitted, :to_accept, :accepted] + end + event :to_accept do + transitions to: :to_accept, from: [:submitted, :rejected, :accepted] + end + event :accept do + transitions to: :accepted, from: [:submitted, :to_accept] + end + event :reject do + transitions to: :rejected, from: [:submitted] + end + event :reset do + transitions to: :submitted, from: [:to_accept] + end + end + + def transition_possible?(transition) + self.class.state_machine.events_for(current_state).include?(transition) + end end diff --git a/app/models/booth_request.rb b/app/models/booth_request.rb index d29c88fd..91099bc6 100644 --- a/app/models/booth_request.rb +++ b/app/models/booth_request.rb @@ -7,4 +7,6 @@ class BoothRequest < ActiveRecord::Base :role, presence: true + ROLES = [%w[Submitter submitter], %w[Responsible responsible], %w[Secondary secondary]] + end diff --git a/app/views/admin/booths/_change_state_dropdown.html.haml b/app/views/admin/booths/_change_state_dropdown.html.haml new file mode 100644 index 00000000..2225de76 --- /dev/null +++ b/app/views/admin/booths/_change_state_dropdown.html.haml @@ -0,0 +1,25 @@ +- if booth.transition_possible? :accept + %li= link_to 'Accept booth', + accept_admin_conference_booth_path(@conference.short_title, booth), + method: :patch, id: "accept_booth_#{booth.id}" + +- if booth.transition_possible? :reject + %li= link_to 'Reject booth', + reject_admin_conference_booth_path(@conference.short_title, booth), + method: :patch, confirm: 'Are you sure?', id: "reject_booth_#{booth.id}" + + +- if booth.transition_possible? :restart + %li= link_to 'Start review', + restart_admin_conference_booth_path(@conference.short_title, booth), + method: :patch, id: "restart_booth_#{booth.id}" + +- if booth.transition_possible? :to_accept + %li= link_to 'To accept booth', + to_accept_admin_conference_booth_path(@conference.short_title, booth), + method: :patch, id: "to_accept_booth_#{booth.id}" + +- if booth.transition_possible? :cancel + %li= link_to 'Cancel booth', + cancel_admin_conference_booth_path(@conference.short_title, booth), + method: :patch, id: "cancel_booth_#{booth.id}" diff --git a/app/views/admin/booths/_form.html.haml b/app/views/admin/booths/_form.html.haml index 4f85e0f1..500ce632 100644 --- a/app/views/admin/booths/_form.html.haml +++ b/app/views/admin/booths/_form.html.haml @@ -1,28 +1,17 @@ -= form_for @booth do |f| - - if @booth.errors.any? - #error_explanation - %h2= "#{pluralize(@booth.errors.count, "error")} prohibited this booth from being saved:" - %ul - - @booth.errors.full_messages.each do |msg| - %li= msg +.row + .col-md-12 + .page-header + %title Request a Booth +.row + .col-md-8 + = semantic_form_for(@booth, url: admin_conference_booths_path(@conference.short_title), html: {multipart: true}) do |f| + = f.input :title, as: :string, required: true + = f.input :description, input_html: { rows: 5, data: { provide: 'markdown-editable' } }, required: true + = f.input :reasoning, input_html: { rows: 5, data: { provide: 'markdown-editable' } }, required: true + = f.input :logo_link, as: :string, required: true - .field - = f.label :title - = f.text_field :title - .field - = f.label :conference_id - = f.number_field :conference_id - .field - = f.label :description - = f.text_area :description - .field - = f.label :state - = f.text_field :state - .field - = f.label :reasoning - = f.text_area :reasoning - .field - = f.label :logo_link - = f.text_field :logo_link - .actions - = f.submit 'Save' + %p.text-right + - if @booth.new_record? + = f.submit 'Create Booth Request', class: 'btn btn-success' + - else + = f.submit 'Update Booth Request', class: 'btn btn-success' diff --git a/app/views/admin/booths/edit.html.haml b/app/views/admin/booths/edit.html.haml index 222d6c97..d614398b 100644 --- a/app/views/admin/booths/edit.html.haml +++ b/app/views/admin/booths/edit.html.haml @@ -1,7 +1,3 @@ %h1 Editing booth = render 'form' - -= link_to 'Show', @booth -\| -= link_to 'Back', booths_path diff --git a/app/views/admin/booths/index.html.haml b/app/views/admin/booths/index.html.haml index 74287f50..43db4b1a 100644 --- a/app/views/admin/booths/index.html.haml +++ b/app/views/admin/booths/index.html.haml @@ -1,29 +1,47 @@ -%h1 Listing booths - -%table - %tr - %th Title - %th Conference - %th Description - %th State - %th Reasoning - %th Logo link - %th - %th - %th - - - @booths.each do |booth| - %tr - %td= booth.title - %td= booth.conference_id - %td= booth.description - %td= booth.state - %td= booth.reasoning - %td= booth.logo_link - %td= link_to 'Show', booth - %td= link_to 'Edit', edit_booth_path(booth) - %td= link_to 'Destroy', booth, :method => :delete, :data => { :confirm => 'Are you sure?' } - -%br - -= link_to 'New Booth', new_admin_conference_booth_path +.row + .col-md-12 + .page-header + %h1 + Booths + = "(#{@booths.length})" if @booths.any? + .pull-right + - if can? :create, Booth + =link_to 'Add Booth', new_admin_conference_booth_path(@conference.short_title), class: 'button btn btn-default btn-info' + %p.text-muted + All the booth requests +.row + .col-md-12 + .margin-event-table + %table.table.table-striped.table-bordered.table-hover.datatable + %thead + %th + %b ID + %th + %b Title + %th + %b Submitter + %th + %b Responsible + %th + %b Secondary Responsible + %th + %b State + - @booths.each do |booth| + %tr + %td + = booth.id + %td + = link_to booth.title, admin_conference_booth_path(@conference.short_title, booth) + %td + a + %td + b + %td + c + %td + .btn-group + %button{ type: 'button', class: 'btn btn-link dropdown-toggle', 'data-toggle' => 'dropdown' } + = booth.state.humanize + %span.caret + %ul.dropdown-menu{ role: 'menu' } + = render 'change_state_dropdown', booth: booth diff --git a/app/views/admin/booths/new.html.haml b/app/views/admin/booths/new.html.haml index 3a84ba98..f6dd2fc4 100644 --- a/app/views/admin/booths/new.html.haml +++ b/app/views/admin/booths/new.html.haml @@ -2,4 +2,4 @@ = render 'form' -= link_to 'Back', booths_path += link_to 'Back', admin_conference_booths_path diff --git a/app/views/admin/booths/show.html.haml b/app/views/admin/booths/show.html.haml index 5c076aca..77dd180c 100644 --- a/app/views/admin/booths/show.html.haml +++ b/app/views/admin/booths/show.html.haml @@ -1,24 +1,20 @@ -%p#notice= notice +.row + .col-md-12 + %h3 + = @booth.title + .btn-group.pull-right + = link_to 'Edit', edit_admin_conference_booth_path(@conference.short_title, @booth), class: 'btn btn-mini btn-primary' -%p - %b Title: - = @booth.title -%p - %b Conference: - = @booth.conference_id -%p - %b Description: - = @booth.description -%p - %b State: - = @booth.state -%p - %b Reasoning: - = @booth.reasoning -%p - %b Logo link: - = @booth.logo_link - -= link_to 'Edit', edit_booth_path(@booth) -\| -= link_to 'Back', booths_path +.row + .col-md-12 + %table.table + %tr + %td.col-md-2 + %b Description + %td + = @booth.description + %tr + %td.col-md-2 + %b Reasoning + %td + = @booth.reasoning diff --git a/config/routes.rb b/config/routes.rb index c5889406..ec6392df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,13 +31,25 @@ Osem::Application.routes.draw do resources :conferences do resource :contact, except: [:index, :new, :create, :show, :destroy] resources :schedules, only: [:index, :create, :show, :update, :destroy] - resources :booths + resources :event_schedules, only: [:create, :update, :destroy] get 'commercials/render_commercial' => 'commercials#render_commercial' resources :commercials, only: [:index, :create, :update, :destroy] get '/volunteers_list' => 'volunteers#show' get '/volunteers' => 'volunteers#index', as: 'volunteers_info' patch '/volunteers' => 'volunteers#update', as: 'volunteers_update' + patch '/booths' => 'booths#update' + + resources :booths do + member do + patch :accept + patch :restart + patch :withdrawn + patch :to_accept + patch :reject + patch :reset + end + end resources :registrations, except: [:create, :new] do member do