From 4963c7492562f04eb93d70acc04f0c7d6f5d52a9 Mon Sep 17 00:00:00 2001 From: Andrew Radev Date: Tue, 14 Jul 2015 21:00:02 +0300 Subject: [PATCH] Initial events CRUD --- app/controllers/application_controller.rb | 6 +++ .../management/events_controller.rb | 41 +++++++++++++++++++ app/helpers/application_helper.rb | 36 ++++++++++++++++ app/views/management/events/edit.html.slim | 22 ++++++++++ app/views/management/events/index.html.slim | 27 +++++++++++- app/views/management/events/show.html.slim | 26 +++++++++++- config/locales/bg.yml | 3 ++ 7 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 app/views/management/events/edit.html.slim diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a35a580..4d04934 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,10 @@ class ApplicationController < ActionController::Base + def require_current_conference + if not current_conference? + redirect_to '/', alert: 'No conference selected' + end + end + # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception diff --git a/app/controllers/management/events_controller.rb b/app/controllers/management/events_controller.rb index 1600d3b..93cb141 100644 --- a/app/controllers/management/events_controller.rb +++ b/app/controllers/management/events_controller.rb @@ -1,9 +1,50 @@ module Management class EventsController < ManagementController + before_action :require_current_conference + def index + # TODO (2015-07-14) Scoped by conference? Why no conference_id + @events = Event.all end def show + @event = Event.find(params[:id]) + end + + def edit + @event = Event.find(params[:id]) + end + + def update + @event = Event.find(params[:id]) + + if @event.update_attributes(event_params) + flash[:notice] = 'Event was successfully updated.' + redirect_to [:management, @event] + else + render action: 'edit' + end + end + + def destroy + @event = Event.find(params[:id]) + @event.destroy + + redirect_to action: :index + end + + private + + def event_params + params.require(:event).permit( + :title, + :subtitle, + :length, + :language, + :abstract, + :description, + :notes + ) end end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be79..51e8e78 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,38 @@ module ApplicationHelper + def action_buttons(record, actions = [:index, :show, :edit, :destroy]) + klass = record.class + output = '' + + if actions.include? :index + output += link_to(icon(:list), [:management, 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], { + 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], { + 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], { + method: :delete, + data: {confirm: t('actions.are_you_sure')}, + title: t('actions.edit.button', model: klass.model_name.human), + class: 'btn btn-danger' + }) + end + + output.html_safe + end end diff --git a/app/views/management/events/edit.html.slim b/app/views/management/events/edit.html.slim new file mode 100644 index 0000000..8dc7dde --- /dev/null +++ b/app/views/management/events/edit.html.slim @@ -0,0 +1,22 @@ +.row + .col-lg-12 + = simple_nested_form_for [:management, @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' + + .panel-body + .row + .col-lg-12 + = f.input :title + = f.input :subtitle + = f.input :length + = f.input :language, collection: %w(en bg) + = f.input :abstract + = f.input :description + = f.input :notes + + .panel-footer.text-right + = f.submit class: 'btn btn-primary' diff --git a/app/views/management/events/index.html.slim b/app/views/management/events/index.html.slim index 3603360..4055b98 100644 --- a/app/views/management/events/index.html.slim +++ b/app/views/management/events/index.html.slim @@ -1 +1,26 @@ -p Hello +.row + .col-lg-12 + .panel.panel-default + .panel-heading + h2.panel-title= Event.model_name.human(count: 2).mb_chars.capitalize + + .panel-body + table.table.table-striped.table-hover.record-table + thead + tr + th = Event.human_attribute_name :title + th = Event.human_attribute_name :subtitle + th = Event.human_attribute_name :length + th = Event.human_attribute_name :language + th.actions + tbody + - @events.each do |event| + tr + td= event.title + td= event.subtitle + td #{event.length} minutes + td= event.language + + td.actions + div.btn-group.btn-group-sm + = action_buttons(event, [:show, :edit, :destroy]) diff --git a/app/views/management/events/show.html.slim b/app/views/management/events/show.html.slim index 3603360..9c5aae2 100644 --- a/app/views/management/events/show.html.slim +++ b/app/views/management/events/show.html.slim @@ -1 +1,25 @@ -p Hello +.row + .col-lg-12 + .panel.panel-default + .panel-heading + h2= @event.title + br + h3= @event.subtitle + + h4 #{Event.human_attribute_name :length}: #{@event.length}m, #{Event.human_attribute_name :language}: #{@event.language} + + .panel-body + h3= Event.human_attribute_name :abstract + p= simple_format @event.abstract + + h3= Event.human_attribute_name :description + p= simple_format @event.description + + h3= Event.human_attribute_name :notes + p= simple_format @event.notes + + h3 TODO Author? + + .panel-footer + div.btn-group.btn-group-sm + = action_buttons(@event, [:index, :edit, :destroy]) diff --git a/config/locales/bg.yml b/config/locales/bg.yml index fe15f53..dd16602 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -24,6 +24,9 @@ bg: bg: БГ en: EN actions: + index: + button: Виж всички %{models} + title: Всички %{models} create: button: Създай %{model} title: Създаване на %{model}