Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate events from inherited_resources #214

Merged
merged 1 commit into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 63 additions & 12 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
# frozen_string_literal: true

class EventsController < InheritedResources::Base
respond_to :html, :js, :json
class EventsController < ApplicationController
skip_before_action :authenticate_and_audit_user, only: %i[index show]
skip_load_and_authorize_resource only: %i[index show]
before_action :set_types
before_action :set_event, only: %i[show edit update destroy]

def index
@q ||= Event.ransack(params[:q])
# Default, only current and future events are displayed
@q.end_date_gteq = Date.today if params[:q].nil? || params[:q][:end_date_gteq].nil?
@q.sorts = 'start_date asc' if @q.sorts.empty?
@index ||= @q.result(distinct: true).page(params[:page]).per(20)
end

def show; end

def new
@event = Event.new
end

def create
@event = Event.new(event_params)

respond_to do |format|
if @event.save
format.html { redirect_to event_url(@event), notice: t(:event_create) }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end

def edit; end

def update
respond_to do |format|
if @event.update(event_params)
format.html { redirect_to event_url(@event), notice: t(:event_update) }
format.json { render :show, status: :updated, location: @event }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end

def destroy
@event.destroy

respond_to do |format|
format.html { redirect_to events_url, notice: t(:event_destroyed) }
format.json { head :no_content }
end
end

def participants
@breadcrumbs = [
Expand All @@ -18,19 +69,19 @@ def participants

protected

def collection
@q ||= end_of_association_chain.ransack(params[:q])
# Default, only current and future events are displayed
@q.end_date_gteq = Date.today if params[:q].nil? || params[:q][:end_date_gteq].nil?
@q.sorts = 'start_date asc' if @q.sorts.empty?
@collection ||= @q.result(distinct: true).page(params[:page]).per(20)
end

def set_types
@shipment_types = Rails.configuration.site['shipments']['types']
end

def permitted_params
private

def set_event
@event = Event.find(params[:id])

redirect_back(fallback_location: events_url) unless @event
end

def event_params
attrs = %i[name description start_date end_date url country_code
validated visa_letters request_creation_deadline
reimbursement_creation_deadline budget_id shipment_type]
Expand All @@ -40,6 +91,6 @@ def permitted_params
end
end
attrs.delete(:budget_id) if cannot? :read, Budget
params.permit(event: attrs)
params.require(:event).permit(attrs)
end
end
6 changes: 3 additions & 3 deletions app/views/events/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
=simple_form_for(resource, html: { class: 'form-horizontal form-inputs' }, wrapper: :horizontal_form) do |f|
=simple_form_for(@event, html: { class: 'form-horizontal form-inputs' }, wrapper: :horizontal_form) do |f|
.col-md-6
= f.input :name
= f.input :start_date, :as => :dpicker, :input_html => {class: 'form-control'}
= f.input :end_date, :as => :dpicker, :input_html => {class: 'form-control'}
= f.input :country_code, :as => :country, :input_html => {:autocomplete => "off"}
= f.input :description, :input_html => {:rows => 3}
= f.input :url
- if can? :validate, resource
- if can? :validate, @event
- if enabled? 'travel_sponsorships'
.col-md-offset-4
= f.input :visa_letters
Expand All @@ -21,5 +21,5 @@

.well.col-md-12
= f.button :submit, :class => 'btn btn-primary'
= link_to t('.cancel', :default => t("helpers.links.cancel")), collection_path, :class => 'btn btn-default'
= link_to t('.cancel', :default => t("helpers.links.cancel")), events_path, :class => 'btn btn-default'

5 changes: 5 additions & 0 deletions app/views/events/edit.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- model_class = Event
= content_for :page_header do
%h1= t('.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human)

= render :partial => "form"
10 changes: 5 additions & 5 deletions app/views/events/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
%nav.navbar.navbar-default.nav-tsp
.container-fluid
.navbar-header
= link_to t('.new', :default => t("helpers.links.new")), new_resource_path, :class => 'navbar-btn btn btn-primary', :data => { :confirm => t(:new_event_warning) }
= link_to t('.new', :default => t("helpers.links.new")), new_event_path, :class => 'navbar-btn btn btn-primary', :data => { :confirm => t(:new_event_warning) }
.nav.navbar-right
= search_form_for @q, :builder => SimpleForm::FormBuilder, :class => "navbar-form" do |f|
- t_start = f.input_field :end_date_gteq, :as => :dpicker, class:'form-control input-sm inpt-w'
- t_end = f.input_field :start_date_lteq, :as => :dpicker, class:'form-control input-sm inpt-w'
- t_text = f.text_field :name_or_description_cont, class:'form-control input-sm inpt-w'
= raw(t(:events_search, :start => t_start, :end => t_end, :text => t_text))
= f.submit t(:search), :class => "btn btn-default"
= link_to t(:reset_search), collection_path(:q => {:end_date_gteq => '0'}), :class => "btn btn-default"
= link_to t(:reset_search), events_path(:q => {:end_date_gteq => '0'}), :class => "btn btn-default"

%table.table.table-striped.events
%thead
Expand All @@ -26,9 +26,9 @@
%th= sort_link(@q, :shipment_type)
%th= t(:ask_for)
%tbody
- collection.each do |r|
- @index.each do |r|
%tr
%td= link_to r.name, resource_path(r)
%td= link_to r.name, event_path(r)
%td= country_label(r.country_code)
%td= l(r.start_date)
%td= l(r.end_date)
Expand All @@ -40,4 +40,4 @@
- if r.accepting_shipments?
= link_to t(:new_shipment), new_shipment_path(:event_id => r), :class => 'btn btn-default btn-xs'

= paginate collection
= paginate @index
5 changes: 5 additions & 0 deletions app/views/events/new.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- model_class = Event
= content_for :page_header do
%h1= t('.title', :default => [:'helpers.titles.new', 'New %{model}'], :model => model_class.model_name.human)

= render :partial => "form"
28 changes: 14 additions & 14 deletions app/views/events/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@
%h1= t('.title', :default => model_class.model_name.human)
.col-md-6
%br
- if user_signed_in? && can?(:update, resource)
= link_to t('.edit', :default => t("helpers.links.edit")), edit_resource_path(resource), :class => 'btn btn-default'
- if user_signed_in? && can?(:destroy, resource)
= link_to t('.destroy', :default => t("helpers.links.destroy")), resource_path(resource), :method => "delete", :data => { :confirm => t("helpers.links.confirm") }, :class => 'btn btn-default'
- if resource.accepting_requests?
= link_to t(:new_travel), new_travel_sponsorship_path(:event_id => resource), :class => 'btn btn-primary'
- if resource.accepting_shipments?
= link_to t(:new_shipment), new_shipment_path(:event_id => resource), :class => 'btn btn-primary'
- if user_signed_in? && can?(:participants, resource)
- if user_signed_in? && can?(:update, @event)
= link_to t('.edit', :default => t("helpers.links.edit")), edit_event_path(@event), :class => 'btn btn-default'
- if user_signed_in? && can?(:destroy, @event)
= link_to t('.destroy', :default => t("helpers.links.destroy")), event_path(@event), :method => "delete", :data => { :confirm => t("helpers.links.confirm") }, :class => 'btn btn-default'
- if @event.accepting_requests?
= link_to t(:new_travel), new_travel_sponsorship_path(:event_id => @event), :class => 'btn btn-primary'
- if @event.accepting_shipments?
= link_to t(:new_shipment), new_shipment_path(:event_id => @event), :class => 'btn btn-primary'
- if user_signed_in? && can?(:participants, @event)
= link_to "Participants", participants_event_path, class: "btn btn-success"
- if user_signed_in? && can?(:index, EventOrganizer)
= link_to "Organizers", event_event_organizers_path(@event), class: "btn btn-primary"
- if user_signed_in? && can?(:create, @event => EventEmail)
= link_to "Email", event_event_emails_path(@event), class: "btn btn-info"

= show_for resource do |r|
= show_for @event do |r|
.row
.col-md-12
= r.attribute :name
Expand All @@ -30,13 +30,13 @@
= r.attribute :end_date
.col-md-6
= r.attribute :country do
= country_label(resource.country_code)
= country_label(@event.country_code)
= r.attribute :url do
= link_to *([resource.url]*2)
= link_to *([@event.url]*2)
.row
.col-md-12
= r.attribute :description do
= simple_format(resource.description)
= simple_format(@event.description)
.row
.col-md-6
- if enabled? 'shipments'
Expand All @@ -47,7 +47,7 @@
- if enabled? 'travel_sponsorships'
= r.attribute :request_creation_deadline
= r.attribute :reimbursement_creation_deadline
- if user_signed_in? && can?(:validate, resource)
- if user_signed_in? && can?(:validate, @event)
.row
.col-md-12
= r.attribute :validated
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,6 @@ en:
activerecord:
errors:
<<: *errors
event_create: Event was successfully created
event_update: Event was successfully updated
event_destroyed: Event was successfully destroyed
4 changes: 2 additions & 2 deletions spec/features/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
select 'United States', from: 'country'

click_button 'Create event'
page.should have_content 'vent was successfully created'
page.should have_content 'Event was successfully created'
page.should_not have_content 'validated'
end

Expand All @@ -45,7 +45,7 @@
check 'validated'

click_button 'Create event'
page.should have_content 'vent was successfully created'
page.should have_content 'Event was successfully created'
within('.wrapper.event_visa_letters') do
page.should have_content 'No'
end
Expand Down
2 changes: 1 addition & 1 deletion spec/features/reimbursement_deadline_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
click_link 'Edit'
fill_in 'accepting reimbursements until', with: 12.hours.ago.to_s
click_button 'Update event'
page.should have_content 'event was successfully updated'
page.should have_content 'Event was successfully updated'
logout

sign_in_as_user(users(:luke))
Expand Down